Skip to content

Commit

Permalink
Fix panic in SabreSwap with classical bits
Browse files Browse the repository at this point in the history
This commit fixes a bug introduced in SabreSwap that was caught by the
randomized testing in Qiskit#8635. A copy paste error was causing the rusty
sabre code to panic in cases where there were classical bits of a
particular index assigned prior to the qubit with the same index. This
commit fixes the typo so the behavior is corrected in general and the
panic is also fixed.

Fixes Qiskit#8635
  • Loading branch information
mtreinish committed Aug 30, 2022
1 parent bab9d45 commit df96be4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sabre_swap/sabre_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl SabreDAG {
}
for x in cargs {
if clbit_pos[*x] != usize::MAX {
dag.add_edge(NodeIndex::new(qubit_pos[*x]), gate_index, ());
dag.add_edge(NodeIndex::new(clbit_pos[*x]), gate_index, ());
}
clbit_pos[*x] = gate_index.index();
}
Expand Down
39 changes: 39 additions & 0 deletions test/python/transpiler/test_sabre_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from qiskit.transpiler.passes import SabreLayout
from qiskit.converters import circuit_to_dag
from qiskit.test import QiskitTestCase
from qiskit.compiler.transpiler import transpile
from qiskit.providers.fake_provider import FakeAlmaden
from qiskit.providers.fake_provider import FakeKolkata


class TestSabreLayout(QiskitTestCase):
Expand Down Expand Up @@ -99,6 +101,43 @@ def test_6q_circuit_20q_coupling(self):
self.assertEqual(layout[qr1[1]], 12)
self.assertEqual(layout[qr1[2]], 11)

def test_layout_with_classical_bits(self):
"""Test sabre layout with classical bits recreate from issue #8635."""
qc = QuantumCircuit.from_qasm_str("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q4833[1];
qreg q4834[6];
qreg q4835[7];
creg c982[2];
creg c983[2];
creg c984[2];
rzz(0) q4833[0],q4834[4];
cu(0,-6.1035156e-05,0,1e-05) q4834[1],q4835[2];
swap q4834[0],q4834[2];
cu(-1.1920929e-07,0,-0.33333333,0) q4833[0],q4834[2];
ccx q4835[2],q4834[5],q4835[4];
measure q4835[4] -> c984[0];
ccx q4835[2],q4835[5],q4833[0];
measure q4835[5] -> c984[1];
measure q4834[0] -> c982[1];
u(10*pi,0,1.9) q4834[5];
measure q4834[3] -> c984[1];
measure q4835[0] -> c982[0];
rz(0) q4835[1];
""")
res = transpile(qc, FakeKolkata(), layout_method="sabre", seed_transpiler=1234)
self.assertIsInstance(res, QuantumCircuit)
layout = res._layout
self.assertEqual(layout[qc.qubits[0]], 14)
self.assertEqual(layout[qc.qubits[1]], 19)
self.assertEqual(layout[qc.qubits[2]], 7)
self.assertEqual(layout[qc.qubits[3]], 13)
self.assertEqual(layout[qc.qubits[4]], 6)
self.assertEqual(layout[qc.qubits[5]], 16)
self.assertEqual(layout[qc.qubits[6]], 18)
self.assertEqual(layout[qc.qubits[7]], 26)


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

0 comments on commit df96be4

Please sign in to comment.