|
10 | 10 | "This enables the `%%qsharp` magic and initializes a Q# interpreter singleton." |
11 | 11 | ] |
12 | 12 | }, |
13 | | - { |
14 | | - "cell_type": "markdown", |
15 | | - "id": "ed1b75bf", |
16 | | - "metadata": {}, |
17 | | - "source": [] |
18 | | - }, |
19 | 13 | { |
20 | 14 | "cell_type": "code", |
21 | 15 | "execution_count": null, |
|
69 | 63 | "source": [ |
70 | 64 | "`qsharp.eval()` does the same thing as the `%%qsharp` magic.\n", |
71 | 65 | "\n", |
72 | | - "`DumpMachine()` and `Message()` print to stdout and get displayed in the notebook as plain text" |
| 66 | + "`DumpMachine()` and `Message()` print to stdout and get displayed in the notebook as plain text." |
73 | 67 | ] |
74 | 68 | }, |
75 | 69 | { |
|
84 | 78 | "qsharp.eval(\"Main()\")\n" |
85 | 79 | ] |
86 | 80 | }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "id": "19f4ef6d", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "`qsharp.code` provides direct access to simulating callables defined in Q#." |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": null, |
| 92 | + "id": "30b92222", |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "qsharp.code.Main()" |
| 97 | + ] |
| 98 | + }, |
87 | 99 | { |
88 | 100 | "cell_type": "markdown", |
89 | 101 | "id": "a3bde193", |
|
256 | 268 | }, |
257 | 269 | { |
258 | 270 | "cell_type": "code", |
259 | | - "execution_count": null, |
| 271 | + "execution_count": 11, |
260 | 272 | "id": "eb3cd29f", |
261 | 273 | "metadata": { |
262 | 274 | "vscode": { |
|
382 | 394 | }, |
383 | 395 | { |
384 | 396 | "cell_type": "code", |
385 | | - "execution_count": null, |
| 397 | + "execution_count": 14, |
386 | 398 | "id": "9b85eb2d", |
387 | 399 | "metadata": { |
388 | 400 | "vscode": { |
|
414 | 426 | "source": [ |
415 | 427 | "qsharp.run(\"Bad()\", 10)\n" |
416 | 428 | ] |
| 429 | + }, |
| 430 | + { |
| 431 | + "cell_type": "markdown", |
| 432 | + "id": "9f738245", |
| 433 | + "metadata": {}, |
| 434 | + "source": [ |
| 435 | + "When invoked from Python, arguments to Q# callables are converted from their Python type to the expected Q# type. If an argument cannot be converted to the right type, it will trigger a runtime exception." |
| 436 | + ] |
| 437 | + }, |
| 438 | + { |
| 439 | + "cell_type": "code", |
| 440 | + "execution_count": null, |
| 441 | + "id": "9ae2729d", |
| 442 | + "metadata": {}, |
| 443 | + "outputs": [], |
| 444 | + "source": [ |
| 445 | + "qsharp.eval(\"\"\"\n", |
| 446 | + " function AddTwoInts(a : Int, b : Int) : Int {\n", |
| 447 | + " return a + b;\n", |
| 448 | + " }\n", |
| 449 | + " \"\"\")\n", |
| 450 | + "\n", |
| 451 | + "from qsharp.code import AddTwoInts\n", |
| 452 | + "\n", |
| 453 | + "print(AddTwoInts(2, 3))\n", |
| 454 | + "\n", |
| 455 | + "try:\n", |
| 456 | + " AddTwoInts(2, 3.0)\n", |
| 457 | + "except TypeError as e:\n", |
| 458 | + " print(f\"TypeError: {e}\")" |
| 459 | + ] |
| 460 | + }, |
| 461 | + { |
| 462 | + "cell_type": "markdown", |
| 463 | + "id": "0f0795e9", |
| 464 | + "metadata": {}, |
| 465 | + "source": [ |
| 466 | + "If you define any Q# callables in a namespace (or when initializing with a Q# project), those callables will be exposed with a matching hierarchy of modules in Python:" |
| 467 | + ] |
| 468 | + }, |
| 469 | + { |
| 470 | + "cell_type": "code", |
| 471 | + "execution_count": null, |
| 472 | + "id": "e7b84a41", |
| 473 | + "metadata": { |
| 474 | + "vscode": { |
| 475 | + "languageId": "qsharp" |
| 476 | + } |
| 477 | + }, |
| 478 | + "outputs": [], |
| 479 | + "source": [ |
| 480 | + "%%qsharp\n", |
| 481 | + "\n", |
| 482 | + "import Std.Diagnostics.DumpMachine;\n", |
| 483 | + "namespace Foo {\n", |
| 484 | + " operation Bar() : Unit {\n", |
| 485 | + " use qs = Qubit[2];\n", |
| 486 | + " for q in qs {\n", |
| 487 | + " H(q);\n", |
| 488 | + " }\n", |
| 489 | + " DumpMachine();\n", |
| 490 | + " ResetAll(qs);\n", |
| 491 | + " }\n", |
| 492 | + "}" |
| 493 | + ] |
| 494 | + }, |
| 495 | + { |
| 496 | + "cell_type": "code", |
| 497 | + "execution_count": null, |
| 498 | + "id": "5591587c", |
| 499 | + "metadata": {}, |
| 500 | + "outputs": [], |
| 501 | + "source": [ |
| 502 | + "from qsharp.code.Foo import Bar\n", |
| 503 | + "\n", |
| 504 | + "Bar()" |
| 505 | + ] |
| 506 | + }, |
| 507 | + { |
| 508 | + "cell_type": "markdown", |
| 509 | + "id": "020b244b", |
| 510 | + "metadata": {}, |
| 511 | + "source": [ |
| 512 | + "If you run `qsharp.init()`, the compiler and simulator state are reset and all functions exposed into Python are cleared:" |
| 513 | + ] |
| 514 | + }, |
| 515 | + { |
| 516 | + "cell_type": "code", |
| 517 | + "execution_count": null, |
| 518 | + "id": "3d73c247", |
| 519 | + "metadata": {}, |
| 520 | + "outputs": [], |
| 521 | + "source": [ |
| 522 | + "qsharp.init()\n", |
| 523 | + "\n", |
| 524 | + "try:\n", |
| 525 | + " Bar()\n", |
| 526 | + "except qsharp.QSharpError as e:\n", |
| 527 | + " print(f\"QsharpError: {e}\")" |
| 528 | + ] |
417 | 529 | } |
418 | 530 | ], |
419 | 531 | "metadata": { |
|
432 | 544 | "name": "python", |
433 | 545 | "nbconvert_exporter": "python", |
434 | 546 | "pygments_lexer": "ipython3", |
435 | | - "version": "3.12.1" |
| 547 | + "version": "3.11.11" |
436 | 548 | } |
437 | 549 | }, |
438 | 550 | "nbformat": 4, |
|
0 commit comments