I created a quantum mini-game!

How I explored the quantum universe with the help of GPT-4.

Philippe Nadeau
8 min readJun 3, 2023

I’ve always been fascinated by the concept of Schrödinger’s cat, that unlikely idea that a cat could be both dead and alive until we look at it. Not having a quantum feline at hand, I decided to venture into the mysterious world of quantum computing and do my own experiments on the subject. This is how “Quantum Coin Flip” was born, a mini-game generated on one of IBM’s quantum computers.

And now, you’re probably thinking, ‘Oh my God! I know used a quantum supercomputer, a calculating beast that could break Bitcoin encryption before you finished reading this sentence, or solve equations so complicated they would make Einstein’s mustache spin, a machine that needs to be cooled to a temperature colder than your mother-in-law’s forced smile at Christmas, that consumes more energy than a U2 concert with Bono on amphetamines, and that emits more CO2 than a SpaceX rocket during a miscontrolled fireworks display… All for… what? To flip a virtual coin?’ Well, yes, exactly. Heads or tails. Don’t even try to understand, it’s just quantum art in all its splendor!

My journey into the land of the weird

Intrigued by the magic of the quantum world, where everything can be two things at once, I decided to try an experiment with a real quantum computer, one that costs several million dollars, like the one soon to be installed in Sherbrooke’s quantum innovation zone! The idea? Create a game where a virtual coin would be both heads and tails… until we look at it.

Each side of the coin is a door to infinity

Imagine you have a keychain with billions of keys and only one opens the door to a hidden treasure, or to a trapped love, depending on what motivates you most. With a classical computer, it’s as if you had to try each key one by one, laboriously turning it in the lock, until you could find the right one. But with a quantum computer, it’s as if you could insert all the keys into the lock simultaneously, turning and instantly finding the one that clicks the lock and opens the door. It’s a revolution worthy of a science fiction novel that will transform our way of processing information, solving unfathomable mysteries, and much more. Quantum computing is the future, and it’s more than epic, it’s pure magic, don’t you think?

Let me introduce our superhero: the qubit!

Before we dive headfirst into our game, we need to explain a few basic quantum concepts. Don’t worry, I promise you won’t need an migrain pill!

Qubits: Imagine qubits as the Schrödinger’s cat of the digital world. They are the little prodigies of quantum computing. Their superpower? They can be 0 and 1 at the same time, or happy hour and time to go to work simultaneously. You might say, there’s no need for quantum to reconcile the two… Anyway!

Superposition: This is the state where a qubit does its balancing act, remaining both 0 and 1. It’s as if you had a coin that, defying all logic, is both heads and tails until you look at it. It’s the kind of thing that would plunge David Copperfield into the utmost perplexity!

Measurement: This is where things get really interesting. Measurement is like playing a game of hide-and-seek with a qubit. When you “look” at it, this little cunning qubit chooses to become either 0 or 1. And once it’s made its choice, there’s no turning back. A bit like when you decide to eat the last cookie in the box!

Unitary Operations: Unitary operations are the tools you use to manipulate qubits. You can imagine them as musical instruments, and each time you play one, you change the state of your qubit. By assembling different operations, you can create a complex quantum melody. This is what I call the “Cacofonix method”.

Measurement Error: Measurement error is an important concept in quantum computing. Qubits are delicate particles, and it can be difficult to measure them without changing their state. It’s a bit like trying to take a picture of a shooting star while driving a Ferrari: not easy to get the perfect shot, and dangerous…

Error Decoding: Would you believe that even in our sophisticated quantum world, which seems to come straight out of a Star Trek episode, errors are inevitable? Incredible, isn’t it? It’s as if we’re aboard the Enterprise and Spock has just detected a flaw in the system. But don’t worry! Techniques exist to decipher these errors and get the ship back on the right course. Imagine a kind of magic eraser capable of wiping out our measurement errors, Spock would have certainly loved to have such a gadget, and the Star Trek scriptwriters too!

Entanglement: It’s a phenomenon where two qubits become inextricably linked, the state of one instantly influencing the state of the other, no matter the distance between them. It’s as if, by watching the same Netflix series, you and your best friend could laugh, cry or shudder at the same time, even if you’re miles apart. That’s quantum magic!

Quantum Circuit: It’s a sequence of quantum operations, or “gates”, applied to a set of qubits. These gates can manipulate qubits in different ways, allowing them to interact with each other or change their state. Quantum circuits are at the heart of programming quantum computers.

Hadamard Gate: This mysterious name is a quantum operation that allows a qubit to enter a state of superposition. In simple terms, if you have a qubit in a well-defined state (either 0 or 1), after passing through the Hadamard gate, it finds itself in both state 0 and 1, a bit like if your coin was both heads and tails.

