Tutorial - Colab and Qiskit
In this course, we are going to use Python to run our code on a quantum computer or a simulator. We encourage you to use Google Colaboratory Links to an external site. or local Python installations. This tutorial explains how to access and run the IBM Q quantum computing using the Qiskit framework.
Installing Qiskit
We first install Qiskit by running this command:
!pip install qiskit
If we want to use the IBM Q quantum computer, we need to obtain an access token from IBM Quantum Experience: We need to get an API token that gives us permission to use their quantum devices and send them our code. To obtain that API token, we need to create an account on the IBM Quantum Experience website: quantum-computing.ibm.com. Links to an external site.
After logged into the IBM experience, you should see a page similar to:
This website allows us to:
- To create a quantum circuit graphically using the IBM circuit composer. In this course, we are not going to use it, but it is a possibility.
- Or to use online Qiskit notebooks by going to the left side and clicking Qiskit notebooks or by going to the homepage and clicking Create a notebook.
In order to get our API token, we go to your profile page by clicking this top right bar-> Account settings.
And this is our API token
Copy the token on Jupyter notebook. Then run the following lines of code:
from qiskit import IBMQ
IBMQ.save_account('IBM Token pasted from the website')
Once you do this, your API token is saved onto your computer, and now you are ready to access IBM's quantum devices!
To see that you have access to these devices, you can type:
IBMQ.load_account()
Hello World - Qiskit
Our first hello world application in quantum mechanics using Qiskit will be the so-called Bell Circuit that we will study in the course's second module. The Bell quantum circuit is called the hello world of quantum computing, which includes both the superposition and entanglement effects. For now, you need to understand the functioning, and we will be back to it later in the course:
from qiskit import *
The command above will import everything from Qiskit to have access to all the Qiskit libraries provided. Our Bell quantum circuit consists of 2 qubits:
- two qubit quantum registers, each containing a qubit.
- two classical registers so that we can take measurements from these quantum bits.
The following are the commands to build this circuit:
qreg = QuantumRegister(2)
creg = ClassicalRegister(2)
circuit = QuantumCircuit(qreg,creg)
At any point that we modify the circuit, if you want to look at what the circuit looks like, you can draw it out by doing the following.
%matplotlib inline
circuit.draw()
We have 2 quantum bits q0_0 & q0_1 in the circuit and two classical bits c0_0 & c0_1 in the circuit.
Let's create our first quantum circuit and apply a Hadamard gate (we will study it in the course's second module) onto the first qubit.
circuit.h(qreg[0])
Then, we will create a two-qubit operation called a Controlled NOT (or Control X):
circuit.cx(qreg[0], qreg[1])
Our quantum circuit is now composed of a Hadamard gate and a Controlled-X gate.
We can plot the circuit with matplotlib using the pylatexenc Python module to visualize the circuit. To install the pylatexenc:
!pip install pylatexenc
circuit.draw()

Measure the quantum bits or qubits
Let’s take those measurements and store them into the classical bits.
The measurement results of the qubits are in the quantum register, we store them in the classical registers like this:
circuit.measure(qreg, creg)
So, the measurement helps us understand what happened in the quantum circuit at the end.
Running the circuit
We will try to test two cases:
- Run our quantum circuit on our personal classical computer and simulate a quantum computer.
- Send the same quantum circuit to quantum computer at IBM and see the results of running this quantum circuit on that device.
1 - IBM Quantum Simulator Backend
In order to simulate the circuit, we will use the Aer component of Qiskit.
We use Aer when we need to simulate our quantum circuits on our local computer.
So, the simulator can be imported from Aer:
simulator = Aer.get_backend('qasm_simulator')
The name of the simulator is QASM simulator = Quantum Assembly Language. The backend is where we will execute the simulator we have imported (QASM in our case).
Once we’ve executed our quantum circuit, let’s get the results back by applying the result() function and assigning them to a variable called result:
result = execute(circuit, backend = simulator).result()
Let’s look at what the result itself contains by importing visualization tools from Qiskit:
from qiskit.visualization import plot_histogram
We only need to take that result and call get_counts() and pass in our circuit:
plot_histogram(result.get_counts(circuit))

We got roughly 50% or 0.5 probability of 00 and almost 0.5 probability of 11. These small errors are because we are running a limited number of shots on our simulation instead of an infinite number of shots.
2 - IBM Quantum Computer Backend
Let’s run it on a quantum computer at IBM and see what happens. Let’s load our account. We are ready to choose the device on which to run our code and then continue to get results:
A backend provider provides Backend objects that can be used for executing QuantumCircuit. To access a given provider, we should use the get_provider method of the IBMQ account, filtering by the hub ‘ibm-q’:
provider = IBMQ.get_provider(‘ibm-q’)
To get the list of available backends in service. Let’s run the following code:
from qiskit import IBMQ, Aer
provider = IBMQ.load_account()
available_cloud_backends = provider.backends()
print(‘\n Cloud backends:’)
for i in available_cloud_backends: print(i)
available_local_backends = Aer.backends()
print(‘\n Local backends: ‘)
for i in available_local_backends: print(i)
We obtain the list of available backends. The device name we use is ibmq_belem.
qcomp = provider.get_backend('ibmq_belem')
job = execute(circuit, backend = qcomp)
The jobs execute the circuit that we built on the backend called qcomp.
from qiskit.tools.monitor import job_monitorjob_monitor(job)
And we see that the job is queued now (at the position 53) and executed (it might take a few minutes):
Now, we can run this:
result = job.result()
plot_histogram(result.get_counts(circuit))
The difference is that you only got 00 and 11 in a simulated case. However, when the code was run on a real quantum device, we also had a small number of results showing up in 01 and 10, although the majority of the results are still at the 00 state and 11 state. We will discuss the Bell circuit later on in the course in Module II.
Source: Rishabh Anand, https://towardsdatascience.com/building-your-own-quantum-circuits-in-python-e9031b548fa7 Links to an external site.