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

fix 1q matrix bug in Quantum Shannon Decomposer #10126

Merged
merged 17 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
6 changes: 4 additions & 2 deletions qiskit/quantum_info/synthesis/qsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def qs_decomposition(
circ = decomposer_1q(mat)
elif dim == 4:
if decomposer_2q is None:
if opt_a2:
if opt_a2 and _depth > 0:
from qiskit.extensions.unitary import UnitaryGate # pylint: disable=cyclic-import

def decomp_2q(mat):
Expand Down Expand Up @@ -118,7 +118,7 @@ def decomp_2q(mat):
right_circ = _demultiplex(u1, u2, opt_a1=opt_a1, opt_a2=opt_a2, _depth=_depth)
circ.append(right_circ.to_instruction(), qr)

if opt_a2 and _depth == 0:
if opt_a2 and _depth == 0 and dim > 4:
return _apply_a2(circ)
return circ

Expand Down Expand Up @@ -237,6 +237,8 @@ def _apply_a2(circ):
instr, _, _ = instr_context
if instr.name == "qsd2q":
ind2q.append(i)
if not ind2q:
return ccirc
# rolling over diagonals
ind2 = None # lint
for ind1, ind2 in zip(ind2q[0:-1:], ind2q[1::]):
Expand Down
14 changes: 10 additions & 4 deletions test/python/quantum_info/test_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,17 +1530,23 @@ def test_hermitian(self, nqubits):
expected_cx = self._qsd_l2_cx_count(nqubits) - self._qsd_l2_a1_mod(nqubits)
self.assertLessEqual(ccirc.count_ops().get("cx"), expected_cx)

@data(*list(range(3, 6)))
@data(*list(range(1, 6)))
def test_opt_a1a2(self, nqubits):
"""Test decomposition with both optimization a1 and a2 from shende2006"""
dim = 2**nqubits
umat = scipy.stats.unitary_group.rvs(dim, random_state=1224)
circ = self.qsd(umat, opt_a1=True, opt_a2=True)
ccirc = transpile(circ, basis_gates=["u", "cx"], optimization_level=0)
self.assertTrue(Operator(umat) == Operator(ccirc))
self.assertEqual(
ccirc.count_ops().get("cx"), (23 / 48) * 4**nqubits - (3 / 2) * 2**nqubits + 4 / 3
)
if nqubits > 2:
self.assertEqual(
ccirc.count_ops().get("cx"),
(23 / 48) * 4**nqubits - (3 / 2) * 2**nqubits + 4 / 3,
)
elif nqubits == 1:
self.assertEqual(ccirc.count_ops().get("cx", 0), 0)
elif nqubits == 2:
self.assertLessEqual(ccirc.count_ops().get("cx", 0), 3)


class TestTwoQubitDecomposeUpToDiagonal(QiskitTestCase):
Expand Down