Skip to content

Commit 8909a9c

Browse files
committed
Merge branch 'main' into 2024/SR/planted-noisy-kxor-algorithm
2 parents 490afa9 + 64acb52 commit 8909a9c

21 files changed

+367
-194
lines changed

qualtran/_infra/adjoint_test.py

+2-38
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,20 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from functools import cached_property
15-
from typing import cast, Dict, TYPE_CHECKING
14+
from typing import cast
1615

1716
import pytest
1817
import sympy
1918

2019
import qualtran.testing as qlt_testing
21-
from qualtran import Adjoint, Bloq, CompositeBloq, Side, Signature
20+
from qualtran import Adjoint, CompositeBloq, Side
2221
from qualtran._infra.adjoint import _adjoint_cbloq
2322
from qualtran.bloqs.basic_gates import CNOT, CSwap, ZeroState
2423
from qualtran.bloqs.for_testing.atom import TestAtom
2524
from qualtran.bloqs.for_testing.with_call_graph import TestBloqWithCallGraph
2625
from qualtran.bloqs.for_testing.with_decomposition import TestParallelCombo, TestSerialCombo
27-
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
2826
from qualtran.drawing import LarrowTextBox, RarrowTextBox, Text
2927

30-
if TYPE_CHECKING:
31-
from qualtran import BloqBuilder, SoquetT
32-
3328

3429
def test_serial_combo_adjoint():
3530
# The normal decomposition is three `TestAtom` tagged atom{0,1,2}.
@@ -168,37 +163,6 @@ def test_wire_symbol():
168163
assert isinstance(adj_ws, RarrowTextBox)
169164

170165

171-
class TAcceptsAdjoint(TestAtom):
172-
def _t_complexity_(self, adjoint: bool = False) -> TComplexity:
173-
return TComplexity(t=2 if adjoint else 1)
174-
175-
176-
class TDoesNotAcceptAdjoint(TestAtom):
177-
def _t_complexity_(self) -> TComplexity:
178-
return TComplexity(t=3)
179-
180-
181-
class DecomposesIntoTAcceptsAdjoint(Bloq):
182-
@cached_property
183-
def signature(self) -> Signature:
184-
return Signature.build(q=1)
185-
186-
def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> Dict[str, 'SoquetT']:
187-
soqs = bb.add_d(TAcceptsAdjoint(), **soqs)
188-
return soqs
189-
190-
191-
def test_t_complexity():
192-
assert TAcceptsAdjoint().t_complexity().t == 1
193-
assert Adjoint(TAcceptsAdjoint()).t_complexity().t == 2
194-
195-
assert DecomposesIntoTAcceptsAdjoint().t_complexity().t == 1
196-
assert Adjoint(DecomposesIntoTAcceptsAdjoint()).t_complexity().t == 2
197-
198-
assert TDoesNotAcceptAdjoint().t_complexity().t == 3
199-
assert Adjoint(TDoesNotAcceptAdjoint()).t_complexity().t == 3
200-
201-
202166
@pytest.mark.notebook
203167
def test_notebook():
204168
qlt_testing.execute_notebook('../Adjoint')

qualtran/bloqs/arithmetic/comparison_test.py

-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ def test_clineardepthgreaterthan_classical_action_unsigned(ctrl, dtype, bitsize)
338338
cb = b.decompose_bloq()
339339
for c, target in itertools.product(range(2), repeat=2):
340340
for (x, y) in itertools.product(range(2**bitsize), repeat=2):
341-
print(f'{c=} {target=} {x=} {y=}')
342341
assert b.call_classically(ctrl=c, a=x, b=y, target=target) == cb.call_classically(
343342
ctrl=c, a=x, b=y, target=target
344343
)
@@ -351,7 +350,6 @@ def test_clineardepthgreaterthan_classical_action_signed(ctrl, bitsize):
351350
cb = b.decompose_bloq()
352351
for c, target in itertools.product(range(2), repeat=2):
353352
for (x, y) in itertools.product(range(-(2 ** (bitsize - 1)), 2 ** (bitsize - 1)), repeat=2):
354-
print(f'{c=} {target=} {x=} {y=}')
355353
assert b.call_classically(ctrl=c, a=x, b=y, target=target) == cb.call_classically(
356354
ctrl=c, a=x, b=y, target=target
357355
)

qualtran/bloqs/arithmetic/controlled_addition.py

-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from qualtran.bloqs.arithmetic.addition import Add
3636
from qualtran.bloqs.bookkeeping import Cast
3737
from qualtran.bloqs.mcmt.and_bloq import And
38-
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
3938
from qualtran.resource_counting.generalizers import ignore_split_join
4039
from qualtran.simulation.classical_sim import add_ints
4140

