Quantum Computing 101
Quantum Gates
Quantum gates are reversible transformations: matrices that move amplitude around without losing information.
In this lesson you will learn to
- Apply X, Z, H, S and T to state vectors.
- Separate what a gate does to probabilities from what it does to phase.
- Use rotation gates to dial in any amplitudes.
- Predict what CX does to basis states and to superpositions.
- Use simple circuit identities to rewrite gates.
Single-Qubit Gates
A gate transforms the state without measuring it, so nothing random happens. Every gate is a matrix that keeps states normalized, and every gate can be undone, which makes quantum circuits reversible.
X swaps the amplitudes of |0⟩ and |1⟩, like a NOT. Z leaves |0⟩ alone and flips the sign of |1⟩. H mixes the two, turning |0⟩ into |+⟩ and |1⟩ into |−⟩. S and T are smaller phase steps: S multiplies the |1⟩ amplitude by i, and T multiplies it by e^(iπ/4).
The common single-qubit gates
Worked example: Z changes phase, not probability
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
qc = QuantumCircuit(1)
qc.h(0)
qc.z(0)
qc.h(0)
print(Statevector.from_instruction(qc).probabilities_dict(decimals=3))
# {'1': 1.0}›Check yourself: What single gate equals H·Z·H?
X. Surrounding Z with Hadamards swaps the roles of the X and Z axes, so a phase flip becomes a bit flip. The circuit in the worked example is exactly this: it turns |0⟩ into |1⟩.
Rotations
Rotation gates turn the state by any angle you choose. RY(θ) maps |0⟩ to cos(θ/2)|0⟩ + sin(θ/2)|1⟩, so it can dial in any pair of real amplitudes.
RZ(θ) changes only the relative phase between |0⟩ and |1⟩, and RX(θ) rotates around the X axis. Combined, rotations can reach every single-qubit state.
Worked example: aim for P(1) = 1/4
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
import math
theta = 2 * math.asin(math.sqrt(0.25))
qc = QuantumCircuit(1)
qc.ry(theta, 0)
print(round(theta, 4), Statevector.from_instruction(qc).probabilities())
# 1.0472 [0.75 0.25]›Check yourself: Which RY angle gives P(1) = 1/2?
θ = π/2, because sin²(π/4) = 1/2. RY(π/2)|0⟩ is |+⟩, the same state H makes from |0⟩.
Controlled Gates
A controlled gate acts on a target qubit only when its control qubit is 1. CX, the controlled-NOT, flips the target when the control is 1 and leaves it alone when the control is 0.
The control is never measured. If it is in superposition, CX acts on each branch separately, and that is exactly how it creates entanglement.
Worked example: the CX truth table
Circuit Identities
Different circuits can implement the same operation. Knowing a few identities lets you fit a circuit to hardware that only supports some gates.
Three CX gates with alternating directions make a SWAP. Hadamards on both qubits reverse the direction of a CX. And since H·X·H = Z, a CX with H on both sides of its target becomes a CZ.
Worked example: SWAP from three CX gates
from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
swap = QuantumCircuit(2)
swap.swap(0, 1)
three_cx = QuantumCircuit(2)
three_cx.cx(0, 1)
three_cx.cx(1, 0)
three_cx.cx(0, 1)
print(Operator(three_cx).equiv(Operator(swap)))
# TrueKey Takeaways
- Gates are reversible matrices; they never introduce randomness.
- X flips amplitudes, Z, S and T change phase, and H mixes |0⟩ and |1⟩.
- RY(θ) sets real amplitudes cos(θ/2) and sin(θ/2).
- CX flips its target only in the branch where the control is 1, which can entangle qubits.
- Identities like SWAP = three CX gates let you rewrite circuits for real hardware.
What To Practice
Build each operation from a restricted gate set, the way real hardware forces you to.
- Flip the CNOTEasy ★Reverse a CX using only the direction the hardware supports.
- Swap Across a GapEasy ★Swap two qubits that are not neighbours using only nearest-neighbour CX gates.
- Controlled Hadamard Without CHEasy ★Turn a controlled-Z into a controlled-Hadamard with rotations.
- Controlled Global PhaseEasy ★Discover why a global phase stops being global once it is controlled.
- CCZ From Clifford+TMedium ★★Build a doubly-controlled Z from CX, T and T† alone.
- Phase Kickback DialEasy ★Use a controlled phase to write a phase onto the control qubit.