Skip to content

Commit

Permalink
Fix decomposition for a single qubit and clbit (#8614) (#8616)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit c008008)

Co-authored-by: Julien Gacon <gaconju@gmail.com>
  • Loading branch information
mergify[bot] and Cryoris authored Aug 25, 2022
1 parent 23c5af5 commit f1d1374
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion qiskit/transpiler/passes/basis/decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def run(self, dag: DAGCircuit) -> DAGCircuit:
continue
# TODO: allow choosing among multiple decomposition rules
rule = node.op.definition.data
if len(rule) == 1 and len(node.qargs) == len(rule[0].qubits) == 1:
if (
len(rule) == 1
and len(node.qargs) == len(rule[0].qubits) == 1 # to preserve gate order
and len(node.cargs) == len(rule[0].clbits) == 0
):
if node.op.definition.global_phase:
dag.global_phase += node.op.definition.global_phase
dag.substitute_node(node, rule[0].operation, inplace=True)
Expand Down
13 changes: 13 additions & 0 deletions releasenotes/notes/fix-decomp-1q-1c-84f369f9a897a5b7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
fixes:
- |
Fixed a bug where decomposing an instruction with one qubit and one classical bit
containing a single quantum gate failed. Now the following decomposes as expected::
block = QuantumCircuit(1, 1)
block.h(0)
circuit = QuantumCircuit(1, 1)
circuit.append(block, [0], [0])
decomposed = circuit.decompose()
15 changes: 15 additions & 0 deletions test/python/transpiler/test_decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,18 @@ def test_decompose_reps(self):
decom_circ = self.complex_circuit.decompose(reps=2)
decomposed = self.complex_circuit.decompose().decompose()
self.assertEqual(decom_circ, decomposed)

def test_decompose_single_qubit_clbit(self):
"""Test the decomposition of a block with a single qubit and clbit works.
Regression test of Qiskit/qiskit-terra#8591.
"""
block = QuantumCircuit(1, 1)
block.h(0)

circuit = QuantumCircuit(1, 1)
circuit.append(block, [0], [0])

decomposed = circuit.decompose()

self.assertEqual(decomposed, block)

0 comments on commit f1d1374

Please sign in to comment.