• Sai Nandan Morapakula

How to flip a coin using a Quantum Computer?

What if I say that you are reading this and not reading this at the same time?

You are breathing and not breathing at the same time?

You are seeing and not seeing at the same time?

(until someone measures you)*


When we go to the microscopic level of the universe, things won't be the same as the macroscopic level!


Let us take a look at some situations you might have faced while watching a TV -

who should bat first or who should bowl first in a cricket match, who should take whites and who should go with blacks and there are many more examples with tossing a coin in our day to day life. In this article, I’ll try to give some good understanding to you on some of the following topics


  1. What is Quantum Computing?

  2. Why should we use quantum computers over advanced classical computers?

  3. Why should we toss a coin using a quantum computer when this task can also be done using a classical computer and how do we do this?

  4. Do we need to replace classical computers with classical-quantum computers?


What is Quantum Computing?

Quantum Computing is something we have been hearing more and more about in the last few years. Quantum Computers are wizards, they can perform magics and wonders where we fail to understand what's happening with our naked eye. I would say classical computers are not different from quantum computers but they are opposite.


Classical computers represent data with 0’s or 1’s (bits), for example number 2 is represented as 10 in a classical computer(binary). Whereas in quantum computers data can be represented with a certain probability of 0 and a certain probability of 1, for example - 50% 0 and 50% 1(qubits or quantum bits).



This picture is worth a thousand words!

Why should we use quantum computers over advanced classical computers?


As we saw above, when compared to classical computers we can represent more complex data using quantum computers. Quantum computers can take a deep dive in every field for instance Machine Learning, Cryptography, Finance, Security, Drug Discovery, Chemistry, and can also help us in Climate Change, where classical computers fail to take apart. And quantum computers use the basic principles of our nature to work, they use the most fundamental principles such as superposition, entanglement and quantum annealing. In our high schools we were taught that the ground state energy of a hydrogen atom is -13.6eV, but did we ever see the practical results with our naked eye? Did we ever try to do that?


You may wonder, but yes quantum computers help us in achieving this practice and it didn’t put its limits to a simple hydrogen atom, we can also see the ground state energy of lithium hydride and some small molecules, and I am pretty sure in the future we may discover new elements or may find some new drugs or can break an RSA encryption with advanced quantum computers!


It’s so exciting and fascinating to build a future where things work according to the rules and laws of nature. Thanks to Feynman for letting us know this, David Deutsch, Shor and many scientists and enthusiasts who are working hard to achieve “Quantum Supremacy”. And thanks to my parents who brought me into this era where quantum computing is growing exponentially day by day, if they didn’t I might be someone in the future who just uses a quantum computer but I would never know how they originated. And I guess now you should thank your parents too.


Take a deep breath, thank them and search on.


Tossing a coin using a quantum computer by IBM (QISKIT)


What if we toss a coin using a quantum computer? You might be wondering why we should use a quantum computer when we can do this using a classical computer. If you are one of them, congratulate yourself, you are on the track. What if the task is to know the probability of the coin landing on heads or to know the probability of the coin landing on tails? One cannot simply toss a coin twice and decide the solution to it right? So then one needs to toss a coin nearly in large amounts may be nearly 1000 or 2000 times, this is a waste of time and energy for a human to simply sit and toss a coin to see the probability. And here the quantum computers take the stage.


In this section let us see how to build a circuit that is equivalent to tossing a coin and see the results. Before we build the circuit equivalent to tossing a coin, let’s have some basic intuition on the gates that we use in the circuit. Get ready to dive into quantum computing.

H-GATE:


H gate is a single qubit gate and it is called Hadamard gate, don't worry about the pronunciation, we can simply call it H gate.


Source: https://qiskit.org/textbook/ch-states/single-qubit-gates.html#hgate

The one that is in the blue is a Hadamard gate and the one that is in the black is a measurement operator.


The total number of states is given by the formula 2^n (2 to the power n) where n is the number of qubits. Let us think we have a circuit with a single qubit, so we’ll have 2 states as per the above formula (2^1) i.e (|00> |01> ), H gate takes the two states into superposition, a state which contains |00> and |01> at the same time and when we measure the qubit it falls into a single state i.e., either |00> and |01>


