|
38 | 38 | }, |
39 | 39 | { |
40 | 40 | "cell_type": "code", |
| 41 | + "execution_count": 1, |
41 | 42 | "id": "8f2d9565", |
42 | 43 | "metadata": { |
43 | 44 | "ExecuteTime": { |
44 | 45 | "end_time": "2025-09-10T10:32:59.941117Z", |
45 | 46 | "start_time": "2025-09-10T10:32:58.690552Z" |
46 | 47 | } |
47 | 48 | }, |
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 | | - ], |
55 | 49 | "outputs": [ |
56 | 50 | { |
57 | 51 | "name": "stdout", |
|
61 | 55 | ] |
62 | 56 | } |
63 | 57 | ], |
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 | + ] |
65 | 65 | }, |
66 | 66 | { |
67 | 67 | "cell_type": "markdown", |
|
76 | 76 | }, |
77 | 77 | { |
78 | 78 | "cell_type": "code", |
| 79 | + "execution_count": 2, |
79 | 80 | "id": "3fa13efe", |
80 | 81 | "metadata": { |
81 | 82 | "ExecuteTime": { |
82 | 83 | "end_time": "2025-09-10T10:33:00.001551Z", |
83 | 84 | "start_time": "2025-09-10T10:32:59.998454Z" |
84 | 85 | } |
85 | 86 | }, |
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 | | - ], |
94 | 87 | "outputs": [ |
95 | 88 | { |
96 | 89 | "name": "stdout", |
|
105 | 98 | ] |
106 | 99 | } |
107 | 100 | ], |
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 | + ] |
109 | 109 | }, |
110 | 110 | { |
111 | 111 | "cell_type": "markdown", |
|
122 | 122 | }, |
123 | 123 | { |
124 | 124 | "cell_type": "code", |
| 125 | + "execution_count": 3, |
125 | 126 | "id": "c53a755e", |
126 | 127 | "metadata": { |
127 | 128 | "ExecuteTime": { |
128 | 129 | "end_time": "2025-09-10T10:33:00.018234Z", |
129 | 130 | "start_time": "2025-09-10T10:33:00.014110Z" |
130 | 131 | } |
131 | 132 | }, |
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 | | - ], |
140 | 133 | "outputs": [ |
141 | 134 | { |
142 | 135 | "name": "stdout", |
|
147 | 140 | ] |
148 | 141 | } |
149 | 142 | ], |
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 | + ] |
151 | 151 | }, |
152 | 152 | { |
153 | 153 | "cell_type": "markdown", |
|
162 | 162 | }, |
163 | 163 | { |
164 | 164 | "cell_type": "code", |
| 165 | + "execution_count": 4, |
165 | 166 | "id": "6542deab", |
166 | 167 | "metadata": { |
167 | 168 | "ExecuteTime": { |
168 | 169 | "end_time": "2025-09-10T10:33:00.335589Z", |
169 | 170 | "start_time": "2025-09-10T10:33:00.031885Z" |
170 | 171 | } |
171 | 172 | }, |
172 | | - "source": [ |
173 | | - "samples = cq.sample(batch=512, format=\"count_dict_bin\") # e.g., '00', '11', '22'\n", |
174 | | - "samples" |
175 | | - ], |
176 | 173 | "outputs": [ |
177 | 174 | { |
178 | 175 | "data": { |
|
185 | 182 | "output_type": "execute_result" |
186 | 183 | } |
187 | 184 | ], |
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 | + ] |
189 | 189 | }, |
190 | 190 | { |
191 | 191 | "cell_type": "markdown", |
|
206 | 206 | }, |
207 | 207 | { |
208 | 208 | "cell_type": "code", |
| 209 | + "execution_count": 5, |
209 | 210 | "id": "e4e2b769", |
210 | 211 | "metadata": { |
211 | 212 | "ExecuteTime": { |
212 | 213 | "end_time": "2025-09-10T10:33:00.350793Z", |
213 | 214 | "start_time": "2025-09-10T10:33:00.346566Z" |
214 | 215 | } |
215 | 216 | }, |
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 | | - ], |
227 | 217 | "outputs": [ |
228 | 218 | { |
229 | 219 | "data": { |
|
240 | 230 | "output_type": "execute_result" |
241 | 231 | } |
242 | 232 | ], |
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 | + ] |
244 | 244 | }, |
245 | 245 | { |
246 | 246 | "cell_type": "markdown", |
|
260 | 260 | }, |
261 | 261 | { |
262 | 262 | "cell_type": "code", |
| 263 | + "execution_count": 6, |
263 | 264 | "id": "a56be75e", |
264 | 265 | "metadata": { |
265 | 266 | "ExecuteTime": { |
266 | 267 | "end_time": "2025-09-10T10:33:00.361091Z", |
267 | 268 | "start_time": "2025-09-10T10:33:00.357370Z" |
268 | 269 | } |
269 | 270 | }, |
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 | | - ], |
278 | 271 | "outputs": [ |
279 | 272 | { |
280 | 273 | "data": { |
|
289 | 282 | "output_type": "execute_result" |
290 | 283 | } |
291 | 284 | ], |
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 | + ] |
293 | 293 | }, |
294 | 294 | { |
295 | 295 | "cell_type": "markdown", |
|
304 | 304 | }, |
305 | 305 | { |
306 | 306 | "cell_type": "code", |
| 307 | + "execution_count": 7, |
307 | 308 | "id": "fe4ed499", |
308 | 309 | "metadata": { |
309 | 310 | "ExecuteTime": { |
310 | 311 | "end_time": "2025-09-10T10:33:00.372256Z", |
311 | 312 | "start_time": "2025-09-10T10:33:00.367671Z" |
312 | 313 | } |
313 | 314 | }, |
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 | | - ], |
324 | 315 | "outputs": [ |
325 | 316 | { |
326 | 317 | "data": { |
|
333 | 324 | "output_type": "execute_result" |
334 | 325 | } |
335 | 326 | ], |
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 | + ] |
337 | 337 | }, |
338 | 338 | { |
339 | | - "metadata": {}, |
340 | 339 | "cell_type": "markdown", |
| 340 | + "id": "648ee02776427b3c", |
| 341 | + "metadata": {}, |
341 | 342 | "source": [ |
342 | 343 | "### Apply Arbitrary Gate\n", |
343 | 344 | "\n", |
344 | 345 | "Just directly using ``any`` API by feeding the corresponding unitary" |
345 | | - ], |
346 | | - "id": "648ee02776427b3c" |
| 346 | + ] |
347 | 347 | }, |
348 | 348 | { |
| 349 | + "cell_type": "code", |
| 350 | + "execution_count": 8, |
| 351 | + "id": "4d73e00b952f2ac4", |
349 | 352 | "metadata": { |
350 | 353 | "ExecuteTime": { |
351 | 354 | "end_time": "2025-09-10T10:33:14.972605Z", |
352 | 355 | "start_time": "2025-09-10T10:33:00.385060Z" |
353 | 356 | } |
354 | 357 | }, |
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", |
366 | 358 | "outputs": [ |
367 | 359 | { |
368 | 360 | "data": { |
|
410 | 402 | "output_type": "execute_result" |
411 | 403 | } |
412 | 404 | ], |
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 | + ] |
414 | 414 | }, |
415 | 415 | { |
416 | 416 | "cell_type": "markdown", |
|
429 | 429 | "\n", |
430 | 430 | "All the functions are similar to the `tc.Circuit`\n" |
431 | 431 | ] |
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 |
445 | 432 | } |
446 | 433 | ], |
447 | | - "metadata": {}, |
| 434 | + "metadata": { |
| 435 | + "language_info": { |
| 436 | + "name": "python" |
| 437 | + } |
| 438 | + }, |
448 | 439 | "nbformat": 4, |
449 | 440 | "nbformat_minor": 5 |
450 | 441 | } |
0 commit comments