Skip to content

Commit

Permalink
Collect classical registers using a set (#10496) (#10505) (#11157)
Browse files Browse the repository at this point in the history
* Collect classical registers using a set (#10496)

This adds a test case that adds multiple conditions on the same
register. The test case is fixed by using a set to collect the
classical registers in collapse_to_operation.

* Add release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit b47c28b)

Co-authored-by: Chris Harris <cjh@lbl.gov>
  • Loading branch information
mergify[bot] and cjh1 authored Nov 1, 2023
1 parent 79f09d6 commit 4b7b117
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions qiskit/dagcircuit/collect_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def collapse_to_operation(self, blocks, collapse_fn):

# Additionally, find the set of classical registers used in conditions over full registers
# (in such a case, we need to add that register to the block circuit, not just its clbits).
cur_clregs = []
cur_clregs = set()

for node in block:
cur_qubits.update(node.qargs)
Expand All @@ -355,7 +355,7 @@ def collapse_to_operation(self, blocks, collapse_fn):
if cond is not None:
cur_clbits.update(condition_resources(cond).clbits)
if isinstance(cond[0], ClassicalRegister):
cur_clregs.append(cond[0])
cur_clregs.add(cond[0])

# For reproducibility, order these qubits/clbits compatibly with the global order.
sorted_qubits = sorted(cur_qubits, key=lambda x: global_index_map[x])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
The :class:`.BlockCollapser` transpiler pass will now correctly handle circuits that contain
more than one condition on the same classical register.
17 changes: 17 additions & 0 deletions test/python/dagcircuit/test_collect_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,23 @@ def test_split_layers_dagdependency(self):
self.assertEqual(len(blocks[2]), 1)
self.assertEqual(len(blocks[3]), 1)

def test_block_collapser_register_condition(self):
"""Test that BlockCollapser can handle a register being used more than once."""
qc = QuantumCircuit(1, 2)
qc.x(0).c_if(qc.cregs[0], 0)
qc.y(0).c_if(qc.cregs[0], 1)

dag = circuit_to_dag(qc)
blocks = BlockCollector(dag).collect_all_matching_blocks(
lambda _: True, split_blocks=False, min_block_size=1
)
dag = BlockCollapser(dag).collapse_to_operation(blocks, lambda circ: circ.to_instruction())
collapsed_qc = dag_to_circuit(dag)

self.assertEqual(len(collapsed_qc.data), 1)
self.assertEqual(collapsed_qc.data[0].operation.definition.num_qubits, 1)
self.assertEqual(collapsed_qc.data[0].operation.definition.num_clbits, 2)


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

0 comments on commit 4b7b117

Please sign in to comment.