Skip to content

Commit 8a688c8

Browse files
committed
Upgrade AddK - use dtype and simplify controls
1 parent 179396d commit 8a688c8

File tree

11 files changed

+199
-299
lines changed

11 files changed

+199
-299
lines changed

qualtran/bloqs/arithmetic/addition.ipynb

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "c231998b",
5+
"id": "c9483021",
66
"metadata": {
77
"cq.autogen": "title_cell"
88
},
@@ -13,7 +13,7 @@
1313
{
1414
"cell_type": "code",
1515
"execution_count": null,
16-
"id": "b572d769",
16+
"id": "2dd760e6",
1717
"metadata": {
1818
"cq.autogen": "top_imports"
1919
},
@@ -30,7 +30,7 @@
3030
},
3131
{
3232
"cell_type": "markdown",
33-
"id": "d3f4ce95",
33+
"id": "95d8998c",
3434
"metadata": {
3535
"cq.autogen": "Add.bloq_doc.md"
3636
},
@@ -55,7 +55,7 @@
5555
{
5656
"cell_type": "code",
5757
"execution_count": null,
58-
"id": "6426bb53",
58+
"id": "4cb5b704",
5959
"metadata": {
6060
"cq.autogen": "Add.bloq_doc.py"
6161
},
@@ -66,7 +66,7 @@
6666
},
6767
{
6868
"cell_type": "markdown",
69-
"id": "3c8caad1",
69+
"id": "69f415a2",
7070
"metadata": {
7171
"cq.autogen": "Add.example_instances.md"
7272
},
@@ -77,46 +77,46 @@
7777
{
7878
"cell_type": "code",
7979
"execution_count": null,
80-
"id": "73929b65",
80+
"id": "b0c9815d",
8181
"metadata": {
82-
"cq.autogen": "Add.add_small"
82+
"cq.autogen": "Add.add_symb"
8383
},
8484
"outputs": [],
8585
"source": [
86-
"add_small = Add(QUInt(bitsize=4))"
86+
"n = sympy.Symbol('n')\n",
87+
"add_symb = Add(QInt(bitsize=n))"
8788
]
8889
},
8990
{
9091
"cell_type": "code",
9192
"execution_count": null,
92-
"id": "fbd4dbe9",
93+
"id": "b481bd7d",
9394
"metadata": {
94-
"cq.autogen": "Add.add_large"
95+
"cq.autogen": "Add.add_small"
9596
},
9697
"outputs": [],
9798
"source": [
98-
"add_large = Add(QUInt(bitsize=64))"
99+
"add_small = Add(QUInt(bitsize=4))"
99100
]
100101
},
101102
{
102103
"cell_type": "code",
103104
"execution_count": null,
104-
"id": "e749abdb",
105+
"id": "90bc9e29",
105106
"metadata": {
106-
"cq.autogen": "Add.add_symb"
107+
"cq.autogen": "Add.add_large"
107108
},
108109
"outputs": [],
109110
"source": [
110-
"n = sympy.Symbol('n')\n",
111-
"add_symb = Add(QInt(bitsize=n))"
111+
"add_large = Add(QUInt(bitsize=64))"
112112
]
113113
},
114114
{
115115
"cell_type": "code",
116116
"execution_count": null,
117-
"id": "e5b746c1",
117+
"id": "2fbbb318",
118118
"metadata": {
119-
"collapsed": false
119+
"cq.autogen": "Add.add_diff_size_regs"
120120
},
121121
"outputs": [],
122122
"source": [
@@ -125,7 +125,7 @@
125125
},
126126
{
127127
"cell_type": "markdown",
128-
"id": "c3c1a7e4",
128+
"id": "253212ec",
129129
"metadata": {
130130
"cq.autogen": "Add.graphical_signature.md"
131131
},
@@ -136,7 +136,7 @@
136136
{
137137
"cell_type": "code",
138138
"execution_count": null,
139-
"id": "0f92be09",
139+
"id": "ed24479f",
140140
"metadata": {
141141
"cq.autogen": "Add.graphical_signature.py"
142142
},
@@ -149,7 +149,7 @@
149149
},
150150
{
151151
"cell_type": "markdown",
152-
"id": "350cb374",
152+
"id": "1c55412d",
153153
"metadata": {
154154
"cq.autogen": "Add.call_graph.md"
155155
},
@@ -160,7 +160,7 @@
160160
{
161161
"cell_type": "code",
162162
"execution_count": null,
163-
"id": "c36a3fc4",
163+
"id": "90569a0c",
164164
"metadata": {
165165
"cq.autogen": "Add.call_graph.py"
166166
},
@@ -174,7 +174,7 @@
174174
},
175175
{
176176
"cell_type": "markdown",
177-
"id": "ffc76cc5",
177+
"id": "8ae20541",
178178
"metadata": {
179179
"cq.autogen": "OutOfPlaceAdder.bloq_doc.md"
180180
},
@@ -202,7 +202,7 @@
202202
{
203203
"cell_type": "code",
204204
"execution_count": null,
205-
"id": "c2450d62",
205+
"id": "7c51ccda",
206206
"metadata": {
207207
"cq.autogen": "OutOfPlaceAdder.bloq_doc.py"
208208
},
@@ -213,7 +213,7 @@
213213
},
214214
{
215215
"cell_type": "markdown",
216-
"id": "b5c79e25",
216+
"id": "6e0af527",
217217
"metadata": {
218218
"cq.autogen": "OutOfPlaceAdder.example_instances.md"
219219
},
@@ -224,7 +224,7 @@
224224
{
225225
"cell_type": "code",
226226
"execution_count": null,
227-
"id": "e31acd2b",
227+
"id": "f062b497",
228228
"metadata": {
229229
"cq.autogen": "OutOfPlaceAdder.add_oop_symb"
230230
},
@@ -237,7 +237,7 @@
237237
{
238238
"cell_type": "code",
239239
"execution_count": null,
240-
"id": "ef8871a6",
240+
"id": "0bde421f",
241241
"metadata": {
242242
"cq.autogen": "OutOfPlaceAdder.add_oop_small"
243243
},
@@ -249,7 +249,7 @@
249249
{
250250
"cell_type": "code",
251251
"execution_count": null,
252-
"id": "e57b8c8d",
252+
"id": "ce284cd8",
253253
"metadata": {
254254
"cq.autogen": "OutOfPlaceAdder.add_oop_large"
255255
},
@@ -260,7 +260,7 @@
260260
},
261261
{
262262
"cell_type": "markdown",
263-
"id": "01915f46",
263+
"id": "052cf86a",
264264
"metadata": {
265265
"cq.autogen": "OutOfPlaceAdder.graphical_signature.md"
266266
},
@@ -271,7 +271,7 @@
271271
{
272272
"cell_type": "code",
273273
"execution_count": null,
274-
"id": "d117e345",
274+
"id": "672073d0",
275275
"metadata": {
276276
"cq.autogen": "OutOfPlaceAdder.graphical_signature.py"
277277
},
@@ -284,7 +284,7 @@
284284
},
285285
{
286286
"cell_type": "markdown",
287-
"id": "3b6469e0",
287+
"id": "ab488ede",
288288
"metadata": {
289289
"cq.autogen": "OutOfPlaceAdder.call_graph.md"
290290
},
@@ -295,7 +295,7 @@
295295
{
296296
"cell_type": "code",
297297
"execution_count": null,
298-
"id": "ff84301f",
298+
"id": "311c752e",
299299
"metadata": {
300300
"cq.autogen": "OutOfPlaceAdder.call_graph.py"
301301
},
@@ -309,7 +309,7 @@
309309
},
310310
{
311311
"cell_type": "markdown",
312-
"id": "2813f173",
312+
"id": "62b014b7",
313313
"metadata": {
314314
"cq.autogen": "AddK.bloq_doc.md"
315315
},
@@ -324,13 +324,12 @@
324324
"only clifford operations.\n",
325325
"\n",
326326
"#### Parameters\n",
327-
" - `bitsize`: Number of bits used to represent each integer.\n",
327+
" - `dtype`: data type of the input register `x`\n",
328328
" - `k`: The classical integer value to be added to x.\n",
329-
" - `cvs`: A tuple of control values. Each entry specifies whether that control line is a \"positive\" control (`cv[i]=1`) or a \"negative\" control (`cv[i]=0`).\n",
330-
" - `signed`: A boolean condition which controls whether the x register holds a value represented in 2's Complement or Unsigned. This affects the ability to add a negative constant. \n",
329+
" - `is_controlled`: if True, construct a singly-controlled bloq. \n",
331330
"\n",
332331
"#### Registers\n",
333-
" - `x`: A bitsize-sized input register (register x above). \n",
332+
" - `x`: register of type `self.dtype` \n",
334333
"\n",
335334
"#### References\n",
336335
" - [Improved quantum circuits for elliptic curve discrete logarithms](https://arxiv.org/abs/2001.09580). Haner et al. 2020. Section 3: Components. \"Integer addition\" and Fig 2a.\n"
@@ -339,7 +338,7 @@
339338
{
340339
"cell_type": "code",
341340
"execution_count": null,
342-
"id": "cd255bf9",
341+
"id": "3155864a",
343342
"metadata": {
344343
"cq.autogen": "AddK.bloq_doc.py"
345344
},
@@ -350,7 +349,7 @@
350349
},
351350
{
352351
"cell_type": "markdown",
353-
"id": "7538f9a5",
352+
"id": "9009b701",
354353
"metadata": {
355354
"cq.autogen": "AddK.example_instances.md"
356355
},
@@ -361,43 +360,43 @@
361360
{
362361
"cell_type": "code",
363362
"execution_count": null,
364-
"id": "4305289f",
363+
"id": "7e2a9fdf",
365364
"metadata": {
366365
"cq.autogen": "AddK.add_k"
367366
},
368367
"outputs": [],
369368
"source": [
370369
"n, k = sympy.symbols('n k')\n",
371-
"add_k = AddK(bitsize=n, k=k)"
370+
"add_k = AddK(QUInt(n), k=k)"
372371
]
373372
},
374373
{
375374
"cell_type": "code",
376375
"execution_count": null,
377-
"id": "f6048819",
376+
"id": "f4e218da",
378377
"metadata": {
379378
"cq.autogen": "AddK.add_k_small"
380379
},
381380
"outputs": [],
382381
"source": [
383-
"add_k_small = AddK(bitsize=4, k=2, signed=False)"
382+
"add_k_small = AddK(QUInt(4), k=2)"
384383
]
385384
},
386385
{
387386
"cell_type": "code",
388387
"execution_count": null,
389-
"id": "b67fd469",
388+
"id": "97073a76",
390389
"metadata": {
391390
"cq.autogen": "AddK.add_k_large"
392391
},
393392
"outputs": [],
394393
"source": [
395-
"add_k_large = AddK(bitsize=64, k=-23, signed=True)"
394+
"add_k_large = AddK(QInt(64), k=-23)"
396395
]
397396
},
398397
{
399398
"cell_type": "markdown",
400-
"id": "b8b04228",
399+
"id": "57f1032d",
401400
"metadata": {
402401
"cq.autogen": "AddK.graphical_signature.md"
403402
},
@@ -408,7 +407,7 @@
408407
{
409408
"cell_type": "code",
410409
"execution_count": null,
411-
"id": "e93e7f2e",
410+
"id": "d5d7dad4",
412411
"metadata": {
413412
"cq.autogen": "AddK.graphical_signature.py"
414413
},
@@ -421,7 +420,7 @@
421420
},
422421
{
423422
"cell_type": "markdown",
424-
"id": "13552795",
423+
"id": "f1a51ffb",
425424
"metadata": {
426425
"cq.autogen": "AddK.call_graph.md"
427426
},
@@ -432,7 +431,7 @@
432431
{
433432
"cell_type": "code",
434433
"execution_count": null,
435-
"id": "d8d6584e",
434+
"id": "cbfc2d39",
436435
"metadata": {
437436
"cq.autogen": "AddK.call_graph.py"
438437
},
@@ -443,37 +442,16 @@
443442
"show_call_graph(add_k_g)\n",
444443
"show_counts_sigma(add_k_sigma)"
445444
]
446-
},
447-
{
448-
"cell_type": "code",
449-
"execution_count": null,
450-
"id": "8853ae5d",
451-
"metadata": {
452-
"cq.autogen": "Add.add_diff_size_regs"
453-
},
454-
"outputs": [],
455-
"source": [
456-
"add_diff_size_regs = Add(QUInt(bitsize=4), QUInt(bitsize=16))"
457-
]
458445
}
459446
],
460447
"metadata": {
461448
"kernelspec": {
462-
"display_name": "Python 3 (ipykernel)",
449+
"display_name": "Python 3",
463450
"language": "python",
464451
"name": "python3"
465452
},
466453
"language_info": {
467-
"codemirror_mode": {
468-
"name": "ipython",
469-
"version": 3
470-
},
471-
"file_extension": ".py",
472-
"mimetype": "text/x-python",
473-
"name": "python",
474-
"nbconvert_exporter": "python",
475-
"pygments_lexer": "ipython3",
476-
"version": "3.11.7"
454+
"name": "python"
477455
}
478456
},
479457
"nbformat": 4,

0 commit comments

Comments
 (0)