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 idle qubit detection to ignore barriers #639

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions circuit_knitting/utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ def _partition_labels_from_circuit(
# Determine which qubit wires are idle/unused
idle_wires = set(range(circuit.num_qubits))
for instruction in circuit.data:
if instruction.operation.name == "barrier":
continue
for q1 in instruction.qubits:
q1_id = circuit.find_bit(q1).index
idle_wires.discard(q1_id)
Expand Down
42 changes: 42 additions & 0 deletions test/utils/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,48 @@ def test_separate_circuit(self):
[("A", 0), ("B", 0), ("C", 0)], separated_circuits.qubit_map
)

with self.subTest("barriers are ignored"):
qreg = QuantumRegister(3)
circuit = QuantumCircuit(qreg)
circuit.h(0)
circuit.x(0)
circuit.barrier(0)
circuit.x(1)
circuit.h(1)
circuit.y(2)
circuit.h(2)

separated_circuits = separate_circuit(circuit, partition_labels="ABC")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I believe this test does not actually check that the relevant bug is fixed.

The idle wire detection code only runs when partition_labels is not provided (or is None). In this case, the partition labels are determined automatically from the connectivity of the circuit. #594 improved on this code so that idle wires are removed from the circuit during separation, but the problem is that any qubit with a barrier (e.g., if a barrier is placed over all qubits, even the idle ones) will be falsely detected as an active qubit. Your change should indeed fix #621 but the test does not ensure that it is fixed. The test is actually almost testing the check desired in #626, but not quite.


compare1 = QuantumCircuit(1)
compare1.h(0)
compare1.x(0)
compare2 = QuantumCircuit(1)
compare2.x(0)
compare2.h(0)
compare3 = QuantumCircuit(1)
compare3.y(0)
compare3.h(0)

for i, operation in enumerate(compare1.data):
self.assertEqual(
operation.operation.name,
separated_circuits.subcircuits["A"].data[i].operation.name,
)
for i, operation in enumerate(compare2.data):
self.assertEqual(
operation.operation.name,
separated_circuits.subcircuits["B"].data[i].operation.name,
)
for i, operation in enumerate(compare3.data):
self.assertEqual(
operation.operation.name,
separated_circuits.subcircuits["C"].data[i].operation.name,
)
self.assertEqual(
[("A", 0), ("B", 0), ("C", 0)], separated_circuits.qubit_map
)

with self.subTest("Test bit mapping with partition labels"):
# Prepare a HWEA and add some measurements to clbits in a random order
circuit = prepare_hwea()
Expand Down