Obvious, right?

Creating a game with IBM Quantum Lab and a little help from GPT-4

To realize this game idea, I had to put on my new quantum tinking cap and connected to the online IBM Quantum Lab, akin to Ali Baba’s cave for anyone interested in quantum computing. Who was to accompany me on this adventure? GPT-4 of course.

AI + quantum, a very promising cocktail!

At first, GPT-4 raised a digital eyebrow, ‘Quantum is not for playing,’ it murmured. But after a little persuasion, and the promise that I wouldn’t attempt to decipher the secrets of the universe before 5 p.m., it became cooperative.

Where I expected a Mount Everest of programming, I discovered a rather pleasant expirence. It reminded me of my early days of programing in Basic on my Oric Atmos, where each line of code that worked was a small victory, a wonderful discovery of what the machine could do. That was in the 80s, a time when quantum was just an unknown star in the sky of home computing. And today, here I am transcribing lines of quantum code with almost the same excitement as in the 80s.

But without further ado, here’s how I proceeded to code the game “Quantum Coin Flip”:

  1. I connected to IBM Quantum Experience and went to IBM Quantum Lab (it’s very simple, and free).
  2. I created a new notebook.
  3. In a new cell, I inserted this code, developed by GPT-4:
from qiskit import QuantumCircuit, execute, Aer

# Create a quantum circuit with 1 qubit
qc = QuantumCircuit(1, 1)

# Apply the Hadamard gate
qc.h(0)

# Measure the qubit
qc.measure(0, 0)

# Execute the circuit on the quantum simulator
simulator = Aer.get_backend('qasm_simulator')
job = execute(qc, simulator, shots=1)
result = job.result()

# Get the results
counts = result.get_counts(qc)
print("The result is: ", counts)

4. Finally, I executed the cell and discovered the result of my quantum coin toss!

The result could have been either {‘0’: 1} or {‘1’: 1}, representing “Heads” or “Tails” respectively.

The result was {‘0’: 1}. So, Heads won!

To run this program on an actual quantum computer, you need to log on to IBM Quantum Experience and get an API key. Then, you can use the API key to connect to the quantum computer via Qiskit from your computer’s terminal. Here’s how you can do that:

  1. Open a text editor or Integrated Development Environment (IDE) and paste this Python code into a new file:
from qiskit import QuantumCircuit, execute, IBMQ

# Loading IBM Q accounts
IBMQ.load_account()

# Creating a quantum circuit with 1 qubit
qc = QuantumCircuit(1, 1)

# Applying the Hadamard gate
qc.h(0)

# Measuring the qubit
qc.measure(0, 0)

# Selecting the provider and choosing a quantum computer
provider = IBMQ.get_provider(hub='ibm-q')
quantum_computer = provider.get_backend('ibmq_16_melbourne')

# Executing the circuit on the quantum computer
job = execute(qc, backend=quantum_computer, shots=1)
result = job.result()

# Getting the results
counts = result.get_counts(qc)
print("The result is: ", counts)

2. Save the file with a .py extension, for example “quantum_flip.py”.
3. Open your terminal or command line.
4. Navigate to the directory where you saved “quantum_flip.py”.
5. Run the command python quantum_flip.py.
6. You should get a result, Heads or Tails, just like with the simulator.

Before doing this, make sure you have Python and all the necessary packages (in this case, Qiskit) installed on your machine. If not, you’ll need to install them first.

pip install qiskit

Qiskit is an open-source Python library created by IBM for the development and execution of programs on their quantum computers available via the cloud. It is used to create, manipulate, and optimize quantum circuits and to run experiments with these circuits on quantum computers and simulators. Remember that running programs on real quantum computers can take time due to queues.

Conclusion: The start of an adventure that defies reality

Walking into this quantum universe to create this mini-game was like embarking on an adventure into the land of the white rabbit, as simple as it might seem. I discovered a scientific universe where the angel of the bizarre is king, and where cats, defying all logic, we can be both dead and alive without being extras in a zombie movie. It’s a bit like Alice in Wonderland, but with more calculations and less tea!

Thanks to my trusty sidekick GPT-4, I was able to do this little experiment in a few lines of code and navigate the deep waters of quantum computing. And what an adventure it has been!

Now that you’ve seen how I created Quantum Coin Flip, why not give it a try yourself? Quantum computing might seem intimidating, but with a bit of curiosity and an artificial intelligence as an accomplice, you’ll discover a world of endless exploration.

So, heads or tails?

Good luck and have fun going down the rabbit hole of quantum computing!

--

--

Philippe Nadeau

Directeur général du DigiHub, un pôle en convergence technologique déployé à Shawinigan | e/acc