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.
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.
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)
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).
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)
// 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
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.
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.
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)
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:
// 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
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:
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.
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.
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:
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):
📖 References
- The core textbook: Nielsen & Chuang — Quantum Computation and Quantum Information — the classic and most comprehensive textbook on quantum computation.
- Shor's algorithm: Wikipedia — Shor's algorithm — the factoring algorithm in detail and its impact on RSA cryptography.
- Quantum entanglement & the Bell circuit: Wikipedia — Quantum entanglement — the theoretical foundation and the experiments confirming entanglement.
Comments