Skip to content

Commit

Permalink
Address comments, fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tanujkhattar committed Jul 6, 2024
1 parent 4dc4760 commit 45971d1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
9 changes: 6 additions & 3 deletions qualtran/_infra/binst_graph_iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
if TYPE_CHECKING:
from qualtran import BloqInstance

_INFINITY: int = int(1e16)
_ALLOCATION_PRIORITY: int = int(1e16)
"""A large constant value to ensure that allocations are performed as late as possible
and de-allocations (with -_ALLOCATION_PRIORITY priority) are performed as early as possible.
To determine ordering among allocations, we may add a priority to this base value."""


def _priority(node: 'BloqInstance') -> int:
Expand All @@ -31,10 +34,10 @@ def _priority(node: 'BloqInstance') -> int:
return 0

if node.bloq_is(Allocate):
return _INFINITY
return _ALLOCATION_PRIORITY

if node.bloq_is(Free):
return -_INFINITY
return -_ALLOCATION_PRIORITY

signature = node.bloq.signature
return total_bits(signature.rights()) - total_bits(signature.lefts())
Expand Down
50 changes: 26 additions & 24 deletions qualtran/bloqs/swap_network/swap_with_zero_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,51 +102,53 @@ def test_swap_with_zero_cirq_gate_diagram():
def test_swap_with_zero_cirq_gate_diagram_multi_dim():
gate = SwapWithZero((2, 1), 2, (3, 2))
gh = cq_testing.GateHelper(gate)
# Bloq -> Cirq conversion preserves insertion ordering when all operations are THRU
# operations
cirq.testing.assert_has_diagram(
cirq.Circuit(gh.operation, cirq.decompose_once(gh.operation)),
"""
┌──────────────────┐
selection0_0: ───────@(r⇋0)────────────────────────────────────────────────────@(approx)───
│ │
selection0_1: ───────@(r⇋0)──────────────────────────────@(approx)─────────────┼───────────
selection1_: ────────@(r⇋0)─────@(approx)───@(approx)────┼────────@(approx)────┼───────────
selection0_1: ───────@(r⇋0)───────────────────────────────────────@(approx)────┼───────────
selection1_: ────────@(r⇋0)─────@(approx)───@(approx)────@(approx)┼────────────┼───────────
│ │ │ │ │ │
targets[0, 0][0]: ───swap_0_0───×(x)────────┼────────────×(x)─────────────────×(x)────────
targets[0, 0][0]: ───swap_0_0───×(x)────────┼────────────────────×(x)─────────×(x)────────
│ │ │ │ │ │
targets[0, 0][1]: ───swap_0_0───×(x)────────┼────────────×(x)─────────────────×(x)────────
targets[0, 0][1]: ───swap_0_0───×(x)────────┼────────────────────×(x)─────────×(x)────────
│ │ │ │ │ │
targets[0, 1][0]: ───swap_0_1───×(y)────────┼────────────┼────────┼────────────┼───────────
│ │ │ │ │ │
targets[0, 1][1]: ───swap_0_1───×(y)────────┼────────────┼────────┼────────────┼───────────
│ │ │ │ │
targets[1, 0][0]: ───swap_1_0───────────────×(x)─────────×(y)─────────────────┼───────────
targets[1, 0][0]: ───swap_1_0───────────────×(x)─────────────────×(y)─────────┼───────────
│ │ │ │ │
targets[1, 0][1]: ───swap_1_0───────────────×(x)─────────×(y)─────┼────────────┼───────────
│ │ │ │
targets[1, 1][0]: ───swap_1_1───────────────×(y)──────────────────┼────────────┼───────────
│ │ │ │
targets[1, 1][1]: ───swap_1_1───────────────×(y)──────────────────┼────────────┼───────────
│ │ │
targets[2, 0][0]: ───swap_2_0─────────────────────────────────────×(x)─────────×(y)────────
│ │ │
targets[2, 0][1]: ───swap_2_0─────────────────────────────────────×(x)─────────×(y)────────
│ │
targets[2, 1][0]: ───swap_2_1─────────────────────────────────────×(y)─────────────────────
│ │
targets[2, 1][1]: ───swap_2_1─────────────────────────────────────×(y)─────────────────────
└──────────────────┘
""",
targets[1, 0][1]: ───swap_1_0───────────────×(x)─────────┼────────×(y)─────────┼───────────
│ │ │ │
targets[1, 1][0]: ───swap_1_1───────────────×(y)─────────┼─────────────────────┼───────────
│ │ │ │
targets[1, 1][1]: ───swap_1_1───────────────×(y)─────────┼─────────────────────┼───────────
│ │ │
targets[2, 0][0]: ───swap_2_0────────────────────────────×(x)──────────────────×(y)────────
│ │ │
targets[2, 0][1]: ───swap_2_0────────────────────────────×(x)──────────────────×(y)────────
│ │
targets[2, 1][0]: ───swap_2_1────────────────────────────×(y)──────────────────────────────
│ │
targets[2, 1][1]: ───swap_2_1────────────────────────────×(y)──────────────────────────────
└──────────────────┘""",
)


def test_swap_with_zero_classically():
data = np.array([131, 255, 92, 2])
swz = SwapWithZero(selection_bitsizes=2, target_bitsize=8, n_target_registers=4)

for sel in range(2**2):
sel, out_data = swz.call_classically(selection=sel, targets=data) # type: ignore[assignment]
print(sel, out_data)
for sel_in in range(2**2):
sel_out, out_data = swz.call_classically(selection=sel_in, targets=data) # type: ignore[assignment]
assert sel_in == sel_out
assert out_data[0] == data[sel_in]


@pytest.mark.parametrize(
Expand Down
8 changes: 5 additions & 3 deletions qualtran/cirq_interop/_cirq_to_bloq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ def signature(self) -> Signature:
cbloq = cirq_optree_to_cbloq(circuit)
assert cbloq.signature == qualtran.Signature([qualtran.Register('qubits', QBit(), shape=(28,))])
bloq_instances = [binst for binst, _, _ in cbloq.iter_bloqnections()]
assert all(bloq_instances[i].bloq == Join(QAny(2)) for i in range(14))
assert bloq_instances[14].bloq == CirqGateWithRegisters(reg1)
assert bloq_instances[14].bloq.signature == qualtran.Signature(
# Greedy iteration of iter_bloqnections first joins only qubits needed
# for the first gate.
assert all(bloq_instances[i].bloq == Join(QAny(2)) for i in range(12))
assert bloq_instances[12].bloq == CirqGateWithRegisters(reg1)
assert bloq_instances[12].bloq.signature == qualtran.Signature(
[qualtran.Register('x', QAny(bitsize=2), shape=(3, 4))]
)
assert bloq_instances[15].bloq == CirqGateWithRegisters(anc_reg)
Expand Down

0 comments on commit 45971d1

Please sign in to comment.