microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/notebooks/openqasm.ipynb
421lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "ae56fce0", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "# QDK Interop with OpenQASM" |
| 9 | ] |
| 10 | }, |
| 11 | { |
| 12 | "cell_type": "markdown", |
| 13 | "id": "2b838c23", |
| 14 | "metadata": {}, |
| 15 | "source": [ |
| 16 | "The QDK provides interoperability with OpenQASM 3 programs built upon the core QDK compiler infrastructure.\n", |
| 17 | "\n", |
| 18 | "This core enables integration and local resource estimation without relying on external tools. Users are able to estimate resources for their OpenQASM programs locally (see the [resource estimation with OpenQASM sample notebook](../estimation/estimation-openqasm.ipynb)), leveraging the QDK compiler's capabilities for analysis, transformation, code generation, and simulation. This also enables the generation of QIR from OpenQASM progams leveraging the [QDKs advanced code generation capabilities](https://devblogs.microsoft.com/qsharp/integrated-hybrid-support-in-the-azure-quantum-development-kit/).\n", |
| 19 | "\n", |
| 20 | "This includes support for classical instructions available in OpenQASM such as for loops, if statements, switch statements, while loops, binary expresssions, and more.\n", |
| 21 | "\n", |
| 22 | "### Simulating OpenQASM programs" |
| 23 | ] |
| 24 | }, |
| 25 | { |
| 26 | "cell_type": "code", |
| 27 | "execution_count": null, |
| 28 | "id": "016b3815", |
| 29 | "metadata": {}, |
| 30 | "outputs": [], |
| 31 | "source": [ |
| 32 | "from qsharp.openqasm import run\n", |
| 33 | "\n", |
| 34 | "source = \"\"\"\n", |
| 35 | " include \"stdgates.inc\";\n", |
| 36 | " bit[2] c;\n", |
| 37 | " qubit[2] q;\n", |
| 38 | " h q[0];\n", |
| 39 | " cx q[0], q[1];\n", |
| 40 | " c = measure q;\n", |
| 41 | "\"\"\"\n", |
| 42 | "\n", |
| 43 | "# We'll pass as_bitstring=True to convert bit[n] to a bitstring in the output.\n", |
| 44 | "# Otherwise, the output would be a list of Result values.\n", |
| 45 | "run(source, as_bitstring=True)" |
| 46 | ] |
| 47 | }, |
| 48 | { |
| 49 | "cell_type": "markdown", |
| 50 | "id": "04429707", |
| 51 | "metadata": {}, |
| 52 | "source": [ |
| 53 | "The OpenQASM programs can also be run with noise just as with Q#." |
| 54 | ] |
| 55 | }, |
| 56 | { |
| 57 | "cell_type": "code", |
| 58 | "execution_count": null, |
| 59 | "id": "dc017684", |
| 60 | "metadata": {}, |
| 61 | "outputs": [], |
| 62 | "source": [ |
| 63 | "from qsharp import DepolarizingNoise\n", |
| 64 | "from qsharp.openqasm import run\n", |
| 65 | "from qsharp_widgets import Histogram\n", |
| 66 | "\n", |
| 67 | "source = \"\"\"\n", |
| 68 | " include \"stdgates.inc\";\n", |
| 69 | " bit[2] c;\n", |
| 70 | " qubit[2] q;\n", |
| 71 | " h q[0];\n", |
| 72 | " cx q[0], q[1];\n", |
| 73 | " c = measure q;\n", |
| 74 | "\"\"\"\n", |
| 75 | "\n", |
| 76 | "Histogram(run(source, noise=DepolarizingNoise(0.01), as_bitstring=True))\n" |
| 77 | ] |
| 78 | }, |
| 79 | { |
| 80 | "cell_type": "markdown", |
| 81 | "id": "981d489a", |
| 82 | "metadata": {}, |
| 83 | "source": [ |
| 84 | "### Compiling OpenQASM to Quantum Intermediate Representation (QIR)\n", |
| 85 | "\n", |
| 86 | "We can directly compile OpenQASM to QIR with the `compile` function." |
| 87 | ] |
| 88 | }, |
| 89 | { |
| 90 | "cell_type": "code", |
| 91 | "execution_count": null, |
| 92 | "id": "fc71f209", |
| 93 | "metadata": {}, |
| 94 | "outputs": [], |
| 95 | "source": [ |
| 96 | "from qsharp.openqasm import compile\n", |
| 97 | "\n", |
| 98 | "source = \"\"\"\n", |
| 99 | " include \"stdgates.inc\";\n", |
| 100 | " bit[2] c;\n", |
| 101 | " qubit[2] q;\n", |
| 102 | " h q[0];\n", |
| 103 | " cx q[0], q[1];\n", |
| 104 | " c = measure q;\n", |
| 105 | "\"\"\"\n", |
| 106 | "\n", |
| 107 | "compilation = compile(source)\n", |
| 108 | "\n", |
| 109 | "print(compilation)" |
| 110 | ] |
| 111 | }, |
| 112 | { |
| 113 | "cell_type": "markdown", |
| 114 | "id": "95d82f22", |
| 115 | "metadata": {}, |
| 116 | "source": [ |
| 117 | "> For parameterized circuits the `import_openqasm` function must be used to first create a Python callable. A sample parameterized circuit can be found later in this notebook.\n", |
| 118 | "\n", |
| 119 | "### Run OpenQASM 3 Code in interactive session\n", |
| 120 | "\n", |
| 121 | "Import the `qsharp` module.\n", |
| 122 | "\n", |
| 123 | "This initializes a QDK interpreter singleton." |
| 124 | ] |
| 125 | }, |
| 126 | { |
| 127 | "cell_type": "code", |
| 128 | "execution_count": null, |
| 129 | "id": "75b8b81b", |
| 130 | "metadata": {}, |
| 131 | "outputs": [], |
| 132 | "source": [ |
| 133 | "import qsharp\n", |
| 134 | "qsharp.init(target_profile=qsharp.TargetProfile.Base)" |
| 135 | ] |
| 136 | }, |
| 137 | { |
| 138 | "cell_type": "markdown", |
| 139 | "id": "5dc25416", |
| 140 | "metadata": {}, |
| 141 | "source": [ |
| 142 | "With the runtime initialized, we can import an OpenQASM program as a Python callable. Here we'll compile the OpenQASM program to a callable name `\"bell\"`." |
| 143 | ] |
| 144 | }, |
| 145 | { |
| 146 | "cell_type": "code", |
| 147 | "execution_count": null, |
| 148 | "id": "012cc902", |
| 149 | "metadata": {}, |
| 150 | "outputs": [], |
| 151 | "source": [ |
| 152 | "from qsharp.openqasm import import_openqasm\n", |
| 153 | "\n", |
| 154 | "source = \"\"\"\n", |
| 155 | " include \"stdgates.inc\";\n", |
| 156 | " bit[2] c;\n", |
| 157 | " qubit[2] q;\n", |
| 158 | " h q[0];\n", |
| 159 | " cx q[0], q[1];\n", |
| 160 | " c = measure q;\n", |
| 161 | "\"\"\"\n", |
| 162 | "\n", |
| 163 | "import_openqasm(source, name=\"bell\")" |
| 164 | ] |
| 165 | }, |
| 166 | { |
| 167 | "cell_type": "markdown", |
| 168 | "id": "b95a0c8b", |
| 169 | "metadata": {}, |
| 170 | "source": [ |
| 171 | "We can now import it via the QDK's Python bindings and run it:" |
| 172 | ] |
| 173 | }, |
| 174 | { |
| 175 | "cell_type": "code", |
| 176 | "execution_count": null, |
| 177 | "id": "db043bda", |
| 178 | "metadata": {}, |
| 179 | "outputs": [], |
| 180 | "source": [ |
| 181 | "from qsharp.code import bell\n", |
| 182 | "bell()" |
| 183 | ] |
| 184 | }, |
| 185 | { |
| 186 | "cell_type": "markdown", |
| 187 | "id": "8db074dc", |
| 188 | "metadata": {}, |
| 189 | "source": [ |
| 190 | "Additionally, since it is defined in the runtime, we can run it directly from a Q# cell:" |
| 191 | ] |
| 192 | }, |
| 193 | { |
| 194 | "cell_type": "code", |
| 195 | "execution_count": null, |
| 196 | "id": "ad6d0331", |
| 197 | "metadata": { |
| 198 | "vscode": { |
| 199 | "languageId": "qsharp" |
| 200 | } |
| 201 | }, |
| 202 | "outputs": [], |
| 203 | "source": [ |
| 204 | "%%qsharp\n", |
| 205 | "bell()" |
| 206 | ] |
| 207 | }, |
| 208 | { |
| 209 | "cell_type": "markdown", |
| 210 | "id": "6578cadc", |
| 211 | "metadata": {}, |
| 212 | "source": [ |
| 213 | "This also unlocks all of the other `qsharp` package functionality. Like noisy simulation. Here we'll use the `run` function showing how we can call into the program from Python and display a histogram:" |
| 214 | ] |
| 215 | }, |
| 216 | { |
| 217 | "cell_type": "code", |
| 218 | "execution_count": null, |
| 219 | "id": "5bbd6d92", |
| 220 | "metadata": {}, |
| 221 | "outputs": [], |
| 222 | "source": [ |
| 223 | "from qsharp import DepolarizingNoise\n", |
| 224 | "from qsharp.openqasm import run\n", |
| 225 | "from qsharp.code import bell\n", |
| 226 | "from qsharp_widgets import Histogram\n", |
| 227 | "\n", |
| 228 | "Histogram(run(bell, shots=1000, noise=DepolarizingNoise(0.01)))" |
| 229 | ] |
| 230 | }, |
| 231 | { |
| 232 | "cell_type": "markdown", |
| 233 | "id": "85d48772", |
| 234 | "metadata": {}, |
| 235 | "source": [ |
| 236 | "We can draw the progam as a textual circuit rendering passing the Python callable into the circuit function:" |
| 237 | ] |
| 238 | }, |
| 239 | { |
| 240 | "cell_type": "code", |
| 241 | "execution_count": null, |
| 242 | "id": "a0a50634", |
| 243 | "metadata": {}, |
| 244 | "outputs": [], |
| 245 | "source": [ |
| 246 | "from qsharp.code import bell\n", |
| 247 | "\n", |
| 248 | "qsharp.circuit(bell)" |
| 249 | ] |
| 250 | }, |
| 251 | { |
| 252 | "cell_type": "markdown", |
| 253 | "id": "5865ea0c", |
| 254 | "metadata": {}, |
| 255 | "source": [ |
| 256 | "In notebooks, we can do a bit better leveraging the circuit widget:" |
| 257 | ] |
| 258 | }, |
| 259 | { |
| 260 | "cell_type": "code", |
| 261 | "execution_count": null, |
| 262 | "id": "c3fc87ae", |
| 263 | "metadata": {}, |
| 264 | "outputs": [], |
| 265 | "source": [ |
| 266 | "from qsharp.code import bell\n", |
| 267 | "from qsharp_widgets import Circuit\n", |
| 268 | "\n", |
| 269 | "Circuit(qsharp.circuit(bell))" |
| 270 | ] |
| 271 | }, |
| 272 | { |
| 273 | "cell_type": "markdown", |
| 274 | "id": "0e1a29c8", |
| 275 | "metadata": {}, |
| 276 | "source": [ |
| 277 | "And finally when getting ready to submit to hardware, we can compile the program to QIR:" |
| 278 | ] |
| 279 | }, |
| 280 | { |
| 281 | "cell_type": "code", |
| 282 | "execution_count": null, |
| 283 | "id": "ec6db1e5", |
| 284 | "metadata": {}, |
| 285 | "outputs": [], |
| 286 | "source": [ |
| 287 | "from qsharp.code import bell\n", |
| 288 | "\n", |
| 289 | "print(qsharp.compile(bell))" |
| 290 | ] |
| 291 | }, |
| 292 | { |
| 293 | "cell_type": "markdown", |
| 294 | "id": "759f6a5e", |
| 295 | "metadata": {}, |
| 296 | "source": [ |
| 297 | "We can also define input for the compiled OpenQASM code so that we can parameterize input with imported callables:" |
| 298 | ] |
| 299 | }, |
| 300 | { |
| 301 | "cell_type": "code", |
| 302 | "execution_count": null, |
| 303 | "id": "bd179f1a", |
| 304 | "metadata": {}, |
| 305 | "outputs": [], |
| 306 | "source": [ |
| 307 | "from qsharp import init, TargetProfile\n", |
| 308 | "from qsharp.openqasm import import_openqasm\n", |
| 309 | "\n", |
| 310 | "source = \"\"\"\n", |
| 311 | "include \"stdgates.inc\";\n", |
| 312 | "input float theta;\n", |
| 313 | "bit[2] c;\n", |
| 314 | "qubit[2] q;\n", |
| 315 | "rx(theta) q[0];\n", |
| 316 | "rx(-theta) q[1];\n", |
| 317 | "c = measure q;\n", |
| 318 | "\"\"\"\n", |
| 319 | "\n", |
| 320 | "init(target_profile=TargetProfile.Base)\n", |
| 321 | "import_openqasm(source, name=\"parameterized_program\")\n" |
| 322 | ] |
| 323 | }, |
| 324 | { |
| 325 | "cell_type": "code", |
| 326 | "execution_count": null, |
| 327 | "id": "5b7a97d2", |
| 328 | "metadata": {}, |
| 329 | "outputs": [], |
| 330 | "source": [ |
| 331 | "from qsharp.code import parameterized_program\n", |
| 332 | "from qsharp.openqasm import compile\n", |
| 333 | "\n", |
| 334 | "print(compile(parameterized_program, 1.57))" |
| 335 | ] |
| 336 | }, |
| 337 | { |
| 338 | "cell_type": "markdown", |
| 339 | "id": "31d20b6c", |
| 340 | "metadata": {}, |
| 341 | "source": [ |
| 342 | "When running an OpenQASM program in simulation with qubit loss, additional `Result.Loss` values may be returned that indicate the measured qubit was lost during execution:" |
| 343 | ] |
| 344 | }, |
| 345 | { |
| 346 | "cell_type": "code", |
| 347 | "execution_count": null, |
| 348 | "id": "168c3226", |
| 349 | "metadata": {}, |
| 350 | "outputs": [], |
| 351 | "source": [ |
| 352 | "init()\n", |
| 353 | "\n", |
| 354 | "source = \"\"\"\n", |
| 355 | "include \"stdgates.inc\";\n", |
| 356 | "bit[2] c;\n", |
| 357 | "qubit[2] q;\n", |
| 358 | "c = measure q;\n", |
| 359 | "\"\"\"\n", |
| 360 | "\n", |
| 361 | "import_openqasm(source, name=\"measure2\")\n", |
| 362 | "\n", |
| 363 | "from qsharp.code import measure2\n", |
| 364 | "\n", |
| 365 | "Histogram(run(measure2, shots=1000, qubit_loss=0.1))" |
| 366 | ] |
| 367 | }, |
| 368 | { |
| 369 | "cell_type": "markdown", |
| 370 | "id": "4773ac0e", |
| 371 | "metadata": {}, |
| 372 | "source": [ |
| 373 | "By using the special include `\"qdk.inc\"` you can check for loss at runtime using the `mresetz_checked` function. It returns an integer with two bits to indicate whether or not loss has occurred, such that `0` or `1` correspond to the qubit measurement and `2` corresponds to loss:" |
| 374 | ] |
| 375 | }, |
| 376 | { |
| 377 | "cell_type": "code", |
| 378 | "execution_count": null, |
| 379 | "id": "02a97625", |
| 380 | "metadata": {}, |
| 381 | "outputs": [], |
| 382 | "source": [ |
| 383 | "source = \"\"\"\n", |
| 384 | "include \"stdgates.inc\";\n", |
| 385 | "include \"qdk.inc\";\n", |
| 386 | "qubit q;\n", |
| 387 | "output int res;\n", |
| 388 | "h q;\n", |
| 389 | "res = mresetz_checked(q);\n", |
| 390 | "\"\"\"\n", |
| 391 | "\n", |
| 392 | "import_openqasm(source, name=\"mresetz_checked_example\")\n", |
| 393 | "\n", |
| 394 | "from qsharp.code import mresetz_checked_example\n", |
| 395 | "\n", |
| 396 | "Histogram(run(mresetz_checked_example, shots=1000, qubit_loss=0.1))\n" |
| 397 | ] |
| 398 | } |
| 399 | ], |
| 400 | "metadata": { |
| 401 | "kernelspec": { |
| 402 | "display_name": "Python 3", |
| 403 | "language": "python", |
| 404 | "name": "python3" |
| 405 | }, |
| 406 | "language_info": { |
| 407 | "codemirror_mode": { |
| 408 | "name": "ipython", |
| 409 | "version": 3 |
| 410 | }, |
| 411 | "file_extension": ".py", |
| 412 | "mimetype": "text/x-python", |
| 413 | "name": "python", |
| 414 | "nbconvert_exporter": "python", |
| 415 | "pygments_lexer": "ipython3", |
| 416 | "version": "3.11.13" |
| 417 | } |
| 418 | }, |
| 419 | "nbformat": 4, |
| 420 | "nbformat_minor": 5 |
| 421 | } |