Skip to content

Commit

Permalink
Fix BarrierBeforeFinalMeasurements with loose bits (#8924)
Browse files Browse the repository at this point in the history
We simply need to consider loose bits as well when constructing the new
DAG layer.  This isn't `DAGCircuit.copy_empty_like` because we don't
want things like metadata and global phase.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit 64c5eb6)
  • Loading branch information
jakelishman authored and mergify[bot] committed Oct 18, 2022
1 parent e0befd7 commit 2fa35a9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ def run(self, dag):
if not final_ops:
return dag

# Create a layer with the barrier and add registers from the original dag.
# Create a layer with the barrier and add both bits and registers from the original dag.
barrier_layer = DAGCircuit()
barrier_layer.add_qubits(dag.qubits)
for qreg in dag.qregs.values():
barrier_layer.add_qreg(qreg)
barrier_layer.add_clbits(dag.clbits)
for creg in dag.cregs.values():
barrier_layer.add_creg(creg)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed the :class:`.BarrierBeforeFinalMeasurements` transpiler pass when there
are conditions on loose :class:`.Clbit`\ s immediately before the final measurement
layer. Previously, this would fail claiming that the bit was not present
in an internal temporary circuit.
24 changes: 23 additions & 1 deletion test/python/transpiler/test_barrier_before_final_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest
from qiskit.transpiler.passes import BarrierBeforeFinalMeasurements
from qiskit.converters import circuit_to_dag
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
from qiskit.circuit import QuantumRegister, QuantumCircuit, ClassicalRegister, Clbit
from qiskit.test import QiskitTestCase


Expand Down Expand Up @@ -370,6 +370,28 @@ def test_barrier_doesnt_reorder_gates(self):

self.assertEqual(result, circuit_to_dag(expected))

def test_conditioned_on_single_bit(self):
"""Test that the pass can handle cases where there is a loose-bit condition."""
circuit = QuantumCircuit(QuantumRegister(3), ClassicalRegister(2), [Clbit()])
circuit.h(range(3))
circuit.measure(range(3), range(3))
circuit.h(0).c_if(circuit.cregs[0], 3)
circuit.h(1).c_if(circuit.clbits[-1], True)
circuit.h(2).c_if(circuit.clbits[-1], False)
circuit.measure(range(3), range(3))

expected = circuit.copy_empty_like()
expected.h(range(3))
expected.measure(range(3), range(3))
expected.h(0).c_if(expected.cregs[0], 3)
expected.h(1).c_if(expected.clbits[-1], True)
expected.h(2).c_if(expected.clbits[-1], False)
expected.barrier(range(3))
expected.measure(range(3), range(3))

pass_ = BarrierBeforeFinalMeasurements()
self.assertEqual(expected, pass_(circuit))


if __name__ == "__main__":
unittest.main()

0 comments on commit 2fa35a9

Please sign in to comment.