Skip to content
Kartikey Purohit
← Course overview

Day 7 — Visualization & QASM

Math Prep

Solve this BEFORE lesson.md. Day 7's tools (plot_bloch_multivector, plot_state_qsphere, plot_state_city, histograms) each draw a specific mathematical object. Know the object, and every plot question becomes a computation.

Requires: Day 1 + Day 3 math prep. Video back-up: freeCodeCamp course (Bloch sphere section); Watrous Lesson 09 (Bloch ball); notes in resource-artifactory/notes/video-notes/.


1. The Bloch sphere — every 1-qubit state is a point on a ball

Any 1-qubit pure state can be written (global phase fixed away) as

ψ=cosθ20+eiϕsinθ21,θ[0,π], ϕ[0,2π)|\psi\rangle = \cos\tfrac{\theta}{2}\,|0\rangle + e^{i\phi}\sin\tfrac{\theta}{2}\,|1\rangle,\qquad \theta\in[0,\pi],\ \phi\in[0,2\pi)

(θ,ϕ)(\theta, \phi) are spherical coordinates: θ from the north pole, φ around the equator. Equivalently, the Bloch vector

r=(X,Y,Z)=(sinθcosϕ, sinθsinϕ, cosθ)\vec r = (\langle X\rangle, \langle Y\rangle, \langle Z\rangle) = (\sin\theta\cos\phi,\ \sin\theta\sin\phi,\ \cos\theta)

Landmarks (memorize as pictures):

State (θ,ϕ)(\theta,\phi) Bloch vector Position
0\|0\rangle (0,)(0,-) (0,0,1)(0,0,1) north pole
1\|1\rangle (π,)(\pi,-) (0,0,1)(0,0,-1) south pole
+\|+\rangle (π/2,0)(\pi/2,0) (1,0,0)(1,0,0) +x
\|-\rangle (π/2,π)(\pi/2,\pi) (1,0,0)(-1,0,0) −x
+i\|{+i}\rangle (π/2,π/2)(\pi/2,\pi/2) (0,1,0)(0,1,0) +y
i\|{-i}\rangle (π/2,3π/2)(\pi/2,3\pi/2) (0,1,0)(0,-1,0) −y

Orthogonal states are antipodal (NOT perpendicular) on the sphere — Hilbert-space 90° becomes 180° on the sphere because of the half-angle.

2. Gates are rotations of the sphere

  • X,Y,ZX, Y, Z: 180° rotations about their own axes. (XX swaps poles — hence NOT.)
  • Rx(θ),Ry(θ),Rz(θ)R_x(\theta), R_y(\theta), R_z(\theta): rotations by exactly θ about x/y/z. (Matrix half-angle ↔ full sphere angle — Day 2.)
  • HH: 180° rotation about the diagonal axis (x+z)/2(x{+}z)/\sqrt2 — swaps x-axis ↔ z-axis, so 0+|0\rangle \leftrightarrow |+\rangle, 1|1\rangle \leftrightarrow |-\rangle.
  • SS: 90° rotation about z: ++ii+|+\rangle \to |{+i}\rangle \to |-\rangle \to |{-i}\rangle \to |+\rangle. TT: 45° about z.
  • Phase gates never move the poles (only rotate about z) — that's "Z-family gates don't change measurement probabilities" drawn geometrically.

Mixed states live INSIDE the ball: ρ=12(I+rσ)\rho = \tfrac12(I + \vec r\cdot\vec\sigma) with r1|\vec r| \le 1; pure ⇔ surface, maximally mixed I/2I/2 ⇔ center (r=0\vec r = 0). Each qubit of a Bell pair plots at the center — that's how plot_bloch_multivector betrays entanglement (it shows per-qubit reduced states, and can never show the correlations themselves).

3. The Q-sphere — one ball for the WHOLE register

plot_state_qsphere is a different object: one node per basis state x|x\rangle of all nn qubits.

  • Node position: latitude by Hamming weight of xx (00|0\cdots0\rangle north pole, 11|1\cdots1\rangle south pole).
  • Node size ∝ αx2|\alpha_x|^2 (probability).
  • Node color = phase arg(αx)\arg(\alpha_x): 0 and π sit on opposite sides of the color wheel.

So Bell Φ+|\Phi^+\rangle: two equal nodes (00|00\rangle north, 11|11\rangle south), same color. 12(0011)\tfrac{1}{\sqrt2}(|00\rangle - |11\rangle): same two nodes, opposite colors — the plot that distinguishes what histograms can't.

4. City / matrix plots — drawing ρ itself

plot_state_city draws the density matrix entries ρjk\rho_{jk} as bars (real and imaginary parts separately).

  • Diagonal bars ρxx\rho_{xx} = probabilities.
  • Off-diagonal bars = coherences: present for superpositions, ZERO for classical mixtures — the visual pure-vs-mixed test (Day 3 §5).
  • Bell state: 4 corner bars of height 0.5 at positions (00,00), (00,11), (11,00), (11,11).

5. Histograms — drawing the sampled distribution

plot_histogram(counts) is Day 5's p^x\hat p_x as bars; plot_distribution normalizes to probabilities. Phases are invisible here (Day 5 §1). GHZ and Bell histograms look identical in shape (two bars) — only the labels ('000'/'111' vs '00'/'11') differ.


PROBLEMS

