Skip to content

Commit be11f91

Browse files
committed
finale
1 parent f9d7d31 commit be11f91

File tree

16 files changed

+310
-40
lines changed

16 files changed

+310
-40
lines changed

qualtran/_infra/registers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,11 @@ def n_qubits(self) -> int:
188188
is taken to be the greater of the number of left or right qubits. A bloq with this
189189
signature uses at least this many qubits.
190190
"""
191+
from qualtran.resource_counting.symbolic_counting_utils import smax
192+
191193
left_size = sum(reg.total_bits() for reg in self.lefts())
192194
right_size = sum(reg.total_bits() for reg in self.rights())
193-
return max(left_size, right_size)
195+
return smax(left_size, right_size)
194196

195197
def __repr__(self):
196198
return f'Signature({repr(self._registers)})'

qualtran/_infra/registers_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import cirq
1616
import numpy as np
1717
import pytest
18+
import sympy
1819

1920
from qualtran import BoundedQUInt, QAny, QBit, QInt, Register, Side, Signature
2021
from qualtran._infra.gate_with_registers import get_named_qubits
@@ -196,3 +197,12 @@ def test_dtypes_converter():
196197
r2 = Register("my_reg", QAny(5))
197198
r2 = Register("my_reg", QInt(5))
198199
assert r1 != r2
200+
201+
202+
def test_symbolic_reg():
203+
n = sympy.Symbol('n', positive=True, integer=True)
204+
sig = Signature(
205+
[Register('x', QAny(n), side=Side.LEFT), Register('y', QAny(2 * n), side=Side.RIGHT)]
206+
)
207+
208+
assert sig.n_qubits() == 2 * n

qualtran/bloqs/basic_gates/hadamard.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ def short_name(self) -> 'str':
9898
def wire_symbol(self, soq: 'Soquet') -> 'WireSymbol':
9999
return TextBox('H')
100100

101+
def __str__(self):
102+
return 'H'
103+
101104

102105
@bloq_example
103106
def _hadamard() -> Hadamard:

qualtran/bloqs/basic_gates/on_each_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ def test_classical_simulation():
4242
h_on_each = OnEach(10, Hadamard())
4343
with pytest.raises(
4444
NotImplementedError,
45-
match=r'.*does not support classical simulation: '
46-
r'Hadamard\(\) is not classically simulable\.',
45+
match=r'.*does not support classical simulation: ' r'H is not classically simulable\.',
4746
):
4847
h_on_each.call_classically(q=0)
4948

qualtran/bloqs/basic_gates/t_gate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ def pretty_name(self) -> str:
108108
return f'T{maybe_dag}'
109109

110110
def __str__(self):
111-
maybe_dag = 'is_adjoint=True' if self.is_adjoint else ''
112-
return f'TGate({maybe_dag})'
111+
maybe_dag = '' if self.is_adjoint else ''
112+
return f'T{maybe_dag}'
113113

114114
def wire_symbol(self, soq: 'Soquet') -> 'WireSymbol':
115115
return TextBox(self.pretty_name())

qualtran/bloqs/basic_gates/toffoli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ def wire_symbol(self, soq: 'Soquet') -> 'WireSymbol':
121121
return ModPlus()
122122
raise ValueError(f'Bad wire symbol soquet: {soq}')
123123

124+
def __str__(self):
125+
return 'Toffoli'
126+
124127

125128
@bloq_example
126129
def _toffoli() -> Toffoli:

qualtran/bloqs/chemistry/resource_estimation.ipynb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@
133133
"from qualtran.drawing.musical_score import get_musical_score_data, draw_musical_score\n",
134134
"msd = get_musical_score_data(block_encoding_bloq.decompose_bloq())\n",
135135
"fig, ax = draw_musical_score(msd)\n",
136-
"plt.tick_params(left=False, right=False, labelleft=False, labelbottom=False, bottom=False)\n",
137136
"fig.set_size_inches(8, 4)"
138137
]
139138
},
@@ -185,6 +184,47 @@
185184
"print(f'qualtran = {num_toff} vs. ref = 10880, delta = {num_toff - 10880}')"
186185
]
187186
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"id": "e79d3c99-cd23-4333-a177-6d6ab3dca72a",
191+
"metadata": {},
192+
"outputs": [],
193+
"source": [
194+
"# qualtran = 26749.0 vs. ref = 10880, delta = 15869.0"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": null,
200+
"id": "c61a4b30-b875-4414-b198-e08774df0c4a",
201+
"metadata": {},
202+
"outputs": [],
203+
"source": [
204+
"from qualtran.resource_counting import BloqCount, query_costs, get_cost_value, QECGatesCost\n",
205+
"from qualtran.resource_counting.generalizers import ignore_alloc_free, ignore_split_join, generalize_rotation_angle"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"id": "a126c934-1528-425a-aa4d-93a4bb880236",
212+
"metadata": {},
213+
"outputs": [],
214+
"source": [
215+
"get_cost_value(block_encoding_bloq, BloqCount.for_gateset(\"t+tof+cswap\"))"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": null,
221+
"id": "e68450ff-d582-400f-abd1-f3d24dd43979",
222+
"metadata": {},
223+
"outputs": [],
224+
"source": [
225+
"46976/4 + 30480/4 + 7105 + 280"
226+
]
227+
},
188228
{
189229
"cell_type": "markdown",
190230
"id": "dbd1615f",

qualtran/bloqs/data_loading/qrom.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Quantum read-only memory."""
1616

