Skip to content
Kartikey Purohit
← Course overview

Day 2 — Parameterized & Dynamic Circuits, Transpilation

Math Prep

Solve this BEFORE lesson.md. Day 2 is about circuits whose gates carry angle parameters and about rewriting circuits into hardware basis gates. The math you need: rotation gates as matrix exponentials, phase bookkeeping, and universal decomposition.

Requires: Day 1 math prep. Video back-up: Watrous Lesson 03 Quantum Circuits; notes in resource-artifactory/notes/video-notes/.


1. Rotation gates are matrix exponentials

For any Pauli P{X,Y,Z}P \in \{X, Y, Z\} (each satisfies P2=IP^2 = I):

RP(θ)=eiθP/2=cosθ2Iisinθ2PR_P(\theta) = e^{-i\theta P/2} = \cos\tfrac{\theta}{2}\, I - i \sin\tfrac{\theta}{2}\, P

Plug in each Pauli and you get the Day-1 matrices. Two consequences you must internalize:

  1. The half angle. θ\theta is the rotation angle on the Bloch sphere, but the matrix contains θ/2\theta/2. So a Bloch-sphere rotation by π\pi around X is Rx(π)=iXR_x(\pi) = -iX, i.e. X up to global phase.
  2. Period 4π4\pi, not 2π2\pi: RP(θ+2π)=RP(θ)R_P(\theta + 2\pi) = -R_P(\theta) (a global-phase flip), and only RP(θ+4π)=RP(θ)R_P(\theta+4\pi) = R_P(\theta) exactly. Parameterized-circuit sweep questions use this.

Composition along the same axis is addition: Rz(α)Rz(β)=Rz(α+β)R_z(\alpha)R_z(\beta) = R_z(\alpha+\beta). Rotations about different axes do NOT commute: RxRzRzRxR_x R_z \ne R_z R_x in general — that's why gate order matters and why assign_parameters order never matters (it's just substituting numbers) but circuit order does.

2. Phase bookkeeping: p vs rz, one more time

P(λ)=(100eiλ),Rz(λ)=eiλ/2P(λ)P(\lambda) = \begin{pmatrix}1&0\\0&e^{i\lambda}\end{pmatrix}, \qquad R_z(\lambda) = e^{-i\lambda/2} P(\lambda)

Alone: identical physics (global phase). Controlled: different gates! Controlling a unitary promotes its global phase to a relative phase on the control:

C-(eiγU)=(I00eiγU)C-U when eiγ1C\text{-}(e^{i\gamma}U) = \begin{pmatrix}I & 0\\ 0 & e^{i\gamma}U\end{pmatrix} \ne C\text{-}U \text{ when } e^{i\gamma}\ne1

So cp(λ) ≠ controlled-rz(λ) — they differ by a p(λ/2) on the control. This is why Qiskit tracks qc.global_phase at all: the transpiler must keep it to make controlled/composed circuits exact.

3. Euler decomposition — why 3 parameters are enough

Any single-qubit unitary can be written (up to global phase) as three rotations:

U=eiγRz(ϕ)Ry(θ)Rz(λ)U = e^{i\gamma}\, R_z(\phi)\, R_y(\theta)\, R_z(\lambda)

which is exactly Qiskit's U(θ,ϕ,λ)U(\theta,\phi,\lambda) from Day 1. Equivalently with the hardware-native set:

U(θ,ϕ,λ)Rz(ϕ+π)  X  Rz(θ+π)  X  Rz(λ)U(\theta,\phi,\lambda) \sim R_z(\phi+\pi)\; \sqrt{X}\; R_z(\theta+\pi)\; \sqrt{X}\; R_z(\lambda)

This identity is the whole reason IBM hardware only needs {rz, sx, x} plus one entangler (cx/cz/ecr): any 1q gate = at most 2 physical X\sqrt{X} pulses + virtual RzR_z's. And RzR_z is "free" (virtual): it's implemented as a frame rotation in software, zero error, zero duration. That's why transpiled circuits are stuffed with rz and why optimization levels merge runs of 1q gates into at most rz-sx-rz-sx-rz.

4. Conjugation identities — the transpiler's rewrite rules

"Conjugating" AA by BB means BABBAB^\dagger. Memorize this table (HH and SS conjugations move rotation axes):

Identity Meaning
HXH=ZHXH = Z, HZH=XHZH = X, HYH=YHYH = -Y H swaps X↔Z axes
HRx(θ)H=Rz(θ)H R_x(\theta) H = R_z(\theta) rewrite rx using rz
SXS=YS X S^\dagger = Y S rotates X-axis to Y-axis
(HH)CX01(HH)=CX10(H\otimes H)\,CX_{0\to1}\,(H\otimes H) = CX_{1\to0} CNOT direction flip
CZ=(IH)CX(IH)CZ = (I\otimes H)\, CX\, (I\otimes H) CX ↔ CZ with 2 Hadamards on target
SWAP=CX01CX10CX01SWAP = CX_{01}CX_{10}CX_{01} 3 CNOTs (routing cost!)

The CNOT-direction flip and the SWAP identity explain transpiler behavior on real topologies: if the coupling map only has 0 ⁣ ⁣10\!\to\!1, a cx(1,0) costs 4 extra H's; a gate between non-neighbors costs SWAPs = 3 CNOTs each.

5. Commutation — when the order doesn't matter

