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

Learn Fundamental Concepts in Quantum Mechanics, Build & Execute A Quantum Circuit using Python on A Real Quantum Computer!

(EJ) Vivek Pandey
Jan 13 · 10 min read
Enter the Quantum Realm

If you’ve stumbled across this page in the vast Medium Universe, chances are — you’ve already heard some rumors about all the amazing things quantum computers can (or potentially can) do — including a future where quantum teleportation might be a reality ;).

But how do you even get started as an everyday person who isn’t a scientist in one of the research labs? Maybe I can help since I was on the same boat myself until a few months ago.

We’ll start with some fundamental concepts in quantum physics and tie them to the inner workings of a quantum computer. Then we’ll go through a hands-on Python tutorial using Qiskit SDK by IBM Research to see these concepts in action by coding, simulating, and even running a Quantum Circuit on a Real Quantum Computer!

1,000,000 Ft. View — Quantum Physics

Unlike Classical Physics, which describes the nature at an ordinary macroscopic level, Quantum Physics is the study of nature at the atomic and subatomic scales, which often abide by a whole separate set of rules — sometimes counterintuitive and baffling to our everyday understanding of nature.

Differential Equations, cause finding boards with quantum mechanics math was hard

500,000 Ft. View — Quantum Physics in Play

Using the principles of Quantum Physics, it is possible to make real physical objects, which in this case are the quantum chips powering Quantum Computers, display quantum properties like Superposition, Entanglement, & Interference (ugh, big words). We’ll go over these main quantum properties in a minute, but for now, what you need to understand is —

What makes a Quantum Computer so powerful is its ability to leverage these quantum properties to perform computations using a completely different approach and set of rules than we are used to in our classical computers.

100,000 Ft. View — The Quantum Computer

Yup! That’s the inside of a quantum computer.

Yup! That’s what the inside of a quantum computer looks like. This particular one is from IBM Research, and we’ll be running our circuit later in a similar one. Doesn’t it take you back to the MARK I days of classical computing where computers occupied a full room?

That glowing metal is actually real gold, used for its superconductivity. The quantum chips are placed at the bottom under the lowest circular plate. And this whole giant thing is later enclosed within a cryogenic freezer — known as the Dilution Refrigerator. The temperature inside the Refrigerator is as low as 1mK which is roughly about -273 C or -460 F. At such super cold conditions, the quantum chips display quantum properties that makes possible some of the mind-bending concepts we will talk about next.

10,000 Ft. View — The Quantum Properties


You’ve probably heard of the famous Schrödinger’s Cat paradox.

Schrödinger stated that if you place a cat and something that could kill the cat (a radioactive atom) in a box and sealed it, you would not know if the cat was dead or alive until you opened the box, so that until the box was opened, the cat was (in a sense) both “dead and alive”. — Wikipedia

Doesn’t that sound super counter-intuitive? This property of being in two states at the same time is essentially what superposition is.

Superposition is like a spinning coin

Now, imagine a coin with two possible states — heads or tails. When you toss it, only one side can come on top, right? Kind of like binary bits of classical computers — every bit is either a 0 or 1. But what if the coin is actually spinning, where it could be both heads and tails at the same exact time?

What if a bit could be both 0 and 1 AT THE SAME TIME? That’s superposition. Classical bits stored states as a scalar value (0 or 1) whereas Quantum bits (Qubits) are capable of storing states in the form of probabilistic wave functions (a superposition where it is both 0 and 1). And they will only collapse to scalar values when you decide to “measure” the qubits.

How does superposition exactly make quantum computers powerful, you ask?

At a single point in timeClassical Computer with n bits
1st bit — 1 state (0 or 1)
2nd bit — 1 state
nth bit — 1 state
Computer's state = 1^n = 1
Quantum Computer with n Qubits
1st bit - 2 states (Both 0 & 1)
2nd bit - 2 states
nth bit - 2 states
Computer's state = 2^n

As you can see, the ability of each qubit to be in two states at the same time exponentially boosts the capacity of quantum computers to process large computations without space and memory constraints that limit classical computers.

Tip: View superposition as a function rather than a single value.


Two entangled qubits

To explain entanglement using the coin example from above —

If you were to spin two coins next to each other in the real world, you know their outcomes are mutually exclusive of each other. But what if you could entangle them together so both represent the same wave function? That’s what entanglement is all about.

So when you entangle a qubit A with qubit B, you are essentially pairing them up so they represent the same wave function, and hence yield the same outcome if you measure them.

Tip: Think of entanglement as a bonding between the twins, where each can “sense” the other.


Probabilistic Wave + Interference = Resulting Wave

Remember I mentioned earlier — the superposition states have to eventually collapse to specific values when you decide to measure the qubits.

A fundamental idea in quantum computing is to control the probability of the measurement a system of qubits collapses into. Quantum interference allows us to bias the measurement of a qubit toward a desired state or set of states.

Just like how a noise-canceling headphone cancels out noise using destructive interference — by generating its own ambient waves to “cancel out” the noise — quantum interference allows you to bias the measurement of the qubits and counteract against errors due to the environment, sensitivity, etc.


Decoherence is the death of quantum states.

If the quantum wave functions of the two states are not coherent, neither can they maintain superposition nor entangle. Decoherence (loss of coherence) thus destroys these fundamentally quantum properties, and the states behaving like classical systems.

Decoherence is one of the biggest obstacles in achieving quantum supremacy — because of which you can only retain quantum information for so long. And the reasons for it range from purely technical to the environment and surroundings.

1,000 Ft. View — Qiskit Framework by IBM

IBM has been leading the quantum computing revolution — from allowing public access to run jobs on their quantum computers to open-sourcing Qiskit, their quantum computing software development kit.

Qiskit is comprised of four different elements

  • Terra: Composing quantum programs at the level of circuits and pulses
  • Aer: Accelerating development via simulators, emulators, and debuggers
  • Aqua: Addressing noise and errors
  • Ignis: Building algorithms and applications

For today’s tutorial, we’re only going to be using Terra and Aer.

Ground Level — The Hands-on Tutorial

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.

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!

Let’s clarify a few things

  • Quantum computers are faster only for special tasks like machine learning and modeling new medicines where you can leverage the quantum states and probabilities. For regular tasks like making word documents, or listening to music, classical computers are still the way to go.
  • Quantum computing is the death of encryption isn’t necessarily a universally correct statement. Sure, given fault-tolerant quantum computers, quantum algorithms like Shor’s Algorithm can be used to find prime factors of really large numbers — which can put encryptions like RSA (based on factorization) at risk. But we are still far away from attaining such near-perfect quantum computers equipped with accurately error-corrected qubits. Even when we get to that point, there are other encryption algorithms that operate under principles other than factorization.

Questions and Feedback?

Please let me know if you have any further questions/feedback in the comments section, or if there is any new piece of tech you want me to cover.

Thanks for the read!

EJ is one of the co-founders of fashion-tech startups Qarece & HackWears, and the creator of the open-source e-commerce framework Veniqa. He has also consulted for multiple American Fortune 500 companies wearing the hats of a technical lead, data scientist and senior full-stack engineer on projects ranging from application development to big data analytics and machine learning.

The Startup

Medium's largest active publication, followed by +606K people.

(EJ) Vivek Pandey

Written by

Creator of Veniqa. Co-Founder of Qarece & HackWears. Data Scientist, Senior Full Stack Engineer, R&B Recording Artist, Producer. (

The Startup

Medium's largest active publication, followed by +606K people. Follow to join our community.

More From Medium

More from The Startup

More from The Startup