1717
from functools import cached_property
18-
from typing import Callable, Dict, Iterable, Sequence, Set, Tuple
18+
from typing import Any, Callable, Dict, Iterable, Sequence, Set, Tuple, Union
1919

2020
import attrs
2121
import cirq
@@ -28,7 +28,7 @@
2828
from qualtran.bloqs.mcmt.and_bloq import And, MultiAnd
2929
from qualtran.bloqs.multiplexers.unary_iteration_bloq import UnaryIterationGate
3030
from qualtran.drawing import Circle, TextBox, WireSymbol
31-
from qualtran.resource_counting import BloqCountT
31+
from qualtran.resource_counting import BloqCountT, CostKey, QubitCount
3232
from qualtran.simulation.classical_sim import ClassicalValT
3333

3434

@@ -203,6 +203,11 @@ def on_classical_vals(self, **vals: 'ClassicalValT') -> Dict[str, 'ClassicalValT
203203
targets = {k: v ^ vals[k] for k, v in targets.items()}
204204
return controls | selections | targets
205205

206+
def my_static_costs(self, cost_key: 'CostKey') -> Union[Any, NotImplemented]:
207+
if cost_key == QubitCount():
208+
return self.num_controls + 2 * sum(self.selection_bitsizes) + sum(self.target_bitsizes)
209+
return super().my_static_costs(cost_key)
210+
206211
def _circuit_diagram_info_(self, _) -> cirq.CircuitDiagramInfo:
207212
wire_symbols = ["@"] * self.num_controls
208213
wire_symbols += ["In"] * total_bits(self.selection_registers)
@@ -240,6 +245,9 @@ def nth_operation_callgraph(self, **kwargs: int) -> Set['BloqCountT']:
240245
selection_idx = tuple(kwargs[reg.name] for reg in self.selection_registers)
241246
return {(CNOT(), sum(int(d[selection_idx]).bit_count() for d in self.data))}
242247

248+
def __str__(self):
249+
return 'QROM'
250+
243251

244252
@bloq_example
245253
def _qrom_small() -> QROM:

qualtran/bloqs/data_loading/select_swap_qrom.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,6 @@ def short_name(self) -> str:
254254

255255
def _value_equality_values_(self):
256256
return self.block_size, self._target_bitsizes, self.data
257+
258+
def __str__(self):
259+
return 'SelectSwapQROM'

qualtran/bloqs/for_testing/costing_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def test_costing_bloqs():
2424
== """\
2525
Algo -- 1 -> Func1
2626
Algo -- 1 -> Func2
27-
Func1 -- 10 -> Hadamard()
28-
Func1 -- 10 -> TGate()
29-
Func1 -- 10 -> TGate(is_adjoint=True)
30-
Func2 -- 100 -> Toffoli()
31-
Toffoli() -- 4 -> TGate()"""
27+
Func1 -- 10 -> H
28+
Func1 -- 10 -> T
29+
Func1 -- 10 -> T†
30+
Func2 -- 100 -> Toffoli
31+
Toffoli -- 4 -> T"""
3232
)

0 commit comments

Comments
 (0)