Skip to content
Kartikey Purohit
← Course overview

Day 1 — Circuit Basics

Math Prep

Solve this BEFORE lesson.md. Goal: when the lesson says "x, y, z, h, s, sdg, t, tdg, p, rx, ry, rz, u, sx, sxdg, id are methods on the circuit," you know exactly what 2×2 matrix each name is and what it does to a state.

Video back-up: freeCodeCamp Quantum Computing – Math and Theory for Beginners (resource-artifactory/videos/freecodecamp-qc-math/) and Watrous Lesson 01 Single Systems, Lesson 02 Multiple Systems (resource-artifactory/videos/watrous-uqic/). Notes: resource-artifactory/notes/video-notes/.


1. Complex numbers in 10 minutes

A complex number: z=a+biz = a + bi, with i2=1i^2 = -1.

  • Modulus (length): z=a2+b2|z| = \sqrt{a^2 + b^2}. Note z2=zz|z|^2 = z \cdot z^* where z=abiz^* = a - bi is the conjugate.
  • Euler form: z=reiφ=r(cosφ+isinφ)z = r e^{i\varphi} = r(\cos\varphi + i\sin\varphi).
  • The unit circle values you will use constantly:
φ\varphi eiφe^{i\varphi}
00 11
π/4\pi/4 1+i2\tfrac{1+i}{\sqrt2}
π/2\pi/2 ii
π\pi 1-1
3π/23\pi/2 (or π/2-\pi/2) i-i
2π2\pi 11 (wraps around)

Multiplying by eiφe^{i\varphi} rotates a complex number by angle φ\varphi and never changes its length. That's why phases don't change measurement probabilities on their own.

2. States are column vectors (Dirac notation)

A qubit state is a length-1 column vector in C2\mathbb{C}^2:

0=(10),1=(01)|0\rangle = \begin{pmatrix}1\\0\end{pmatrix}, \qquad |1\rangle = \begin{pmatrix}0\\1\end{pmatrix}
  • |·⟩ ("ket") = column vector. ⟨·| ("bra") = its conjugate transpose (a row vector): ψ=(ψ)\langle\psi| = (|\psi\rangle)^\dagger.
  • General state: ψ=α0+β1=(αβ)|\psi\rangle = \alpha|0\rangle + \beta|1\rangle = \begin{pmatrix}\alpha\\\beta\end{pmatrix} with the normalization rule α2+β2=1|\alpha|^2 + |\beta|^2 = 1.
  • α,β\alpha, \beta are amplitudes (complex). α2|\alpha|^2 = probability of measuring 0, β2|\beta|^2 = probability of measuring 1 (Born rule).
  • Inner product: ϕψ\langle\phi|\psi\rangle = row·column = a complex number. ψψ=1\langle\psi|\psi\rangle = 1 for normalized states. Two states are orthogonal when ϕψ=0\langle\phi|\psi\rangle=0.

The four named single-qubit states you must recognize on sight:

+=12(11),=12(11),+i (aka r)=12(1i),i (aka l)=12(1i)|+\rangle = \tfrac{1}{\sqrt2}\begin{pmatrix}1\\1\end{pmatrix},\quad |-\rangle = \tfrac{1}{\sqrt2}\begin{pmatrix}1\\-1\end{pmatrix},\quad |{+i}\rangle \text{ (aka } |r\rangle) = \tfrac{1}{\sqrt2}\begin{pmatrix}1\\i\end{pmatrix},\quad |{-i}\rangle \text{ (aka } |l\rangle) = \tfrac{1}{\sqrt2}\begin{pmatrix}1\\-i\end{pmatrix}

All four give 50/50 outcomes in the standard basis — they differ only in relative phase, which other gates can later convert into probability differences. That's the whole trick of quantum interference.

3. Gates are unitary matrices

A gate is a 2n×2n2^n \times 2^n matrix UU applied by multiplication: ψout=Uψin|\psi_{\text{out}}\rangle = U|\psi_{\text{in}}\rangle. It must be unitary: UU=IU^\dagger U = I (where UU^\dagger = conjugate-transpose, called the adjoint or dagger). Unitary ⇔ preserves lengths ⇔ probabilities always sum to 1 ⇔ reversible (inverse is UU^\dagger).

Every gate in the Day-1 list, decoded

Pauli gates — the "classical-ish" ones:

X=(0110)(NOT: swaps 01)X = \begin{pmatrix}0&1\\1&0\end{pmatrix} \quad\text{(NOT: swaps } |0\rangle \leftrightarrow |1\rangle)
Y=(0ii0)(Y=iXZ; flips AND phases)Y = \begin{pmatrix}0&-i\\i&0\end{pmatrix} \quad (Y = iXZ\text{; flips AND phases)}
Z=(1001)(phase-flip: 11)Z = \begin{pmatrix}1&0\\0&-1\end{pmatrix} \quad\text{(phase-flip: } |1\rangle \mapsto -|1\rangle)
I=(1001)(qiskit method: ‘id‘)I = \begin{pmatrix}1&0\\0&1\end{pmatrix} \quad\text{(qiskit method: `id`)}