@@ -156,12 +155,6 @@ def build_composite_bloq(
156155
ctrl = bb.join(np.array([ctrl_q]))
157156
return {'ctrl': ctrl, 'a': a, 'b': b}
158157

159-
def _t_complexity_(self):
160-
n = self.b_dtype.bitsize
161-
num_and = self.a_dtype.bitsize + self.b_dtype.bitsize - 1
162-
num_clifford = 33 * (n - 2) + 43
163-
return TComplexity(t=4 * num_and, clifford=num_clifford)
164-
165158
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
166159
return {
167160
(And(self.cv, 1), self.a_dtype.bitsize),

qualtran/bloqs/arithmetic/permutation.ipynb

+64-31
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "a48878df",
5+
"id": "a697fa0f",
66
"metadata": {
77
"cq.autogen": "title_cell"
88
},
@@ -13,7 +13,7 @@
1313
{
1414
"cell_type": "code",
1515
"execution_count": null,
16-
"id": "d083d346",
16+
"id": "f3a56020",
1717
"metadata": {
1818
"cq.autogen": "top_imports"
1919
},
@@ -30,7 +30,7 @@
3030
},
3131
{
3232
"cell_type": "markdown",
33-
"id": "56539662",
33+
"id": "39be898d",
3434
"metadata": {
3535
"cq.autogen": "Permutation.bloq_doc.md"
3636
},
@@ -62,7 +62,7 @@
6262
{
6363
"cell_type": "code",
6464
"execution_count": null,
65-
"id": "df73124f",
65+
"id": "88b7712d",
6666
"metadata": {
6767
"cq.autogen": "Permutation.bloq_doc.py"
6868
},
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"cell_type": "markdown",
76-
"id": "31147b0d",
76+
"id": "5c4722c6",
7777
"metadata": {
7878
"cq.autogen": "Permutation.example_instances.md"
7979
},
@@ -84,7 +84,7 @@
8484
{
8585
"cell_type": "code",
8686
"execution_count": null,
87-
"id": "ec6460b6",
87+
"id": "8e38bf3a",
8888
"metadata": {
8989
"cq.autogen": "Permutation.permutation"
9090
},
@@ -96,7 +96,7 @@
9696
{
9797
"cell_type": "code",
9898
"execution_count": null,
99-
"id": "0c8e04f0",
99+
"id": "52218544",
100100
"metadata": {
101101
"cq.autogen": "Permutation.permutation_symb"
102102
},
@@ -113,7 +113,7 @@
113113
{
114114
"cell_type": "code",
115115
"execution_count": null,
116-
"id": "8578d036",
116+
"id": "cb1a6988",
117117
"metadata": {
118118
"cq.autogen": "Permutation.permutation_symb_with_cycles"
119119
},
@@ -132,7 +132,7 @@
132132
{
133133
"cell_type": "code",
134134
"execution_count": null,
135-
"id": "84b5606a",
135+
"id": "f58b58f7",
136136
"metadata": {
137137
"cq.autogen": "Permutation.sparse_permutation"
138138
},
@@ -143,9 +143,26 @@
143143
")"
144144
]
145145
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": null,
149+
"id": "e644f2ac",
150+
"metadata": {
151+
"cq.autogen": "Permutation.sparse_permutation_with_symbolic_N"
152+
},
153+
"outputs": [],
154+
"source": [
155+
"import sympy\n",
156+
"\n",
157+
"N = sympy.symbols(\"N\", positive=True, integer=True)\n",
158+
"sparse_permutation_with_symbolic_N = Permutation.from_partial_permutation_map(\n",
159+
" N, {0: 1, 1: 3, 2: 4, 3: 7}\n",
160+
")"
161+
]
162+
},
146163
{
147164
"cell_type": "markdown",
148-
"id": "369df9cb",
165+
"id": "ace174d5",
149166
"metadata": {
150167
"cq.autogen": "Permutation.graphical_signature.md"
151168
},
@@ -156,20 +173,20 @@
156173
{
157174
"cell_type": "code",
158175
"execution_count": null,
159-
"id": "25b2dc2b",
176+
"id": "1e55dcd4",
160177
"metadata": {
161178
"cq.autogen": "Permutation.graphical_signature.py"
162179
},
163180
"outputs": [],
164181
"source": [
165182
"from qualtran.drawing import show_bloqs\n",
166-
"show_bloqs([permutation, permutation_symb, permutation_symb_with_cycles, sparse_permutation],\n",
167-
" ['`permutation`', '`permutation_symb`', '`permutation_symb_with_cycles`', '`sparse_permutation`'])"
183+
"show_bloqs([permutation, permutation_symb, permutation_symb_with_cycles, sparse_permutation, sparse_permutation_with_symbolic_N],\n",
184+
" ['`permutation`', '`permutation_symb`', '`permutation_symb_with_cycles`', '`sparse_permutation`', '`sparse_permutation_with_symbolic_N`'])"
168185
]
169186
},
170187
{
171188
"cell_type": "markdown",
172-
"id": "ad4321ad",
189+
"id": "32e2b5dd",
173190
"metadata": {
174191
"cq.autogen": "Permutation.call_graph.md"
175192
},
@@ -180,7 +197,7 @@
180197
{
181198
"cell_type": "code",
182199
"execution_count": null,
183-
"id": "3bfbf3f7",
200+
"id": "fab3fd69",
184201
"metadata": {
185202
"cq.autogen": "Permutation.call_graph.py"
186203
},
@@ -194,7 +211,7 @@
194211
},
195212
{
196213
"cell_type": "markdown",
197-
"id": "b3e895db",
214+
"id": "408e209c",
198215
"metadata": {
199216
"cq.autogen": "PermutationCycle.bloq_doc.md"
200217
},
@@ -230,7 +247,7 @@
230247
{
231248
"cell_type": "code",
232249
"execution_count": null,
233-
"id": "d569cd2c",
250+
"id": "bc68fccd",
234251
"metadata": {
235252
"cq.autogen": "PermutationCycle.bloq_doc.py"
236253
},
@@ -241,7 +258,7 @@
241258
},
242259
{
243260
"cell_type": "markdown",
244-
"id": "a93c6e89",
261+
"id": "de4c7922",
245262
"metadata": {
246263
"cq.autogen": "PermutationCycle.example_instances.md"
247264
},
@@ -252,19 +269,23 @@
252269
{
253270
"cell_type": "code",
254271
"execution_count": null,
255-
"id": "264ba946",
272+
"id": "071de1e1",
256273
"metadata": {
257-
"cq.autogen": "PermutationCycle.permutation_cycle"
274+
"cq.autogen": "PermutationCycle.permutation_cycle_symb_N"
258275
},
259276
"outputs": [],
260277
"source": [
261-
"permutation_cycle = PermutationCycle(4, (0, 1, 2))"
278+
"import sympy\n",
279+
"\n",
280+
"N = sympy.symbols(\"n\", positive=True, integer=True)\n",
281+
"cycle = (3, 1, 2)\n",
282+
"permutation_cycle_symb_N = PermutationCycle(N, cycle)"
262283
]
263284
},
264285
{
265286
"cell_type": "code",
266287
"execution_count": null,
267-
"id": "78572528",
288+
"id": "fd61e92a",
268289
"metadata": {
269290
"cq.autogen": "PermutationCycle.permutation_cycle_symb"
270291
},
@@ -279,9 +300,21 @@
279300
"permutation_cycle_symb = PermutationCycle(N, cycle)"
280301
]
281302
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": null,
306+
"id": "b818f3b1",
307+
"metadata": {
308+
"cq.autogen": "PermutationCycle.permutation_cycle"
309+
},
310+
"outputs": [],
311+
"source": [
312+
"permutation_cycle = PermutationCycle(4, (0, 1, 2))"
313+
]
314+
},
282315
{
283316
"cell_type": "markdown",
284-
"id": "87615783",
317+
"id": "3d3c3e1b",
285318
"metadata": {
286319
"cq.autogen": "PermutationCycle.graphical_signature.md"
287320
},
@@ -292,20 +325,20 @@
292325
{
293326
"cell_type": "code",
294327
"execution_count": null,
295-
"id": "f6961010",
328+
"id": "de967993",
296329
"metadata": {
297330
"cq.autogen": "PermutationCycle.graphical_signature.py"
298331
},
299332
"outputs": [],
300333
"source": [
301334
"from qualtran.drawing import show_bloqs\n",
302-
"show_bloqs([permutation_cycle, permutation_cycle_symb],\n",
303-
" ['`permutation_cycle`', '`permutation_cycle_symb`'])"
335+
"show_bloqs([permutation_cycle_symb_N, permutation_cycle_symb, permutation_cycle],\n",
336+
" ['`permutation_cycle_symb_N`', '`permutation_cycle_symb`', '`permutation_cycle`'])"
304337
]
305338
},
306339
{
307340
"cell_type": "markdown",
308-
"id": "16e1c105",
341+
"id": "58deda5c",
309342
"metadata": {
310343
"cq.autogen": "PermutationCycle.call_graph.md"
311344
},
@@ -316,16 +349,16 @@
316349
{
317350
"cell_type": "code",
318351
"execution_count": null,
319-
"id": "27628f10",
352+
"id": "ec16903f",
320353
"metadata": {
321354
"cq.autogen": "PermutationCycle.call_graph.py"
322355
},
323356
"outputs": [],
324357
"source": [
325358
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
326-
"permutation_cycle_g, permutation_cycle_sigma = permutation_cycle.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
327-
"show_call_graph(permutation_cycle_g)\n",
328-
"show_counts_sigma(permutation_cycle_sigma)"
359+
"permutation_cycle_symb_N_g, permutation_cycle_symb_N_sigma = permutation_cycle_symb_N.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
360+
"show_call_graph(permutation_cycle_symb_N_g)\n",
361+
"show_counts_sigma(permutation_cycle_symb_N_sigma)"
329362
]
330363
}
331364
],

0 commit comments

Comments
 (0)