P1. Give (θ,ϕ)(\theta, \phi) and the Bloch vector for ψ=cosπ80+eiπ/2sinπ81|\psi\rangle = \cos\frac{\pi}{8}|0\rangle + e^{i\pi/2}\sin\frac{\pi}{8}|1\rangle. Where does it sit on the sphere?

P2. Start at 0|0\rangle. Apply H, then S, then H. Track the Bloch point through each step and name the final state.

P3. Is ry(π/2) on 0|0\rangle the same state as h on 0|0\rangle? Is ry(π/2) the same gate as h? Check on both 0|0\rangle and 1|1\rangle.

P4. A Bloch-multivector plot of a 2-qubit state shows BOTH qubit arrows with zero length. Give two different global states consistent with the plot — one entangled pure state, one unentangled (mixed) state. What does zero-length force if the global state is known to be pure?

P5. Q-sphere shows nodes at 00|00\rangle and 11|11\rangle, equal size, colors 180° apart on the wheel. Write the state. What would its counts histogram look like vs Φ+|\Phi^+\rangle's?

P6. For ψ=320+121|\psi\rangle = \tfrac{\sqrt3}{2}|0\rangle + \tfrac12|1\rangle: compute the Bloch vector and the density-matrix (city-plot) bars.

P7. TT is applied 8 times to +|+\rangle. Describe the trajectory on the Bloch sphere and the final state.


ANSWERS

A1. θ=π/4\theta = \pi/4, ϕ=π/2\phi = \pi/2. Bloch vector (sinπ4cosπ2, sinπ4sinπ2, cosπ4)=(0,12,12)(\sin\frac{\pi}{4}\cos\frac{\pi}{2},\ \sin\frac{\pi}{4}\sin\frac{\pi}{2},\ \cos\frac{\pi}{4}) = (0, \tfrac{1}{\sqrt2}, \tfrac{1}{\sqrt2}) — in the y–z plane, halfway between +y and +z (45° down from the north pole toward +y).

A2. 0H+|0\rangle \xrightarrow{H} |+\rangle (+x) S+i\xrightarrow{S} |{+i}\rangle (+y) H\xrightarrow{H}: H maps (x,y,z)(z,y,x)(x,y,z)\to(z,-y,x), so (0,1,0)(0,1,0)(0,1,0) \to (0,-1,0) = i=12(0i1)|{-i}\rangle = \tfrac{1}{\sqrt2}(|0\rangle - i|1\rangle) (up to global phase).

A3. Same state on 0|0\rangle: Ry(π/2)0=cosπ40+sinπ41=+=H0R_y(\pi/2)|0\rangle = \cos\frac{\pi}{4}|0\rangle + \sin\frac{\pi}{4}|1\rangle = |+\rangle = H|0\rangle. Different gates: on 1|1\rangle, Ry(π/2)1=120+121=R_y(\pi/2)|1\rangle = -\tfrac{1}{\sqrt2}|0\rangle + \tfrac{1}{\sqrt2}|1\rangle = -|-\rangle while H1=+H|1\rangle = +|-\rangle. Same ray but opposite sign — and that sign is relative once these gates act inside superpositions or controlled contexts, so the operators are genuinely different (H=HH = H^\dagger but Ry(π/2)=Ry(π/2)Ry(π/2)R_y(\pi/2)^\dagger = R_y(-\pi/2) \ne R_y(\pi/2)).

A4. Zero arrows = both reduced states are I/2I/2. Entangled pure option: any Bell state. Unentangled option: the product of two maximally-mixed qubits I2I2\tfrac{I}{2}\otimes\tfrac{I}{2} (two independently randomized qubits). If the global state is pure, zero-length arrows force entanglement — a pure product state always shows two unit-length arrows.

A5. 12(0011)=Φ\tfrac{1}{\sqrt2}(|00\rangle - |11\rangle) = |\Phi^-\rangle (phase π on the 11|11\rangle node). Histogram: identical to Φ+|\Phi^+\rangle's — two equal bars at '00' and '11'. Q-sphere sees phase; histograms never do.

A6. cosθ2=32θ=π3\cos\frac{\theta}{2} = \tfrac{\sqrt3}{2} \Rightarrow \theta = \tfrac{\pi}{3}, ϕ=0\phi = 0. Bloch vector (sinπ3,0,cosπ3)=(32,0,12)(\sin\frac{\pi}{3}, 0, \cos\frac{\pi}{3}) = (\tfrac{\sqrt3}{2}, 0, \tfrac12). ρ=(3/43/43/41/4)\rho = \begin{pmatrix}3/4 & \sqrt3/4\\ \sqrt3/4 & 1/4\end{pmatrix}: city bars 0.75 and 0.25 on the diagonal, 3/40.433\sqrt3/4 \approx 0.433 on both off-diagonals, all real (imaginary city empty).

A7. Each T rotates 45° about z along the equator: +|+\rangle visits 8 equally-spaced equatorial points and returns to +|+\rangle, since T8=(T4)2=Z2=IT^8 = (T^4)^2 = Z^2 = I.


Self-check: you can place any α0+eiϕβ1\alpha|0\rangle + e^{i\phi}\beta|1\rangle on the sphere, rotate it by any named gate mentally, decode q-sphere size/color, and explain why per-qubit Bloch arrows shrink for entangled states.