Hadamard — the superposition-maker:

H=12(1111):0+,  1H = \tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}: \quad |0\rangle\mapsto|+\rangle,\; |1\rangle\mapsto|-\rangle

HH is its own inverse (H2=IH^2 = I) and converts between X-basis and Z-basis: HXH=ZHXH = Z, HZH=XHZH = X.

Phase-family gates — all diagonal, all only touch the 1|1\rangle amplitude:

P(λ)=(100eiλ)P(\lambda) = \begin{pmatrix}1&0\\0&e^{i\lambda}\end{pmatrix}
Gate = P(λ)P(\lambda) with Matrix diag Note
z λ=π\lambda=\pi (1,1)(1,-1)
s λ=π/2\lambda=\pi/2 (1,i)(1,i) S=ZS = \sqrt{Z}
sdg λ=π/2\lambda=-\pi/2 (1,i)(1,-i) SS^\dagger, undoes S
t λ=π/4\lambda=\pi/4 (1,eiπ/4)(1,e^{i\pi/4}) T=ST = \sqrt{S}
tdg λ=π/4\lambda=-\pi/4 (1,eiπ/4)(1,e^{-i\pi/4}) TT^\dagger

So: T2=ST^2 = S, S2=ZS^2 = Z, T4=ZT^4=Z, SS=IS\cdot S^\dagger = I. "dg" = dagger = adjoint = inverse.

Rotation gates — continuous versions, angle θ\theta around a Bloch-sphere axis (half-angle θ/2\theta/2 appears in the matrix — memorize that):

Rx(θ)=(cosθ2isinθ2isinθ2cosθ2),Ry(θ)=(cosθ2sinθ2sinθ2cosθ2),Rz(θ)=(eiθ/200eiθ/2)R_x(\theta)=\begin{pmatrix}\cos\frac{\theta}{2} & -i\sin\frac{\theta}{2}\\ -i\sin\frac{\theta}{2} & \cos\frac{\theta}{2}\end{pmatrix},\quad R_y(\theta)=\begin{pmatrix}\cos\frac{\theta}{2} & -\sin\frac{\theta}{2}\\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2}\end{pmatrix},\quad R_z(\theta)=\begin{pmatrix}e^{-i\theta/2}&0\\0&e^{i\theta/2}\end{pmatrix}

