|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "metadata": {}, |
| 5 | + "metadata": { |
| 6 | + "collapsed": false |
| 7 | + }, |
6 | 8 | "source": [ |
7 | 9 | "# Running ProjectQ code on IBM Q devices" |
8 | 10 | ] |
9 | 11 | }, |
10 | 12 | { |
11 | 13 | "cell_type": "markdown", |
12 | | - "metadata": {}, |
| 14 | + "metadata": { |
| 15 | + "collapsed": false |
| 16 | + }, |
13 | 17 | "source": [ |
14 | 18 | "In this tutorial, we will see how to run code on IBM Q devices directly from within ProjectQ. All that is needed is an IBM Q Experience user account. To sign up, visit https://quantumexperience.ng.bluemix.net/.\n", |
15 | 19 | "\n", |
|
19 | 23 | "First, we import all necessary operations (`Entangle`, measurement), the back-end (`IBMBackend`), and the main compiler engine (`MainEngine`). The Entangle operation is defined as a Hadamard gate on the first qubit (creates an equal superposition of |0> and |1>), followed by controlled NOT gates acting on all other qubits controlled on the first." |
20 | 24 | ] |
21 | 25 | }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": { |
| 30 | + "collapsed": false |
| 31 | + }, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "%matplotlib inline\n", |
| 35 | + "import matplotlib.pyplot as plt" |
| 36 | + ] |
| 37 | + }, |
22 | 38 | { |
23 | 39 | "cell_type": "code", |
24 | 40 | "execution_count": 1, |
25 | | - "metadata": {}, |
| 41 | + "metadata": { |
| 42 | + "collapsed": false |
| 43 | + }, |
26 | 44 | "outputs": [], |
27 | 45 | "source": [ |
28 | 46 | "import projectq.setups.ibm\n", |
|
33 | 51 | }, |
34 | 52 | { |
35 | 53 | "cell_type": "markdown", |
36 | | - "metadata": {}, |
| 54 | + "metadata": { |
| 55 | + "collapsed": false |
| 56 | + }, |
37 | 57 | "source": [ |
38 | 58 | "Next, we instantiate a main compiler engine using the IBM Q back-end and the predefined compiler engines which take care of the qubit placement, translation of operations, etc.:" |
39 | 59 | ] |
40 | 60 | }, |
41 | 61 | { |
42 | 62 | "cell_type": "code", |
43 | 63 | "execution_count": 2, |
44 | | - "metadata": {}, |
| 64 | + "metadata": { |
| 65 | + "collapsed": false |
| 66 | + }, |
45 | 67 | "outputs": [], |
46 | 68 | "source": [ |
47 | 69 | "eng = MainEngine(IBMBackend(use_hardware=True, num_runs=1024,\n", |
|
51 | 73 | }, |
52 | 74 | { |
53 | 75 | "cell_type": "markdown", |
54 | | - "metadata": {}, |
| 76 | + "metadata": { |
| 77 | + "collapsed": false |
| 78 | + }, |
55 | 79 | "source": [ |
56 | 80 | "If `use_hardware` is set to `False`, it will use the IBM Q simulator instead. `num_runs` specifies the number of samples to collect for statistics, `verbose=True` would output additional information which may be helpful for debugging, and the device parameter lets users choose between the two devices (\"ibmqx4\" and \"ibmqx5\").\n", |
57 | 81 | "\n", |
|
61 | 85 | { |
62 | 86 | "cell_type": "code", |
63 | 87 | "execution_count": 3, |
64 | | - "metadata": {}, |
| 88 | + "metadata": { |
| 89 | + "collapsed": false |
| 90 | + }, |
65 | 91 | "outputs": [ |
66 | 92 | { |
67 | 93 | "name": "stdout", |
|
105 | 131 | " eng.flush()\n", |
106 | 132 | "\n", |
107 | 133 | " # access the probabilities via the back-end:\n", |
108 | | - " results = eng.backend.get_probabilities(qureg)\n", |
109 | | - " for state in results:\n", |
110 | | - " print(\"Measured {} with p = {}.\".format(state, results[state]))\n", |
| 134 | + " # results = eng.backend.get_probabilities(qureg)\n", |
| 135 | + " # for state in results:\n", |
| 136 | + " # print(\"Measured {} with p = {}.\".format(state, results[state]))\n", |
| 137 | + " # or plot them directly:\n", |
| 138 | + " histogram(eng.backend, qureg)\n", |
| 139 | + " plt.show()\n", |
111 | 140 | "\n", |
112 | 141 | " # return one (random) measurement outcome.\n", |
113 | 142 | " return [int(q) for q in qureg]\n", |
|
117 | 146 | }, |
118 | 147 | { |
119 | 148 | "cell_type": "markdown", |
120 | | - "metadata": {}, |
| 149 | + "metadata": { |
| 150 | + "collapsed": false |
| 151 | + }, |
121 | 152 | "source": [ |
122 | 153 | "## Retrieving a timed-out execution\n", |
123 | 154 | "Sometimes, the queue is very long and the waiting times may exceed the limit of 5 minutes. In this case, ProjectQ will raise an exception which contains the job ID, as could be seen above, where the job ID was `5b557df2306393003b746da2`." |
124 | 155 | ] |
125 | 156 | }, |
126 | 157 | { |
127 | 158 | "cell_type": "markdown", |
128 | | - "metadata": {}, |
| 159 | + "metadata": { |
| 160 | + "collapsed": false |
| 161 | + }, |
129 | 162 | "source": [ |
130 | 163 | "In order to still retrieve all results at a later point in time, one can simply re-run the entire program using a slightly modified back-end:" |
131 | 164 | ] |
132 | 165 | }, |
133 | 166 | { |
134 | 167 | "cell_type": "code", |
135 | 168 | "execution_count": 4, |
136 | | - "metadata": {}, |
| 169 | + "metadata": { |
| 170 | + "collapsed": false |
| 171 | + }, |
137 | 172 | "outputs": [ |
138 | 173 | { |
139 | 174 | "name": "stdout", |
|
193 | 228 | }, |
194 | 229 | { |
195 | 230 | "cell_type": "markdown", |
196 | | - "metadata": {}, |
| 231 | + "metadata": { |
| 232 | + "collapsed": false |
| 233 | + }, |
197 | 234 | "source": [ |
198 | 235 | "## Entangling more qubits: Using ibmqx5\n", |
199 | 236 | "\n", |
|
209 | 246 | { |
210 | 247 | "cell_type": "code", |
211 | 248 | "execution_count": 5, |
212 | | - "metadata": {}, |
| 249 | + "metadata": { |
| 250 | + "collapsed": false |
| 251 | + }, |
213 | 252 | "outputs": [], |
214 | 253 | "source": [ |
215 | 254 | "import projectq.setups.ibm16 # import setup which contains the grid mapper\n", |
|
220 | 259 | }, |
221 | 260 | { |
222 | 261 | "cell_type": "markdown", |
223 | | - "metadata": {}, |
| 262 | + "metadata": { |
| 263 | + "collapsed": false |
| 264 | + }, |
224 | 265 | "source": [ |
225 | 266 | "and then re-run the example from before via `run_entangle(eng, num_qubits)`. If an execution times out, it can also be retrieved at a later point by providing the additional `retrieve_execution=\"execution_id\"` parameter to the IBMBackend (but this time with `device='ibmqx5'`)." |
226 | 267 | ] |
227 | 268 | }, |
228 | 269 | { |
229 | 270 | "cell_type": "code", |
230 | 271 | "execution_count": 6, |
231 | | - "metadata": {}, |
| 272 | + "metadata": { |
| 273 | + "collapsed": false |
| 274 | + }, |
232 | 275 | "outputs": [ |
233 | 276 | { |
234 | 277 | "name": "stdout", |
|
488 | 531 | ], |
489 | 532 | "metadata": { |
490 | 533 | "kernelspec": { |
| 534 | + "argv": [ |
| 535 | + "python", |
| 536 | + "-m", |
| 537 | + "ipykernel_launcher", |
| 538 | + "-f", |
| 539 | + "{connection_file}" |
| 540 | + ], |
491 | 541 | "display_name": "Python 3", |
| 542 | + "env": null, |
| 543 | + "interrupt_mode": "signal", |
492 | 544 | "language": "python", |
| 545 | + "metadata": null, |
493 | 546 | "name": "python3" |
494 | 547 | }, |
495 | 548 | "language_info": { |
|
503 | 556 | "nbconvert_exporter": "python", |
504 | 557 | "pygments_lexer": "ipython3", |
505 | 558 | "version": "3.6.5" |
506 | | - } |
| 559 | + }, |
| 560 | + "name": "ibmq_tutorial.ipynb" |
507 | 561 | }, |
508 | 562 | "nbformat": 4, |
509 | 563 | "nbformat_minor": 2 |
|
0 commit comments