From d40a28b11b4341d9731cb38c93818e30ac94840b Mon Sep 17 00:00:00 2001 From: John Lapeyre Date: Thu, 18 May 2023 11:29:02 -0400 Subject: [PATCH] Add check that decomposition includes qsd2q gates before optimizing them Co-authored-by: jsmallz333 <90203920+jsmallz333@users.noreply.github.com> --- qiskit/quantum_info/synthesis/qsd.py | 2 ++ test/python/quantum_info/test_synthesis.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/qiskit/quantum_info/synthesis/qsd.py b/qiskit/quantum_info/synthesis/qsd.py index 95b5bd036e75..4aa9e65c5faf 100644 --- a/qiskit/quantum_info/synthesis/qsd.py +++ b/qiskit/quantum_info/synthesis/qsd.py @@ -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::]): diff --git a/test/python/quantum_info/test_synthesis.py b/test/python/quantum_info/test_synthesis.py index 7d6100201dfc..9c088157f6a2 100644 --- a/test/python/quantum_info/test_synthesis.py +++ b/test/python/quantum_info/test_synthesis.py @@ -1542,12 +1542,20 @@ def test_opt_a1a2(self, nqubits): ccirc.count_ops().get("cx"), (23 / 48) * 4**nqubits - (3 / 2) * 2**nqubits + 4 / 3 ) + # 10036 def test_1q_decomposition(self): """Test decomposition of single qubit matrix""" mat = np.array([[0, 1], [1, 0]]) circ = self.qsd(mat) self.assertEqual(Operator(mat), Operator(circ)) + # 10036 + def test_2q_with_no_qsd2q(self): + """Test decomposition of unitary whose decomposition is all "u" and "cx".""" + mat = CXGate().to_matrix() + circ = self.qsd(mat) + self.assertEqual(Operator(mat), Operator(circ)) + class TestTwoQubitDecomposeUpToDiagonal(QiskitTestCase): """test TwoQubitDecomposeUpToDiagonal class"""