Build a Quantum Circuit in 10 mins — ft. Qiskit, IBM’s SDK For Quantum Programming

By (EJ) Vivek Pandey

As you progress through this tutorial, new concepts are introduced and explained where applicable.

The Task

The goal of this tutorial is to:

  • Build a basic quantum circuit with 2 qubits and 2 classical bits
  • Dabble with Qiskit, IBM’s SDK for Quantum Computing
  • Understand and see Quantum Gates in action
  • See superposition and entanglement in action
  • Execute the simulation on a local system
  • Execute the simulation on a Real IBM Quantum Computer


Install Qiskit via pip (Python3 recommended)

pip3 install Qiskit

Install Jupyter Lab if you don’t have it already.

pip3 install jupyterlab

Importing dependencies

Now fire up Jupyter Lab with jupyter lab and get started on a notebook by importing the dependencies.

Importing qiskit dependencies

Initializing the simulators

The circuits we will build have to be run using a simulator, and for our purposes, we will be using two simulators:

  • Qasm Simulator: This is Qiskit’s main simulator backend. It mimics the execution of a quantum circuit on a real quantum chip, measures and returns the results. It can be configured to generate noise based on calibration parameters to emulate the “flaws” in an actual quantum computer.
  • State Vector Simulator: This auxiliary simulator executes the quantum circuit under “ideal conditions” and returns a quantum state vector at the end. We will be using this one to visualize the states.
Initializing qiskit simulators

Initializing the quantum circuit

We start with a barebone quantum circuit with 2 qubits and 2 classical bits. To better visualize the results, think of these 2 qubits as two coins with heads and tails.

Don’t worry about the classical bits too much, they come in play only to measure the results eventually.

Initializing the quantum circuit

Notice that the qubits are all initialized to the default state of 0, which in quantum terms is represented as |0>.

Introducing the gates

To inject quantum properties like Superposition and Entanglement into these qubits, we have to use something called the quantum gate. Remember the AND, OR, NAND, NOR, XOR, NOT logic gates in classical computing? The underlying idea is the same in quantum gates.

Quantum Gate

There are about a dozen different quantum gates, but today we will only look at the two that demonstrate the fundamental concepts we talked about.

  • Hadamard (H) Gate: This gate acts on a single qubit and creates a superposition.
  • Controlled NOT (C-NOT) Gate: This gate is used to entangle and disentangle the qubit states. It accepts 2 qubits (control qubit & target qubit) and flips the target qubit if the control qubit is equal to |1>. If the control qubit is |0>, then it leaves the target qubit unchanged since it is already in |0> the state.

We will apply the H gate to q0 to create superposition, and then entangle it with q1, such that source: q0, target: q1

Applying quantum gates to the circuit

Notice the change in the vector?

Measuring the results

Since the two qubits are entangled, the expectation when we measure the results is that either both qubits should be a 0 or a 1 (comparable to only getting HH or TT in coin spins).

Measuring the results of entangled qubits

Ideally, the probabilities should have been a 50–50, but since we used the Qasm simulator to execute the circuit which emulates conditions on a real quantum computer, there is some added noise.

Running this circuit on a Real Quantum Computer!

  • Sign up for a free IBM Quantum Experience account.
  • Log in and go to My Account to retrieve the API Token to submit jobs to one of their quantum computers.
Qiskit API Token
  • Run the following command in your notebook which stores your API token locally for later use in a configuration file called qiskitrc.
Qiskit IBM Quantum Experience
  • As shown below, load the access credentials and execute the circuit on one of the available quantum computers. You can see a list of all available quantum computers on the portal — try picking one that has the lowest number of jobs in the queue.
Running a quantum circuit on a real Quantum Computer!