Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

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

Save for later
  • 5 min read
  • 20 Apr 2019

article-image

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.

The XH|"0"> circuit using qiskit becomes the following:

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

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:








Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime







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 CNOT using |"+"> 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:

qr = QuantumRegister(2)
circuit = QuantumCircuit(qr)
circuit.h(qr[0])
circuit.cx(qr[0],qr[1])

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:

qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr)
circuit.h(qr[0])
circuit.cx(qr[0],qr[1])


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:

qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr)
circuit.h(qr[0])
circuit.cx(qr[0],qr[1])
circuit.measure(qr, cr)


Note that we can also decide to measure just an individual qubit, by specifying which qubit to measure and which bit to put the output result in the following:

qr = QuantumRegister(2)
cr = ClassicalRegister(2)
circuit = QuantumCircuit(qr, cr)
circuit.h(qr[0])
circuit.cx(qr[0],qr[1])
circuit.measure(qr[0], cr[0])


That's it. We learned how to use Qiskit to write python code to represent different quantum circuits namely, single-qubit circuits, Qiskit's QuantumCircuit class, and universal gate methods, Multiqubit gates in Qiskit, Classical registers in Qiskit circuit, and Measurement in a Qiskit circuit.

If you want to learn other core concepts and principles of Quantum computing with IBM QX, be sure to check out Mastering Quantum Computing with IBM QX.

IBM Q System One, IBM’s standalone quantum computer unveiled at CES 2019

IBM launches Industry's first 'Cybersecurity Operations Center on Wheels' for on-demand cybersecurity support

Say hello to IBM RXN, a free AI Tool in IBM Cloud for predicting chemical reactions