Getting started with Qiskit: while exploring the quantum world, let’s play the coin flip game!

Hey, do you know you can program a real quantum computer and have lots of fun?

Yeah! Many thanks to IBM Q.
IBM Q is an industry first initiative to build universal quantum computers for business, engineering and science. These quantum computers are freely and publicly available for everyone to access using Qiskit. You can read more about this through this link:

Right here, I will be showing you how to build and play a coin flip game on IBM quantum computer using Qiskit.

Qiskit is an open-source framework for quantum computing. It provides tools for creating and manipulating quantum programs and running them on prototype quantum devices and simulators. It follows the circuit model for universal quantum computation, and can be used for any quantum hardware that follows this model. While the purpose of this article is not mainly about Qiskit, I will recommend reading more about it on this link and also go through some tutorials on

The primary version of Qiskit uses the Python programming language. So don’t freak out when you see me writing a python program.

Now, let’s get started.

Coin flipping, coin tossing, or heads or tails is the practice of throwing a coin in the air and checking which side is showing when it lands, in order to choose between two alternatives, sometimes used to resolve a dispute between two parties. It is a form of sortition which inherently has two possible outcomes. The party who calls the side wins.

One thing so amazing about quantum computers is their ability to be in superposition. So it’s very important to know this as we go ahead that an n qubit (In quantum computing, a qubit or quantum bit (sometimes qbit) is the basic unit of quantum information — the quantum version of the classical binary bit physically realized with a two-state device) has a 2n² states.

That is, if we have 1 qubit, we have 2 possible states which we will call 0 and 1 or tail and head in this paper.

First, let’s import necessary libraries:

Image for post
Image for post
Required libraries for our coin flip game

Creating head and tail

The fundamental unit of Qiskit is the quantum circuit. To create a simple head and tail coin, you need to 1 qubit, and 1 classical bit (not necessary) , then create a circuit to act on them. Using the hadamard gate, you can set the circuit to superposition with two possible outcome, i.e 0, 1.

Image for post
Image for post
Building coin flip circuit

Taking measurement

One thing very special about qubit is that it doesn’t have a specific outcome until you take a measurement. This act of measuring will collapse it to one of the classical states above — 0 or 1.

You can easily take a measurement using the code below:

Image for post
Image for post
Taking measurement of the possible state of the coin whether tail or head we want to know

Handling the job to IBM Quantum Computer for Simulation

To simulate this circuit, I use the qasm_simulator in Qiskit Aer. Each run of this circuit will yield either the bitstring 0 or 1. To build up statistics about the distribution of the bitstrings, we need to repeat the circuit many times. For our coin flip we will be repeating the circuit a 1000 times to get enough statistics. You can of course use any number.

As we execute the job as seen above, the quantum computer help flips the coin and we are ready to obtain our possible results. Remember that we will get different outcome for each execution, and that’s what is fun and real about the game.

Let’s play and get our result

Image for post
Image for post


Image for post
Image for post

Where 1 is the head and 0 represents the tail, the first outcome decides the head to be the winner. With 1000 events, we have 510 outcomes that point head as the winner and tail the loser with 490 points.

One more thing — Let’s visualize our outcome

Image for post
Image for post

Qiskit can easily visualize an outcome by calling the library we import above. The plot shows the probabilities of our outcome.

Playing coin flip on quantum computer for the first time? Isn’t this fun?

There are lots of fun stuff you can do on Quantum computers, I decided to write on this to get many people started with both quantum computing and Qiskit.

Quantum Computing is emerging, and now is the time to invest.

I hope you did have some fun!

Some people call me unstable force, while some say I'm quantum dot.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store