[A,B]=ABBA[A,B] = AB - BA. Facts used in dynamic-circuit reasoning and optimization:

  • Diagonal gates (z, s, t, p, rz, cz, cp, rzz) all commute with each other.
  • ZZ-family on the control commutes through CX; XX-family on the target commutes through CX.
  • Measurement in the Z basis commutes with Z-diagonal gates before it — that's why a z right before measure is dead code the optimizer deletes.

6. The deferred measurement principle (dynamic circuits)

Any circuit with a mid-circuit measurement + classically-controlled gate (if_test) is equivalent to one where the measurement is pushed to the end and the classical control becomes a quantum control:

measure q0 → if result: X on q1 ≡ CX(q0→q1) then measure q0.

That's the math behind Day 2's dynamic circuits and behind teleportation's "corrections."


PROBLEMS

P1. Expand Ry(θ)=cosθ2Iisinθ2YR_y(\theta) = \cos\frac{\theta}{2} I - i\sin\frac{\theta}{2} Y into a matrix and confirm it matches Day 1's RyR_y.

P2. Show Rx(2π)=IR_x(2\pi) = -I. What measurement statistics distinguish the rx(2π) circuit from the identity circuit? What does this imply about Operator.equiv vs ==?

P3. Compute Rz(π/2)Rz(π/2)R_z(\pi/2) R_z(\pi/2) and name the resulting gate (up to global phase).

P4. Using Rz(λ)=eiλ/2P(λ)R_z(\lambda) = e^{-i\lambda/2}P(\lambda), find the matrix by which controlled-Rz(λ)R_z(\lambda) and controlled-P(λ)P(\lambda) differ, and express it as a gate on the control qubit.

P5. Verify HRx(θ)H=Rz(θ)H R_x(\theta) H = R_z(\theta) using Rx=cosθ2Iisinθ2XR_x = \cos\frac{\theta}{2}I - i\sin\frac{\theta}{2}X and HXH=ZHXH=Z.

P6. A device's coupling map has only the edge 0 ⁣ ⁣10\!\to\!1 (control 0, target 1). Count the total cx + h gates needed to implement cx(1,0).

P7. How many CNOTs does the transpiler insert to run a cx between qubits that are 2 hops apart (path 0–1–2, gate wanted between 0 and 2), if it SWAPs 0's state to 1 first and doesn't swap back?

P8. θ\theta sweep: for ry(θ) acting on 0|0\rangle, give P(1)P(1) as a function of θ, and evaluate at θ=0,π/2,π,2π\theta = 0, \pi/2, \pi, 2\pi.


ANSWERS

A1. iY=(0110)-iY = \begin{pmatrix}0&-1\\1&0\end{pmatrix}, so Ry(θ)=(cosθ2sinθ2sinθ2cosθ2)R_y(\theta) = \begin{pmatrix}\cos\frac{\theta}{2} & -\sin\frac{\theta}{2}\\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2}\end{pmatrix}. ✓ (All real — that's why ry is the go-to for real-amplitude ansätze.)

A2. Rx(2π)=cosπIisinπX=IR_x(2\pi) = \cos\pi\, I - i\sin\pi\, X = -I. None — no measurement can distinguish I-I from II (global phase). Hence Operator(rx(2π)).equiv(Operator(id)) is True but == is False.

A3. Rz(π/2)2=Rz(π)=iZR_z(\pi/2)^2 = R_z(\pi) = -iZ — that is ZZ (equivalently S2S^2) up to the global phase i-i.

A4. C-Rz(λ)=C-(eiλ/2P(λ))C\text{-}R_z(\lambda) = C\text{-}(e^{-i\lambda/2}P(\lambda)): the control-1|1\rangle block gets an extra factor eiλ/2e^{-i\lambda/2}. So C-Rz(λ)=(P(λ/2) on the control)C-P(λ)C\text{-}R_z(\lambda) = \big(P(-\lambda/2)\ \text{on the control}\big)\cdot C\text{-}P(\lambda) — the difference is a phase gate P(λ/2)P(-\lambda/2) on the control qubit.

A5. HRx(θ)H=cosθ2HIHisinθ2HXH=cosθ2Iisinθ2Z=Rz(θ)H R_x(\theta) H = \cos\frac{\theta}{2}\, HIH - i\sin\frac{\theta}{2}\, HXH = \cos\frac{\theta}{2} I - i \sin\frac{\theta}{2} Z = R_z(\theta). ∎

A6. cx(1,0) = H on both qubits, cx(0,1), H on both: 1 cx + 4 h.

A7. One SWAP (0↔1) = 3 CNOTs, then the desired cx on the now-adjacent pair = 1: 4 CNOTs total (and the logical→physical layout has changed — that's why the transpiled circuit carries a final layout, a concept in the lesson).

A8. Ry(θ)0=cosθ20+sinθ21P(1)=sin2θ2R_y(\theta)|0\rangle = \cos\frac{\theta}{2}|0\rangle + \sin\frac{\theta}{2}|1\rangle \Rightarrow P(1) = \sin^2\frac{\theta}{2}. Values: θ=0\theta=0: 0; θ=π/2\theta=\pi/2: sin2(π/4)=1/2\sin^2(\pi/4) = 1/2; θ=π\theta=\pi: 1; θ=2π\theta=2\pi: 0. (Sweep questions: P(1)P(1) oscillates with period 2π2\pi in θ even though the matrix period is 4π4\pi.)


Self-check: you can state RP(θ)=cosθ2Iisinθ2PR_P(\theta) = \cos\frac{\theta}{2}I - i\sin\frac{\theta}{2}P, explain why rz is "free" on IBM hardware, and produce the CX↔CZ and SWAP identities from memory.