diff --git a/qiskit/transpiler/passes/routing/sabre_swap.py b/qiskit/transpiler/passes/routing/sabre_swap.py index 0e37bc8b4b7d..bd5f2152fca2 100644 --- a/qiskit/transpiler/passes/routing/sabre_swap.py +++ b/qiskit/transpiler/passes/routing/sabre_swap.py @@ -210,11 +210,17 @@ def run(self, dag): dag_list = [] for node in dag.topological_op_nodes(): + cargs = [] + if node.op.condition is not None and not node.cargs: + cargs = [self._clbit_indices[x] for x in dag._bits_in_condition(node.op.condition)] + else: + cargs = [self._clbit_indices[x] for x in node.cargs] + dag_list.append( ( node._node_id, [self._qubit_indices[x] for x in node.qargs], - [self._clbit_indices[x] for x in node.cargs], + cargs, ) ) front_layer = np.asarray([x._node_id for x in dag.front_layer()], dtype=np.uintp) diff --git a/test/python/transpiler/test_sabre_swap.py b/test/python/transpiler/test_sabre_swap.py index 76c14535433f..f25cbf04cdcb 100644 --- a/test/python/transpiler/test_sabre_swap.py +++ b/test/python/transpiler/test_sabre_swap.py @@ -262,6 +262,35 @@ def test_classical_condition(self): actual = PassManager([TrivialLayout(cm), SabreSwap(cm)]).run(qc) self.assertEqual(expected, actual) + def test_classical_condition_cargs(self): + """Test that classical conditions are preserved even if missing from cargs DAGNode field. + + Created from reproduction in https://github.com/Qiskit/qiskit-terra/issues/8675 + """ + with self.subTest("missing measurement"): + qc = QuantumCircuit(3, 1) + qc.cx(0, 2).c_if(0, 0) + qc.measure(1, 0) + qc.h(2).c_if(0, 0) + expected = QuantumCircuit(3, 1) + expected.swap(1, 2) + expected.cx(0, 1).c_if(0, 0) + expected.measure(2, 0) + expected.h(1).c_if(0, 0) + result = SabreSwap(CouplingMap.from_line(3), seed=12345)(qc) + self.assertEqual(result, expected) + with self.subTest("reordered measurement"): + qc = QuantumCircuit(3, 1) + qc.cx(0, 1).c_if(0, 0) + qc.measure(1, 0) + qc.h(0).c_if(0, 0) + expected = QuantumCircuit(3, 1) + expected.cx(0, 1).c_if(0, 0) + expected.measure(1, 0) + expected.h(0).c_if(0, 0) + result = SabreSwap(CouplingMap.from_line(3), seed=12345)(qc) + self.assertEqual(result, expected) + if __name__ == "__main__": unittest.main()