DEV Community

loading...
Cover image for Intro to Quantum Computing with Qiskit and IBM Q

Intro to Quantum Computing with Qiskit and IBM Q

Marouen Helali
Site Reliability Engineer at Bayer 🌱
Updated on ・7 min read

At the end of this tutorial, you will be able to simulate the behavior of quantum circuits on your computer using Qiskit. You will also be able to connect to the IBM Q backend and access IBM’s quantum computer through the cloud (believe it or not, it’s free and everyone can access it).

Before we dive into real work, I want to lay down the requirements, and how to get them:

You will need to have Python ( version > 3.5 ), Anaconda, and the Jupyter Console. You can install all of them at the same time, simply by getting the Anaconda distribution. You can find it here.

Next, you will need to install the Qiskit library. In order to do that, you can use any package management system, but for the purpose of this tutorial I will demonstrate the steps of doing it using PIP. Now, you will need to get PIP which is a package management system for Python. Use this command to install PIP:

sudo apt install python-pip

then use pip to install Qiskit using this command:

pip install qiskit

You are almost there. Now all you need to do is to open up a terminal window. Then, type in:

jupyter console

Alt Text

You should have a similar screen to this.

Congratulations, now you are ready to start executing commands and exploring the behavior of quantum circuits.
I want to cover the theory behind the experiment that we will be doing, but first, it is good to know the difference between a Qubit and a regular bit. If you haven’t heard already, quantum computers use Qubits instead of classical bits to perform their tasks. Qubits are quantum-mechanical systems that can result in a 0 or a 1 or some superposition of them. Here is a good picture to show that:

Alt Text

Reference: https://sureshemre.wordpress.com/2016/11/27/qubit/

In the last picture, you can see on the left the possible values of classical bits, versus on the right, the values a Qubit could take. These values can be represented by a percentage of the relative probability of finding the qubit in one state of the superposition or another. That probability could be given by the distance from the center of the position within a unit sphere. So basically, if it is closer to the bottom (relative to the picture), then it will be a 1, otherwise it will be a 0.

To avoid confusion, I want to stress that quantum bits, just like classical bits, always result in either a 0 or a 1. They do not take on any other different values. But how they get there, is what is different from classical bits, and that is what makes it powerful.

Back to the theory. So the big picture of what we will be doing in this tutorial is trying to cause an entangled state (also known as the Bell State). Buzzwords aside, look at the past sphere: the states along the axis running from 0 to 1 are essentially classical states. Now imagine two Qubits’ positions being right at equator of the circle of the sphere (equidistant from the poles). That is a 50 50 chance of being 0 or 1, and boom that’s an entangled state. In such a superposition state, each Qubit possesses correlations such that neither Qubit’s state can be fully described without referencing the state of the other Qubit. This is the simplest way to demonstrate the difference between a Quantum Computer and a classical one.

Let’s dive right into coding after making sure you are working in your preferred directory. Let’s first start by creating a quantum circuit made out of a quantum register and a classical register. to do so, you will need to import qiskit, and define some variables. If you are familiar with Python, you will find ease at doing this, since under the hood, we are just coding in Python.

import qiskit as qk
qr = qk.QuantumRegister(2)
cr = qk.ClassicalRegister(2)
qc = qk.QuantumCircuit(qr,cr)

Alt Text

At this point, I want to introduce a mobile application called Hello Quantum. It is made by IBM and it looks very similar to a puzzle game. However, it explains how the quantum gates work and their behavior very well. I suggest playing with it for a little bit if you are interested in learning more about the circuit composition. The app’s icon looks like this:

Alt Text

Back to the code, now we can add operations to our circuit. We previously defined two registers (one quantum and one classical) and made a circuit out of them. Next, let’s apply a Hadamard gate on the quantum register and a cnot (controlled not) gate on the classical register.

qc.h(qr[0])
qc.cx(qr[0],qr[1])
Enter fullscreen mode Exit fullscreen mode

Alt Text

Let’s add the measurements to the circuit we just created. Since we want to do the measurements with and without a Hadamard gate beforehand, it will be easier to create separate circuits for the measurements and combine them with our entangling circuit.

