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 handling of conditions in SabreDAG creation #8727

Merged
merged 6 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
9 changes: 8 additions & 1 deletion qiskit/transpiler/passes/routing/sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,18 @@ def run(self, dag):

dag_list = []
for node in dag.topological_op_nodes():
cargs = [self._clbit_indices[x] for x in node.cargs]
if node.op.condition is not None:
for clbit in dag._bits_in_condition(node.op.condition):
clbit_index = self._clbit_indices[clbit]
if clbit_index not in cargs:
cargs.append(clbit_index)
mtreinish marked this conversation as resolved.
Show resolved Hide resolved

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)
Expand Down
45 changes: 45 additions & 0 deletions test/python/transpiler/test_sabre_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,51 @@ 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)

def test_conditional_measurement(self):
"""Test that instructions with cargs and conditions are handled correctly."""
qc = QuantumCircuit(3, 2)
qc.cx(0, 2).c_if(0, 0)
qc.measure(2, 0).c_if(1, 0)
qc.h(2).c_if(0, 0)
qc.measure(1, 1)
expected = QuantumCircuit(3, 2)
expected.swap(1, 2)
expected.cx(0, 1).c_if(0, 0)
expected.measure(1, 0).c_if(1, 0)
expected.h(1).c_if(0, 0)
expected.measure(2, 1)
result = SabreSwap(CouplingMap.from_line(3), seed=12345)(qc)
self.assertEqual(result, expected)


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