Skip to content

Commit c85a925

Browse files
mergify[bot]ElePTalexanderivriimtreinish
authored
Fix classical bit mapping in HLS pass (#14597) (#14624)
* Map classical bits from block to outer circuit * Add regression test * Add reno * Avoid allocating cargs Previously the cargs were being written to a Vec<usize> as an intermediate storage, however we are just returning them directly and don't need to allocate or change the types. This commit just works with the slice returned from the interner directly which both simplifies the code and should also be faster. --------- (cherry picked from commit 9ec464a) Co-authored-by: Elena Peña Tapia <[email protected]> Co-authored-by: Alexander Ivrii <[email protected]> Co-authored-by: Matthew Treinish <[email protected]>
1 parent cdc292d commit c85a925

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

crates/accelerate/src/high_level_synthesis.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ fn run_on_circuitdata(
477477
.iter()
478478
.map(|q| input_qubits[q.index()])
479479
.collect::<Vec<usize>>();
480+
let op_clbits = input_circuit.get_cargs(inst.clbits);
480481

481482
// Start by handling special operations.
482483
// In the future, we can also consider other possible optimizations, e.g.:
@@ -629,8 +630,10 @@ fn run_on_circuitdata(
629630
.iter()
630631
.map(|q| Qubit::new(qubit_map[&q.index()]))
631632
.collect();
632-
let inst_outer_clbits: Vec<Clbit> =
633-
inst_inner_clbits.iter().map(|c| Clbit(c.0)).collect();
633+
let inst_outer_clbits: Vec<Clbit> = inst_inner_clbits
634+
.iter()
635+
.map(|c| op_clbits[c.0 as usize])
636+
.collect();
634637

635638
output_circuit.push_packed_operation(
636639
inst_inner.op.clone(),
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a bug in the :class:`.HighLevelSynthesis` pass where, if the circuit contained high level objects
5+
with classical registers, these would get mapped to the relative index in the object instead of the
6+
corresponding index in the outer circuit. The classical registers are now correctly mapped to the outer
7+
circuit index.

test/python/transpiler/test_high_level_synthesis.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,48 @@ def test_unroll_with_clbit(self):
21202120

21212121
self.assertEqual(block, out)
21222122

2123+
def test_unroll_with_clbit_mapping(self):
2124+
"""Test unrolling a custom definition that has qubits and clbits
2125+
that require mapping to the global clbits.
2126+
Regression test for: https://github.com/Qiskit/qiskit/issues/14569
2127+
"""
2128+
block = QuantumCircuit(2, 2)
2129+
block.h(0)
2130+
block.measure([0, 1], [0, 1])
2131+
2132+
circuit = QuantumCircuit(6, 6)
2133+
circuit.append(block.to_instruction(), [0, 1], [0, 1])
2134+
circuit.append(block.to_instruction(), [2, 3], [3, 2])
2135+
circuit.append(block.to_instruction(), [4, 5], [4, 5])
2136+
2137+
hls = HighLevelSynthesis(basis_gates=["h", "measure"])
2138+
out = hls(circuit)
2139+
2140+
self.assertEqual(
2141+
(out.find_bit(out.data[3].qubits[0]).index, out.find_bit(out.data[3].clbits[0]).index),
2142+
(0, 0),
2143+
)
2144+
self.assertEqual(
2145+
(out.find_bit(out.data[4].qubits[0]).index, out.find_bit(out.data[4].clbits[0]).index),
2146+
(1, 1),
2147+
)
2148+
self.assertEqual(
2149+
(out.find_bit(out.data[5].qubits[0]).index, out.find_bit(out.data[5].clbits[0]).index),
2150+
(3, 2),
2151+
)
2152+
self.assertEqual(
2153+
(out.find_bit(out.data[6].qubits[0]).index, out.find_bit(out.data[6].clbits[0]).index),
2154+
(2, 3),
2155+
)
2156+
self.assertEqual(
2157+
(out.find_bit(out.data[7].qubits[0]).index, out.find_bit(out.data[7].clbits[0]).index),
2158+
(4, 4),
2159+
)
2160+
self.assertEqual(
2161+
(out.find_bit(out.data[8].qubits[0]).index, out.find_bit(out.data[8].clbits[0]).index),
2162+
(5, 5),
2163+
)
2164+
21232165

21242166
class TestGate(Gate):
21252167
"""Mock one qubit zero param gate."""

0 commit comments

Comments
 (0)