Last active
March 31, 2021 09:41
-
-
Save quantumjim/64904b2aa021a284a623b5e5af65eb17 to your computer and use it in GitHub Desktop.
Quantum Circuit Quick Start Guide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Note:** You can run this notebook by uploading it to the [IBM Quantum Lab](https://quantum-computing.ibm.com/lab)\n", | |
"\n", | |
"# Quick Start Guide for Quantum Circuits\n", | |
"\n", | |
"Basic unit of quantum software:\n", | |
"* Define and manipulate qubits (quantum bits);\n", | |
"* Write an output onto normal bits.\n", | |
" \n", | |
"For all the details see [8.2 Qiskit](https://qiskit.org/textbook/ch-appendix/qiskit.html) in the Qiskit Textook.\n", | |
"\n", | |
"This is a very quick start guide." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'01': 1024}\n" | |
] | |
} | |
], | |
"source": [ | |
"'''\n", | |
"Step 1: Import the tools we need.\n", | |
"'''\n", | |
"\n", | |
"# When uses Python, it's always good to have numpy on hand for maths stuff.\n", | |
"import numpy as np\n", | |
"\n", | |
"# Import the QuantumCircuit object from qiskit.\n", | |
"from qiskit import QuantumCircuit\n", | |
"\n", | |
"# Import QuantumRegister and ClassicalRegister, used to define your qubits and bits.\n", | |
"from qiskit import QuantumRegister, ClassicalRegister\n", | |
"\n", | |
"# Import tools needed to run the circuit (by emulation).\n", | |
"# There are different ways to do this in Qiskit. Here we'll use the `execute` command.\n", | |
"from qiskit import execute, Aer\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"'''\n", | |
"Step 2: Create the quantum circuit.\n", | |
"'''\n", | |
"\n", | |
"# In this example we'll use two qubits, which will be called `q[0]` and `q[1]`.\n", | |
"q = QuantumRegister(2)\n", | |
"# And two bits to write their outputs to, `c[0]` and `c[1]`.\n", | |
"c = ClassicalRegister(2)\n", | |
"# We use these to set up the circuit.\n", | |
"qc = QuantumCircuit(q,c)\n", | |
"\n", | |
"\n", | |
"####################### Add Your Gates Here #######################\n", | |
"\n", | |
"# Now we put the quantum gates, which manipulate the qubits.\n", | |
"# As an example, here's a single `x` gate on qubit q[0].\n", | |
"qc.x(q[0])\n", | |
"# Replace this with the gates you want to use.\n", | |
"# You can plan your circuit using the game at qisk.it/hello-qiskit\n", | |
"\n", | |
"##################################################################\n", | |
"\n", | |
"\n", | |
"# The circuit should end with measurement, which is where outputs are written onto the bits.\n", | |
"\n", | |
"# The default measurement in Qiskit is a Z measurement.\n", | |
"# So if we want an X measurement, we need to do some preparation.\n", | |
"\n", | |
"# Leave the following gate commented for a Z measurement of q[0].\n", | |
"# Uncomment for an X measurement of q[0].\n", | |
"#qc.h(q[0])\n", | |
"\n", | |
"# Leave the following gate commented for a Z measurement of q[1].\n", | |
"# Uncomment for an X measurement of q[1].\n", | |
"#qc.h(q[1])\n", | |
"\n", | |
"# Once that's done, we implement the measurement\n", | |
"qc.measure(q,c)\n", | |
"\n", | |
"\n", | |
"'''\n", | |
"Step 3: Run the circuit and get results.\n", | |
"'''\n", | |
"\n", | |
"# To run the circuit we use `execute` to make a `Job` object\n", | |
"job = execute(qc, backend=Aer.get_backend('qasm_simulator'), shots=1024)\n", | |
"# and then extract the result from that.\n", | |
"counts = job.result().get_counts()\n", | |
"\n", | |
"# Now we can take a look at the results\n", | |
"print(counts)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Understanding the Results\n", | |
"\n", | |
"We typically run circuits many times in case there is randomness in the results. In the example above we used `shots=1024`, which means it was run 1024 times.\n", | |
"\n", | |
"The `counts` dictionary we get as a result tells us which outputs were obtained, as well as how many times they occurred.\n", | |
"\n", | |
"Since the circuit we ran above has two bits, outputs will two-bit strings: `00`, `01`, `10` or `11`. The measurement we used writes the result from qubit `q[0]` to bit `c[0]`, and qubit `q[1]` to bit `c[1]`.\n", | |
"\n", | |
"**Note:** Qiskit uses the convention of numbering bits from the left, so `'01'` means that `c[0]='1'` and `c[1]='0'`, whereas `'10'` means that `c[0]='0'` and `c[1]='1'`.\n", | |
"\n", | |
"Here we see that this circuit gives the output `{'01': 1024}`. This means that this circuit gives the result `'01`' with certainty, since all the shots returned this bit string as the output." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Planning Your Circuit\n", | |
"\n", | |
"You can use [this interactive tutorial](https://qiskit.org/textbook/ch-ex/hello-qiskit.html) to quantum circuits to plan your adventures with two qubits.\n", | |
"\n", | |
"To get your circuit, replace the lines used to draw the diagrams\n", | |
"\n", | |
"```\n", | |
"puzzle.get_circuit().draw(output='mpl')\n", | |
"```\n", | |
"\n", | |
"with\n", | |
"\n", | |
"```\n", | |
"puzzle.program\n", | |
"```\n", | |
"\n", | |
"This will then print the list of gates you've used, such as \n", | |
"\n", | |
"```\n", | |
"['qc.ry(np.pi/4,q[1])', 'qc.cx(q[0],q[1])']\n", | |
"```\n", | |
"\n", | |
"Then you can copy and paste each of these commands (`qc.ry(np.pi/4,q[1]` and `qc.cx(q[0],q[1])`) into the circuit above." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment