Introduction: a separate branch, not a continuation of the previous 11 lessons

The previous 11 lessons built up ONE complete classical machine — from logic gates (Lesson 1) to modern chiplet packaging (Lesson 11). This lesson is a SEPARATE BRANCH, an "expanding horizon" — quantum computing is NOT built on the binary bit foundation of the previous 11 lessons but on a COMPLETELY different mathematical model: quantum mechanics. This is not a capstone gathering up earlier material, but an introduction to a different direction in computer architecture.


📚 Background — a separate branch, not the next instalment
It helps to read Lesson 11 first (not technically required, but it follows the narrative: when classical silicon hits physical limits, quantum computing is ONE alternative direction — not a "replacement" but a "supplement" for SPECIFIC problems a classical machine cannot solve in reasonable time).

1. The classical bit against the qubit

A classical bit (throughout Lessons 1–11) is ALWAYS in exactly ONE of two states: 0 or 1. A qubit can be in a state of superposition — simultaneously "partly" 0 AND "partly" 1, represented by a state vector:

$$|\psi\rangle = \alpha|0\rangle + \beta|1\rangle \qquad\qquad |\alpha|^2 + |\beta|^2 = 1$$

$\alpha$ and $\beta$ are amplitudes — COMPLEX NUMBERS, not probabilities directly. The probability of measuring $|0\rangle$ is $|\alpha|^2$ and of measuring $|1\rangle$ is $|\beta|^2$ (the Born rule) — and the constraint $|\alpha|^2+|\beta|^2=1$ keeps the total probability at 1, exactly like any valid probability distribution.

state_vector.js (extract from quantum-sim.js — a SEPARATE module)
function cx(re, im = 0) { return { re, im }; } // so phuc toi gian
function makeZeroState(numQubits) {
  const size = Math.pow(2, numQubits);
  const state = new Array(size).fill(null).map(() => cx(0, 0));
  state[0] = cx(1, 0); // khoi tao luon o |00...0>, bien do 1
  return state;
}
function cAbs2(a) { return a.re * a.re + a.im * a.im; } // |alpha|^2 = xac suat
// Verified: makeZeroState(1) -> P(0)=1, P(1)=0 (chua ap dung cong nao)
⚠️ Pitfall: a quantum computer will NOT speed up your browser or games
Superposition lets $N$ qubits represent $2^N$ states AT ONCE — but that does NOT mean a quantum computer runs FASTER for EVERY kind of computation. The quantum advantage appears ONLY for problems with a special MATHEMATICAL STRUCTURE (integer factorisation, unstructured search, simulating other quantum systems) that allows interference between superposed states to be exploited. Browsers, games and ordinary office applications have no such structure — on a quantum computer they would be NO faster, and could well be far slower than on a classical CPU.

2. Quantum entanglement & quantum logic gates

Quantum entanglement : 2 qubits can be linked such that measuring ONE IMMEDIATELY determines the measurement outcome of the OTHER — wherever they are — in a way that CANNOT be described by treating each qubit independently. Quantum gates transform the state by a linear operation (a unitary matrix): Hadamard (H) creates an even superposition from a basis state; CNOT (2-qubit) flips the target qubit IF the control qubit is $|1\rangle$ — H followed by CNOT is the classic recipe for creating entanglement (the Bell circuit); Pauli X/Y/Z correspond to the 3 180° rotations about the 3 axes of the Bloch sphere (the geometric representation of a single qubit's state).

Classical bit Qubit 1 — can only be here 0 — or here exactly 2 possibilities |1⟩ |0⟩ a superposed state any point on the sphere — infinitely many possibilities measure it and it COLLAPSES to one of the two poles
The Bloch sphere. A classical bit exists only at the two poles; a qubit sits anywhere on the surface. But the moment you MEASURE, it collapses to exactly one pole — so "infinitely many possibilities" does not mean infinitely much information can be read out.
gate_matrices.txt (the 2×2 unitary matrix of each standard gate)
H (Hadamard):        X (Pauli, "NOT"):    Z (Pauli, dao pha):
  1/sqrt2 * [1  1]      [0  1]                [1   0]
            [1 -1]      [1  0]                [0  -1]

CNOT (2-qubit, dieu khien=q0, dich=q1):
  |00> -> |00>   |01> -> |01>   |10> -> |11>   |11> -> |10>
  (CHI lat q1 KHI q0 = 1 - day la nguon goc tao vuong viu khi ket hop voi H)
bell_circuit.js (extract from quantum-sim.js — a SEPARATE module)
// Mach Bell kinh dien: H tren qubit 0, roi CNOT(dieu khien=0, dich=1)
let state = makeZeroState(2); // |00>
state = applySingleQubitGate(state, GATE_H, 0, 2); // (|00> + |10>)/sqrt2
state = applyCNOT(state, 0, 1, 2);                  // (|00> + |11>)/sqrt2 - VUONG VIU!

const probs = measureProbabilities(state);
// Verified: probs = [0.5, 0, 0, 0.5] tuong ung [P(00), P(01), P(10), P(11)]
// Chi 2/4 trang thai co the do duoc - 2 qubit gio LIEN KET voi nhau
⚠️ Pitfall: decoherence — the environment destroys the quantum state
Superposed and entangled states are EXTREMELY fragile — any interaction with the surroundings (thermal vibration, a stray magnetic field, even light) can let a qubit "leak" information outward and LOSE its quantum state (decoherence), collapsing to a classical state UNINTENTIONALLY, before any deliberate measurement. This is why real quantum computers must operate near absolute zero (millikelvin) under extremely strict isolation from their environment.

3. Quantum algorithms & applications

Shor's algorithm factors large integers EXPONENTIALLY faster than the best known classical algorithm — a direct threat to RSA cryptography, whose security rests on factoring being hard. Grover's algorithm searches an unstructured list with a QUADRATIC speedup over sequential scanning — useful for optimisation and some general search problems.

⚠️ The NISQ era: Shor and Grover cannot yet run at a practical scale
Today's quantum computers are in the NISQ (noisy intermediate-scale quantum) era — qubit counts are still low (tens to a few hundred) AND per-gate error rates are still high because of decoherence (section 2). Actually breaking RSA with Shor needs THOUSANDS of error-free qubits (logical qubits, after error correction) — far beyond current hardware. Do not confuse "the algorithm is mathematically proven" with "it can run at real commercial scale TODAY".

4. Computing the output probabilities of a quantum circuit

Verified for real with the engine (the separate quantum-sim.js module, which does NOT share cpu-core.js because the mathematical model is entirely different): a Hadamard gate on $|0\rangle$ gives exactly $P(0)=P(1)=50\%$ (an even superposition). The Bell circuit (H on qubit 0, then CNOT with control=0, target=1) on $|00\rangle$ produces GENUINE ENTANGLEMENT:

$$P(00) = 0,5 \qquad P(01) = 0 \qquad P(10) = 0 \qquad P(11) = 0,5$$

Only 2 trong 4 states can be measured at all — $|01\rangle$ and $|10\rangle$ have probability ABSOLUTELY 0%, not "very small". That is the mathematical signature of entanglement: measure the first qubit as 0 and the second is CERTAIN to be 0 as well (and likewise for 1) — the 2 outcomes ALWAYS agree, even though no "signal" passes between the qubits at measurement time.

measurement.js (the Born rule, extract from quantum-sim.js)
function measureProbabilities(state) {
  return state.map((amplitude) => amplitude.re ** 2 + amplitude.im ** 2); // |alpha|^2
}
// Verified: H|0> -> probs = [0.5, 0.5] (chong chap deu 1 qubit)
// Verified: mach Bell -> probs = [0.5, 0, 0, 0.5] (vuong viu 2 qubit)
// Tong luon = 1 (bao toan xac suat, dinh de Born)
⚠️ Pitfall: the act of measuring collapses the superposition permanently
Before measurement a qubit "holds" superposed information about BOTH states at once. The instant it is measured the state COLLAPSES to EXACTLY ONE of the basis states — all the superposition information vanishes PERMANENTLY and cannot be recovered by measuring again (a second measurement simply returns the SAME collapsed result). This is why quantum algorithms must be designed to "read out" their useful answer ONLY at the FINAL measurement, after the superposition and interference have been fully exploited throughout the circuit.

5. Quantum error correction: why "thousands of logical qubits" means MILLIONS of real ones

The NISQ pitfall above mentioned "logical qubits, after error correction" and moved on. But that is exactly what determines how far quantum computing still is from practicality, so it is worth stopping on.

First, why errors are so lethal here. A circuit is only correct if every gate in it is correct. With a per-gate error rate $p$, the probability that the whole circuit gives a trustworthy answer is $(1-p)^{\text{gates}}$ — falling exponentially, not linearly:

nisq_limit.js (verified with the engine — same hardware, different gate counts)
// 0.1% error per gate - a good figure for today's hardware
circuitSuccessProbability(0.001,   100); // 90.48%   fine
circuitSuccessProbability(0.001,  1000); // 36.77%   marginal
circuitSuccessProbability(0.001, 10000); //  0.0045% pure noise

// 100x more gates costs more than 20,000x in success probability.
// To keep 90% at 10,000 gates the error rate must fall to 0.001%
circuitSuccessProbability(0.00001, 10000); // 90.48%  - 100x better hardware
🔍 A qubit cannot be backed up — so quantum error correction works quite differently
Classical machines fight errors in the simplest possible way: duplication. Store 1 bit as 3 copies, read all 3, take the majority. That approach cannot be used for qubits, because the no-cloning theorem proves no operation can copy an unknown quantum state. Worse still: merely reading the qubit to check it already collapses the very superposition you were protecting.

The way out is subtle: spread one qubit's information across several physical qubits entangled with each other, then measure only the relationships between them rather than their values. That measurement reveals "an error happened somewhere" without revealing the data, so it does not collapse the state. That cluster of physical qubits forms one qubit logic — which is what the algorithm actually runs on.

The price of that spreading is the whole problem. The surface code — the most actively pursued scheme — uses a lattice of distance $d$ and needs $d^2$ physical qubits per logical qubit. Take $d = 25$, a value commonly quoted in RSA-breaking estimates:

error_correction_overhead.js (verified with the engine — the price of a clean qubit)
surfaceCodeOverhead(25);              // 625 physical qubits per logical qubit
physicalQubitsNeeded(4000, 25);       // 2,500,000 physical qubits

// Breaking RSA-2048 needs roughly 4,000 logical qubits.
// Today's best hardware is on the order of 1,000 physical qubits.
// The gap is more than 3 orders of magnitude - and that is the honest answer
// to "how far away is this?", far more than any headline qubit count.
⚠️ Pitfall: reading headline qubit counts as if they were comparable
A "1,000-qubit machine" sounds a quarter of the way to the 4,000 qubits Shor needs. Verified, it is not: 4,000 logic qubits correspond to 2.500.000 physical qubits, meaning the gap is more than three orders of magnitude, not three quarters. Physical and logical qubits are different units, and nearly every published figure is the first kind.

There is one more easily missed condition: error correction only works once the physical error rate is already below a threshold (around 1%). Above it, adding qubits to the code makes things worse rather than better — because the added qubits themselves generate errors faster than they fix them. So this is not a matter of "wait long enough and there will be more qubits", but of reaching quality first and only then multiplying quantity.
⚠️ But do not conclude "so RSA is not a worry yet"
Section 3 said Shor threatens RSA; this section says the hardware is far off. Putting those together makes it easy to reach the wrong conclusion that nothing need be done. The reality is the opposite, for one simple reason: encrypted data collected today can be decrypted later — the tactic known as harvest now, decrypt later. Anything that must stay secret for more than ten years is at risk today, not in the future.

That is why NIST standardised post-quantum cryptography in 2024 — ML-KEM for key exchange, ML-DSA for signatures — built on lattice problems that no quantum algorithm is known to break. The notable part: this is software running on ordinary classical machines. The answer to the quantum threat is not to buy a quantum computer, but to change the algorithms running on exactly the machines the previous 11 lessons built.

6. Hands-on: a 2-qubit quantum circuit simulator

Click the gates to build your own 2-qubit circuit and watch the amplitudes and measurement probabilities change directly. Try the "Bell circuit" button to see REAL entanglement appear instantly:

⚛️ 2-qubit quantum circuit simulator
1-qubit gates
2-qubit gates & utilities
(no gates applied yet)

Summary

  • ✅ A qubit superposes as $|\psi\rangle=\alpha|0\rangle+\beta|1\rangle$ with $|\alpha|^2+|\beta|^2=1$ — but superposition does NOT automatically speed up every kind of computation.
  • ✅ Verified: the Bell circuit (H + CNOT) creates genuine entanglement — P(00)=P(11)=50%, P(01)=P(10)=absolutely 0%.
  • ✅ Pitfall: decoherence (environmental noise) destroys the quantum state UNINTENTIONALLY, before any deliberate measurement.
  • ✅ Shor and Grover prove a mathematical advantage — but today's NISQ hardware lacks the clean qubits to run them for real.
  • ✅ Pitfall: measuring collapses the superposition PERMANENTLY; measuring again does not recover it.
  • ✅ Verified: on the same hardware at 0.1% error per gate — a 100-gate circuit succeeds 90.48% of the time, 1,000 gates 36.77%, and 10,000 gates just 0.0045%. It degrades EXPONENTIALLY in gate count, so NISQ's problem is gate quality rather than qubit count.
  • ✅ Qubits cannot be copied (the no-cloning theorem), so quantum error correction spreads the information across many entangled qubits and measures only the RELATIONSHIPS between them — that cluster forms 1 logical qubit.
  • ✅ Verified: a surface code at $d=25$ costs 625 physical qubits per logical qubit, so the ~4,000 logical qubits for RSA-2048 mean 2,500,000 PHYSICAL qubits — more than three orders of magnitude beyond today's hardware (~1,000).
  • ✅ But the risk is present-tense: data collected now can be decrypted later (harvest now, decrypt later). The answer is post-quantum cryptography (NIST 2024: ML-KEM, ML-DSA) — software running on CLASSICAL machines, not a quantum computer purchase.

Review quiz

Question 1

Why would running an ordinary web browser or game on a quantum computer be NO faster than on a classical CPU?

Question 2

Verified: the Bell circuit gives P(00)=50%, P(11)=50%, P(01)=P(10)=ABSOLUTELY 0%. What proves this is GENUINE entanglement rather than independent randomness?

Question 3

What is decoherence, and why is it such a challenge for real quantum computers?

Question 4

Why does "Shor's algorithm can break RSA" not mean "RSA is broken right now"?

Download the lesson's practice code

File JavaScript quantum-sim.js — a SEPARATE module (it does not share cpu-core.js) for all the quantum computation: complex state vectors, Hadamard/CNOT/Pauli X-Y-Z gates, Born-rule measurement, the NISQ limit as a function of gate count, and surface-code error-correction overhead, with a self-test that checks every number quoted in this lesson (run node quantum-sim.js, nothing to install):

Download quantum-sim.js

📖 References

Related lessons in this series

Lesson 11: The End of Moore's Law & Chiplet Packaging Back to the Computer Architecture roadmap

Comments