If a qubit is a vector, a quantum gate is a matrix that rotates it. Two rules make quantum gates unlike classical logic gates, and both matter. First, every gate is unitary, reversible, information-preserving, never erasing. Second, the only randomness in a quantum computation happens at the very end, when you measure; everything before that is deterministic evolution of amplitudes. This lesson covers the handful of gates you actually need, shows how two-qubit gates create entanglement, builds a Bell pair step by step, and pins down the single idea behind every genuine quantum speedup, interference, while making clear it is not magic parallelism.
Gates are unitary
A gate acting on n qubits is a 2n×2n matrix that is , meaning
Ask the tutor
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
U
unitary
U†U=UU†=I,
where U† is the conjugate transpose. Unitarity has three consequences. It preserves length, so a normalized state stays normalized and the Born probabilities keep summing to one. It is reversible: every gate has an inverse U−1=U†, so no information is destroyed, the opposite of a classical AND gate, which maps two bits to one and forgets the input. And it is linear: a gate applied to a superposition acts on each component and the results add, which is precisely what lets amplitudes interfere. The one non-unitary, irreversible step in the whole model is measurement.
Single-qubit gates
The essential single-qubit gates are the Pauli gates and the Hadamard. Written in the computational basis:
X=(0110),Z=(100−1),H=21(11
The X gate is the quantum bit flip: X∣0⟩=∣1⟩ and X∣1⟩=∣0⟩. The Z gate is a phase flip: it leaves ∣0⟩ alone and sends ∣1⟩→−∣1⟩, touching phase without touching measurement probabilities in the computational basis. The HadamardH is the workhorse that manufactures superposition:
H∣0⟩=21(∣0⟩+∣1⟩)=∣+⟩,H∣1⟩=21(∣0⟩−∣1⟩)=∣−⟩.
H is its own inverse (H2=I), a fact we will use in a moment. Rounding out the toolkit, the phase gates S and T add a fixed relative phase to ∣1⟩ (a quarter and an eighth turn), and together with H and CNOT they form a universal set, enough to approximate any quantum computation.
Gate
Symbol
What it does
Pauli-X
X
Swaps the two basis amplitudes, a bit flip
Pauli-Z
Z
Leaves the zero state, negates the phase of the one state
Hadamard
H
Turns a basis state into an equal superposition; its own inverse
Phase
S, T
Adds a fixed relative phase to the one state
CNOT
CX
Flips the target when the control is set; can create entanglement
Two-qubit gates and entanglement: CNOT
To compute anything interesting you need gates that couple qubits. The controlled-NOT (CNOT, or CX) acts on two qubits, a control and a target, and flips the target only when the control is in state ∣1⟩. In the basis ordered ∣00⟩,∣01⟩,∣10⟩,∣11⟩ (control first),
CNOT=10000100000100,
so it maps ∣00⟩→∣00⟩, ∣01⟩→∣01⟩, ∣10⟩→∣11⟩, and ∣11⟩→∣10⟩. On classical basis inputs it looks mundane. Its power appears when the control is itself in superposition, then CNOT produces a state that cannot be factored into two independent qubits, which is entanglement.
Worked example: building a Bell state
Start both qubits in ∣0⟩, i.e. the two-qubit state ∣00⟩. Apply H to qubit 0 (the control):
H0∣00⟩=(21(∣0⟩+∣1⟩))⊗∣0⟩=21(∣00⟩+∣10⟩).
Now apply CNOT with control 0 and target 1. It leaves the ∣00⟩ term untouched and flips the target of the ∣10⟩ term to give ∣11⟩:
CNOT⋅21(∣00⟩+∣10⟩)=21(∣00⟩+∣11⟩).
This is the Bell state ∣Φ+⟩, a maximally entangled pair. Measure it and you get 00 or 11, each with probability one half, but never 01 or 10: the two outcomes are perfectly correlated no matter how far apart the qubits are carried. Crucially, this correlation cannot be written as any product ∣ψ⟩0⊗∣ϕ⟩1 of separate single-qubit states, that non-factorability is the mathematical definition of entanglement. And it does not allow faster-than-light signaling: each qubit measured alone looks like a fair random coin, and only comparing results later reveals the correlation (the no-communication theorem).
11. InitializeBoth qubits start in the zero state, giving the joint state where both read zero.
22. Hadamard on the controlQubit 0 becomes an equal superposition, so the pair is now an equal blend of both-zero and control-one-target-zero.
33. CNOT couples themThe target flips only in the branch where the control is one, linking the two qubits.
44. Result: a Bell pairAn equal superposition of both-zero and both-one. Measuring gives perfectly correlated outcomes and nothing in between.
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2) # 2 qubits, 2 classical bits
qc.h(0) # Hadamard on qubit 0 -> superposition
qc.cx(0, 1) # CNOT: control 0, target 1 -> entangle
qc.measure([0, 1], [0, 1]) # measure both qubits into the classical bits
# The statevector just before measurement is (|00> + |11>)/sqrt(2).
# Sampling many shots yields roughly 50% '00' and 50% '11',
# and essentially never '01' or '10'.
Interference is the real engine
Here is the fact that separates quantum computing from a classical coin-flipping machine. Because amplitudes are signed (indeed complex), they can cancel. Apply a Hadamard twice to ∣0⟩:
The ∣1⟩ contributions have opposite signs and destructively interfere to zero, while the ∣0⟩ contributions reinforce, you recover ∣0⟩ with certainty. A classical process that flips a fair coin twice never returns to a definite state; a quantum one can, because the intermediate "paths" carry amplitudes that cancel. Every real quantum algorithm, Deutsch–Jozsa, Grover, Shor, is an arrangement of gates that steers destructive interference onto wrong answers and constructive interference onto right ones before the final measurement. This is why "it tries all inputs at once" is the wrong mental model: the machine does not read a giant table, it sculpts amplitudes so the answer you want survives.
Explaining entanglement three ways
Intuition
For a curious beginner
Two entangled qubits are like a single object split in two, not two objects
with a secret agreement. No pre-arranged classical plan can reproduce all
the correlations you see across different measurement choices. Yet each half
on its own looks completely random, which is why you cannot use it to send a
message.
Engineering
How it is actually used
Entanglement is a state your two-qubit vector cannot be factored into a
product of single-qubit vectors. You create it by putting the control into
superposition with a Hadamard and then applying a CNOT. You detect it by
correlated measurement outcomes and, more rigorously, by violating a Bell
inequality across many runs.
Mathematical
The underlying mechanism
A pure two-qubit state is separable if it can be written
∣ψ⟩=∣a⟩⊗∣b⟩; otherwise it is entangled. For
the Bell state 21(∣00⟩+∣11⟩), tracing out
one qubit leaves the reduced density matrix 21I, maximally
mixed. Maximal local randomness with perfect global correlation is the
signature of maximal entanglement.
For a curious beginner
Two entangled qubits are like a single object split in two, not two objects
with a secret agreement. No pre-arranged classical plan can reproduce all
the correlations you see across different measurement choices. Yet each half
on its own looks completely random, which is why you cannot use it to send a
message.
How it is actually used
Entanglement is a state your two-qubit vector cannot be factored into a
product of single-qubit vectors. You create it by putting the control into
superposition with a Hadamard and then applying a CNOT. You detect it by
correlated measurement outcomes and, more rigorously, by violating a Bell
inequality across many runs.
The underlying mechanism
A pure two-qubit state is separable if it can be written
∣ψ⟩=∣a⟩⊗∣b⟩; otherwise it is entangled. For
the Bell state 21(∣00⟩+∣11⟩), tracing out
one qubit leaves the reduced density matrix 21I, maximally
mixed. Maximal local randomness with perfect global correlation is the
signature of maximal entanglement.
Common mistakes
"Quantum speedup means trying every input simultaneously." The engine is
interference, canceling wrong-answer amplitudes and reinforcing right ones,
not parallel evaluation you can read off. Measurement still returns one
outcome. - "CNOT always entangles." Applied to a computational basis input
like ∣00⟩ it just permutes basis states. Entanglement needs the
control in superposition first, which is why the Bell recipe starts with a
Hadamard. - "Entanglement lets you signal faster than light." No. Each
qubit's local measurement statistics are random and unaffected by the distant
partner; only comparing results afterward, over a classical channel, reveals
the correlation. - "Any matrix can be a gate." Only unitary matrices are
physical, they are reversible and length-preserving. Nonlinear or irreversible
"gates" are not allowed except as the final measurement. - "Measurement is
just another reversible step." It is the one irreversible, probabilistic
operation; place it deliberately, usually at the end, because it collapses the
amplitudes you spent the circuit building. - "Qubit ordering and endianness
do not matter." They do. Frameworks differ (Qiskit uses little-endian, so
bit ordering in printed states can surprise you), and a mislabeled control and
target silently changes the circuit.