Interactive~20 minBeginner

Quantum computing, code-first

Quantum computing doesn't have to start with physics equations. This lesson teaches qubits, gates, superposition, and entanglement through code you can run and tweak — the linear algebra comes later, as the "math underneath." Inspired by the programming-first pedagogy developed at UW–Madison.

Why quantum computing matters

Classical computers process information as bits — each one definitively 0 or 1. For most tasks, that's plenty. But some problems grow exponentially: factoring large numbers, simulating molecules, or searching unstructured databases. Double the input, and a classical computer needs exponentially more time.

Quantum computers exploit two phenomena — superposition (a qubit can be 0 and 1 simultaneously) and entanglement (qubits can be correlated in ways classical bits can't) — to explore many solutions at once. For the right problems, this turns centuries of computation into minutes.

The code-first approach

Traditionally, quantum computing is taught through physics and linear algebra. This lesson flips that: every concept starts as code you can run. The math appears as an optional "matrix view" tab — useful, but never a prerequisite.

Unstructured search: classical vs quantum

10

Classical

1.0K steps

210

Quantum (Grover)

32 steps

√210

Classical0%
Quantum0%

Grover's algorithm finds a needle in a haystack of 2n items using only ~√2n queries. At n=20 that's 1,048,576 classical steps vs just 1,024 quantum steps — a 1000× speedup.

Quantum advantage is not universal. Problems that benefit tend to have mathematical structure that quantum algorithms can exploit: periodicity (Shor's algorithm for factoring), symmetry (quantum simulation of molecules), or search with oracle access (Grover's algorithm).

Problems without this structure — like sorting a list or running a web server — see no quantum speedup. Your laptop will always be better at browsing the web than a quantum computer.

The most promising near-term applications are in chemistry (drug discovery, materials science), optimization (logistics, finance), and cryptography (breaking RSA, building quantum-safe protocols).

?

Quick check

A quantum computer is always faster than a classical computer. True or false?

A qubit in code

Forget Dirac notation for now. A qubit is just a variable that can be 0, 1, or — and here's the quantum part — a blend of both. Operations are method calls. Measurement collapses the blend to a definite value, with probabilities determined by the blend.

Step through the code below line by line. Watch the Bloch sphere (the circular diagram) rotate as each operation transforms the qubit's state. Try all four presets to build intuition.

Equal superposition — 50/50 chance of 0 or 1

1q = Qubit()# starts as |0⟩
2q.hadamard()# superposition: (|0⟩+|1⟩)/√2
3measure(q)# collapses to 0 or 1

Qubit state

|0〉|1〉
P(0)=100%P(1)=0%

Probabilities

|0⟩
100.0%
|1⟩
0

A physical qubit is a two-level quantum system — an electron's spin (up/down), a photon's polarization (horizontal/vertical), or a superconducting circuit's energy levels (ground/excited). "Superposition" means the system exists in a weighted combination of both levels simultaneously.

Mathematically, the state is a vector in a 2D complex space: α|0⟩ + β|1⟩ where |α|² + |β|² = 1. Measurement collapses to |0⟩ with probability |α|² and |1⟩ with probability |β|². That's the linear algebra that the code view hides.

The Bloch sphere is a way to visualize this: the north pole is |0⟩, south pole is |1⟩, and the equator represents equal superpositions with different phases.

?

Quick check

After applying a Hadamard gate to a qubit initially in state |0⟩, what's the probability of measuring 1?

Gates as functions

In classical computing, logic gates (AND, OR, NOT) transform bits. Quantum gates transform qubits. The key difference: quantum gates are reversible — every gate has an inverse that undoes its effect. (This is required by quantum physics.)

Click each gate below to see it as code (the programming view) or as a matrix (the math underneath). Then try the preset circuits to see how gates compose.

H Gate

Creates equal superposition. Turns |0⟩ into (|0⟩+|1⟩)/√2

q.hadamard()

Try a circuit

q0:\u2500\u2500[H]\u2500\u2500 \u2192

H gate → superposition

Output state

|0⟩
50.0%
|1⟩
50.0%

1000 measurements

|0⟩
501
|1⟩
499

Quantum mechanics is governed by unitary evolution — the total probability must always sum to 1, and information is never truly destroyed (until measurement). A unitary matrix U satisfies U†U = I, which means every gate has an inverse: U†.

Classical gates like AND are irreversible: AND(1,1)=1 and AND(1,0)=0, but given output 0 you can't tell which inputs produced it. In quantum computing, you can always reverse the computation to recover the inputs. This constraint seems limiting but actually leads to richer computational models (like quantum error correction).

Fun fact: some gates are their own inverse. The Hadamard gate H satisfies H·H = I — applying it twice returns you to the original state. You can verify this in the "Double Hadamard" preset above.

?

Quick check

If you apply the Hadamard gate twice (H then H again), what happens to a qubit in state |0⟩?

Entanglement: two qubits, one fate

Entanglement is the feature that makes quantum computing genuinely weird — and genuinely powerful. When two qubits are entangled, measuring one instantly determines the other, regardless of distance. No classical system can replicate this.

Step through the five stages below. At step 4, click "Measure q0" repeatedly — you'll see that q0 and q1 always match. Then at step 5, try to beat quantum with a classical strategy.

Two qubits, starting at zero

We start with two independent qubits, both in state |0⟩. The combined system is in state |00⟩ with 100% probability. Think of two coins both showing heads.

q0 = Qubit() # |0⟩ q1 = Qubit() # |0⟩
q0q1Independent qubits

Combined state

|00⟩
100.0%
|01⟩
0
|10⟩
0
|11⟩
0
?

Quick check

After creating a Bell pair (H on q0, then CNOT) and measuring q0 as 1, what will q1 be?

No, and this is one of the most common misconceptions. When Alice measures her qubit, she gets a random result (0 or 1 with equal probability). From her perspective alone, the outcome looks completely random — nothing changed from what she'd see with an unentangled qubit.

The "magic" is only visible when Alice and Bob compare results later (through a classical channel, which is limited to the speed of light). They discover their results are perfectly correlated, but neither could have predicted or controlled which outcome they'd get.

This is formalized as the "no-communication theorem": quantum entanglement cannot transmit classical information. It's a correlation, not a communication channel.

Your first quantum algorithm: Deutsch's oracle

Time to see why all this matters. Deutsch's algorithm is the simplest problem where a quantum computer provably beats a classical one. The problem: you have a mystery function f(x) that takes a single bit. Is f constant (same output for 0 and 1) or balanced (different outputs)? Classically, you need to call f twice. Quantumly: just once.

🌀

Superposition

Query the function with both inputs at once — |0⟩ and |1⟩ simultaneously.

🌊

Interference

Amplitudes cancel or reinforce, concentrating probability on the answer.

🔄

Phase kickback

The oracle marks the answer in phase, not amplitude — invisible until interference reveals it.

📏

Measurement

A single measurement extracts the global property (constant vs balanced) directly.

The Deutsch circuit (balanced oracle: f(x) = x)

# Setup
q0 = Qubit()         # input qubit
q1 = Qubit()         # output qubit
q1.flip()            # put q1 in |1⟩
# Superposition on both
q0.hadamard()
q1.hadamard()
# Oracle: CNOT implements f(x) = x
cnot(control=q0, target=q1)
# Interference on input qubit
q0.hadamard()
# Measure input qubit only
result = measure(q0)
# result = 1 → balanced!
# result = 0 → constant!

The Deutsch Oracle preset is available in the Playground below — try it! Load the preset, and observe that measuring q0 always gives 1 for the balanced oracle (CNOT). If you remove the CNOT (making f constant), q0 will always measure 0. One query, one answer.

The trick is putting the output qubit q1 in the |-⟩ state (H applied to |1⟩). When the CNOT fires (because q0 is |1⟩ in superposition), it flips q1 between |+⟩ and |-⟩. But |-⟩ has a global phase of -1 relative to |+⟩, so the CNOT effectively multiplies q0's |1⟩ component by -1.

This phase flip on q0 is invisible until we apply H again. The final H converts the phase difference into an amplitude difference: |0⟩ vs |1⟩. Measurement then reads out whether the oracle was constant (no phase flip, measure 0) or balanced (phase flip, measure 1).

This "phase kickback" pattern — writing information into phases and then extracting it via interference — is the core technique behind almost every quantum algorithm, from Grover's search to Shor's factoring.

?

Quick check

What's the key quantum advantage in Deutsch's algorithm?

Open playground

Build your own quantum circuits with up to 3 qubits. Drag gates, switch between code and circuit views, and run thousands of simulated measurements. Try recreating the circuits from the lesson, or experiment with your own — the GHZ state and quantum teleportation presets are good starting points.

Key takeaway

Quantum computing is programming with probability amplitudes instead of bits. Superposition lets you explore many paths at once, interference cancels wrong answers, entanglement creates correlations no classical system can match, and measurement reads out the result. The math is elegant, but you don't need it to start building quantum intuition — code-first gets you there.

Finished this lesson?

Mark it as complete to track your progress and get a certificate.