The measure_Z circuit will just do our measurement in the standard basis. Afterwards, we use a qiskit function to store the result in the classical register

measure_Z = qk.QuantumCircuit(qr,cr)
measure_Z.measure(qr,cr)
Enter fullscreen mode Exit fullscreen mode

We’ll call our second measure measure_X and we’ll apply a Hadamard gate to it before the measurement.

measure_X = qk.QuantumCircuit(qr,cr)
measure_X.h(qr)
measure_X.measure(qr,cr)
Enter fullscreen mode Exit fullscreen mode

Now that we have defined our measurement circuits, we can append them to our entangling circuit, creating two new circuits that we will label test_Z and test_X.

test_Z = qc + measure_Z
test_X = qc + measure_X
Enter fullscreen mode Exit fullscreen mode

These are all the previous commands executed successfully to help make sure you nail them.

Alt Text

Now we get to the more exciting stuff! Real Quantum Computer connection!

Navigate to https://quantumexperience.ng.bluemix.net/qx and make an account and login. Once you are logged in, click on the avatar picture on the top right corner and go to account, then to advanced. You will be able to see a similar screen to this.

Alt Text

Copy your API Token, we will use it shortly.

Next, import IBMQ and get ready to connect to a quantum computer!

from qiskit import execute, IBMQ
IBMQ.enable_account("Paste your APItoken here")
Enter fullscreen mode Exit fullscreen mode

Make sure you pass in your APItoken as a string. Now if you run IBMQ.backend() you should be able to see all the backends available to you by IBM. If you look closely, they are under IBM Q Backend Acces on your IBM Q account. Now let’s define a variable for our backend and start executing some code on the QC.

backend = IBMQ.get_backend('ibmqx4')
Enter fullscreen mode Exit fullscreen mode

I chose ibmqx4 for this example, but this is purely up to you what to use. Sometimes some backends go on maintenance, so I always check what backends are available before I choose one. Here is quick summary of the backend connecting.

Alt Text

Now remember the test jobs that we were preparing out of the circuits earlier? Now we are going to use them! let’s create a job this way:

job= qk.execute([test_Z, test_X], backend = backend, shots = 1000)
Enter fullscreen mode Exit fullscreen mode

Next, let’s execute job.status() a few times, to monitor the status of the job. It should be queued if there are a lot of use for the quantum machines (or if you picked a busy one, a quick fix is to go back and redefine the variable for the backend with a different provider!), and after a bit, it should succeed. This is what I got from following that.

Alt Text

At this point, the next step is to get the result for the execution of the job.

result = job.result()
result.get_counts(test_Z)
result.get_counts(test_X)
from qiskit.tools.visualization import plot_histogram
plot_histogram(result.get_counts(test_Z))
Enter fullscreen mode Exit fullscreen mode

Alt Text

those were the results from my execution. Yours may vary. The interpretation of that is that because of the execution on a quantum computer, we have shown the existence of the “Bell State”, which is when a Qubit is in the right middle between a 0 and 1 and, and only happens in a quantum system. That is given to us by the middle values on the x-axis (01 and 10), which represents the “Bell state”, otherwise known as the entangled state.

Don’t believe me? See for yourself! let’s wrap this up by executing the same job on a local simulator (not quantum). These are the commands you will need to connect to Aer and use their local simulator, and re-execute the past job:

from qiskit import Aer
backend = Aer.get_backend('qasm_simulator')
job = qk.execute([test_Z, test_X], backend= backend, shots=1000)
result = job.result()
plot_histogram(result.get_counts(test_Z))
Enter fullscreen mode Exit fullscreen mode

Here are the results:

Alt Text

As you see from the graphs, there are no entangled state. All boring regular 0 and 1 bits. However, this was just your proof that you were using a quantum computer!
Congratulations if you made it this far and were able to connect to the ibm Q backend and execute your first job!
I want to thank Mr. McClure for being my biggest resource for this article. I utilized a lot of his Youtube video, I suggest you check it out!

Discussion (0)

Forem Open with the Forem app