Back to Learn

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).

Kub sends |0⟩ through X, H, Z and H. Watch the signed amplitudes and the probabilities separately: Z flips the sign of β without changing any probability, and the final H turns that hidden sign difference back into a certain |0⟩.

The common single-qubit gates

X=[0110]Z=[100−1]H=12[111−1]X=\begin{bmatrix}0&1\\1&0\end{bmatrix}\quad Z=\begin{bmatrix}1&0\\0&-1\end{bmatrix}\quad H=\frac{1}{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}
S=[100i]T=[100eiπ/4]X(α∣0⟩+β∣1⟩)=β∣0⟩+α∣1⟩S=\begin{bmatrix}1&0\\0&i\end{bmatrix}\quad T=\begin{bmatrix}1&0\\0&e^{i\pi/4}\end{bmatrix}\quad X\left(\alpha|0\rangle+\beta|1\rangle\right)=\beta|0\rangle+\alpha|1\rangle

Worked example: Z changes phase, not probability

Z∣+⟩=Z(∣0⟩+∣1⟩2)=∣0⟩−∣1⟩2=∣−⟩Z|+\rangle=Z\left(\frac{|0\rangle+|1\rangle}{\sqrt{2}}\right)=\frac{|0\rangle-|1\rangle}{\sqrt{2}}=|-\rangle
Before and after Z:P(0)=12,P(1)=12\text{Before and after }Z:\qquad P(0)=\frac{1}{2},\quad P(1)=\frac{1}{2}
H∣−⟩=∣1⟩H|-\rangle=|1\rangle
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

RY(θ)∣0⟩=cos⁡θ2∣0⟩+sin⁡θ2∣1⟩P(1)=sin⁡2θ2R_Y(\theta)|0\rangle=\cos\tfrac{\theta}{2}|0\rangle+\sin\tfrac{\theta}{2}|1\rangle\qquad P(1)=\sin^2\tfrac{\theta}{2}
sin⁡2θ2=14 ⇒ sin⁡θ2=12 ⇒ θ=π3\sin^2\tfrac{\theta}{2}=\tfrac14\ \Rightarrow\ \sin\tfrac{\theta}{2}=\tfrac12\ \Rightarrow\ \theta=\tfrac{\pi}{3}
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.

CX leaves the target alone when the control is 0 and flips it when the control is 1. With the control in superposition it does both at once, one per branch, which entangles the two qubits.

Worked example: the CX truth table

InputOutput after CX∣00⟩∣00⟩∣01⟩∣01⟩∣10⟩∣11⟩∣11⟩∣10⟩\begin{array}{c|c} \text{Input} & \text{Output after }CX\\ \hline |00\rangle&|00\rangle\\|01\rangle&|01\rangle\\|10\rangle&|11\rangle\\|11\rangle&|10\rangle\end{array}
CX(∣00⟩+∣10⟩2)=CX∣00⟩+CX∣10⟩2=∣00⟩+∣11⟩2CX\left(\frac{|00\rangle+|10\rangle}{\sqrt{2}}\right)=\frac{CX|00\rangle+CX|10\rangle}{\sqrt{2}}=\frac{|00\rangle+|11\rangle}{\sqrt{2}}

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

SWAP=CX0→1 CX1→0 CX0→1(H⊗H) CX0→1 (H⊗H)=CX1→0\mathrm{SWAP}=CX_{0\to1}\,CX_{1\to0}\,CX_{0\to1}\qquad (H\otimes H)\,CX_{0\to1}\,(H\otimes H)=CX_{1\to0}
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)))
# True

Key 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.