Skip to content

Commit

Permalink
Fix bug in qs_decomposition (#10850) (#10957)
Browse files Browse the repository at this point in the history
* fix bug in qs_decomposition

* reno

---------

Co-authored-by: Luciano Bello <[email protected]>
(cherry picked from commit dda063e)

Co-authored-by: ewinston <[email protected]>
  • Loading branch information
mergify[bot] and ewinston authored Oct 4, 2023
1 parent 110776a commit 1630b6e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
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
7 changes: 7 additions & 0 deletions releasenotes/notes/10787-078d7caa70fc7de8.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fix the Quantum Shannon Decomposition implemented in :func:`.qs_decomposition`. When a unitary
could not take advantage of the diagonal commutation optimization, it used to error.
Now, it leaves it as undecomposed 2-qubit unitary gate.
Fixes `#10787 <https://github.com/Qiskit/qiskit/issues/10787>`__
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

0 comments on commit 1630b6e

Please sign in to comment.