In this post we will describe how to get started with implementing quantum circuits and executing them on simulators and real quantum computers. For this we will use Qiskit and IBM’s quantum computers.
Installing conda and start a jupyter notebook with a qiskit environment
- Start by installing anaconda. It can be downloaded here https://www.anaconda.com/distribution/. The size of the installer is around 0.5 GB, but it is worth it, since it is great at managing python libraries, dependencies, and environments.
- To create an environment with qiskit you can run the following commands in a terminal (Linux) or Anaconda Powershell (Windows):
conda create -n qiskit_env python=3 scipy numpy matplotlib jupyterto create a python 3 environment.
- Active the environment through
conda activate qiskit_env
- Install qiskit in the environment through
pip install qiskit qiskit qiskit-terra[visualization]
- Now you should have a working qiskit environment. I recommend to start a jupyter notebook with the command
Check if your environment is working
- Once you have started your jupyter notebook, you can create a new python 3 notebook, just by clicking on “new” and selecting “python 3”
- The first thing you can check is the version information of the qiskit software. You can do so by executing
and you should see a response similar to this (the version numbers might differ):
import qiskit.tools.jupyter %qiskit_version_table
Create and execute your first quantum circuit
- Next we will create a simple circuit that produces a Bell state, i.e. a quantum state of two qubits that are maximally entangled, i.e., in the state .
You can download the full notebook here.
from qiskit import * q = QuantumRegister(2)# create a quantum register of two qubits c = ClassicalRegister(2)# create a classical register for our measurements circ = QuantumCircuit(q,c) circ.h(q)#Add the Hadamard gate on qubit 0, putting this qubit in superposition circ.cx(q,q)#add a controlled NOT gate with control qubit 0 and target qubit 1, # this puts the qubits into one of the Bell states circ.measure(q,c)#measure the qubits into the classical register
- Now you can visualize the circuit you have created. You can set output to be latex, latex_source, text, and mpl.
- Next we want to simulate the circuit. We can do so by running the following code.
which should print out a roughly 50/50% distribution of the states ’00’ and ’11’.
backend_sim = Aer.get_backend('qasm_simulator')#use qasm simulator as backend job_sim = execute(circ, backend_sim, shots=1024)#execute the circuit using 1024 repetitions counts_sim = job_sim.result().get_counts()#get the results form the execution print(counts_sim)
- We can also plot the histogram
from qiskit.visualization import plot_histogram plot_histogram(counts_sim)
Executing the circuit on IBM’s quantum computers
In order to execute our circuits on IBM’s quantum computers you need to do the following.
- Create a free IBM Q Experience account at https://quantum-computing.ibm.com/login
- Navigate to “My Account” to view your account settings. There you will find an API token that you need to copy and paste into the follwowing code.
IBMQ.save_account('yourAPItoken')#this needs to be run only the first time IBMQ.load_account() provider = IBMQ.get_provider(hub='ibm-q')#choose the provider provider.backends()#this prints a list of available quantum computers we can execute our code on
- We are going to execute our circuit on IBM’s QX2 device. This will put the execution of the circuit onto the queue of the device. We can use a job monitor to follow the status.
from qiskit.tools.monitor import job_monitor backend_qx2 = provider.get_backend('ibmqx2') job_qx2 = execute(circ, backend_qx2, shots=1024) job_monitor(job_qx2)
- Once the status is reported as a successful run (the wait time varies greatly) we can compare the result to the simulator.
counts_qx2 = job_qx2.result().get_counts()#get the results form the execution plot_histogram([counts_qx2,counts_sim], legend=['QX2', 'Simulator'])
Where to go from here
Now you have successfully created and execute your first circuit on a real quantum computer. There is much more to learn and discover. For the basics you might want to have a look at the Qiskit tutorials or the Qiskit Textbook.