Skip to content

Commit 15cb661

Browse files
Fix AutoPartition indexing bug with multiple Unused (#1198)
1 parent e1bf394 commit 15cb661

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

qualtran/bloqs/bookkeeping/auto_partition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ def build_composite_bloq(self, bb: BloqBuilder, **soqs: SoquetT) -> Dict[str, So
106106
parts: Dict[str, Partition] = dict()
107107
in_regs: Dict[str, SoquetT] = dict()
108108
unused_regs: Dict[str, SoquetT] = dict()
109-
for out_reg, bloq_regs in self.partitions:
109+
for parti, (out_reg, bloq_regs) in enumerate(self.partitions):
110110
part = Partition(
111111
out_reg.bitsize,
112112
regs=tuple(
113113
(
114114
self.bloq.signature.get_left(r)
115115
if isinstance(r, str)
116-
else Register(f"_unused{i}", QAny(r.bitsize))
116+
else Register(f"_unused{parti}_{i}", QAny(r.bitsize))
117117
)
118118
for i, r in enumerate(bloq_regs)
119119
),

qualtran/bloqs/bookkeeping/auto_partition_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import pytest
1717
from attrs import evolve
1818

19+
from qualtran import Controlled, CtrlSpec, QAny, Register
20+
from qualtran.bloqs.basic_gates import Swap
1921
from qualtran.bloqs.bookkeeping.auto_partition import (
2022
_auto_partition,
2123
_auto_partition_unused,
@@ -172,5 +174,38 @@ def test_auto_partition_unused():
172174
_ = evolve(bloq, left_only=True)
173175

174176

177+
def test_auto_partition_unused_index():
178+
bloq = Controlled(Swap(1), CtrlSpec())
179+
bloq = AutoPartition(
180+
bloq,
181+
[
182+
(Register('x', QAny(3)), [Unused(1), 'ctrl', 'x']),
183+
(Register('y', QAny(1)), ['y']),
184+
(Register('z', QAny(2)), [Unused(2)]),
185+
],
186+
)
187+
188+
assert tuple(bloq.signature.lefts()) == (
189+
Register('x', dtype=QAny(3)),
190+
Register('y', dtype=QAny(1)),
191+
Register('z', dtype=QAny(2)),
192+
)
193+
194+
assert tuple(bloq.signature.rights()) == tuple(bloq.signature.lefts())
195+
196+
x, y, z = bloq.call_classically(x=0b111, y=0b0, z=0b10)
197+
assert x == 0b110
198+
assert y == 0b1
199+
assert z == 0b10
200+
201+
x, y, z = bloq.call_classically(x=0b001, y=0b0, z=0b01)
202+
assert x == 0b001
203+
assert y == 0b0
204+
assert z == 0b01
205+
206+
with pytest.raises(ValueError):
207+
_ = evolve(bloq, left_only=True)
208+
209+
175210
def test_no_circular_import():
176211
subprocess.check_call(['python', '-c', 'from qualtran.bloqs.bookkeeping import auto_partition'])

0 commit comments

Comments
 (0)