Skip to content

Commit 252639d

Browse files
authored
Add Split2/Join2 for simpler partitioning (#1730)
<img width="754" height="1146" alt="image" src="https://github.com/user-attachments/assets/976e7b55-2851-41aa-8f77-08dc99bfad59" />
1 parent 2f29aa5 commit 252639d

13 files changed

Lines changed: 1475 additions & 1233 deletions

File tree

dev_tools/qualtran_dev_tools/notebook_specs.py

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,6 @@
251251
module=qualtran.bloqs.basic_gates.identity,
252252
bloq_specs=[qualtran.bloqs.basic_gates.identity._IDENTITY_DOC],
253253
),
254-
NotebookSpecV2(
255-
title='Bookkeeping Bloqs',
256-
module=qualtran.bloqs.bookkeeping,
257-
bloq_specs=[
258-
qualtran.bloqs.bookkeeping.allocate._ALLOC_DOC,
259-
qualtran.bloqs.bookkeeping.free._FREE_DOC,
260-
qualtran.bloqs.bookkeeping.split._SPLIT_DOC,
261-
qualtran.bloqs.bookkeeping.join._JOIN_DOC,
262-
qualtran.bloqs.bookkeeping.partition._PARTITION_DOC,
263-
qualtran.bloqs.bookkeeping.auto_partition._AUTO_PARTITION_DOC,
264-
qualtran.bloqs.bookkeeping.cast._CAST_DOC,
265-
qualtran.bloqs.bookkeeping.always._ALWAYS_DOC,
266-
],
267-
),
268254
NotebookSpecV2(
269255
title='Control Specification (And)',
270256
module=qualtran.bloqs.mcmt.ctrl_spec_and,
@@ -887,6 +873,52 @@
887873
),
888874
]
889875

876+
BOOKKEEPING: List[NotebookSpecV2] = [
877+
NotebookSpecV2(
878+
title='Split / Join',
879+
module=qualtran.bloqs.bookkeeping.split,
880+
bloq_specs=[
881+
qualtran.bloqs.bookkeeping.split._SPLIT_DOC,
882+
qualtran.bloqs.bookkeeping.join._JOIN_DOC,
883+
],
884+
),
885+
NotebookSpecV2(
886+
title='Split2 / Join2',
887+
module=qualtran.bloqs.bookkeeping.partition,
888+
path_stem='split2',
889+
bloq_specs=[
890+
qualtran.bloqs.bookkeeping.partition._SPLIT2_DOC,
891+
qualtran.bloqs.bookkeeping.partition._JOIN2_DOC,
892+
],
893+
),
894+
NotebookSpecV2(
895+
title='Alloc / Free',
896+
module=qualtran.bloqs.bookkeeping.allocate,
897+
bloq_specs=[
898+
qualtran.bloqs.bookkeeping.allocate._ALLOC_DOC,
899+
qualtran.bloqs.bookkeeping.free._FREE_DOC,
900+
],
901+
),
902+
NotebookSpecV2(
903+
title='Partition',
904+
module=qualtran.bloqs.bookkeeping.partition,
905+
bloq_specs=[
906+
qualtran.bloqs.bookkeeping.partition._PARTITION_DOC,
907+
qualtran.bloqs.bookkeeping.auto_partition._AUTO_PARTITION_DOC,
908+
],
909+
),
910+
NotebookSpecV2(
911+
title='Cast',
912+
module=qualtran.bloqs.bookkeeping.cast,
913+
bloq_specs=[qualtran.bloqs.bookkeeping.cast._CAST_DOC],
914+
),
915+
NotebookSpecV2(
916+
title='Always',
917+
module=qualtran.bloqs.bookkeeping.always,
918+
bloq_specs=[qualtran.bloqs.bookkeeping.always._ALWAYS_DOC],
919+
),
920+
]
921+
890922
# --------------------------------------------------------------------------
891923
# ----- Other ----------------------------------------------------------
892924
# --------------------------------------------------------------------------
@@ -1013,5 +1045,6 @@
10131045
('Rotations', ROT_QFT_PE),
10141046
('Block Encoding', BLOCK_ENCODING),
10151047
('Optimization', OPTIMIZATION),
1048+
('Bookkeeping', BOOKKEEPING),
10161049
('Other', OTHER),
10171050
]

