Saturday 21 August 2021

Using a Quantum Computer to Roll a Dice

There has been some buzz about Quantum Computers (QC) over the last years. The QC's that are in use today are very limited but the capacity is growing rapidly. QC advocates hope to see computers that can solve problems much faster than classical computers can do. Other fear that some of the current cryptography systems may be broken with Quantum algorithms, such as Shoor's algorithm.

In this post, I will show how easy it is for a random blogger to create a simple Quantum algorithm and run on a Quantum computer.

Really Cool.
Coolness level: 15 mK ( -273.13 degrees Celsius)


Why a Dice?

There are tons of electronical dices out there. But they are made for classical computers and classical computers are built to be predictable. So if you know the random seed and the algorithm for the random generator, it is possible to predict the outcome of a dice. 

For a physical dice, it is also possible (in theory) to predict the outcome, given the rotation, position and thetranslational movement of the dice, combined with the surface and athmospheric conditions of the environment. 

For Quantum Computers, the outcome is by it's very nature random. The randomness comes from a superpositon of two equal states that collapses into one state.

Step 0: Foundations

I strongly recommend taking a university level course in quantum mechanics to get familiar with the basic concepts. 

I enjoyed reading Jack Hidary's book Quantum Computing: An Applied Approach



Anastasia Marchenkova has an interesting Youtube channel where she covers up-to date material about Quantum Computing.

If you're really short on time, you can see Wired's five levels of difficulty introduction to Quantum Computing


Step 1: Accessing IBM Quantum (Web)

The second step is to create an account on IBM Quantum. IBM Quantum allows the general public to upload programs to QC's and run for free.

Once I've got an account, I can create programs to run, either on a simulated or physical Quantum computer. There is an online Development Editor that is useful for learning. 


Step 2: Create and Run a Simple Program:
It is quite easy to add elements to the program - click and drag elements to the proper places.


In the program above, I use two Qubits. One is set to a superposition of |0> and |1>. The other Qubit is entangled with the first Qubit. Finally, both Qubits are measured, one by one.

To run the program on a QC, I select "Setup and Run", where I can select the QC or simulator to use
After the completion, I explore the result:

Theory says that both Qubits shall have the same value. This happens most of the times, but sometimes the Quantum Computer fails. This means that there should be some caution when looking into the results of these kinds of QC's.


Step 3: Creating the Dice from Python

I prefer to write the program in Python and it is easy to do it using the API key

The Python program is using qiskit (one of the more popular syntaxes for Quantum Computing).


The program sets up three Qubits that are set into a superposition of |0> and |1> (50% likelihood each). It is measuring the three qbits individually and the result is saved to a binary bitmask of size 3 ("000" -> "111"). To map the bitmask to a six sided dice, I repeat the circuit until I get "001"/1 up to "110"/6. This is the output from the dice.
The red text is a hardcoded warning that I 
wasn't able to supress. 
The dice repeated once, but the final value was 1.

That's it. There are no excuses. YOU can program a quantum computer and YOU can run the program on a physical quantum computer.