Using Qiskit with IBM QX to generate quantum circuits [Tutorial]

This tutorial expands on the idea of quantum gates to introduce quantum circuits, the quantum analog of classical circuits. It goes over how classical gates can be reproduced by quantum circuits and proceeds to introduce a visual representation of quantum circuits that can be used to easily define a quantum circuit without reference to mathematics or use of a programming language. In this tutorial, we will look at how to use Qiskkit to generate quantum circuits. The Jupyter Notebook for this tutorial is available under chapter 05 at Github.

This tutorial is an excerpt taken from the book Mastering Quantum Computing with IBM QX written by Dr. Christine Corbett Moran. The book explores principles of quantum computing and the areas in which they can be applied. You’ll also learn about the IBM Ecosystem and Qiskit.

Note: Every classic bit, either 0 or 1, can be written as either a “0” or a “1” qubit, which to show that it is a qubit, is written as a name surrounded by | and >. So, for example, the qubit named “0” is written as |”0″> and the qubit named “1” is written as |”1″>. Throughout this post, the qubits will always be written as names surrounded by quotation marks and | and > to indicate that they are, indeed, qubits.

Qiskit is the Quantum Information Science Kit. It is an SDK for working with the IBM QX quantum processors. It also has a variety of tools to simulate a quantum computer in Python. In this tutorial, we are going to learn to use it to generate quantum circuits.

Single-qubit circuits in Qiskit

First, let’s import the tools to create classical and quantum registers as well as quantum circuits from qiskit:

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

Next let’s make the X |”0″> circuit using qiskit:

qr = QuantumRegister(1)
circuit = QuantumCircuit(qr)
circuit.x(qr[0])

Note that the argument to QuantumRegister is 1; this indicates that the quantum register is to contain one qubit.

Qiskit’s QuantumCircuit class and universal gate methods

We can see that the QuantumCircuit class allows us to execute a variety of gates on particular qubits in the quantum register used to initial it. The full gate set is available in the QuantumCircuit documentation, but I will give the equivalent of the gates we have learned so far:

Gate

Qiskit QuantumCircuit class method name

I

iden

X

x

Y

y

Z

z

H

h

S

s

S†

sdg

T

t

T†

tdg

CNOT

cx

Multiqubit gates in Qiskit

Now suppose we want to use qiskit to construct a circuit for CNOTusing |”+”> as the control qubit and |”0″> as the target qubit. We will need to create a quantum register to hold two qubits with qr = QuantumRegister(2). We will also need to give each qubit in the register as an argument to the cx method of the QuantumCircuit class. The first qubit argument to cx is the control qubit; the second is the target qubit. The code is as follows:

Classical registers in Qiskit circuit

We can add a classical register to our quantum circuit. We will need a classical register to hold the output of a measurement. Here is an example of adding a classical register to the circuit for CNOT using |”+”> as the control qubit and |”0″> as the target qubit:

Here we can see that just like creating an instance of the QuantumRegister class requires us to specify the length of the quantum register in qubits, creating an instance of the ClassicalRegister class requires us to specify the size of the classical register in bits. Here we can see that initializing a member of the QuantumCircuit class with a classical register means that we need to give the ClassicalRegister instance as a second argument to the QuantumCircuit constructor.

Measurement in a Qiskit circuit

Now that we have a circuit with a two-qubit quantum register and a two-qubit classical register, we can perform a measurement of all the qubits in the circuit with the measure method of the QuantumCircuit class. This method takes as input the quantum register to measure as well as the classical register in which to place the result. Here is an example: