Skip to content

Commit f4ec2cc

Browse files
authored
Support more symbolic decompositions (#1369)
* allow more symbolic decomp in Permutation bloqs * `StatePreparationViaRotation` - support symbolic phasegrad bitsize * permutation - add more symbolic examples * state-prep-rotations - symbolic examples + autotest * assert decomp exists * regen notebooks * update bloq_ex
1 parent a36c50f commit f4ec2cc

File tree

7 files changed

+223
-78
lines changed

7 files changed

+223
-78
lines changed

qualtran/bloqs/arithmetic/permutation.ipynb

Lines changed: 64 additions & 31 deletions
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
],

qualtran/bloqs/arithmetic/permutation.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ def signature(self) -> Signature:
104104
def bitsize(self):
105105
return bit_length(self.N - 1)
106106

107-
def is_symbolic(self):
108-
return is_symbolic(self.N, self.cycle)
109-
110107
def build_composite_bloq(self, bb: 'BloqBuilder', x: 'SoquetT') -> dict[str, 'SoquetT']:
111108
if is_symbolic(self.cycle):
112109
raise DecomposeTypeError(f"cannot decompose symbolic {self}")
@@ -126,7 +123,7 @@ def build_composite_bloq(self, bb: 'BloqBuilder', x: 'SoquetT') -> dict[str, 'So
126123
return {'x': x}
127124

128125
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
129-
if self.is_symbolic():
126+
if is_symbolic(self.cycle):
130127
x = ssa.new_symbol('x')
131128
cycle_len = slen(self.cycle)
132129
return {
@@ -143,6 +140,16 @@ def _permutation_cycle() -> PermutationCycle:
143140
return permutation_cycle
144141

145142

143+
@bloq_example
144+
def _permutation_cycle_symb_N() -> PermutationCycle:
145+
import sympy
146+
147+
N = sympy.symbols("n", positive=True, integer=True)
148+
cycle = (3, 1, 2)
149+
permutation_cycle_symb_N = PermutationCycle(N, cycle)
150+
return permutation_cycle_symb_N
151+
152+
146153
@bloq_example
147154
def _permutation_cycle_symb() -> PermutationCycle:
148155
import sympy
@@ -158,7 +165,7 @@ def _permutation_cycle_symb() -> PermutationCycle:
158165
_PERMUTATION_CYCLE_DOC = BloqDocSpec(
159166
bloq_cls=PermutationCycle,
160167
import_line='from qualtran.bloqs.arithmetic.permutation import PermutationCycle',
161-
examples=[_permutation_cycle, _permutation_cycle_symb],
168+
examples=[_permutation_cycle_symb_N, _permutation_cycle_symb, _permutation_cycle],
162169
)
163170

164171

@@ -261,7 +268,7 @@ def build_composite_bloq(self, bb: 'BloqBuilder', x: 'Soquet') -> dict[str, 'Soq
261268
return {'x': x}
262269

263270
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
264-
if self.is_symbolic():
271+
if is_symbolic(self.cycles):
265272
# worst case cost: single cycle of length N
266273
cycle = Shaped((self.N,))
267274
return {(PermutationCycle(self.N, cycle), 1)}
@@ -307,8 +314,25 @@ def _sparse_permutation() -> Permutation:
307314
return sparse_permutation
308315

309316

317+
@bloq_example
318+
def _sparse_permutation_with_symbolic_N() -> Permutation:
319+
import sympy
320+
321+
N = sympy.symbols("N", positive=True, integer=True)
322+
sparse_permutation_with_symbolic_N = Permutation.from_partial_permutation_map(
323+
N, {0: 1, 1: 3, 2: 4, 3: 7}
324+
)
325+
return sparse_permutation_with_symbolic_N
326+
327+
310328
_PERMUTATION_DOC = BloqDocSpec(
311329
bloq_cls=Permutation,
312330
import_line='from qualtran.bloqs.arithmetic.permutation import Permutation',
313-
examples=[_permutation, _permutation_symb, _permutation_symb_with_cycles, _sparse_permutation],
331+
examples=[
332+
_permutation,
333+
_permutation_symb,
334+
_permutation_symb_with_cycles,
335+
_sparse_permutation,
336+
_sparse_permutation_with_symbolic_N,
337+
],
314338
)

0 commit comments

Comments
 (0)