In classical programming, the first program many of us learn is ‘Hello, World!’, a simple piece of code that prints a message to the screen. It’s a rite of passage that confirms our development environment is working correctly.
In quantum computing, the equivalent ‘Hello, World!’ program is a simple circuit that demonstrates the fundamental concepts of superposition and measurement. Let’s walk through it together and see how these strange new rules of programming actually work in practice.
🧑💻 The Simplest Quantum Program
The simplest quantum program involves a single qubit. We start by initializing the qubit to a known state, typically the basis state $|0\rangle$. We then send this qubit into a meter, a device that measures its state. When a meter receives a qubit in the definite state of $|0\rangle$, it will always report a classical bit with the value of 0. This is the baseline.
We then make it a little more interesting by adding a quantum gate, or qugate, before the measurement. For example, inserting an X gate (the quantum NOT gate) flips the qubit from $|0\rangle$ to $|1\rangle$. Now, when the meter measures the qubit, it will predictably output a 1.
✨ The Magical Hadamard Gate
The real magic happens when we replace the X gate with a Hadamard (H) gate. As we know, the H gate takes a qubit in the state $|0\rangle$ and puts it into an equal superposition of both $|0\rangle$ and $|1\rangle$. In this state, a measurement is no longer predictable.
The qubit now has a 50% probability of collapsing to $|0\rangle$ and a 50% probability of collapsing to $|1\rangle$. When we run this program, we can’t say for certain if we will get a 0 or a 1. However, if we run it many times (for thousands of ‘shots’), we would expect to see roughly equal numbers of 0s and 1s. This is the quantum equivalent of flipping a coin—a true quantum coin flipper!
🧠 Putting It All Together: Interference in Action
To demonstrate the power of interference, let’s take our quantum coin flipper one step further by adding a second H gate before the measurement. This means our qubit goes from $|0\rangle$ to the superposition $|+\rangle$ after the first gate, and then through a second H gate.
This second application of the H gate causes the amplitudes of the superposition to interfere with each other. The amplitudes for the $|1\rangle$ state cancel each other out in a process called destructive interference, while the amplitudes for the $|0\rangle$ state constructively add up. The result is that the qubit returns to its original state of $|0\rangle$. Despite traveling through two quantum gates, the qubit emerges in its initial state. This ability to precisely control how a superposition behaves is what allows us to design complex algorithms that produce the results we want.
—
Glassner, Andrew. Quantum Computing: From Concepts to Code. No Starch Press, 2025.
More Topics
- The Quantum Threat: How Shor’s Algorithm Puts Modern Encryption at Risk
- Cracking the Code: How Quantum Parallelism Solves Deutsch’s Problem in One Step
- The Deutsch-Jozsa Algorithm: An Exponential Leap in Quantum Problem Solving
- The Bernstein-Vazirani Algorithm: Unmasking a Secret With a Single Query
- Simon’s Algorithm: The First Quantum Speedup That Left Classical Computers in the Dust
- Scaling Up: How Quantum Computers Handle Multiple Qubits
- The ‘Spooky Action’: What Happens When Qubits Entangle?