Rz(θ)R_z(\theta) vs P(θ)P(\theta): same up to the global phase eiθ/2e^{-i\theta/2} — physically identical alone, but the difference matters once controlled. (This is exactly the lesson's p(pi) == z exact vs rz(pi) = -iZ trap.)

Square-root-of-X:

SX=X=12(1+i1i1i1+i),SXSX=X;sxdg=SXSX = \sqrt{X} = \tfrac{1}{2}\begin{pmatrix}1+i & 1-i\\ 1-i & 1+i\end{pmatrix},\qquad SX\cdot SX = X;\quad \texttt{sxdg} = SX^\dagger

The universal one-qubit gate u: every single-qubit unitary (up to global phase) is

U(θ,ϕ,λ)=(cosθ2eiλsinθ2eiϕsinθ2ei(ϕ+λ)cosθ2)U(\theta,\phi,\lambda) = \begin{pmatrix}\cos\frac{\theta}{2} & -e^{i\lambda}\sin\frac{\theta}{2}\\ e^{i\phi}\sin\frac{\theta}{2} & e^{i(\phi+\lambda)}\cos\frac{\theta}{2}\end{pmatrix}

Check: U(π/2,0,π)=HU(\pi/2, 0, \pi) = H,   U(0,0,λ)=P(λ)\;U(0,0,\lambda) = P(\lambda),   U(π,0,π)=X\;U(\pi, 0, \pi) = X.

Global vs relative phase — the exam's favorite trap

  • ψ|\psi\rangle and eiγψe^{i\gamma}|\psi\rangle are the same physical state (global phase, unobservable).
  • 12(0+1)\tfrac{1}{\sqrt2}(|0\rangle + |1\rangle) vs 12(01)\tfrac{1}{\sqrt2}(|0\rangle - |1\rangle) differ by a relative phase — different states (+|+\rangle vs |-\rangle), distinguishable after an H.

4. Multiple qubits: the tensor product ⊗

Two qubits live in C4\mathbb{C}^4; states of independent qubits combine with the Kronecker/tensor product:

ab=ab,(a0a1)(b0b1)=(a0b0a0b1a1b0a1b1)|a\rangle \otimes |b\rangle = |ab\rangle,\qquad \begin{pmatrix}a_0\\a_1\end{pmatrix}\otimes\begin{pmatrix}b_0\\b_1\end{pmatrix} = \begin{pmatrix}a_0 b_0\\ a_0 b_1\\ a_1 b_0\\ a_1 b_1\end{pmatrix}

Basis order: 00,01,10,11|00\rangle, |01\rangle, |10\rangle, |11\rangle = indices 0,1,2,3 (binary!). Gates combine the same way: applying AA to one qubit and BB to the other is ABA \otimes B, a 4×4 matrix. nn qubits ⇒ 2n2^n amplitudes — that's the exponential state space.

Qiskit's little-endian twist: qubit 0 is the LEAST significant bit. A state written q1q0|q_1 q_0\rangle means the left symbol is qubit 1. x(0) on 2 qubits gives bitstring '01'. Matrix-wise Qiskit's 2-qubit operator for "A on q1, B on q0" is ABA \otimes B — matching a.tensor(b) putting b on the lower wires.

Controlled gates. CNOT (cx) flips the target iff the control is 1|1\rangle. With control = q0 (Qiskit order, control on the low qubit):

CX01=(1000000100100100)(in the little-endian basis q1q0:0111)CX_{0\to1} = \begin{pmatrix}1&0&0&0\\0&0&0&1\\0&0&1&0\\0&1&0&0\end{pmatrix} \quad\text{(in the little-endian basis } |q_1q_0\rangle: |01\rangle \leftrightarrow |11\rangle)

General recipe: C-U=00ctlI+11ctlUC\text{-}U = |0\rangle\langle0|_{\text{ctl}} \otimes I + |1\rangle\langle1|_{\text{ctl}} \otimes U. Because CZCZ's action (1-1 only on 11|11\rangle) doesn't care which qubit is "control," CZ/CP/SWAP-type gates are symmetric; CX/CY/CH are not — exactly the lesson's symmetric/asymmetric table.

Entanglement. A state is entangled iff it cannot be written as ab|a\rangle\otimes|b\rangle. The Bell state

Φ+=12(00+11)=(H on q0, then CX)00|\Phi^+\rangle = \tfrac{1}{\sqrt2}(|00\rangle + |11\rangle) = \text{(H on q0, then CX)}\,|00\rangle

has no such factorization (try it: you'd need a0b0=a1b1=12a_0b_0 = a_1b_1 = \tfrac{1}{\sqrt2} but a0b1=0a_0b_1 = 0 — impossible).

5. Measurement (just enough for Day 1)

Measuring in the computational basis: outcome xx with probability xψ2|\langle x|\psi\rangle|^2 = |amplitude|², then the state collapses to x|x\rangle. Measurement is NOT unitary and NOT reversible — that's why inverse() refuses circuits containing measure.


PROBLEMS — do on paper, answers below

P1. Normalize (34i)\begin{pmatrix}3\\4i\end{pmatrix} and give the probability of measuring 1.

P2. Compute H1H|1\rangle by matrix multiplication. Which named state is it?

P3. Show SS=ZS \cdot S = Z and TT=IT^\dagger T = I by multiplying matrices.

P4. Compute HZHHZH and identify the resulting gate.

P5. What does XX do to |-\rangle? Give the resulting state and explain why measuring it in the standard basis can't tell the difference.

P6. Verify Rz(π)=eiπ/2Z=iZR_z(\pi) = e^{-i\pi/2} Z = -iZ. Why is Rz(π)R_z(\pi) alone physically identical to ZZ?

P7. Write out +1|+\rangle \otimes |1\rangle as a 4-vector. If this is "q1 = |+⟩, q0 = |1⟩" in Qiskit convention, which bitstrings can be measured and with what probabilities?

P8. Prove 12(00+11)\tfrac{1}{\sqrt2}(|00\rangle + |11\rangle) is entangled (no product decomposition exists).

P9. Apply CNOT (control q0, target q1) to +|+\rangle on q0 and 0|0\rangle on q1, i.e. the state 12(00+01)\tfrac{1}{\sqrt2}(|00\rangle+|01\rangle) written as q1q0|q_1q_0\rangle. What comes out?

P10. Using U(θ,ϕ,λ)U(\theta,\phi,\lambda), verify U(π/2,0,π)=HU(\pi/2,0,\pi) = H.


ANSWERS

A1. Norm =32+4i2=9+16=5= \sqrt{|3|^2 + |4i|^2} = \sqrt{9+16} = 5. State: 15(34i)\tfrac{1}{5}\begin{pmatrix}3\\4i\end{pmatrix}. P(1)=4i/52=16/25P(1) = |4i/5|^2 = 16/25.

A2. H1=12(1111)(01)=12(11)=H|1\rangle = \tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}\begin{pmatrix}0\\1\end{pmatrix} = \tfrac{1}{\sqrt2}\begin{pmatrix}1\\-1\end{pmatrix} = |-\rangle.

A3. S2=(100i)2=(100i2)=(1001)=ZS^2 = \begin{pmatrix}1&0\\0&i\end{pmatrix}^2 = \begin{pmatrix}1&0\\0&i^2\end{pmatrix} = \begin{pmatrix}1&0\\0&-1\end{pmatrix} = Z. TT=(100eiπ/4)(100eiπ/4)=IT^\dagger T = \begin{pmatrix}1&0\\0&e^{-i\pi/4}\end{pmatrix}\begin{pmatrix}1&0\\0&e^{i\pi/4}\end{pmatrix} = I.

A4. HZH=XHZH = X. (Compute: ZH=12(1111)ZH = \tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\-1&1\end{pmatrix}, then H(ZH)=(0110)H(ZH) = \begin{pmatrix}0&1\\1&0\end{pmatrix}.) H swaps the X and Z bases.

A5. X=12(X0X1)=12(10)=X|-\rangle = \tfrac{1}{\sqrt2}(X|0\rangle - X|1\rangle) = \tfrac{1}{\sqrt2}(|1\rangle - |0\rangle) = -|-\rangle. Only a global phase changed; standard-basis probabilities are 50/50 before AND after, and global phase is unobservable.

A6. Rz(π)=(eiπ/200eiπ/2)=(i00i)=i(1001)=iZR_z(\pi) = \begin{pmatrix}e^{-i\pi/2}&0\\0&e^{i\pi/2}\end{pmatrix} = \begin{pmatrix}-i&0\\0&i\end{pmatrix} = -i\begin{pmatrix}1&0\\0&-1\end{pmatrix} = -iZ. The factor i-i is a global phase ⇒ same physical action as ZZ (but Operator(rz(π)) == Operator(z) is False while .equiv is True).

A7. +1=12(11)(01)=12(0,1,0,1)T|+\rangle\otimes|1\rangle = \tfrac{1}{\sqrt2}\begin{pmatrix}1\\1\end{pmatrix}\otimes\begin{pmatrix}0\\1\end{pmatrix} = \tfrac{1}{\sqrt2}(0,1,0,1)^T, i.e. 12(01+11)\tfrac{1}{\sqrt2}(|01\rangle + |11\rangle) reading q1q0|q_1q_0\rangle. Measured bitstrings: '01' and '11', 50% each — q0 is always 1 (rightmost bit), q1 is random. In code: plus.tensor(one) since a.tensor(b) puts b (here 1|1\rangle) on qubit 0.

A8. Suppose (a00+a11)(b00+b11)(a_0|0\rangle + a_1|1\rangle)\otimes(b_0|0\rangle + b_1|1\rangle) has amplitudes a0b0=12a_0b_0 = \tfrac{1}{\sqrt2}, a0b1=0a_0b_1 = 0, a1b0=0a_1b_0 = 0, a1b1=12a_1b_1 = \tfrac{1}{\sqrt2}. From a0b1=0a_0b_1=0: either a0=0a_0=0 (contradicts a0b00a_0b_0\ne0) or b1=0b_1=0 (contradicts a1b10a_1b_1\ne0). No factorization ⇒ entangled. ∎

A9. CNOT maps 0000|00\rangle \mapsto |00\rangle and (control q0 = 1) 0111|01\rangle \mapsto |11\rangle. Result: 12(00+11)=Φ+\tfrac{1}{\sqrt2}(|00\rangle + |11\rangle) = |\Phi^+\rangle — the Bell state. This is exactly h(0); cx(0,1) from the lesson.

A10. U(π/2,0,π)U(\pi/2,0,\pi): cosπ4=sinπ4=12\cos\frac{\pi}{4} = \sin\frac{\pi}{4} = \tfrac{1}{\sqrt2}, so the matrix is (12eiπ1212eiπ12)=12(1111)=H\begin{pmatrix}\tfrac{1}{\sqrt2} & -e^{i\pi}\tfrac{1}{\sqrt2}\\ \tfrac{1}{\sqrt2} & e^{i\pi}\tfrac{1}{\sqrt2}\end{pmatrix} = \tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix} = H. ✓


Self-check before starting lesson.md: you should be able to write from memory the matrices for X, Y, Z, H, S, T, P(λ), Rx, Ry, Rz, and CNOT, and state the Born rule and the tensor-product recipe. If not, redo P2–P4 and re-read §3.