microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/python_interop/openqasm.ipynb
366lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "ae56fce0", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "# Q# Interop with OpenQASM" |
| 9 | ] |
| 10 | }, |
| 11 | { |
| 12 | "cell_type": "markdown", |
| 13 | "id": "2b838c23", |
| 14 | "metadata": {}, |
| 15 | "source": [ |
| 16 | "The modern QDK provides interoperability with OpenQASM 3 programs built upon the core Q# 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 Qiskit sample notebook](../../estimation/estimation-openqasm.ipynb)), leveraging the Q# compiler's capabilities for analysis, transformation, code generation, and simulation. This also enables the generation of QIR from OpenQASM progams leveraging the [modern 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." |
| 21 | ] |
| 22 | }, |
| 23 | { |
| 24 | "cell_type": "markdown", |
| 25 | "id": "4f761649", |
| 26 | "metadata": {}, |
| 27 | "source": [ |
| 28 | "### Run OpenQASM 3 Code in interactive session\n", |
| 29 | "Interactive sessions have different semantics from program execution. We no longer have inferred output and input. Instead we treat qasm lines as code fragments and interpret them one at a time (though they are all compiled together). Due to scoping rules in OpenQASM, all code used in the the program must be defined in the snippet and can't use compilation state from other cells or calls.\n", |
| 30 | "\n", |
| 31 | "\n", |
| 32 | "\n", |
| 33 | "Import the Q# module.\n", |
| 34 | "\n", |
| 35 | "This enables the `%%openqasm` (and `%%qsharp`) magic and initializes a interpreter singleton." |
| 36 | ] |
| 37 | }, |
| 38 | { |
| 39 | "cell_type": "code", |
| 40 | "execution_count": 1, |
| 41 | "id": "e91dd2d7", |
| 42 | "metadata": {}, |
| 43 | "outputs": [ |
| 44 | { |
| 45 | "data": { |
| 46 | "application/javascript": "// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n// This file provides CodeMirror syntax highlighting for Q# magic cells\n// in classic Jupyter Notebooks. It does nothing in other (Jupyter Notebook 7,\n// VS Code, Azure Notebooks, etc.) environments.\n\n// Detect the prerequisites and do nothing if they don't exist.\nif (window.require && window.CodeMirror && window.Jupyter) {\n // The simple mode plugin for CodeMirror is not loaded by default, so require it.\n window.require([\"codemirror/addon/mode/simple\"], function defineMode() {\n let rules = [\n {\n token: \"comment\",\n regex: /(\\/\\/).*/,\n beginWord: false,\n },\n {\n token: \"string\",\n regex: String.raw`^\\\"(?:[^\\\"\\\\]|\\\\[\\s\\S])*(?:\\\"|$)`,\n beginWord: false,\n },\n {\n token: \"keyword\",\n regex: String.raw`(namespace|open|as|operation|function|body|adjoint|newtype|controlled|internal)\\b`,\n beginWord: true,\n },\n {\n token: \"keyword\",\n regex: String.raw`(if|elif|else|repeat|until|fixup|for|in|return|fail|within|apply)\\b`,\n beginWord: true,\n },\n {\n token: \"keyword\",\n regex: String.raw`(Adjoint|Controlled|Adj|Ctl|is|self|auto|distribute|invert|intrinsic)\\b`,\n beginWord: true,\n },\n {\n token: \"keyword\",\n regex: String.raw`(let|set|use|borrow|mutable)\\b`,\n beginWord: true,\n },\n {\n token: \"operatorKeyword\",\n regex: String.raw`(not|and|or)\\b|(w/)`,\n beginWord: true,\n },\n {\n token: \"operatorKeyword\",\n regex: String.raw`(=)|(!)|(<)|(>)|(\\+)|(-)|(\\*)|(/)|(\\^)|(%)|(\\|)|(&&&)|(~~~)|(\\.\\.\\.)|(\\.\\.)|(\\?)`,\n beginWord: false,\n },\n {\n token: \"meta\",\n regex: String.raw`(Int|BigInt|Double|Bool|Qubit|Pauli|Result|Range|String|Unit)\\b`,\n beginWord: true,\n },\n {\n token: \"atom\",\n regex: String.raw`(true|false|Pauli(I|X|Y|Z)|One|Zero)\\b`,\n beginWord: true,\n },\n ];\n let simpleRules = [];\n for (let rule of rules) {\n simpleRules.push({\n token: rule.token,\n regex: new RegExp(rule.regex, \"g\"),\n sol: rule.beginWord,\n });\n if (rule.beginWord) {\n // Need an additional rule due to the fact that CodeMirror simple mode doesn't work with ^ token\n simpleRules.push({\n token: rule.token,\n regex: new RegExp(String.raw`\\W` + rule.regex, \"g\"),\n sol: false,\n });\n }\n }\n\n // Register the mode defined above with CodeMirror\n window.CodeMirror.defineSimpleMode(\"qsharp\", { start: simpleRules });\n window.CodeMirror.defineMIME(\"text/x-qsharp\", \"qsharp\");\n\n // Tell Jupyter to associate %%qsharp magic cells with the qsharp mode\n window.Jupyter.CodeCell.options_default.highlight_modes[\"qsharp\"] = {\n reg: [/^%%qsharp/],\n };\n\n // Force re-highlighting of all cells the first time this code runs\n for (const cell of window.Jupyter.notebook.get_cells()) {\n cell.auto_highlight();\n }\n });\n}\n", |
| 47 | "text/plain": [] |
| 48 | }, |
| 49 | "metadata": {}, |
| 50 | "output_type": "display_data" |
| 51 | }, |
| 52 | { |
| 53 | "data": { |
| 54 | "application/x.qsharp-config": "{\"targetProfile\":\"base\",\"languageFeatures\":null,\"manifest\":null}", |
| 55 | "text/plain": [ |
| 56 | "Q# initialized with configuration: {'targetProfile': 'base', 'languageFeatures': None, 'manifest': None}" |
| 57 | ] |
| 58 | }, |
| 59 | "execution_count": 1, |
| 60 | "metadata": {}, |
| 61 | "output_type": "execute_result" |
| 62 | } |
| 63 | ], |
| 64 | "source": [ |
| 65 | "import qsharp\n", |
| 66 | "qsharp.init(target_profile=qsharp.TargetProfile.Base)" |
| 67 | ] |
| 68 | }, |
| 69 | { |
| 70 | "cell_type": "markdown", |
| 71 | "id": "922f8420", |
| 72 | "metadata": {}, |
| 73 | "source": [ |
| 74 | "With the runtime initialized we can now run OpenQASM programs in their own cells with the `%%qasm` magic. Running this cell will simulate the program using the default simulator and its output will be displayed." |
| 75 | ] |
| 76 | }, |
| 77 | { |
| 78 | "cell_type": "code", |
| 79 | "execution_count": null, |
| 80 | "id": "7c69aeac", |
| 81 | "metadata": { |
| 82 | "vscode": { |
| 83 | "languageId": "openqasm" |
| 84 | } |
| 85 | }, |
| 86 | "outputs": [], |
| 87 | "source": [ |
| 88 | "%%openqasm --shots 1024\n", |
| 89 | "include \"stdgates.inc\";\n", |
| 90 | "qubit[2] q;\n", |
| 91 | "h q[0];\n", |
| 92 | "cx q[0], q[1];\n", |
| 93 | "bit[2] c;\n", |
| 94 | "c[0] = measure q[0];\n", |
| 95 | "c[1] = measure q[1];" |
| 96 | ] |
| 97 | }, |
| 98 | { |
| 99 | "cell_type": "markdown", |
| 100 | "id": "62b2970d", |
| 101 | "metadata": {}, |
| 102 | "source": [ |
| 103 | "We can leverage the Jupyter `_` variable to access the run output from the previous cell if we want to work with the output." |
| 104 | ] |
| 105 | }, |
| 106 | { |
| 107 | "cell_type": "code", |
| 108 | "execution_count": null, |
| 109 | "id": "c80ddb17", |
| 110 | "metadata": {}, |
| 111 | "outputs": [], |
| 112 | "source": [ |
| 113 | "last_output = _ # type: ignore\n", |
| 114 | "print(last_output)" |
| 115 | ] |
| 116 | }, |
| 117 | { |
| 118 | "cell_type": "markdown", |
| 119 | "id": "ffd986f4", |
| 120 | "metadata": {}, |
| 121 | "source": [ |
| 122 | "We can add an optional `name` parameter to compile the program into a callable operation in the interactive session." |
| 123 | ] |
| 124 | }, |
| 125 | { |
| 126 | "cell_type": "code", |
| 127 | "execution_count": null, |
| 128 | "id": "2c1c9a73", |
| 129 | "metadata": { |
| 130 | "vscode": { |
| 131 | "languageId": "openqasm" |
| 132 | } |
| 133 | }, |
| 134 | "outputs": [], |
| 135 | "source": [ |
| 136 | "%%openqasm --name bell\n", |
| 137 | "include \"stdgates.inc\";\n", |
| 138 | "qubit[2] q;\n", |
| 139 | "h q[0];\n", |
| 140 | "cx q[0], q[1];\n", |
| 141 | "qit[2] c;\n", |
| 142 | "c[0] = measure q[0];\n", |
| 143 | "c[1] = measure q[1];" |
| 144 | ] |
| 145 | }, |
| 146 | { |
| 147 | "cell_type": "markdown", |
| 148 | "id": "a177ac9b", |
| 149 | "metadata": {}, |
| 150 | "source": [ |
| 151 | "With the OpenQASM program loaded into a callable name `bell`, we can now import it via the QDK's Python bindings:" |
| 152 | ] |
| 153 | }, |
| 154 | { |
| 155 | "cell_type": "code", |
| 156 | "execution_count": null, |
| 157 | "id": "828cdc9e", |
| 158 | "metadata": {}, |
| 159 | "outputs": [], |
| 160 | "source": [ |
| 161 | "from qsharp.code import bell\n", |
| 162 | "bell()" |
| 163 | ] |
| 164 | }, |
| 165 | { |
| 166 | "cell_type": "markdown", |
| 167 | "id": "11f805c6", |
| 168 | "metadata": {}, |
| 169 | "source": [ |
| 170 | "Additionally, since it is defined in the session, we can run it directly from a Q# cell:" |
| 171 | ] |
| 172 | }, |
| 173 | { |
| 174 | "cell_type": "code", |
| 175 | "execution_count": null, |
| 176 | "id": "9baddcfa", |
| 177 | "metadata": { |
| 178 | "vscode": { |
| 179 | "languageId": "qsharp" |
| 180 | } |
| 181 | }, |
| 182 | "outputs": [], |
| 183 | "source": [ |
| 184 | "%%qsharp\n", |
| 185 | "c" |
| 186 | ] |
| 187 | }, |
| 188 | { |
| 189 | "cell_type": "markdown", |
| 190 | "id": "db62e61f", |
| 191 | "metadata": {}, |
| 192 | "source": [ |
| 193 | "This also unlocks all of the other `qsharp` package functionality. Like noisy simulation:" |
| 194 | ] |
| 195 | }, |
| 196 | { |
| 197 | "cell_type": "code", |
| 198 | "execution_count": null, |
| 199 | "id": "a2ee4980", |
| 200 | "metadata": {}, |
| 201 | "outputs": [], |
| 202 | "source": [ |
| 203 | "from qsharp_widgets import Histogram\n", |
| 204 | "\n", |
| 205 | "Histogram(qsharp.run(\"bell()\", shots=1000, noise=qsharp.DepolarizingNoise(0.01)))" |
| 206 | ] |
| 207 | }, |
| 208 | { |
| 209 | "cell_type": "markdown", |
| 210 | "id": "b1a49542", |
| 211 | "metadata": {}, |
| 212 | "source": [ |
| 213 | "### Circuit Rendering (textual)" |
| 214 | ] |
| 215 | }, |
| 216 | { |
| 217 | "cell_type": "code", |
| 218 | "execution_count": null, |
| 219 | "id": "de7ec5f2", |
| 220 | "metadata": {}, |
| 221 | "outputs": [], |
| 222 | "source": [ |
| 223 | "qsharp.circuit(qsharp.code.bell)" |
| 224 | ] |
| 225 | }, |
| 226 | { |
| 227 | "cell_type": "markdown", |
| 228 | "id": "1eb60c84", |
| 229 | "metadata": {}, |
| 230 | "source": [ |
| 231 | "### Circuit Rendering (widget)" |
| 232 | ] |
| 233 | }, |
| 234 | { |
| 235 | "cell_type": "code", |
| 236 | "execution_count": null, |
| 237 | "id": "1ae21fe1", |
| 238 | "metadata": {}, |
| 239 | "outputs": [], |
| 240 | "source": [ |
| 241 | "from qsharp_widgets import Circuit\n", |
| 242 | "Circuit(qsharp.circuit(qsharp.code.bell))" |
| 243 | ] |
| 244 | }, |
| 245 | { |
| 246 | "cell_type": "markdown", |
| 247 | "id": "96446087", |
| 248 | "metadata": {}, |
| 249 | "source": [ |
| 250 | "### Compilation\n" |
| 251 | ] |
| 252 | }, |
| 253 | { |
| 254 | "cell_type": "code", |
| 255 | "execution_count": null, |
| 256 | "id": "fdfcab02", |
| 257 | "metadata": {}, |
| 258 | "outputs": [], |
| 259 | "source": [ |
| 260 | "print(qsharp.compile(bell))" |
| 261 | ] |
| 262 | }, |
| 263 | { |
| 264 | "cell_type": "markdown", |
| 265 | "id": "62e33a40", |
| 266 | "metadata": {}, |
| 267 | "source": [ |
| 268 | "#### QIR generation semantic errors\n", |
| 269 | "\n", |
| 270 | "When targetting harware by compiling to QIR there are additional restrictions which may cause compilation errors. Most common scenarios:\n", |
| 271 | "- Trying to generate QIR when the profile is set to `Unrestricted`. `Unrestricted` is only valid for simulation. Either `TargetProfile.Base` or `TargetProfile.Adaptive_RI` must be used.\n", |
| 272 | "- Not all bits in classical registers have been assigned to. Usually because there were no measurements, or extra registers were declared.\n", |
| 273 | "\n" |
| 274 | ] |
| 275 | }, |
| 276 | { |
| 277 | "cell_type": "markdown", |
| 278 | "id": "ac63ad88", |
| 279 | "metadata": {}, |
| 280 | "source": [ |
| 281 | "Example, generating QIR with `Unrestricted`" |
| 282 | ] |
| 283 | }, |
| 284 | { |
| 285 | "cell_type": "code", |
| 286 | "execution_count": null, |
| 287 | "id": "10d7dcb0", |
| 288 | "metadata": { |
| 289 | "vscode": { |
| 290 | "languageId": "openqasm" |
| 291 | } |
| 292 | }, |
| 293 | "outputs": [], |
| 294 | "source": [ |
| 295 | "%%openqasm\n", |
| 296 | "include \"stdgates.inc\";\n", |
| 297 | "qubit[3] q;\n", |
| 298 | "h q[1];\n", |
| 299 | "cx q[1], q[2];\n", |
| 300 | "cx q[0], q[1];\n", |
| 301 | "h q[0];\n", |
| 302 | "bit[2] c;\n", |
| 303 | "c[0] = measure q[0];\n", |
| 304 | "c[1] = measure q[1];\n", |
| 305 | "if (c[0] == 1) z q[2];\n", |
| 306 | "if (c[1] == 1) x q[2];\n" |
| 307 | ] |
| 308 | }, |
| 309 | { |
| 310 | "cell_type": "markdown", |
| 311 | "id": "fa219de1", |
| 312 | "metadata": {}, |
| 313 | "source": [ |
| 314 | "To avoid this issue, set the `target_profile` argument either in the `QSharpBackend` creation or in the `backend.qir` call." |
| 315 | ] |
| 316 | }, |
| 317 | { |
| 318 | "cell_type": "markdown", |
| 319 | "id": "3170569e", |
| 320 | "metadata": {}, |
| 321 | "source": [ |
| 322 | "When generating `QIR`, all output registers must be read into before generating QIR. Failure to do so results in a `QSharpError`.\n", |
| 323 | "\n", |
| 324 | "In this next example, we declare two output bits, but only measure into one. This causes an error because result values can only be a side effect of measurement, and cannot be used like classical variables when compiling for hardware." |
| 325 | ] |
| 326 | }, |
| 327 | { |
| 328 | "cell_type": "code", |
| 329 | "execution_count": null, |
| 330 | "id": "071480db", |
| 331 | "metadata": {}, |
| 332 | "outputs": [], |
| 333 | "source": [ |
| 334 | "circuit = QuantumCircuit(2, 2)\n", |
| 335 | "circuit.x(0)\n", |
| 336 | "circuit.measure(0, 1)\n", |
| 337 | "backend = QSharpBackend(target_profile=TargetProfile.Base)\n", |
| 338 | "try:\n", |
| 339 | " print(backend.qir(circuit))\n", |
| 340 | "except QSharpError as ex:\n", |
| 341 | " print(ex)" |
| 342 | ] |
| 343 | } |
| 344 | ], |
| 345 | "metadata": { |
| 346 | "kernelspec": { |
| 347 | "display_name": ".venv", |
| 348 | "language": "python", |
| 349 | "name": "python3" |
| 350 | }, |
| 351 | "language_info": { |
| 352 | "codemirror_mode": { |
| 353 | "name": "ipython", |
| 354 | "version": 3 |
| 355 | }, |
| 356 | "file_extension": ".py", |
| 357 | "mimetype": "text/x-python", |
| 358 | "name": "python", |
| 359 | "nbconvert_exporter": "python", |
| 360 | "pygments_lexer": "ipython3", |
| 361 | "version": "3.13.3" |
| 362 | } |
| 363 | }, |
| 364 | "nbformat": 4, |
| 365 | "nbformat_minor": 5 |
| 366 | } |
| 367 | |