I guess by now one should get a basic idea behind this gate or why we are using it here, if you got it, go get a sweet and congratulate yourself. If you didn’t, don't worry here we go!


Think of tossing a coin, you tossed a coin into the air and it's in the air still. Can you tell the face of that coin when it is in the air? Heads or Tails?


If you ask me, I would simply say I cannot predict, not only me, no one can, until and unless he/she has some supernatural powers. The takeaway here is we cannot predict the state of the coin when it is in the air (spinning), we call this a superposition in the language of quantum. When you look at the H gate even it is doing the same, it spins the two states (|00> |01>) into the air and takes them into a state of superposition. We can only know the state of the coin when it falls on the ground, it may be tails or heads. The same applies in quantum computing, when the H gate takes the states into superposition, we can only know in which state the qubit is when we measure it. We measure it and store the output into a classical bit.


The one that is in the blue is a Hadamard gate and the one that is in the black is a measurement operator.


CNOT GATE:

CNOT gate is a two-qubit gate. It can be said that the CNOT gate is a conditional gate i.e., it flips the second qubit (target) if the first qubit (control) is 1.

CNOT Gate
t - target bit | c - control bit

The above two pictures give us the basic intuition of the CNOT gate. The takeaway here is when control is zero(0), there will be no effect on the target qubit but when the control is one, the target qubit flips (if 0 it becomes 1 and if 1 it becomes 0). If you don’t get it, simply look at the table given above.


Implementing using qiskit:

Now let us see how we can toss a coin using a quantum computer by IBM using the QISKIT - an open-source framework that uses python mostly.


pip install qiskit it - If you want to install the qiskit library

Now let us import all the required components from the qiskit library.

Let us build the circuit now, for tossing a coin we need a single qubit, so let us take a single qubit and apply a Hadamard gate on qubit 1, which takes our states into superposition(0 and 1 in superposition). Now we measure the circuit into classical bits by which if we get 0 we code it as heads and if we get 1 we code it as tails.

We need to run the circuit we built above on a simulator we like, for time being let’s go with ‘qasm simulator. If you want to know more about 'qasm simulator' you can always head to the IBM qiskit textbook where you get to know every detail about the qiskit library. Here’s the link: Learn Quantum Computation using Qiskit

Let’s get back to the question where we asked ourselves why we should use quantum computers when there are classical computers that can do the same job, for which we were counter backed with the question ‘What if we need to toss a coin 1000 or 2000 times?’ where classical computers take a step back and quantum computers take the lead. In the above snippet we gave shots = 1; by which you ordered the quantum computer to run the circuit only once. If you want to toss a coin 1000 times you can do it by simply taking shots = 1000. See how easy it is for a quantum computer when it is complicated for a classical computer. After the measurement, superposition falls into either 0 or 1, we store that data into counts.


Using python we define that if we get the output of counts as 0 we name it as heads and if we get the output of counts as 1 we name it as tails.

So if the counts is 0 we get the output as ‘congo! Its Heads’ and if the counts is 1, we get the output as ‘congo! Its Tails’.


We had built a circuit that is equivalent to tossing a coin and saw the results using a quantum computer. Now we also know why we should use quantum computers when there are classical computers that can do the same work.


Here is the Github repository link to the above: flipping a coin using QC. Go check it out and cheers if you are excited to toss a coin using your code or at least try to do it. And let me know whether you are going to bat first or bowl first. 😉


Do we need to replace classical computers with quantum computers?

To be precise we are not going to replace classical computers with quantum computers, at least not soon. Both of them have their advantages and disadvantages, one cannot use a quantum computer at room temperature and it's very very costly. Whereas classical computers are user friendly, one can carry them wherever they want to and they are available at low prices when compared to quantum computers. So don’t fall for them when they say ‘Quantum computers are going to replace classical computers.

Image Source


You are always welcome to ask anything related to this article. Please feel free to mail to this Mail ID. See you on the other side. Happy learning and SEARCH ON!

Data OilSt.