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