‘Hello, World!’ has been the most iconic computer program. There is a good chance that you, our reader, also began your journey in the world of programming by printing ‘Hello, World!’ on your screen. In this article, we aim to develop a quantum version of the ‘Hello, World!’ program that we call ‘Aloha, Quantum World!’. Before we do that, let us discuss the significance of the traditional “Hello, World!” program.
The Hello world program was developed by Brian Kernighan, in 1974. Its purpose was to be
- the simplest possible program for the language
- a demonstration of the syntax for a particular language
- and to check everything is working correctly. Remember these were the days when people were still figuring out how to connect a display device to a computer. So, the program also checked if the program compiled correctly and if the connection to the input and output devices (such as a monitor) is not erring.
Following the reasoning behind the classical ‘Hello, World!’, let us discuss what we would want to accomplish with quantum ‘Hello, World!’
- Similar to ‘Hello, World!’ we would want it to be the simplest quantum program. And, our version of quantum ‘Hello, World!’ is called ‘Aloha, Quantum World!’.
- ‘Aloha, Quantum World!’ should go through the quantum software and hardware layer to ensure the faithfulness of the stack.
- Then, we want to go one step further. If you look into the details of ‘Aloha, Quantum World!’, it should present to you the details for how quantum computing is fundamentally different than classical computing.
Now, let us present to you the ‘Aloha, Quantum World!’ program. The function is called using the following piece of code:
Just like the classical ‘Hello, World!’ program, it is a simple program. If it is called without input, it always outputs ‘Hello, World!’. But, if ‘quantum’ is passed as input then, it sometimes outputs the ‘Hello, World!’ and other times it outputs ‘Bye, World!’. This is the reason why we thought naming this article ‘Aloha, Quantum World!’ was apt. If ran enough number of times, one will observe that the program in-fact prints ‘Hello, World!’ and ‘Bye, World!’ 50% of the time. You can play with the code at qBraid.com
Now, you may ponder as to what may be going on behind the scenes in the program. Since the program seems to be printing ‘Hello, World!’ and ‘Bye, World!’ with 50% probability, your initial guess maybe that it is similar to a coin flip, And yes, it is similar but not quite! First, note that
- in the macroscopic world for which we have built our intuition, probability arises out of incomplete knowledge of a given system. e.g. in the case of a coin flip, if all the parameters for the coin flip are known, like the force with which it was flipped, the exact force of gravity, friction due to air, etc. then theoretically it will be possible to predict whether it will turn up heads or tails. But, because you don’t know these parameters, the best you can do is predict with some probabilities. Again, I want you to appreciate that in the classical world probabilities arise out of not knowing.
- Another important detail to note is that when the coin lands on the ground, it lands on either heads or tails. Let us say that you toss a coin in a dark room where you can’t see whether the heads or tails have turned up. Even before you turn on the light to look, you can be sure that it is either heads or tails, it cannot be both at the same time.
aloha_quantum_world('quantum') that we ran above is non-compliant with the two points that we just mentioned. The program uses a qubit or quantum bit in a ‘superposition’ of being ‘0’ and ‘1’.
Qubit is a quantum analog of a bit. A qubit could behave like a bit but also allows for more exotic behavior.
Quantum Superposition, in very simple terms, is the property of quantum systems which allow them to be in two distinct states at the same time. When measured, the qubit returns, either 0 or 1.
Corresponding to the two points we mentioned for a coin, note the following points for a qubit in superposition:
- First, even with complete knowledge of the state of a qubit, knowing everything that there is to be known for a state of a qubit, we will still not be able to predict the outcome. In fact, the universe itself does not ‘know’ whether the qubit will return 0 or 1 when measured. The probability in quantum systems does not arise from ‘not knowing’ but is actually built into the fabric of nature.
- Second, the qubit in the state of equal superposition of ‘0’ and ‘1’, is not like a coin lying on the ground in a dark room where it is one of those but we just don’t know, but it is both ‘0’ and ‘1’ at the same time!!
It is understandable if you are shaking your head in disbelief and asking yourself, ‘How could this be?’. You are not alone in asking this question and the answer is: ‘It is just the way nature is, and quantum mechanics is a theory based on observations.’. To learn more about quantum computation, we invite you to take our QuBes (quantum beginners) course on qBraid.com. We introduce all the background material and teach you quantum computing. The background material will cover the experiments that convinced physicist that,
- nature is probabilistic, and
- a qubit in superposition is both ‘0’ and ‘1’ at the same time.
You will also be able to run the above program multiple times yourself to see that it indeed returns ‘0’ and ‘1’ each 50% of the time. Further, the superposition of a qubit could be leveraged to make quantum algorithms faster than their classical counterparts. One such algorithm covered in QuBes course is Deutsch’s algorithm. You will also be able to run the algorithm on IBM’s actual quantum computer to see it in action!
In-depth discussion of ‘Aloha, Quantum World!’
Now, we present the details for the inner workings of the
aloha_quantum_world('quantum') program. Before that, let us present a different version of the classical 'Hello, World!' program. This would help us better understand the quantum program by bringing out the contrast.
The following python program is a variant of ‘Hello, World!’. Our aim here is to associate the strings with the values of a bit (done using the type bool in python), which is the fundamental unit of information in classical computing. Here, the string ‘Hello, World!’ is associated with the state ‘0’ of a bool variable, and ‘Bye World’ is associated with the state ‘1’. The program takes as an input a bit value and outputs the corresponding string.
It is easy to see that the bit can only have two values, 0 and 1. So, the function could run with only two inputs, as demonstrated above.
Note 1: we wrote the program that takes an input bit and returns a string corresponding to the bit value.
Now, we ask, what are the possible operations that can be performed on a bit? And it can be seen that the only non-trivial operation possible on a bit is the bit-flip, where one changes the value of the bit, 0→1 and 0→1. We can write the code for a function that implements the bit flip operation:
We can use this bit-flip function in conjunction with our hello_bye_world() function, to turn a hello world into a bye world.
Note 2: The only possible operation on a bit is a bit-flip operation.
The program so far is deterministic (not random). We ask, how can we make a probabilistic ‘Hello, World!’ program. For that, we write a coin flip program which returns each ‘0’ and ‘1’, 50% of the time.
With this program we can write a small program for printing ‘Hello, World!’ and ‘Bye, World!’ randomly.
It can be checked by running the program multiple times that the outputs are random.
Note 3: The randomness in the numbers generated by the NumPy package arises out of some incomplete knowledge of the processes in the CPU of your computer.
Now, we finally move on to the aloha_quantum_world(‘quantum’) program. Following is the code for the
The above program uses a ‘qubit’ or a quantum bit for a two-state system. A qubit just like a bit can be in a ‘0’ or ‘1’ state. If the aloha_quantum_world() is called without an argument, it initializes the qubit in state ‘0’ and measures it. The measurement returns ‘0’ and prints ‘Hello, World!’
aloha_quantum_world() can also take an argument for applying an X-gate which is similar to the NOT-gate in classical computing. And it can be seen that the application of
aloha_quantum_world('classical', True) prints 'Bye, World!'
So, we establish that the qubits can behave like bits and return the greeting corresponding to the value (Note 1).
Recall that there was only one single-bit possible in classical computing (Note 2). In contrast, quantum computing allows for an infinite number of single-qubit gates and states of a qubit which are a superposition of ‘0’ and ‘1’.
Let’s run the aloha_quantum_world(‘quantum’), 5 times.
If we analyze the code for the aloha_quantum_world(), we see that the H-gate is applied to the qubit if the first argument ‘quantum’ is passed. With H-gate the qubit is put in a state of superposition and prints ‘Hello, World!’ and ‘Bye, World!’ with 50% probability. You may ask that how is the behavior of a qubit in a superposition different than a ‘bit’ whose state is determined by a coin lying on the ground after a flip in a dark room?
It will become clear that these two cases are different from the following experiment. If rather than determining the state of the bit with a single coin flip, we were to determine it using two coin flips, then it can be seen that the state of the bit will remain random. This is because the two coin flips are independent, the outcome of the first does not affect the outcome of the second coin flip. This implies that the probability of the state of the bit to be ‘0’ or ‘1’ still 50%. In contrast, if we were to start with a qubit in state 0 and apply an H-gate and then apply another H-gate, then the state of the qubit will be ‘0’ with 100% probability. As we saw that above that applying a single H-gate behaves like a coin flip, but surprisingly, two consecutive H-gates cancel each other. This gives you a flavor for why the quantum world is different, interesting, and maybe a little bit bizarre. To learn more about quantum computing, you can go to qBraid.com and take the QuBes course.