microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/notebooks/sample.ipynb
552lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "a2f48f2b", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "Import the Q# module.\n", |
| 9 | "\n", |
| 10 | "This enables the `%%qsharp` magic and initializes a Q# interpreter singleton." |
| 11 | ] |
| 12 | }, |
| 13 | { |
| 14 | "cell_type": "code", |
| 15 | "execution_count": null, |
| 16 | "id": "1e8e4faa", |
| 17 | "metadata": {}, |
| 18 | "outputs": [], |
| 19 | "source": [ |
| 20 | "import qsharp\n" |
| 21 | ] |
| 22 | }, |
| 23 | { |
| 24 | "cell_type": "markdown", |
| 25 | "id": "3a536d53", |
| 26 | "metadata": {}, |
| 27 | "source": [ |
| 28 | "Run Q# using the `%%qsharp` magic.\n", |
| 29 | "\n", |
| 30 | "`DumpMachine()` and `Message()` output get formatted as HTML. Return value is shown as cell output." |
| 31 | ] |
| 32 | }, |
| 33 | { |
| 34 | "cell_type": "code", |
| 35 | "execution_count": null, |
| 36 | "id": "9df62352", |
| 37 | "metadata": { |
| 38 | "vscode": { |
| 39 | "languageId": "qsharp" |
| 40 | } |
| 41 | }, |
| 42 | "outputs": [], |
| 43 | "source": [ |
| 44 | "%%qsharp\n", |
| 45 | "\n", |
| 46 | "operation Main() : Result {\n", |
| 47 | " use q = Qubit();\n", |
| 48 | " X(q);\n", |
| 49 | " Std.Diagnostics.DumpMachine();\n", |
| 50 | " let r = M(q);\n", |
| 51 | " Message($\"The result of the measurement is {r}\");\n", |
| 52 | " Reset(q);\n", |
| 53 | " r\n", |
| 54 | "}\n", |
| 55 | "\n", |
| 56 | "Main()\n" |
| 57 | ] |
| 58 | }, |
| 59 | { |
| 60 | "cell_type": "markdown", |
| 61 | "id": "4584c494", |
| 62 | "metadata": {}, |
| 63 | "source": [ |
| 64 | "`qsharp.eval()` does the same thing as the `%%qsharp` magic.\n", |
| 65 | "\n", |
| 66 | "`DumpMachine()` and `Message()` print to stdout and get displayed in the notebook as plain text." |
| 67 | ] |
| 68 | }, |
| 69 | { |
| 70 | "cell_type": "code", |
| 71 | "execution_count": null, |
| 72 | "id": "7d0995bf", |
| 73 | "metadata": { |
| 74 | "scrolled": true |
| 75 | }, |
| 76 | "outputs": [], |
| 77 | "source": [ |
| 78 | "qsharp.eval(\"Main()\")\n" |
| 79 | ] |
| 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 | }, |
| 99 | { |
| 100 | "cell_type": "markdown", |
| 101 | "id": "a3bde193", |
| 102 | "metadata": {}, |
| 103 | "source": [ |
| 104 | "Assign a result to a Python variable." |
| 105 | ] |
| 106 | }, |
| 107 | { |
| 108 | "cell_type": "code", |
| 109 | "execution_count": null, |
| 110 | "id": "50383f8a", |
| 111 | "metadata": {}, |
| 112 | "outputs": [], |
| 113 | "source": [ |
| 114 | "result = qsharp.eval(\"1 + 2\")\n", |
| 115 | "\n", |
| 116 | "print(f\"Result: {result} (type: {type(result).__name__})\")\n" |
| 117 | ] |
| 118 | }, |
| 119 | { |
| 120 | "cell_type": "markdown", |
| 121 | "id": "b06b7857", |
| 122 | "metadata": {}, |
| 123 | "source": [ |
| 124 | "Errors are exceptions. \n", |
| 125 | "\n", |
| 126 | "Catch and handle compilation errors." |
| 127 | ] |
| 128 | }, |
| 129 | { |
| 130 | "cell_type": "code", |
| 131 | "execution_count": null, |
| 132 | "id": "33fd3c4d", |
| 133 | "metadata": {}, |
| 134 | "outputs": [], |
| 135 | "source": [ |
| 136 | "from qsharp import QSharpError\n", |
| 137 | "\n", |
| 138 | "try:\n", |
| 139 | " qsharp.eval(\n", |
| 140 | " \"\"\"\n", |
| 141 | "operation Foo() : Unit {\n", |
| 142 | " Bar();\n", |
| 143 | " Baz();\n", |
| 144 | "}\n", |
| 145 | "\"\"\"\n", |
| 146 | " )\n", |
| 147 | "except QSharpError as ex:\n", |
| 148 | " print(ex)\n" |
| 149 | ] |
| 150 | }, |
| 151 | { |
| 152 | "cell_type": "markdown", |
| 153 | "id": "00f82a16", |
| 154 | "metadata": {}, |
| 155 | "source": [ |
| 156 | "Catch and handle runtime errors." |
| 157 | ] |
| 158 | }, |
| 159 | { |
| 160 | "cell_type": "code", |
| 161 | "execution_count": null, |
| 162 | "id": "e70a95d9", |
| 163 | "metadata": {}, |
| 164 | "outputs": [], |
| 165 | "source": [ |
| 166 | "try:\n", |
| 167 | " qsharp.eval(\"operation Foo() : Unit { use q = Qubit(); X(q) } Foo()\")\n", |
| 168 | "except QSharpError as ex:\n", |
| 169 | " print(ex)\n" |
| 170 | ] |
| 171 | }, |
| 172 | { |
| 173 | "cell_type": "markdown", |
| 174 | "id": "3e294471", |
| 175 | "metadata": {}, |
| 176 | "source": [ |
| 177 | "A runtime error that's not caught gets reported as a Python exception." |
| 178 | ] |
| 179 | }, |
| 180 | { |
| 181 | "cell_type": "code", |
| 182 | "execution_count": null, |
| 183 | "id": "d40d86cb", |
| 184 | "metadata": {}, |
| 185 | "outputs": [], |
| 186 | "source": [ |
| 187 | "qsharp.eval(\"operation Foo() : Unit { use q = Qubit(); X(q) } Foo()\")\n" |
| 188 | ] |
| 189 | }, |
| 190 | { |
| 191 | "cell_type": "markdown", |
| 192 | "id": "ba2f98ad", |
| 193 | "metadata": {}, |
| 194 | "source": [ |
| 195 | "In `%%qsharp` cells, exceptions are handled and displayed as error text." |
| 196 | ] |
| 197 | }, |
| 198 | { |
| 199 | "cell_type": "code", |
| 200 | "execution_count": null, |
| 201 | "id": "1b55e53c", |
| 202 | "metadata": { |
| 203 | "scrolled": false, |
| 204 | "vscode": { |
| 205 | "languageId": "qsharp" |
| 206 | } |
| 207 | }, |
| 208 | "outputs": [], |
| 209 | "source": [ |
| 210 | "%%qsharp\n", |
| 211 | "\n", |
| 212 | "operation Bar() : Unit {\n", |
| 213 | " use q = Qubit();\n", |
| 214 | " Std.Diagnostics.DumpMachine();\n", |
| 215 | " X(q);\n", |
| 216 | "}\n", |
| 217 | "\n", |
| 218 | "Bar()\n" |
| 219 | ] |
| 220 | }, |
| 221 | { |
| 222 | "cell_type": "markdown", |
| 223 | "id": "98247ac2", |
| 224 | "metadata": {}, |
| 225 | "source": [ |
| 226 | "Streaming output for long running operations." |
| 227 | ] |
| 228 | }, |
| 229 | { |
| 230 | "cell_type": "code", |
| 231 | "execution_count": null, |
| 232 | "id": "bd25ae87", |
| 233 | "metadata": { |
| 234 | "vscode": { |
| 235 | "languageId": "qsharp" |
| 236 | } |
| 237 | }, |
| 238 | "outputs": [], |
| 239 | "source": [ |
| 240 | "%%qsharp\n", |
| 241 | "\n", |
| 242 | "import Std.Diagnostics.*;\n", |
| 243 | "\n", |
| 244 | "operation Main() : Unit {\n", |
| 245 | " Message(\"Generating random bit... \");\n", |
| 246 | " for i in 0..400000 {\n", |
| 247 | " use q = Qubit();\n", |
| 248 | " H(q);\n", |
| 249 | " let r = M(q);\n", |
| 250 | " if (i % 100000) == 0 {\n", |
| 251 | " DumpMachine();\n", |
| 252 | " Message($\"Result: {r}\");\n", |
| 253 | " }\n", |
| 254 | " Reset(q);\n", |
| 255 | " }\n", |
| 256 | "}\n", |
| 257 | "\n", |
| 258 | "Main()\n" |
| 259 | ] |
| 260 | }, |
| 261 | { |
| 262 | "cell_type": "markdown", |
| 263 | "id": "2a2d9e7d", |
| 264 | "metadata": {}, |
| 265 | "source": [ |
| 266 | "Running multiple shots for an expression. Each shot uses an independent instance of the simulator. A list of results (or runtime errors) is returned." |
| 267 | ] |
| 268 | }, |
| 269 | { |
| 270 | "cell_type": "code", |
| 271 | "execution_count": null, |
| 272 | "id": "eb3cd29f", |
| 273 | "metadata": { |
| 274 | "vscode": { |
| 275 | "languageId": "qsharp" |
| 276 | } |
| 277 | }, |
| 278 | "outputs": [], |
| 279 | "source": [ |
| 280 | "%%qsharp\n", |
| 281 | "\n", |
| 282 | "operation RandomBit() : Result {\n", |
| 283 | " use q = Qubit();\n", |
| 284 | " H(q);\n", |
| 285 | " let res = M(q);\n", |
| 286 | " Reset(q);\n", |
| 287 | " return res;\n", |
| 288 | "}\n" |
| 289 | ] |
| 290 | }, |
| 291 | { |
| 292 | "cell_type": "code", |
| 293 | "execution_count": null, |
| 294 | "id": "9a9f5335", |
| 295 | "metadata": {}, |
| 296 | "outputs": [], |
| 297 | "source": [ |
| 298 | "results = qsharp.run(\"RandomBit()\", 10)\n", |
| 299 | "\n", |
| 300 | "results\n" |
| 301 | ] |
| 302 | }, |
| 303 | { |
| 304 | "cell_type": "markdown", |
| 305 | "id": "6a476e6d", |
| 306 | "metadata": {}, |
| 307 | "source": [ |
| 308 | "The results can then be processed, e.g. plotted in a histogram using popular Python libraries." |
| 309 | ] |
| 310 | }, |
| 311 | { |
| 312 | "cell_type": "code", |
| 313 | "execution_count": null, |
| 314 | "id": "7bd77379", |
| 315 | "metadata": {}, |
| 316 | "outputs": [], |
| 317 | "source": [ |
| 318 | "%pip install matplotlib\n" |
| 319 | ] |
| 320 | }, |
| 321 | { |
| 322 | "cell_type": "code", |
| 323 | "execution_count": null, |
| 324 | "id": "83cee4e8", |
| 325 | "metadata": {}, |
| 326 | "outputs": [], |
| 327 | "source": [ |
| 328 | "import matplotlib.pyplot as plt\n", |
| 329 | "import numpy as np\n", |
| 330 | "from collections import Counter\n", |
| 331 | "\n", |
| 332 | "# Sort the results so that the histogram labels appear in the correct order\n", |
| 333 | "results.sort()\n", |
| 334 | "# Count the number of times each result appears\n", |
| 335 | "counts = Counter(results)\n", |
| 336 | "\n", |
| 337 | "(values, counts) = counts.keys(), counts.values()\n", |
| 338 | "xlabels = np.arange(len(counts))\n", |
| 339 | "plt.bar(xlabels, counts)\n", |
| 340 | "plt.xticks(xlabels, values)\n", |
| 341 | "plt.show()\n" |
| 342 | ] |
| 343 | }, |
| 344 | { |
| 345 | "cell_type": "code", |
| 346 | "execution_count": null, |
| 347 | "id": "eaf766f4", |
| 348 | "metadata": {}, |
| 349 | "outputs": [], |
| 350 | "source": [ |
| 351 | "%pip install pandas\n" |
| 352 | ] |
| 353 | }, |
| 354 | { |
| 355 | "cell_type": "code", |
| 356 | "execution_count": null, |
| 357 | "id": "8095640a", |
| 358 | "metadata": {}, |
| 359 | "outputs": [], |
| 360 | "source": [ |
| 361 | "import pandas\n", |
| 362 | "from collections import Counter\n", |
| 363 | "\n", |
| 364 | "# Sort the results so that the histogram labels appear in the correct order\n", |
| 365 | "results.sort()\n", |
| 366 | "pandas.Series(results).value_counts(sort=False).plot(kind='bar')\n" |
| 367 | ] |
| 368 | }, |
| 369 | { |
| 370 | "cell_type": "markdown", |
| 371 | "id": "990642e4", |
| 372 | "metadata": {}, |
| 373 | "source": [ |
| 374 | "A compiler error in the entry expression:" |
| 375 | ] |
| 376 | }, |
| 377 | { |
| 378 | "cell_type": "code", |
| 379 | "execution_count": null, |
| 380 | "id": "f045ad9c", |
| 381 | "metadata": {}, |
| 382 | "outputs": [], |
| 383 | "source": [ |
| 384 | "qsharp.run(\"\"\"RandomBit(\"a\")\"\"\", 10)\n" |
| 385 | ] |
| 386 | }, |
| 387 | { |
| 388 | "cell_type": "markdown", |
| 389 | "id": "0ba56462", |
| 390 | "metadata": {}, |
| 391 | "source": [ |
| 392 | "Some shots throw runtime errors:" |
| 393 | ] |
| 394 | }, |
| 395 | { |
| 396 | "cell_type": "code", |
| 397 | "execution_count": null, |
| 398 | "id": "9b85eb2d", |
| 399 | "metadata": { |
| 400 | "vscode": { |
| 401 | "languageId": "qsharp" |
| 402 | } |
| 403 | }, |
| 404 | "outputs": [], |
| 405 | "source": [ |
| 406 | "%%qsharp\n", |
| 407 | "\n", |
| 408 | "operation Bad() : Unit {\n", |
| 409 | " use q = Qubit();\n", |
| 410 | " H(q);\n", |
| 411 | " let res = M(q);\n", |
| 412 | " if (res == One) {\n", |
| 413 | " // Do something bad, sometimes\n", |
| 414 | " use q2 = Qubit();\n", |
| 415 | " X(q2);\n", |
| 416 | " }\n", |
| 417 | "}\n" |
| 418 | ] |
| 419 | }, |
| 420 | { |
| 421 | "cell_type": "code", |
| 422 | "execution_count": null, |
| 423 | "id": "2445df05", |
| 424 | "metadata": {}, |
| 425 | "outputs": [], |
| 426 | "source": [ |
| 427 | "qsharp.run(\"Bad()\", 10)\n" |
| 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 MyNamespace {\n", |
| 484 | " operation MyOperation() : 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.MyNamespace import MyOperation\n", |
| 503 | "\n", |
| 504 | "MyOperation()" |
| 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 | " MyOperation()\n", |
| 526 | "except qsharp.QSharpError as e:\n", |
| 527 | " print(f\"QsharpError: {e}\")" |
| 528 | ] |
| 529 | } |
| 530 | ], |
| 531 | "metadata": { |
| 532 | "kernelspec": { |
| 533 | "display_name": "Python 3", |
| 534 | "language": "python", |
| 535 | "name": "python3" |
| 536 | }, |
| 537 | "language_info": { |
| 538 | "codemirror_mode": { |
| 539 | "name": "ipython", |
| 540 | "version": 3 |
| 541 | }, |
| 542 | "file_extension": ".py", |
| 543 | "mimetype": "text/x-python", |
| 544 | "name": "python", |
| 545 | "nbconvert_exporter": "python", |
| 546 | "pygments_lexer": "ipython3", |
| 547 | "version": "3.11.14" |
| 548 | } |
| 549 | }, |
| 550 | "nbformat": 4, |
| 551 | "nbformat_minor": 5 |
| 552 | } |
| 553 | |