|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Using Qasm in Qiskit\n", |
| 8 | + "\n", |
| 9 | + "You can write quantum programs in qasm quantum assembly code.\n", |
| 10 | + "\n", |
| 11 | + "We say \"qasm\" meaning the currently supported quantum assembly language.\n", |
| 12 | + "\n", |
| 13 | + "See:\n", |
| 14 | + "* [OPENQASM 2.x](https:arxiv.org/abs/1707.03429)\n", |
| 15 | + "* [Qiskit/openqasm: Gate and operation specification for ... - GitHub](https://github.com/Qiskit/openqasmhttps://github.com/Qiskit/openqasm)\n", |
| 16 | + "\n", |
| 17 | + "You can use Qiskit to execute your quantum programs on any supported backend.\n" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "## Backends\n", |
| 25 | + "\n", |
| 26 | + "Backends are environments which run your code.\n", |
| 27 | + "\n", |
| 28 | + "All of the \"real\" work of quantum processing takes place \"remotely\". Here, \"remotely\" may possibly mean \"in the Qiskit Aer simulator on the user's own workstation\". In that case, communication still takes place in the same communication model, whether it's a local simulator, a remote simulator in the cloud, or a remote IBM Q quantum processing unit in the cloud.\n", |
| 29 | + "\n", |
| 30 | + "Generally the backends you encounter will be one of the following:\n", |
| 31 | + "* the Qiskit Aer simulator running on your local workstation\n", |
| 32 | + "* the [IBM Q](https://quantum-computing.ibm.com) simulator running in the cloud\n", |
| 33 | + "* one of the [IBM Q](https://quantum-computing.ibm.com) cloud-hosted quantum processors\n", |
| 34 | + "\n", |
| 35 | + "Qiskit provides an abstract backend and communicates with backend implementations via protocols, such as [JSON](https://www.json.org/) documents.\n", |
| 36 | + "\n", |
| 37 | + "### Custom backends\n", |
| 38 | + "Qiskit is open source: anyone can write a backend implementation and host their own services reachable by Qiskit code.\n", |
| 39 | + "\n", |
| 40 | + "A backend may be\n", |
| 41 | + "* a simulator\n", |
| 42 | + "* an IBM Q (or other) quantum processor\n", |
| 43 | + "* an abacus operated by a clockwork automaton\n", |
| 44 | + "* a little old person in a backroom with paper and a pencil.\n", |
| 45 | + "\n", |
| 46 | + "The first two are implemented by Qiskit and IBM Q Experience. The latter two exotic possibilities mentioned above can be implemented by you or others when you create new backend implementations." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "## Primary Relevant Qiskit Components\n", |
| 54 | + "\n", |
| 55 | + "* [Qiskit Terra](https://github.com/Qiskit/qiskit-terra)\n", |
| 56 | + " * qasm translation\n", |
| 57 | + " * circuit generation\n", |
| 58 | + " * communications to backend\n", |
| 59 | + " * abstract backend\n", |
| 60 | + "* [Qiskit IBMQ Provider](https://github.com/Qiskit/qiskit-ibmq-provider)\n", |
| 61 | + " * qiskit-ibmq-provider backend implementation for communications to IBMQ backend" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "## Qasm and Circuit\n", |
| 69 | + "\n", |
| 70 | + "Qasm quantum assembly language is a theoretical gate model which evolved independently of IBM in the quantum computing community.\n", |
| 71 | + "\n", |
| 72 | + "Theorists express quantum operations using linear algebraic expressions.\n", |
| 73 | + "\n", |
| 74 | + "Qasm high-level operators represent quantum operations taking arguments of register and other types.\n", |
| 75 | + "\n", |
| 76 | + "These textual specifications of operations are rendered into [circuit](https://github.com/Qiskit/qiskit-tutorials/blob/master/qiskit/terra/visualizing_a_quantum_circuit.ipynb) form. Those circuits are then [transpiled](https://github.com/Qiskit/qiskit-tutorials/blob/master/qiskit/terra/using_the_transpiler.ipynb) into a sequence of low-level operations of typically limited variety offered by the target backend.\n", |
| 77 | + "\n", |
| 78 | + "Qasm can express circuits and circuits can express qasm.\n", |
| 79 | + "\n", |
| 80 | + "Typically, your Qiskit program creates a Circuit from your qasm code. Transpilation happens automatically. Then your Qiskit program submits the circuit derived from your qasm code to the backend as a Job for execution.\n", |
| 81 | + "\n", |
| 82 | + "Programmable levels exist below the higher level of qasm. Such matters are part of [OpenPulse](https://arxiv.org/pdf/1809.03452.pdf).\n", |
| 83 | + "\n", |
| 84 | + "### Transpilation\n", |
| 85 | + "\n", |
| 86 | + "When qasm programs are **transpiled** (compiled with transformation) for backend consumption, they may be transpiled into longer or shorter strings of lower level operators called \"basis gates\", i.e., the limited set of operators physically or virtually implemented at the next level which will be receiving the transpilation output.\n", |
| 87 | + "\n", |
| 88 | + "Qiskit offers visibility into the output of transpilation." |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## Put it all together\n", |
| 96 | + "\n", |
| 97 | + "Now let's take a simple qasm program and run it." |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 1, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "# importing Qiskit\n", |
| 107 | + "from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister\n", |
| 108 | + "from qiskit import BasicAer, IBMQ, execute\n", |
| 109 | + "\n", |
| 110 | + "backend = BasicAer.get_backend('qasm_simulator') # run on local simulator by default\n", |
| 111 | + "\n", |
| 112 | + "# Uncomment the following lines to run on a real device\n", |
| 113 | + "# IBMQ.load_accounts()\n", |
| 114 | + "# from qiskit.providers.ibmq import least_busy\n", |
| 115 | + "# backend = least_busy(IBMQ.backends(operational=True, simulator=False))\n", |
| 116 | + "# print(\"the best backend is \" + backend.name())" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": 2, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [], |
| 124 | + "source": [ |
| 125 | + "# You might get your qasm by opening a file instead\n", |
| 126 | + "qasm_source = \"\"\"// yiqing line (one of many possible)\n", |
| 127 | + "OPENQASM 2.0;\n", |
| 128 | + "include \"qelib1.inc\";\n", |
| 129 | + "qreg q[3];\n", |
| 130 | + "creg c[3];\n", |
| 131 | + "h q[0];\n", |
| 132 | + "h q[1];\n", |
| 133 | + "h q[2];\n", |
| 134 | + "barrier q[0],q[1],q[2];\n", |
| 135 | + "measure q[0] -> c[0];\n", |
| 136 | + "measure q[1] -> c[1];\n", |
| 137 | + "measure q[2] -> c[2];\n", |
| 138 | + "\"\"\"" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 3, |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [ |
| 146 | + { |
| 147 | + "name": "stdout", |
| 148 | + "output_type": "stream", |
| 149 | + "text": [ |
| 150 | + " ┌──────────┐ ░ ┌─┐ \n", |
| 151 | + "q_0: |0>┤ U2(0,pi) ├─░─┤M├──────\n", |
| 152 | + " ├──────────┤ ░ └╥┘┌─┐ \n", |
| 153 | + "q_1: |0>┤ U2(0,pi) ├─░──╫─┤M├───\n", |
| 154 | + " ├──────────┤ ░ ║ └╥┘┌─┐\n", |
| 155 | + "q_2: |0>┤ U2(0,pi) ├─░──╫──╫─┤M├\n", |
| 156 | + " └──────────┘ ░ ║ ║ └╥┘\n", |
| 157 | + " c_0: 0 ════════════════╩══╬══╬═\n", |
| 158 | + " ║ ║ \n", |
| 159 | + " c_1: 0 ═══════════════════╩══╬═\n", |
| 160 | + " ║ \n", |
| 161 | + " c_2: 0 ══════════════════════╩═\n", |
| 162 | + " \n" |
| 163 | + ] |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "# Create circuit\n", |
| 168 | + "circ = QuantumCircuit.from_qasm_str(qasm_source)\n", |
| 169 | + "from qiskit.compiler import transpile\n", |
| 170 | + "\n", |
| 171 | + "# View representation of the circuit from a trial transpilation\n", |
| 172 | + "# When the job executes, transpilation will take place again automatically\n", |
| 173 | + "print(transpile(circ, backend=backend))" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": 4, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [ |
| 181 | + { |
| 182 | + "name": "stdout", |
| 183 | + "output_type": "stream", |
| 184 | + "text": [ |
| 185 | + "Job Status: job has successfully run\n" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "source": [ |
| 190 | + "# Number of shots to run the program (experiment); maximum is 8192 shots.\n", |
| 191 | + "shots = 256\n", |
| 192 | + "# Maximum number of credits to spend on executions.\n", |
| 193 | + "max_credits = 6\n", |
| 194 | + "job_exp = execute(circ, backend=backend, shots=shots,\n", |
| 195 | + " max_credits=max_credits, memory=True)\n", |
| 196 | + "from qiskit.tools.monitor import job_monitor\n", |
| 197 | + "\n", |
| 198 | + "# Monitor the job ... the status show below as output will change as the job progresses\n", |
| 199 | + "job_monitor(job_exp)" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": 5, |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [ |
| 207 | + { |
| 208 | + "name": "stdout", |
| 209 | + "output_type": "stream", |
| 210 | + "text": [ |
| 211 | + "Result(backend_name='qasm_simulator', backend_version='2.0.0', header=Obj(backend_name='qasm_simulator', backend_version='2.0.0'), job_id='753f8619-e79c-462c-82ba-a22c64a388ff', qobj_id='9f1207ba-5646-4f73-987c-095bf281c147', results=[ExperimentResult(data=ExperimentResultData(counts=Obj(0x0=35, 0x1=34, 0x2=26, 0x3=25, 0x4=33, 0x5=35, 0x6=38, 0x7=30), memory=['0x6', '0x3', '0x7', '0x5', '0x4', '0x2', '0x2', '0x6', '0x3', '0x6', '0x5', '0x1', '0x4', '0x3', '0x4', '0x6', '0x2', '0x2', '0x5', '0x3', '0x7', '0x7', '0x2', '0x0', '0x0', '0x1', '0x7', '0x1', '0x1', '0x1', '0x1', '0x7', '0x2', '0x0', '0x6', '0x2', '0x6', '0x5', '0x3', '0x4', '0x5', '0x0', '0x7', '0x5', '0x0', '0x3', '0x2', '0x7', '0x1', '0x3', '0x2', '0x5', '0x5', '0x7', '0x6', '0x4', '0x5', '0x7', '0x6', '0x5', '0x6', '0x6', '0x6', '0x7', '0x3', '0x3', '0x0', '0x1', '0x6', '0x5', '0x7', '0x1', '0x1', '0x0', '0x2', '0x0', '0x6', '0x0', '0x0', '0x6', '0x1', '0x5', '0x1', '0x1', '0x2', '0x1', '0x0', '0x2', '0x1', '0x6', '0x4', '0x0', '0x7', '0x3', '0x6', '0x4', '0x4', '0x2', '0x4', '0x5', '0x4', '0x4', '0x3', '0x5', '0x3', '0x6', '0x5', '0x4', '0x5', '0x3', '0x3', '0x3', '0x3', '0x1', '0x4', '0x0', '0x4', '0x0', '0x6', '0x1', '0x3', '0x1', '0x2', '0x4', '0x2', '0x7', '0x0', '0x5', '0x3', '0x7', '0x4', '0x4', '0x0', '0x5', '0x4', '0x0', '0x7', '0x1', '0x5', '0x7', '0x5', '0x6', '0x6', '0x0', '0x1', '0x1', '0x7', '0x5', '0x4', '0x6', '0x7', '0x2', '0x5', '0x2', '0x6', '0x7', '0x2', '0x0', '0x6', '0x3', '0x6', '0x6', '0x7', '0x6', '0x0', '0x1', '0x6', '0x1', '0x0', '0x5', '0x5', '0x2', '0x3', '0x7', '0x5', '0x4', '0x0', '0x1', '0x6', '0x7', '0x5', '0x4', '0x4', '0x6', '0x0', '0x0', '0x4', '0x0', '0x6', '0x1', '0x7', '0x0', '0x5', '0x2', '0x3', '0x7', '0x3', '0x1', '0x1', '0x5', '0x4', '0x1', '0x5', '0x6', '0x5', '0x4', '0x5', '0x6', '0x4', '0x6', '0x1', '0x2', '0x1', '0x4', '0x6', '0x0', '0x5', '0x2', '0x2', '0x2', '0x4', '0x1', '0x7', '0x4', '0x0', '0x0', '0x4', '0x5', '0x5', '0x2', '0x1', '0x4', '0x3', '0x0', '0x2', '0x6', '0x6', '0x4', '0x3', '0x3', '0x6', '0x6', '0x7', '0x7', '0x0', '0x1', '0x0', '0x7', '0x0', '0x7', '0x5', '0x0', '0x4', '0x7', '0x1', '0x0']), header=Obj(clbit_labels=[['c', 0], ['c', 1], ['c', 2]], creg_sizes=[['c', 3]], memory_slots=3, n_qubits=3, name='circuit0', qreg_sizes=[['q', 3]], qubit_labels=[['q', 0], ['q', 1], ['q', 2]]), meas_level=2, name='circuit0', seed_simulator=638276239, shots=256, status='DONE', success=True, time_taken=0.0035185813903808594)], status='COMPLETED', success=True, time_taken=0.005647182464599609)\n" |
| 212 | + ] |
| 213 | + } |
| 214 | + ], |
| 215 | + "source": [ |
| 216 | + "# See the entire result expressed as a dictionary\n", |
| 217 | + "result_exp = job_exp.result()\n", |
| 218 | + "print(result_exp)" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": 6, |
| 224 | + "metadata": {}, |
| 225 | + "outputs": [ |
| 226 | + { |
| 227 | + "name": "stdout", |
| 228 | + "output_type": "stream", |
| 229 | + "text": [ |
| 230 | + "{'counts': {'0x5': 35, '0x4': 33, '0x1': 34, '0x3': 25, '0x7': 30, '0x0': 35, '0x2': 26, '0x6': 38}, 'memory': ['0x6', '0x3', '0x7', '0x5', '0x4', '0x2', '0x2', '0x6', '0x3', '0x6', '0x5', '0x1', '0x4', '0x3', '0x4', '0x6', '0x2', '0x2', '0x5', '0x3', '0x7', '0x7', '0x2', '0x0', '0x0', '0x1', '0x7', '0x1', '0x1', '0x1', '0x1', '0x7', '0x2', '0x0', '0x6', '0x2', '0x6', '0x5', '0x3', '0x4', '0x5', '0x0', '0x7', '0x5', '0x0', '0x3', '0x2', '0x7', '0x1', '0x3', '0x2', '0x5', '0x5', '0x7', '0x6', '0x4', '0x5', '0x7', '0x6', '0x5', '0x6', '0x6', '0x6', '0x7', '0x3', '0x3', '0x0', '0x1', '0x6', '0x5', '0x7', '0x1', '0x1', '0x0', '0x2', '0x0', '0x6', '0x0', '0x0', '0x6', '0x1', '0x5', '0x1', '0x1', '0x2', '0x1', '0x0', '0x2', '0x1', '0x6', '0x4', '0x0', '0x7', '0x3', '0x6', '0x4', '0x4', '0x2', '0x4', '0x5', '0x4', '0x4', '0x3', '0x5', '0x3', '0x6', '0x5', '0x4', '0x5', '0x3', '0x3', '0x3', '0x3', '0x1', '0x4', '0x0', '0x4', '0x0', '0x6', '0x1', '0x3', '0x1', '0x2', '0x4', '0x2', '0x7', '0x0', '0x5', '0x3', '0x7', '0x4', '0x4', '0x0', '0x5', '0x4', '0x0', '0x7', '0x1', '0x5', '0x7', '0x5', '0x6', '0x6', '0x0', '0x1', '0x1', '0x7', '0x5', '0x4', '0x6', '0x7', '0x2', '0x5', '0x2', '0x6', '0x7', '0x2', '0x0', '0x6', '0x3', '0x6', '0x6', '0x7', '0x6', '0x0', '0x1', '0x6', '0x1', '0x0', '0x5', '0x5', '0x2', '0x3', '0x7', '0x5', '0x4', '0x0', '0x1', '0x6', '0x7', '0x5', '0x4', '0x4', '0x6', '0x0', '0x0', '0x4', '0x0', '0x6', '0x1', '0x7', '0x0', '0x5', '0x2', '0x3', '0x7', '0x3', '0x1', '0x1', '0x5', '0x4', '0x1', '0x5', '0x6', '0x5', '0x4', '0x5', '0x6', '0x4', '0x6', '0x1', '0x2', '0x1', '0x4', '0x6', '0x0', '0x5', '0x2', '0x2', '0x2', '0x4', '0x1', '0x7', '0x4', '0x0', '0x0', '0x4', '0x5', '0x5', '0x2', '0x1', '0x4', '0x3', '0x0', '0x2', '0x6', '0x6', '0x4', '0x3', '0x3', '0x6', '0x6', '0x7', '0x7', '0x0', '0x1', '0x0', '0x7', '0x0', '0x7', '0x5', '0x0', '0x4', '0x7', '0x1', '0x0']}\n" |
| 231 | + ] |
| 232 | + } |
| 233 | + ], |
| 234 | + "source": [ |
| 235 | + "# Let's just look at the result data from the result without the other overhead\n", |
| 236 | + "print(result_exp.data())" |
| 237 | + ] |
| 238 | + }, |
| 239 | + { |
| 240 | + "cell_type": "code", |
| 241 | + "execution_count": 7, |
| 242 | + "metadata": {}, |
| 243 | + "outputs": [], |
| 244 | + "source": [ |
| 245 | + "# A function to generate a little database of the result data\n", |
| 246 | + "def csv_str(description, sorted_keys, sorted_counts):\n", |
| 247 | + " \"\"\"Generate a cvs as a string from sorted keys and sorted counts\"\"\"\n", |
| 248 | + " csv = []\n", |
| 249 | + " csv.append(description)\n", |
| 250 | + " keys = \"\"\n", |
| 251 | + " for key in sorted_keys:\n", |
| 252 | + " keys += key + ';'\n", |
| 253 | + " csv.append(keys)\n", |
| 254 | + " counts = \"\"\n", |
| 255 | + " for count in sorted_counts:\n", |
| 256 | + " counts += str(count) + ';'\n", |
| 257 | + " csv.append(counts)\n", |
| 258 | + " return csv" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": 8, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [ |
| 266 | + { |
| 267 | + "name": "stdout", |
| 268 | + "output_type": "stream", |
| 269 | + "text": [ |
| 270 | + "qasm_simulator 2019-08-06T08:48:27.268005\n", |
| 271 | + "000;001;010;011;100;101;110;111;\n", |
| 272 | + "35;34;26;25;33;35;38;30;\n" |
| 273 | + ] |
| 274 | + } |
| 275 | + ], |
| 276 | + "source": [ |
| 277 | + "# Get the counts and keys from the result and convert to a csv\n", |
| 278 | + "counts_exp = result_exp.get_counts(circ)\n", |
| 279 | + "sorted_keys = sorted(counts_exp.keys())\n", |
| 280 | + "sorted_counts = []\n", |
| 281 | + "for i in sorted_keys:\n", |
| 282 | + " sorted_counts.append(counts_exp.get(i))\n", |
| 283 | + "\n", |
| 284 | + "import datetime\n", |
| 285 | + "output = csv_str(str(backend) + ' ' + datetime.datetime.now().isoformat(),\n", |
| 286 | + " sorted_keys, sorted_counts)\n", |
| 287 | + "for line in output:\n", |
| 288 | + " print(line)\n" |
| 289 | + ] |
| 290 | + } |
| 291 | + ], |
| 292 | + "metadata": { |
| 293 | + "kernelspec": { |
| 294 | + "display_name": "Python 3", |
| 295 | + "language": "python", |
| 296 | + "name": "python3" |
| 297 | + }, |
| 298 | + "language_info": { |
| 299 | + "codemirror_mode": { |
| 300 | + "name": "ipython", |
| 301 | + "version": 3 |
| 302 | + }, |
| 303 | + "file_extension": ".py", |
| 304 | + "mimetype": "text/x-python", |
| 305 | + "name": "python", |
| 306 | + "nbconvert_exporter": "python", |
| 307 | + "pygments_lexer": "ipython3", |
| 308 | + "version": "3.6.8" |
| 309 | + } |
| 310 | + }, |
| 311 | + "nbformat": 4, |
| 312 | + "nbformat_minor": 2 |
| 313 | +} |
0 commit comments