microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0ffa03cf0743f955072054af5eb5ce6f1cf69663

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/notebooks/openqasm.ipynb

480lines · 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 qdk.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 qdk import DepolarizingNoise\n",
64 "from qdk.openqasm import run\n",
65 "from qdk.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 qdk.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 `init` method from qdk.\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 "from qdk import init, TargetProfile\n",
134 "init(target_profile=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 qdk.openqasm import import_openqasm, ProgramType\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\", program_type=ProgramType.File)"
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 qdk import code\n",
182 "bell = code.qasm_import.bell\n",
183 "\n",
184 "bell()"
185 ]
186 },
187 {
188 "cell_type": "markdown",
189 "id": "8db074dc",
190 "metadata": {},
191 "source": [
192 "Additionally, since it is defined in the runtime, we can run it directly from a Q# cell:"
193 ]
194 },
195 {
196 "cell_type": "code",
197 "execution_count": null,
198 "id": "ad6d0331",
199 "metadata": {
200 "vscode": {
201 "languageId": "qsharp"
202 }
203 },
204 "outputs": [],
205 "source": [
206 "%%qsharp\n",
207 "qasm_import.bell()"
208 ]
209 },
210 {
211 "cell_type": "markdown",
212 "id": "6578cadc",
213 "metadata": {},
214 "source": [
215 "This also unlocks all of the other `qdk` module 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:"
216 ]
217 },
218 {
219 "cell_type": "code",
220 "execution_count": null,
221 "id": "5bbd6d92",
222 "metadata": {},
223 "outputs": [],
224 "source": [
225 "from qdk import DepolarizingNoise\n",
226 "from qdk.openqasm import run\n",
227 "from qdk.widgets import Histogram\n",
228 "\n",
229 "Histogram(run(bell, shots=1000, noise=DepolarizingNoise(0.01)))"
230 ]
231 },
232 {
233 "cell_type": "markdown",
234 "id": "85d48772",
235 "metadata": {},
236 "source": [
237 "We can draw the progam as a textual circuit rendering passing the Python callable into the circuit function:"
238 ]
239 },
240 {
241 "cell_type": "code",
242 "execution_count": null,
243 "id": "a0a50634",
244 "metadata": {},
245 "outputs": [],
246 "source": [
247 "from qdk.openqasm import circuit\n",
248 "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 qdk.widgets import Circuit\n",
267 "\n",
268 "Circuit(circuit(bell))"
269 ]
270 },
271 {
272 "cell_type": "markdown",
273 "id": "0e1a29c8",
274 "metadata": {},
275 "source": [
276 "And finally when getting ready to submit to hardware, we can compile the program to QIR:"
277 ]
278 },
279 {
280 "cell_type": "code",
281 "execution_count": null,
282 "id": "ec6db1e5",
283 "metadata": {},
284 "outputs": [],
285 "source": [
286 "from qdk.openqasm import compile\n",
287 "print(compile(bell))"
288 ]
289 },
290 {
291 "cell_type": "markdown",
292 "id": "bb2ec2c1",
293 "metadata": {},
294 "source": [
295 "Alternatively, an OpenQASM program can be imported as an operation in Q#, where the qubits declared in OpenQASM become arguments passed into the operation. This allows the operation to be called as part of a larger Q# program that may perform additional operations before or after the imported operation."
296 ]
297 },
298 {
299 "cell_type": "code",
300 "execution_count": null,
301 "id": "ec34deca",
302 "metadata": {},
303 "outputs": [],
304 "source": [
305 "source = \"\"\"\n",
306 " include \"stdgates.inc\";\n",
307 " bit[2] c;\n",
308 " qubit[2] q;\n",
309 " h q[0];\n",
310 " cx q[0], q[1];\n",
311 " c = measure q;\n",
312 "\"\"\"\n",
313 "\n",
314 "import_openqasm(source, name=\"BellOp\", program_type=ProgramType.Operation)"
315 ]
316 },
317 {
318 "cell_type": "markdown",
319 "id": "1161a64d",
320 "metadata": {},
321 "source": [
322 "Here, we call the resulting operation from within a Q# cell by first allocating and preparing the qubits before passing them as arguments, resetting them before they are released at the end of the scope."
323 ]
324 },
325 {
326 "cell_type": "code",
327 "execution_count": null,
328 "id": "28e8932e",
329 "metadata": {
330 "vscode": {
331 "languageId": "qsharp"
332 }
333 },
334 "outputs": [],
335 "source": [
336 "%%qsharp\n",
337 "{\n",
338 " use qs = Qubit[2];\n",
339 " // Change the state of the second qubit before invoking the operation to show\n",
340 " // an anti-correlation in the results.\n",
341 " X(qs[1]);\n",
342 " let result = BellOp(qs);\n",
343 " ResetAll(qs);\n",
344 " result\n",
345 "}"
346 ]
347 },
348 {
349 "cell_type": "markdown",
350 "id": "759f6a5e",
351 "metadata": {},
352 "source": [
353 "We can also define input for the compiled OpenQASM code so that we can parameterize input with imported callables:"
354 ]
355 },
356 {
357 "cell_type": "code",
358 "execution_count": null,
359 "id": "bd179f1a",
360 "metadata": {},
361 "outputs": [],
362 "source": [
363 "from qdk import init, TargetProfile\n",
364 "\n",
365 "source = \"\"\"\n",
366 "include \"stdgates.inc\";\n",
367 "input float theta;\n",
368 "bit[2] c;\n",
369 "qubit[2] q;\n",
370 "rx(theta) q[0];\n",
371 "rx(-theta) q[1];\n",
372 "c = measure q;\n",
373 "\"\"\"\n",
374 "\n",
375 "init(target_profile=TargetProfile.Base)\n",
376 "import_openqasm(source, name=\"parameterized_program\", program_type=ProgramType.File)\n"
377 ]
378 },
379 {
380 "cell_type": "code",
381 "execution_count": null,
382 "id": "5b7a97d2",
383 "metadata": {},
384 "outputs": [],
385 "source": [
386 "from qdk import code\n",
387 "from qdk.openqasm import compile\n",
388 "\n",
389 "qasm_import = code.qasm_import\n",
390 "parameterized_program = qasm_import.parameterized_program\n",
391 "\n",
392 "print(compile(parameterized_program, 1.57))"
393 ]
394 },
395 {
396 "cell_type": "markdown",
397 "id": "31d20b6c",
398 "metadata": {},
399 "source": [
400 "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:"
401 ]
402 },
403 {
404 "cell_type": "code",
405 "execution_count": null,
406 "id": "168c3226",
407 "metadata": {},
408 "outputs": [],
409 "source": [
410 "init()\n",
411 "\n",
412 "source = \"\"\"\n",
413 "include \"stdgates.inc\";\n",
414 "bit[2] c;\n",
415 "qubit[2] q;\n",
416 "c = measure q;\n",
417 "\"\"\"\n",
418 "\n",
419 "import_openqasm(source, name=\"measure2\", program_type=ProgramType.File)\n",
420 "\n",
421 "from qdk import code\n",
422 "measure2 = code.qasm_import.measure2\n",
423 "\n",
424 "Histogram(run(measure2, shots=1000, qubit_loss=0.1))"
425 ]
426 },
427 {
428 "cell_type": "markdown",
429 "id": "4773ac0e",
430 "metadata": {},
431 "source": [
432 "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:"
433 ]
434 },
435 {
436 "cell_type": "code",
437 "execution_count": null,
438 "id": "02a97625",
439 "metadata": {},
440 "outputs": [],
441 "source": [
442 "source = \"\"\"\n",
443 "include \"stdgates.inc\";\n",
444 "include \"qdk.inc\";\n",
445 "qubit q;\n",
446 "output int res;\n",
447 "h q;\n",
448 "res = mresetz_checked(q);\n",
449 "\"\"\"\n",
450 "\n",
451 "import_openqasm(source, name=\"mresetz_checked_example\", program_type=ProgramType.File)\n",
452 "\n",
453 "mresetz_checked_example = code.qasm_import.mresetz_checked_example\n",
454 "\n",
455 "Histogram(run(mresetz_checked_example, shots=1000, qubit_loss=0.1))\n"
456 ]
457 }
458 ],
459 "metadata": {
460 "kernelspec": {
461 "display_name": "Python 3",
462 "language": "python",
463 "name": "python3"
464 },
465 "language_info": {
466 "codemirror_mode": {
467 "name": "ipython",
468 "version": 3
469 },
470 "file_extension": ".py",
471 "mimetype": "text/x-python",
472 "name": "python",
473 "nbconvert_exporter": "python",
474 "pygments_lexer": "ipython3",
475 "version": "3.14.3"
476 }
477 },
478 "nbformat": 4,
479 "nbformat_minor": 5
480}
481