{{announcement.body}}
{{announcement.title}}

Introduction to Quantum Computing Using Qiskit and IBM Q

DZone 's Guide to

Introduction to Quantum Computing Using Qiskit and IBM Q

At the end of this tutorial, you will be able to simulate the behavior of quantum circuits on your computer using Qiskit.

· Web Dev Zone ·
Free Resource


0
 Advanced issue found

 
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 the 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. To do that, you can use any package management system, but for 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:

Shell
 




x


1
sudo apt install python-pip



Then use pip to install Qiskit using this command:

Shell
 




xxxxxxxxxx
1


 
1
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

python screen
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:



0
 Advanced issue found

 

Qubit
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. 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. 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 the 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.

Python
 




x


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



python code

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:Qubit

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])

qiskit

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 on the standard basis. Afterward, we use a qiskit function to store the result in the classical register

Python
 




xxxxxxxxxx
1


 
1
measure_Z = qk.QuantumCircuit(qr,cr)
2
measure_Z.measure(qr,cr)



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

Python
 




xxxxxxxxxx
1


 
1
measure_X = qk.QuantumCircuit(qr,cr)
2
measure_X.h(qr)
3
measure_X.measure(qr,cr)



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.

Python
 




xxxxxxxxxx
1


 
1
test_Z = qc + measure_Z
2
test_X = qc + measure_X



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

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.imq

Copy your API Token, we will use it shortly.

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

Python
 




xxxxxxxxxx
1


 
1
from qiskit import execute, IBMQIBMQ.enable_account("Paste your APItoken here")



Make sure you pass in your API token 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')

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 a quick summary of the backend connecting.

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:

Python
 




xxxxxxxxxx
1


1
job= qk.execute([test_Z, test_X], backend = backend, shots = 1000)


Next, let’s execute the 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.job.status

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

Python
 




xxxxxxxxxx
1


 
1
result = job.result()
2
result.get_counts(test_Z)
3
result.get_counts(test_X)from qiskit.tools.visualization import plot_histogramplot_histogram(result.get_counts(test_Z))



anaconda

Those were the results of 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:

Python
 




xxxxxxxxxx
1


 
1
from qiskit import Aerbackend = 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))



Here are the results:See.result

As you see from the graphs, there is 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!

Topics:
ibm ,ibm q ,python ,qiskit ,quantum computing ,tutorial ,web dev

Published at DZone with permission of Marouen Helali . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}