docs/bloqs/index.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Bloqs Library
4646
swap_network/swap_network.ipynb
4747
basic_gates/global_phase.ipynb
4848
basic_gates/identity.ipynb
49-
bookkeeping/bookkeeping.ipynb
5049
mcmt/ctrl_spec_and.ipynb
5150
mcmt/controlled_via_and.ipynb
5251

@@ -163,6 +162,17 @@ Bloqs Library
163162
optimization/k_xor_sat/kikuchi_adjacency_matrix.ipynb
164163
optimization/k_xor_sat/kikuchi_block_encoding.ipynb
165164

165+
.. toctree::
166+
:maxdepth: 2
167+
:caption: Bookkeeping:
168+
169+
bookkeeping/split.ipynb
170+
bookkeeping/split2.ipynb
171+
bookkeeping/allocate.ipynb
172+
bookkeeping/partition.ipynb
173+
bookkeeping/cast.ipynb
174+
bookkeeping/always.ipynb
175+
166176
.. toctree::
167177
:maxdepth: 2
168178
:caption: Other:

qualtran/bloqs/bookkeeping/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
from qualtran.bloqs.bookkeeping.cast import Cast
2121
from qualtran.bloqs.bookkeeping.free import Free
2222
from qualtran.bloqs.bookkeeping.join import Join
23-
from qualtran.bloqs.bookkeeping.partition import Partition
23+
from qualtran.bloqs.bookkeeping.partition import Join2, Partition, Split2
2424
from qualtran.bloqs.bookkeeping.split import Split
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "a7be8545",
6+
"metadata": {
7+
"cq.autogen": "title_cell"
8+
},
9+
"source": [
10+
"# Alloc / Free"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "01fdc670",
17+
"metadata": {
18+
"cq.autogen": "top_imports"
19+
},
20+
"outputs": [],
21+
"source": [
22+
"from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n",
23+
"from qualtran import QBit, QInt, QUInt, QAny\n",
24+
"from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n",
25+
"from typing import *\n",
26+
"import numpy as np\n",
27+
"import sympy\n",
28+
"import cirq"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"id": "9639fbe4",
34+
"metadata": {
35+
"cq.autogen": "Allocate.bloq_doc.md"
36+
},
37+
"source": [
38+
"## `Allocate`\n",
39+
"Allocate an `n` bit register.\n",
40+
"\n",
41+
"#### Parameters\n",
42+
" - `dtype`: the quantum data type of the allocated register.\n",
43+
" - `dirty`: If true, represents a borrowing operation where allocated qubits can be dirty. \n",
44+
"\n",
45+
"#### Registers\n",
46+
" - `reg [right]`: The allocated register.\n"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"id": "3a32133e",
53+
"metadata": {
54+
"cq.autogen": "Allocate.bloq_doc.py"
55+
},
56+
"outputs": [],
57+
"source": [
58+
"from qualtran.bloqs.bookkeeping import Allocate"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"id": "3e445f38",
64+
"metadata": {
65+
"cq.autogen": "Allocate.example_instances.md"
66+
},
67+
"source": [
68+
"### Example Instances"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"id": "64eae920",
75+
"metadata": {
76+
"cq.autogen": "Allocate.alloc"
77+
},
78+
"outputs": [],
79+
"source": [
80+
"n = sympy.Symbol('n')\n",
81+
"alloc = Allocate(QUInt(n))"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"id": "9799a674",
87+
"metadata": {
88+
"cq.autogen": "Allocate.graphical_signature.md"
89+
},
90+
"source": [
91+
"#### Graphical Signature"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"id": "cc30ba5a",
98+
"metadata": {
99+
"cq.autogen": "Allocate.graphical_signature.py"
100+
},
101+
"outputs": [],
102+
"source": [
103+
"from qualtran.drawing import show_bloqs\n",
104+
"show_bloqs([alloc],\n",
105+
" ['`alloc`'])"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"id": "b3279532",
111+
"metadata": {
112+
"cq.autogen": "Allocate.call_graph.md"
113+
},
114+
"source": [
115+
"### Call Graph"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"id": "9109defa",
122+
"metadata": {
123+
"cq.autogen": "Allocate.call_graph.py"
124+
},
125+
"outputs": [],
126+
"source": [
127+
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
128+
"alloc_g, alloc_sigma = alloc.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
129+
"show_call_graph(alloc_g)\n",
130+
"show_counts_sigma(alloc_sigma)"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"id": "d97992a2",
136+
"metadata": {
137+
"cq.autogen": "Free.bloq_doc.md"
138+
},
139+
"source": [
140+
"## `Free`\n",
141+
"Free (i.e. de-allocate) a register.\n",
142+
"\n",
143+
"The tensor decomposition assumes the register is uncomputed and is in the zero\n",
144+
"state before getting freed. To verify that is the case, one can compute the resulting state\n",
145+
"vector after freeing qubits and make sure it is normalized.\n",
146+
"\n",
147+
"#### Parameters\n",
148+
" - `dtype`: The quantum data type of the register to be freed.\n",
149+
" - `dirty`: If true, represents adjoint of a borrowing operation where deallocated qubits were borrowed dirty from another part of the algorithm and must be free'd in the same dirty state. \n",
150+
"\n",
151+
"#### Registers\n",
152+
" - `reg [left]`: The register to free.\n"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"id": "02f58e9f",
159+
"metadata": {
160+
"cq.autogen": "Free.bloq_doc.py"
161+
},
162+
"outputs": [],
163+
"source": [
164+
"from qualtran.bloqs.bookkeeping import Free"
165+
]
166+
},
167+
{
168+
"cell_type": "markdown",
169+
"id": "2330f781",
170+
"metadata": {
171+
"cq.autogen": "Free.example_instances.md"
172+
},
173+
"source": [
174+
"### Example Instances"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"id": "57cb3a2d",
181+
"metadata": {
182+
"cq.autogen": "Free.free"
183+
},
184+
"outputs": [],
185+
"source": [
186+
"n = sympy.Symbol('n')\n",
187+
"free = Free(QUInt(n))"
188+
]
189+
},
190+
{
191+
"cell_type": "markdown",
192+
"id": "20d2be05",
193+
"metadata": {
194+
"cq.autogen": "Free.graphical_signature.md"
195+
},
196+
"source": [
197+
"#### Graphical Signature"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"id": "b5a9e693",
204+
"metadata": {
205+
"cq.autogen": "Free.graphical_signature.py"
206+
},
207+
"outputs": [],
208+
"source": [
209+
"from qualtran.drawing import show_bloqs\n",
210+
"show_bloqs([free],\n",
211+
" ['`free`'])"
212+
]
213+
},
214+
{
215+
"cell_type": "markdown",
216+
"id": "203cfda5",
217+
"metadata": {
218+
"cq.autogen": "Free.call_graph.md"
219+
},
220+
"source": [
221+
"### Call Graph"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": null,
227+
"id": "34f77821",
228+
"metadata": {
229+
"cq.autogen": "Free.call_graph.py"
230+
},
231+
"outputs": [],
232+
"source": [
233+
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
234+
"free_g, free_sigma = free.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
235+
"show_call_graph(free_g)\n",
236+
"show_counts_sigma(free_sigma)"
237+
]
238+
}
239+
],
240+
"metadata": {
241+
"kernelspec": {
242+
"display_name": "Python 3",
243+
"language": "python",
244+
"name": "python3"
245+
},
246+
"language_info": {
247+
"name": "python"
248+
}
249+
},
250+
"nbformat": 4,
251+
"nbformat_minor": 5
252+
}

0 commit comments

Comments
 (0)