Skip to content

Commit a47b67b

Browse files
delete blank cells
1 parent 5007193 commit a47b67b

File tree

2 files changed

+161
-179
lines changed

2 files changed

+161
-179
lines changed

docs/source/tutorials/circuit_qudit_basics.ipynb

Lines changed: 83 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,14 @@
3838
},
3939
{
4040
"cell_type": "code",
41+
"execution_count": 1,
4142
"id": "8f2d9565",
4243
"metadata": {
4344
"ExecuteTime": {
4445
"end_time": "2025-09-10T10:32:59.941117Z",
4546
"start_time": "2025-09-10T10:32:58.690552Z"
4647
}
4748
},
48-
"source": [
49-
"import tensorcircuit as tc\n",
50-
"from tensorcircuit.quditcircuit import QuditCircuit\n",
51-
"\n",
52-
"tc.set_backend(\"numpy\") # or \"jax\", \"tensorflow\", \"pytorch\"\n",
53-
"print(\"tensorcircuit version:\", tc.__version__)"
54-
],
5549
"outputs": [
5650
{
5751
"name": "stdout",
@@ -61,7 +55,13 @@
6155
]
6256
}
6357
],
64-
"execution_count": 1
58+
"source": [
59+
"import tensorcircuit as tc\n",
60+
"from tensorcircuit.quditcircuit import QuditCircuit\n",
61+
"\n",
62+
"tc.set_backend(\"numpy\") # or \"jax\", \"tensorflow\", \"pytorch\"\n",
63+
"print(\"tensorcircuit version:\", tc.__version__)"
64+
]
6565
},
6666
{
6767
"cell_type": "markdown",
@@ -76,21 +76,14 @@
7676
},
7777
{
7878
"cell_type": "code",
79+
"execution_count": 2,
7980
"id": "3fa13efe",
8081
"metadata": {
8182
"ExecuteTime": {
8283
"end_time": "2025-09-10T10:33:00.001551Z",
8384
"start_time": "2025-09-10T10:32:59.998454Z"
8485
}
8586
},
86-
"source": [
87-
"c = QuditCircuit(nqudits=1, dim=13)\n",
88-
"c.h(0) # generalized Hadamard on the only qudit\n",
89-
"psi = c.wavefunction() # state vector of length 13^1 = 13\n",
90-
"probs = c.probability() # probability vector (length 3)\n",
91-
"print(r\"\\psi:\", psi)\n",
92-
"print(\"P:\", probs)"
93-
],
9487
"outputs": [
9588
{
9689
"name": "stdout",
@@ -105,7 +98,14 @@
10598
]
10699
}
107100
],
108-
"execution_count": 2
101+
"source": [
102+
"c = QuditCircuit(nqudits=1, dim=13)\n",
103+
"c.h(0) # generalized Hadamard on the only qudit\n",
104+
"psi = c.wavefunction() # state vector of length 13^1 = 13\n",
105+
"probs = c.probability() # probability vector (length 3)\n",
106+
"print(r\"\\psi:\", psi)\n",
107+
"print(\"P:\", probs)"
108+
]
109109
},
110110
{
111111
"cell_type": "markdown",
@@ -122,21 +122,14 @@
122122
},
123123
{
124124
"cell_type": "code",
125+
"execution_count": 3,
125126
"id": "c53a755e",
126127
"metadata": {
127128
"ExecuteTime": {
128129
"end_time": "2025-09-10T10:33:00.018234Z",
129130
"start_time": "2025-09-10T10:33:00.014110Z"
130131
}
131132
},
132-
"source": [
133-
"cq = QuditCircuit(nqudits=2, dim=3) # two qutrits\n",
134-
"cq.h(0) # superpose control\n",
135-
"cq.csum(0, 1) # qudit CNOT analog (control=0, target=1)\n",
136-
"psi = cq.wavefunction()\n",
137-
"probs = cq.probability()\n",
138-
"print(r\"|\\psi|^2 (length 3^2=9):\", probs)"
139-
],
140133
"outputs": [
141134
{
142135
"name": "stdout",
@@ -147,7 +140,14 @@
147140
]
148141
}
149142
],
150-
"execution_count": 3
143+
"source": [
144+
"cq = QuditCircuit(nqudits=2, dim=3) # two qutrits\n",
145+
"cq.h(0) # superpose control\n",
146+
"cq.csum(0, 1) # qudit CNOT analog (control=0, target=1)\n",
147+
"psi = cq.wavefunction()\n",
148+
"probs = cq.probability()\n",
149+
"print(r\"|\\psi|^2 (length 3^2=9):\", probs)"
150+
]
151151
},
152152
{
153153
"cell_type": "markdown",
@@ -162,17 +162,14 @@
162162
},
163163
{
164164
"cell_type": "code",
165+
"execution_count": 4,
165166
"id": "6542deab",
166167
"metadata": {
167168
"ExecuteTime": {
168169
"end_time": "2025-09-10T10:33:00.335589Z",
169170
"start_time": "2025-09-10T10:33:00.031885Z"
170171
}
171172
},
172-
"source": [
173-
"samples = cq.sample(batch=512, format=\"count_dict_bin\") # e.g., '00', '11', '22'\n",
174-
"samples"
175-
],
176173
"outputs": [
177174
{
178175
"data": {
@@ -185,7 +182,10 @@
185182
"output_type": "execute_result"
186183
}
187184
],
188-
"execution_count": 4
185+
"source": [
186+
"samples = cq.sample(batch=512, format=\"count_dict_bin\") # e.g., '00', '11', '22'\n",
187+
"samples"
188+
]
189189
},
190190
{
191191
"cell_type": "markdown",
@@ -206,24 +206,14 @@
206206
},
207207
{
208208
"cell_type": "code",
209+
"execution_count": 5,
209210
"id": "e4e2b769",
210211
"metadata": {
211212
"ExecuteTime": {
212213
"end_time": "2025-09-10T10:33:00.350793Z",
213214
"start_time": "2025-09-10T10:33:00.346566Z"
214215
}
215216
},
216-
"source": [
217-
"import numpy as np\n",
218-
"\n",
219-
"c = QuditCircuit(nqudits=1, dim=5) # a ququint\n",
220-
"c.h(0) # start in equal superposition\n",
221-
"c.rx(0, theta=np.pi / 3, j=1, k=3) # rotate levels 1 and 3\n",
222-
"c.rz(0, theta=np.pi / 5, j=4) # add a phase to level 4\n",
223-
"psi = c.wavefunction()\n",
224-
"probs = c.probability()\n",
225-
"psi, probs"
226-
],
227217
"outputs": [
228218
{
229219
"data": {
@@ -240,7 +230,17 @@
240230
"output_type": "execute_result"
241231
}
242232
],
243-
"execution_count": 5
233+
"source": [
234+
"import numpy as np\n",
235+
"\n",
236+
"c = QuditCircuit(nqudits=1, dim=5) # a ququint\n",
237+
"c.h(0) # start in equal superposition\n",
238+
"c.rx(0, theta=np.pi / 3, j=1, k=3) # rotate levels 1 and 3\n",
239+
"c.rz(0, theta=np.pi / 5, j=4) # add a phase to level 4\n",
240+
"psi = c.wavefunction()\n",
241+
"probs = c.probability()\n",
242+
"psi, probs"
243+
]
244244
},
245245
{
246246
"cell_type": "markdown",
@@ -260,21 +260,14 @@
260260
},
261261
{
262262
"cell_type": "code",
263+
"execution_count": 6,
263264
"id": "a56be75e",
264265
"metadata": {
265266
"ExecuteTime": {
266267
"end_time": "2025-09-10T10:33:00.361091Z",
267268
"start_time": "2025-09-10T10:33:00.357370Z"
268269
}
269270
},
270-
"source": [
271-
"c2 = QuditCircuit(nqudits=2, dim=4) # two ququarts\n",
272-
"c2.h(0)\n",
273-
"c2.h(1)\n",
274-
"c2.rxx(0, 1, theta=np.pi / 4, j1=0, k1=2, j2=1, k2=3)\n",
275-
"c2.rzz(0, 1, theta=np.pi / 7, j1=0, k1=1, j2=0, k2=1)\n",
276-
"c2.probability()"
277-
],
278271
"outputs": [
279272
{
280273
"data": {
@@ -289,7 +282,14 @@
289282
"output_type": "execute_result"
290283
}
291284
],
292-
"execution_count": 6
285+
"source": [
286+
"c2 = QuditCircuit(nqudits=2, dim=4) # two ququarts\n",
287+
"c2.h(0)\n",
288+
"c2.h(1)\n",
289+
"c2.rxx(0, 1, theta=np.pi / 4, j1=0, k1=2, j2=1, k2=3)\n",
290+
"c2.rzz(0, 1, theta=np.pi / 7, j1=0, k1=1, j2=0, k2=1)\n",
291+
"c2.probability()"
292+
]
293293
},
294294
{
295295
"cell_type": "markdown",
@@ -304,23 +304,14 @@
304304
},
305305
{
306306
"cell_type": "code",
307+
"execution_count": 7,
307308
"id": "fe4ed499",
308309
"metadata": {
309310
"ExecuteTime": {
310311
"end_time": "2025-09-10T10:33:00.372256Z",
311312
"start_time": "2025-09-10T10:33:00.367671Z"
312313
}
313314
},
314-
"source": [
315-
"# Example: build a diagonal operator on a single qutrit (dim=3)\n",
316-
"import numpy as np\n",
317-
"\n",
318-
"c = QuditCircuit(1, dim=3)\n",
319-
"c.h(0)\n",
320-
"op = np.diag([0.0, 0.5, 1.0]) # acts on subspace levels 0,1,2\n",
321-
"expval = c.expectation((op, [0]))\n",
322-
"expval"
323-
],
324315
"outputs": [
325316
{
326317
"data": {
@@ -333,36 +324,37 @@
333324
"output_type": "execute_result"
334325
}
335326
],
336-
"execution_count": 7
327+
"source": [
328+
"# Example: build a diagonal operator on a single qutrit (dim=3)\n",
329+
"import numpy as np\n",
330+
"\n",
331+
"c = QuditCircuit(1, dim=3)\n",
332+
"c.h(0)\n",
333+
"op = np.diag([0.0, 0.5, 1.0]) # acts on subspace levels 0,1,2\n",
334+
"expval = c.expectation((op, [0]))\n",
335+
"expval"
336+
]
337337
},
338338
{
339-
"metadata": {},
340339
"cell_type": "markdown",
340+
"id": "648ee02776427b3c",
341+
"metadata": {},
341342
"source": [
342343
"### Apply Arbitrary Gate\n",
343344
"\n",
344345
"Just directly using ``any`` API by feeding the corresponding unitary"
345-
],
346-
"id": "648ee02776427b3c"
346+
]
347347
},
348348
{
349+
"cell_type": "code",
350+
"execution_count": 8,
351+
"id": "4d73e00b952f2ac4",
349352
"metadata": {
350353
"ExecuteTime": {
351354
"end_time": "2025-09-10T10:33:14.972605Z",
352355
"start_time": "2025-09-10T10:33:00.385060Z"
353356
}
354357
},
355-
"cell_type": "code",
356-
"source": [
357-
"d = 36\n",
358-
"c = tc.QuditCircuit(2, dim=d)\n",
359-
"h_matrix = tc.quditgates.h_matrix_func(d)\n",
360-
"c.any(0, unitary=h_matrix)\n",
361-
"csum_matrix = tc.quditgates.csum_matrix_func(d)\n",
362-
"c.any(0, 1, unitary=csum_matrix)\n",
363-
"c.sample(1024, format=\"count_dict_bin\")"
364-
],
365-
"id": "4d73e00b952f2ac4",
366358
"outputs": [
367359
{
368360
"data": {
@@ -410,7 +402,15 @@
410402
"output_type": "execute_result"
411403
}
412404
],
413-
"execution_count": 8
405+
"source": [
406+
"d = 36\n",
407+
"c = tc.QuditCircuit(2, dim=d)\n",
408+
"h_matrix = tc.quditgates.h_matrix_func(d)\n",
409+
"c.any(0, unitary=h_matrix)\n",
410+
"csum_matrix = tc.quditgates.csum_matrix_func(d)\n",
411+
"c.any(0, 1, unitary=csum_matrix)\n",
412+
"c.sample(1024, format=\"count_dict_bin\")"
413+
]
414414
},
415415
{
416416
"cell_type": "markdown",
@@ -429,22 +429,13 @@
429429
"\n",
430430
"All the functions are similar to the `tc.Circuit`\n"
431431
]
432-
},
433-
{
434-
"metadata": {
435-
"ExecuteTime": {
436-
"end_time": "2025-09-10T10:33:14.985131Z",
437-
"start_time": "2025-09-10T10:33:14.983579Z"
438-
}
439-
},
440-
"cell_type": "code",
441-
"source": "",
442-
"id": "9a33f318814fa510",
443-
"outputs": [],
444-
"execution_count": null
445432
}
446433
],
447-
"metadata": {},
434+
"metadata": {
435+
"language_info": {
436+
"name": "python"
437+
}
438+
},
448439
"nbformat": 4,
449440
"nbformat_minor": 5
450441
}

0 commit comments

Comments
 (0)