Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error for invalid section_size in synth_cnot_count_full_pmh #12166

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions qiskit/synthesis/linear/cnot_synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
from qiskit.circuit import QuantumCircuit
from qiskit.exceptions import QiskitError

MULTIPLIER_CONSTANT = 2.64


def synth_cnot_count_full_pmh(
state: list[list[bool]] | np.ndarray[bool], section_size: int = 2
state: list[list[bool]] | np.ndarray[bool], section_size: int = None
) -> QuantumCircuit:
"""
Synthesize linear reversible circuits for all-to-all architecture
Expand All @@ -43,7 +45,9 @@ def synth_cnot_count_full_pmh(
QuantumCircuit: a CX-only circuit implementing the linear transformation.

Raises:
QiskitError: when variable ``state`` isn't of type ``numpy.ndarray``
QiskitError:
- when variable ``state`` isn't of type ``numpy.ndarray``
- when ``section_size`` is greater than the number of qubits

References:
1. Patel, Ketan N., Igor L. Markov, and John P. Hayes,
Expand All @@ -56,6 +60,18 @@ def synth_cnot_count_full_pmh(
"state should be of type list or numpy.ndarray, "
"but was of the type {}".format(type(state))
)

num_qubits = len(state)
if section_size is None:
default_section_size = MULTIPLIER_CONSTANT * np.log(num_qubits)
default_section_size = 1 if default_section_size == 0 else default_section_size
section_size = int(default_section_size)
if not section_size <= num_qubits:
raise QiskitError(
"section_size should be less than or equal to the number of qubits ({0}), "
"but was {1}".format(num_qubits, section_size)
)

state = np.array(state)
# Synthesize lower triangular part
[state, circuit_l] = _lwr_cnot_synth(state, section_size)
Expand Down
2 changes: 1 addition & 1 deletion qiskit/synthesis/linear_phase/cnot_phase_synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def synth_cnot_phase_aam(
cnots: list[list[int]], angles: list[str], section_size: int = 2
cnots: list[list[int]], angles: list[str], section_size: int = None
) -> QuantumCircuit:
r"""This function is an implementation of the `GraySynth` algorithm of
Amy, Azimadeh and Mosca.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def run(self, high_level_object, coupling_map=None, target=None, qubits=None, **
"PMHSynthesisLinearFunction only accepts objects of type LinearFunction"
)

section_size = options.get("section_size", 2)
section_size = options.get("section_size", None)
use_inverted = options.get("use_inverted", False)
use_transposed = options.get("use_transposed", False)

Expand Down
23 changes: 22 additions & 1 deletion test/python/synthesis/test_cnot_phase_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import unittest
import ddt

from qiskit import QiskitError
from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import UnitaryGate
from qiskit.quantum_info.operators import Operator
from qiskit.synthesis.linear import synth_cnot_count_full_pmh
from qiskit.synthesis.linear import synth_cnot_count_full_pmh, random_invertible_binary_matrix
from qiskit.synthesis.linear_phase import synth_cnot_phase_aam
from test import QiskitTestCase # pylint: disable=wrong-import-order

Expand Down Expand Up @@ -269,6 +270,26 @@ def test_patel_markov_hayes(self):
# Check if the two circuits are equivalent
self.assertEqual(unitary_patel, unitary_compare)

def test_invalid_state_type(self):
"""Test invalid state type"""
with self.assertRaises(QiskitError):
synth_cnot_count_full_pmh("invalid_state", 2)

def test_invalid_section_size(self):
"""Test invalid section_size"""
state = [[1, 0], [0, 1]]
with self.assertRaises(QiskitError):
synth_cnot_count_full_pmh(state, 3)

@ddt.data(1, 2, 5, 10, 20)
def test_defaulting_section_size(self, num_qubits):
"""Test defaulting section_size doesn't throw exception"""
try:
state = random_invertible_binary_matrix(num_qubits, seed=1234)
synth_cnot_count_full_pmh(state, num_qubits)
except Exception:
self.fail("synth_cnot_count_full_pmh() raised an exception unexpectedly!")


if __name__ == "__main__":
unittest.main()
8 changes: 4 additions & 4 deletions test/python/synthesis/test_linear_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ def test_example_circuit(self):
mat = LinearFunction(qc).linear

optimized_qc = optimize_cx_4_options(synth_cnot_count_full_pmh, mat, optimize_count=True)
self.assertEqual(optimized_qc.depth(), 17)
self.assertEqual(optimized_qc.count_ops()["cx"], 20)
self.assertEqual(optimized_qc.depth(), 12)
self.assertEqual(optimized_qc.count_ops()["cx"], 12)

optimized_qc = optimize_cx_4_options(synth_cnot_count_full_pmh, mat, optimize_count=False)
self.assertEqual(optimized_qc.depth(), 15)
self.assertEqual(optimized_qc.count_ops()["cx"], 23)
self.assertEqual(optimized_qc.depth(), 12)
self.assertEqual(optimized_qc.count_ops()["cx"], 12)

@data(5, 6)
def test_invertible_matrix(self, n):
Expand Down
Loading