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 bug in qs_decomposition #10850

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 1 deletion qiskit/quantum_info/synthesis/qsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ def _apply_a2(circ):
for i, instruction in enumerate(ccirc.data):
if instruction.operation.name == "qsd2q":
ind2q.append(i)
if not ind2q:
if len(ind2q) == 0:
return ccirc
elif len(ind2q) == 1:
# No neighbors to merge diagonal into; revert name
ccirc.data[ind2q[0]].operation.name = "Unitary"
return ccirc
# rolling over diagonals
ind2 = None # lint
Expand Down
99 changes: 99 additions & 0 deletions test/python/quantum_info/test_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,105 @@ def test_opt_a1a2(self, nqubits):
elif nqubits == 2:
self.assertLessEqual(ccirc.count_ops().get("cx", 0), 3)

def test_a2_opt_single_2q(self):
"""
Test a2_opt when a unitary causes a single final 2-qubit unitary for which this optimization
won't help. This came up in issue 10787.
"""
# this somewhat unique signed permutation matrix seems to cause the issue
mat = np.array(
[
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
1.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
1.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.0 + 0.0j,
],
[
1.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
1.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
-1.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
[
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
-1.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
0.0 + 0.0j,
],
]
)

gate = UnitaryGate(mat)
qc = QuantumCircuit(3)
qc.append(gate, range(3))
try:
qc.to_gate().control(1)
except UnboundLocalError as uerr:
self.fail(str(uerr))


class TestTwoQubitDecomposeUpToDiagonal(QiskitTestCase):
"""test TwoQubitDecomposeUpToDiagonal class"""
Expand Down