Lecture 4 - Computation, Measurement, and Dynamic Circuits

Lecture 4 - Computation, Measurement, and Dynamic Circuits

Updated 14 Oct 2026

1. Revision

Clifford Set of Quantum Gates

Table 1: Single-qubit gates

| Gates | Symbols | From ∣0⟩|0\rangle | From ∣1⟩|1\rangle | |---|---|---|---| | NOT | X | ∣1⟩|1\rangle | ∣0⟩|0\rangle | | Hadamard | H | ∣0⟩+∣1⟩2\frac{|0\rangle+|1\rangle}{\sqrt2} | ∣0⟩−∣1⟩2\frac{|0\rangle-|1\rangle}{\sqrt2} | | Phase shift | S | ∣0⟩|0\rangle | i∣1⟩i|1\rangle | | Pauli-X | X | ∣1⟩|1\rangle | ∣0⟩|0\rangle | | Pauli-Y | Y | i∣1⟩i|1\rangle | −i∣0⟩-i|0\rangle | | Pauli-Z | Z | ∣0⟩|0\rangle | −∣1⟩-|1\rangle |

Table 2: Two-qubit gates

GatesSymbolsDescriptions
CNOTCNOT$
SwapSWAP$
CZCZ$

Operator Augmentation

  • If we have a quantum operator

Q=∑k=1K∣ϕk⟩⟨ψk∣\mathbf{Q} = \sum_{k=1}^{K}|\phi_k\rangle\langle\psi_k|

we can **augment** $\mathbf{Q}$ with trivial qubits by

I⊗M⊗Q=∑k=1K∣x1,…,xM,ϕk⟩⟨x1,…,xM,ψk∣\mathbf{I}^{\otimes M}\otimes\mathbf{Q} = \sum_{k=1}^{K}|x_1,\dots,x_M,\phi_k\rangle\langle x_1,\dots,x_M,\psi_k| Q⊗I⊗M=∑k=1K∣ϕk,x1,…,xM⟩⟨ψk,x1,…,xM∣\mathbf{Q}\otimes\mathbf{I}^{\otimes M} = \sum_{k=1}^{K}|\phi_k,x_1,\dots,x_M\rangle\langle\psi_k,x_1,\dots,x_M|

  • This is useful for accommodating all entangled qubits, some of which are not involved with the operator

  • For any quantum operator Q\mathbf{Q}, we can augment it with trivial qubits by

Qp1,…,pM(N)=∑k=1K∣x1′,…,xN′⟩⟨x1,…,xN∣\boxed{\mathbf{Q}^{(N)}_{p_1,\dots,p_M} = \sum_{k=1}^{K}|x_1',\dots,x_N'\rangle\langle x_1,\dots,x_N|}

📝 Instructor's annotation: on the recap slide, again drew an arrow from the superscript NN to "N qubits in total" and from the subscript p1,…,pMp_1,\dots,p_M to "only apply on these qubits" — the same key from Chapter 3, re-added by hand here for the revision.

where the output mixed state is specified by the indices $p_1,\dots,p_M$, i.e.

∣xp1′,…,xpM′⟩=Q∣xp1,…,xpM⟩|x_{p_1}',\dots,x_{p_M}'\rangle = \mathbf{Q}|x_{p_1},\dots,x_{p_M}\rangle

while the remaining input qubits are unchanged, i.e. $x_{p_j}'=x_{p_j}$ for all indices $j\notin\{p_1,\dots,p_M\}$

2. Non-Clifford Set

Non-Clifford Set of Quantum Logic Gates

  • Limitation: We cannot construct all arbitrary quantum operators using only Clifford gates; for example,

Q=[10\0cis⁡π4]\mathbf{Q} = \begin{bmatrix}1&0\0&\operatorname{cis}\frac{\pi}{4}\end{bmatrix}

  • Gottesman-Knill Theorem: "Some quantum algorithms can be efficiently simulated by classical computers, even though they use superposition and entanglement."
  • Non-Clifford Set: All quantum operators that cannot be constructed using only the Clifford gates
  • Additional building blocks are needed: Toffoli gate, phase shift gate, rotation gates, and universal gate
  • Simulation of these gates on classical computers is inefficient, which means it takes non-polynomial computation time
  • When combined with superpositions and entanglement, these gates make some NP-hard problems solvable in polynomial time complexity

💡 Analogy: The Clifford set is "the safe, well-understood toolbox" — powerful enough to build entanglement and superposition, but also efficiently trackable by an ordinary computer (that's the Gottesman-Knill theorem), so it never gives quantum computers a genuine speed advantage. The Non-Clifford gates are what actually pushes past classical simulation — they're the reason quantum computers can, in principle, beat classical ones.

Toffoli Gate

📝 Instructor's annotation: next to the title, wrote "sometimes called CCNOT".

  • Toffoli gate flips the third qubit only if the first and second qubits are ∣1⟩|1\rangle

∣00x⟩↦∣00x⟩∣01x⟩↦∣01x⟩∣10x⟩↦∣10x⟩∣11x⟩↦∣11xˉ⟩|00x\rangle\mapsto|00x\rangle \qquad |01x\rangle\mapsto|01x\rangle \qquad |10x\rangle\mapsto|10x\rangle \qquad |11x\rangle\mapsto|11\bar x\rangle

  • The quantum operator CCX is

CCX=[10000000\01000000\00100000\00010000\00001000\00000100\00000001\00000010]\boxed{\text{CCX} = \begin{bmatrix}1&0&0&0&0&0&0&0\0&1&0&0&0&0&0&0\0&0&1&0&0&0&0&0\0&0&0&1&0&0&0&0\0&0&0&0&1&0&0&0\0&0&0&0&0&1&0&0\0&0&0&0&0&0&0&1\0&0&0&0&0&0&1&0\end{bmatrix}}

  • Example: CCX(∣000⟩+∣111⟩2)=∣000⟩+∣110⟩2\text{CCX}\left(\frac{|000\rangle+|111\rangle}{\sqrt2}\right) = \frac{|000\rangle+|110\rangle}{\sqrt2}
  • CCX is self-inversible
  • qml.Toffoli(wires=…)

image-2 Caption — Figure 1: Toffoli gate. Three wires ∣x⟩,∣y⟩,∣z⟩|x\rangle,|y\rangle,|z\rangle: the first two (control) wires pass through unchanged; the third (target) wire comes out as ∣(x∧y)⊕z⟩|(x\wedge y)\oplus z\rangle.

Boolean Universality of Toffoli Gate

  • Toffoli gate is said to be universal: we can construct any classical Boolean logic gate from it, similar to the NAND gate
  • Theoretically, it enables quantum computers to emulate the classical ones with an FPGA-like Toffoli circuit

📝 Instructor's annotation: boxed the whole "Theoretically…" bullet in green and added, next to the AND-gate figure, "All classical programs can be run on quantum computers!" — the headline takeaway of Boolean universality.

image-3 Caption — Figures 2–4: Classical logic gates emulated with Toffoli gates.

  • Figure 2 (NOT): control wires are ∣x⟩|x\rangle and a constant ∣1⟩|1\rangle (both pass through unchanged); the target wire is a second constant ∣1⟩|1\rangle, which comes out as ∣xˉ⟩|\bar x\rangle — since both controls are 1 exactly when x=1x=1, the target flips exactly when x=1x=1, giving 1⊕x=xˉ1\oplus x=\bar x.
  • Figure 3 (AND): the standard CCX layout — controls ∣x⟩,∣y⟩|x\rangle,|y\rangle pass through unchanged, and a target ancilla ∣0⟩|0\rangle comes out as ∣x∧y⟩|x\wedge y\rangle.
  • Figure 4 (OR): built from De Morgan's law x∨y=xˉ∧yˉ‾x\vee y = \overline{\bar x\wedge\bar y} — xx and yy each pass through a NOT sub-circuit (as in Figure 2), the two results feed an AND sub-circuit (as in Figure 3), and the AND's output passes through one more NOT sub-circuit to yield ∣x∨y⟩|x\vee y\rangle.

💡 Analogy: This is exactly how classical CPUs are built from NAND gates alone — except here the one universal building block is the Toffoli gate, and "wiring constants into some of the inputs" is the quantum equivalent of hard-wiring a 1 into a classical logic gate's input pin.

Generalized Controlled Gate

  • Any single-qubit quantum operator U\mathbf{U} can be expressed in the exponential form

U=cis⁡(HU)\mathbf{U} = \operatorname{cis}(\mathbf{H}_U)

📝 Instructor's annotation: the printed line originally read "where HU\mathbf{H}_U is the Hermitian matrix of U\mathbf{U}" — the instructor crossed out "Hermitian matrix" and wrote "Hamiltonian matrix" above it. (HU\mathbf{H}_U is still required to be Hermitian — the correction is about what to call it: it's U\mathbf{U}'s Hamiltonian.)

where $\mathbf{H}_U$ is the Hamiltonian matrix of $\mathbf{U}$
  • Generalized controlled gate of U\mathbf{U} is denoted by CU\mathbf{CU}, where

CU=cis⁡((I−Z)⊗HU2)=[1000\0100\00u11u12\00u21u22]\boxed{\mathbf{CU} = \operatorname{cis}\left(\frac{(\mathbf{I}-\mathbf{Z})\otimes\mathbf{H}_U}{2}\right) = \begin{bmatrix}1&0&0&0\0&1&0&0\0&0&u_{11}&u_{12}\0&0&u_{21}&u_{22}\end{bmatrix}}

📝 Instructor's annotation: circled the bottom-right 2×22\times2 block \begin{bmatrix}u_{11}&u_{12}\u_{21}&u_{22}\end{bmatrix} and drew an arrow labeling it simply "U\mathbf{U}" — i.e. that block is U\mathbf{U} itself: when the control reads 1, the target block is exactly U\mathbf{U}; when the control reads 0, it's left as identity.

  • qml.ctrl(opr, control=…)
  • Example: CH=[1000\0100\001212\0012−12]\mathbf{CH} = \begin{bmatrix}1&0&0&0\0&1&0&0\0&0&\frac{1}{\sqrt2}&\frac{1}{\sqrt2}\0&0&\frac{1}{\sqrt2}&-\frac{1}{\sqrt2}\end{bmatrix}

image-4 Caption — Figure 5: Controlled gate. Two cases shown on stacked circuits: with control ∣0⟩|0\rangle, the control wire passes through as ∣0⟩|0\rangle and the target wire ∣y⟩|y\rangle passes through unchanged; with control ∣1⟩|1\rangle, the control wire passes through as ∣1⟩|1\rangle and the target wire ∣y⟩|y\rangle comes out as U∣y⟩\mathbf{U}|y\rangle.

Example 1

import pennylane as qml
from pennylane import numpy as np
 
dev = qml.device('default.qubit', wires=[0,1,2,3,4,5])
 
@qml.qnode(dev)
def circuit():
    # Prepare wires[0,1] = |00> + |11>
    qml.H(wires=[0])
    qml.CNOT(wires=[0,1])
 
    # Prepare wires[2,3] = |00> + i|11>
    qml.H(wires=[2])
    qml.ctrl(qml.Y(wires=[3]), control=[2])
 
    # Compute wires[0,1] AND wires[2,3]
    qml.Toffoli(wires=[0,2,4])
    qml.Toffoli(wires=[1,3,5])
 
    # Expect |000000> + |001100> + i|110000> + i|111111>
    return qml.state()
 
print(circuit())

Exercise 4.1 — Toffoli Gate and Controlled Gates

Answer the following questions.

  1. Design a quantum algorithm emulating an XOR gate using only Toffoli gates.
  2. Design a quantum algorithm emulating a NAND gate using only Toffoli gates.
  3. Design a quantum algorithm emulating a NOR gate using only Toffoli gates.
  4. Why do we have to emulate classical logic gates using the Toffoli gate? What are the properties superior to classical Boolean algebra?
  5. Explain why simulation of a Toffoli gate is inefficient on classical computers.
  6. Design a quantum algorithm that imitates the gate CU\mathbf{CU}, where U\mathbf{U} is a unitary matrix, using only the Toffoli gate and the gate U\mathbf{U}.

Design quantum algorithms that compute the following quantum-logical expressions. Develop PennyLane code to verify them.

  1. ∣11⟩∧∣10⟩|11\rangle\wedge|10\rangle
  2. ∣01⟩∨∣10⟩|01\rangle\vee|10\rangle
  3. ∣010⟩∧∣10⟩|010\rangle\wedge|10\rangle
  4. ∣00⟩+∣11⟩2∧∣10⟩\dfrac{|00\rangle+|11\rangle}{\sqrt2}\wedge|10\rangle
  5. ∣00⟩+∣11⟩2⊕∣01⟩+∣10⟩2\dfrac{|00\rangle+|11\rangle}{\sqrt2}\oplus\dfrac{|01\rangle+|10\rangle}{\sqrt2}
  6. ∣00⟩+∣11⟩2∨∣01⟩\dfrac{|00\rangle+|11\rangle}{\sqrt2}\vee|01\rangle

Generalized Phase Shift Gate

  • We can generalize the phase shift gate so that rotation can be done more freely

P(θ)=[10\0cis⁡θ]\boxed{\mathbf{P}(\theta) = \begin{bmatrix}1&0\0&\operatorname{cis}\theta\end{bmatrix}}

📝 Instructor's annotation: labeled this "State mapping:" in the margin.

  • qml.PhaseShift(angle, wires=…)
  • Some Clifford gates can be redefined in terms of the generalized phase shift gate

Z=P(π)=[10\0−1]S=P(π/2)=[10\0i]\mathbf{Z} = \mathbf{P}(\pi) = \begin{bmatrix}1&0\0&-1\end{bmatrix} \qquad \mathbf{S} = \mathbf{P}(\pi/2) = \begin{bmatrix}1&0\0&i\end{bmatrix}

  • Let's introduce the T gate

T=P(π/4)=[10\0cis⁡π4]\mathbf{T} = \mathbf{P}(\pi/4) = \begin{bmatrix}1&0\0&\operatorname{cis}\frac{\pi}{4}\end{bmatrix}

  • qml.T(wires=…)
  • Note that S=Z1/2\mathbf{S}=\mathbf{Z}^{1/2} and T=Z1/4\mathbf{T}=\mathbf{Z}^{1/4}
  • P(θ)\mathbf{P}(\theta) is not always Hermitian, so we have to incorporate these adjoint gates into the instruction set

\mathbf{S}^{_} = \mathbf{P}(-\pi/2) \qquad \mathbf{T}^{_} = \mathbf{P}(-\pi/4)

  • qml.adjoint(opr)

Generalized Rotation Gates

  • Let M\mathbf{M} be a unitary matrix (i.e. Hermitian \mathbf{M}=\mathbf{M}^{_} and orthonormal \mathbf{M}^{-1}=\mathbf{M}^{_}), where

M=[αγbetaδ]\mathbf{M} = \begin{bmatrix}\alpha&\gamma\\beta&\delta\end{bmatrix}

  • Let's define a generalized rotation gate for a unitary matrix M\mathbf{M} as follows

RM(θ)=cis⁡(−iθ2M)=cos⁡(θ2)I−isin⁡(θ2)M\mathbf{R}_M(\theta) = \operatorname{cis}\left(-\frac{i\theta}{2}\mathbf{M}\right) = \cos\left(\frac{\theta}{2}\right)\mathbf{I} - i\sin\left(\frac{\theta}{2}\right)\mathbf{M}

  • General form:

RM(θ)=[cos⁡θ2−iαsin⁡θ2−iγsin⁡θ2 −iβsin⁡θ2cos⁡θ2−iδsin⁡θ2]\boxed{\mathbf{R}_M(\theta) = \begin{bmatrix}\cos\frac{\theta}{2}-i\alpha\sin\frac{\theta}{2} & -i\gamma\sin\frac{\theta}{2} \ -i\beta\sin\frac{\theta}{2} & \cos\frac{\theta}{2}-i\delta\sin\frac{\theta}{2}\end{bmatrix}}

  • Mapping:

∣0⟩↦(cos⁡θ2−iαsin⁡θ2)∣0⟩−iβsin⁡θ2∣1⟩|0\rangle \mapsto \left(\cos\frac{\theta}{2}-i\alpha\sin\frac{\theta}{2}\right)|0\rangle - i\beta\sin\frac{\theta}{2}|1\rangle ∣1⟩↦−iγsin⁡θ2∣0⟩+(cos⁡θ2−iδsin⁡θ2)∣1⟩|1\rangle \mapsto -i\gamma\sin\frac{\theta}{2}|0\rangle + \left(\cos\frac{\theta}{2}-i\delta\sin\frac{\theta}{2}\right)|1\rangle

📝 Instructor's annotation: at the bottom of the slide, wrote "In most cases, M∈X,Y,ZM\in{X,Y,Z}." — a reassurance that although M\mathbf{M} is defined for any unitary matrix, in practice you'll almost always plug in a Pauli matrix, giving the RX, RY, RZ gates on the next three slides.

X-Rotation Gates

  • X-Rotation gate flips the qubit

RX(θ)=cis⁡(−iθ2X)=[cos⁡θ2−isin⁡θ2\-isin⁡θ2cos⁡θ2]\boxed{\mathbf{RX}(\theta) = \operatorname{cis}\left(-\frac{i\theta}{2}\mathbf{X}\right) = \begin{bmatrix}\cos\frac{\theta}{2}&-i\sin\frac{\theta}{2}\-i\sin\frac{\theta}{2}&\cos\frac{\theta}{2}\end{bmatrix}}

  • Mapping:

∣0⟩↦cos⁡θ2∣0⟩−isin⁡θ2∣1⟩∣1⟩↦−isin⁡θ2∣0⟩+cos⁡θ2∣1⟩|0\rangle \mapsto \cos\frac{\theta}{2}|0\rangle - i\sin\frac{\theta}{2}|1\rangle \qquad |1\rangle \mapsto -i\sin\frac{\theta}{2}|0\rangle + \cos\frac{\theta}{2}|1\rangle

  • qml.RX(angle, wires=…)

📝 Instructor's annotation: wrote at the bottom of the slide: "RX, RY, RZ are used in quantum ML." — flagging these rotation gates as the parametrized building blocks used in variational / quantum machine-learning circuits.

image-5 Caption — Figure 6: X-Rotation gate, shown as a rotation by angle θ\theta around the real axis on the phase circle (Bit 0 at top, Bit 1 at bottom, pos/neg real and pos/neg img axes marked). Figures 7 and 8 (below) show the same style of diagram for Y- and Z-Rotation.

Y-Rotation Gates

  • Y-Rotation gate flips the qubit, the sign, and the amplitude

RY(θ)=cis⁡(−iθ2Y)=[cos⁡θ2−sin⁡θ2sinθ2cos⁡θ2]\boxed{\mathbf{RY}(\theta) = \operatorname{cis}\left(-\frac{i\theta}{2}\mathbf{Y}\right) = \begin{bmatrix}\cos\frac{\theta}{2}&-\sin\frac{\theta}{2}\\sin\frac{\theta}{2}&\cos\frac{\theta}{2}\end{bmatrix}}

  • Mapping:

∣0⟩↦cos⁡θ2∣0⟩+sin⁡θ2∣1⟩∣1⟩↦−sin⁡θ2∣0⟩+cos⁡θ2∣1⟩|0\rangle \mapsto \cos\frac{\theta}{2}|0\rangle + \sin\frac{\theta}{2}|1\rangle \qquad |1\rangle \mapsto -\sin\frac{\theta}{2}|0\rangle + \cos\frac{\theta}{2}|1\rangle

  • qml.RY(angle, wires=…) (Figure 7: Y-Rotation gate — rotation by θ\theta around the imaginary axis)

Z-Rotation Gates

  • Z-Rotation gate flips the sign

RZ(θ)=cis⁡(−iθ2Z)=[cis⁡(−θ2)0\0cis⁡θ2]\boxed{\mathbf{RZ}(\theta) = \operatorname{cis}\left(-\frac{i\theta}{2}\mathbf{Z}\right) = \begin{bmatrix}\operatorname{cis}(-\frac{\theta}{2})&0\0&\operatorname{cis}\frac{\theta}{2}\end{bmatrix}}

  • Mapping:

∣0⟩↦cis⁡(−θ2)∣0⟩∣1⟩↦cis⁡θ2∣1⟩|0\rangle \mapsto \operatorname{cis}\left(-\frac{\theta}{2}\right)|0\rangle \qquad |1\rangle \mapsto \operatorname{cis}\frac{\theta}{2}|1\rangle

💡 Note: the printed slide writes this mapping with cos⁡\cos rather than cis⁡\operatorname{cis} (i.e. "cos⁡(−θ/2)∣0⟩\cos(-\theta/2)|0\rangle" and "cos⁡(θ/2)∣1⟩\cos(\theta/2)|1\rangle"), which doesn't match the diagonal matrix directly above it — the matrix's entries are cis⁡(−θ/2)\operatorname{cis}(-\theta/2) and cis⁡(θ/2)\operatorname{cis}(\theta/2) (complex phases), so the mapping shown here uses cis⁡\operatorname{cis} for consistency with that matrix.

  • qml.RZ(angle, wires=…) (Figure 8: Z-Rotation gate — rotation by θ\theta around the vertical bit-axis)

Universality of Phase Shift and Rotation Gates

  • Any single-qubit operator U\mathbf{U} can be simulated by:

U=RZ(ϕ),RX(θ),RZ(ω),P(λ)\boxed{\mathbf{U} = \mathbf{RZ}(\phi),\mathbf{RX}(\theta),\mathbf{RZ}(\omega),\mathbf{P}(\lambda)}

  • Equivalently, we can also replace RX(θ)\mathbf{RX}(\theta) with RY(θ)\mathbf{RY}(\theta) to flip qubits, signs, and amplitudes
  • Universal quantum gate set: P\mathbf{P}, RX\mathbf{RX}, RY\mathbf{RY}, RZ\mathbf{RZ}, and CNOT

image-6 Caption — Figure 9: Generalized rotation gates, shown as a 5-panel Bloch-sphere decomposition: Start (∣0⟩|0\rangle at top, ∣1⟩|1\rangle at bottom) → Step 1: rotate only ∣1⟩|1\rangle by relative phase λ\lambda → Step 2: rotate ∣0⟩|0\rangle and ∣1⟩|1\rangle together around the Z axis by global phase ω/2\omega/2 → Step 3: rotate ∣0⟩|0\rangle and ∣1⟩|1\rangle together around the X axis by global phase θ/2\theta/2 → Step 4: rotate ∣0⟩|0\rangle and ∣1⟩|1\rangle together around the Z axis by global phase ϕ/2\phi/2.

💡 Analogy: This is the Chapter 2 "3-step universal rotation" (ω→θ/2→ϕ\omega\to\theta/2\to\phi) with one more knob added at the very front: an extra relative-phase twist λ\lambda (via P(λ)\mathbf{P}(\lambda)) before the rotation sequence even begins — together the four steps can reach any single-qubit unitary.

Exercise 4.2 — Generalized Phase Shift and Rotation Gates

Compute quantum operators P\mathbf{P}, RX\mathbf{RX}, RY\mathbf{RY}, and RZ\mathbf{RZ} of the following phases.

  1. 00
  2. π/6\pi/6
  3. π/4\pi/4
  4. π/3\pi/3
  5. π/2\pi/2
  6. 2π/32\pi/3
  7. 3π/43\pi/4
  8. 5π/65\pi/6
  9. π\pi
  10. 2π2\pi

Design quantum algorithms that prepare the following states using generalized phase shift and rotation gates. Develop PennyLane code to verify each of them.

  1. ∣0⟩|0\rangle
  2. ∣1⟩|1\rangle
  3. −∣1⟩-|1\rangle
  4. ∣+⟩|+\rangle
  5. ∣−⟩|-\rangle
  6. cis⁡π4∣0⟩+cis⁡π8∣1⟩\operatorname{cis}\frac{\pi}{4}|0\rangle+\operatorname{cis}\frac{\pi}{8}|1\rangle
  7. cis⁡π2∣0⟩−cis⁡π2∣1⟩\operatorname{cis}\frac{\pi}{2}|0\rangle-\operatorname{cis}\frac{\pi}{2}|1\rangle
  8. cis⁡π3∣0⟩+∣1⟩\operatorname{cis}\frac{\pi}{3}|0\rangle+|1\rangle

Uniform State Preparation (Shukla and Vedula, 2024)

⏭️ Marked "SKIPPED" by the instructor — this section's title slide has a large red "SKIPPED" stamped across it by hand, meaning it was not covered in lecture. It's transcribed below in full from the slides for completeness, but treat it as reference material rather than examined content unless your instructor says otherwise.

  • We can prepare a quantum state that corresponds to our problem's input domain
  • Property: We can create a superposition of two basis states ∣0⟩|0\rangle and ∣1⟩|1\rangle by rotating ∣q⟩|q\rangle around Y with phase θ\theta:

θ=2arcsin⁡P1\theta = 2\arcsin\sqrt{P_1}

where $P_1 = P(\psi_1|q)$ is the probability of $q$ collapsing to 1
  • There is a recursive algorithm that constructs a superposition of equal probability where the number of states is NOT 2N2^N, where NN is the number of qubits

  • Algorithm: Prep(i,S=ψ1,…,ψK,c)\text{Prep}(i, S={\psi_1,\dots,\psi_K}, c), where ii is the index of qubits, each bitstring ψk\psi_k is of length LL, and cc is a control qubit

    1. If K=1K=1, we apply qi′={CIc,qi∣qi⟩ψK[i]=0 CNOTc,qi∣qi⟩ψK[i]=1q_i' = \begin{cases}\mathbf{CI}_{c,q_i}|q_i\rangle & \psi_K[i]=0 \ \text{CNOT}_{c,q_i}|q_i\rangle & \psi_K[i]=1\end{cases} and terminate here.
    2. If K=2L−i+1K=2^{L-i+1} then we apply the controlled Hadamard gates to each qubit ∣q′⟩={H⊗L∣q⟩i=1 ∏k=1KCHc,qk∣q⟩⊗Li>1|q'\rangle = \begin{cases}\mathbf{H}^{\otimes L}|q\rangle & i=1 \ \prod_{k=1}^{K}\mathbf{CH}_{c,q_k}|q\rangle^{\otimes L} & i>1\end{cases} and terminate here.
    3. Compute the phase for rotation with RY\mathbf{RY} θ=2arcsin⁡P1\theta = 2\arcsin\sqrt{P_1} where P_1 = #_k(\psi_k[i]=1)/K is the ratio of the ii-th bit being 1 w.r.t. the set SS
    4. We put rotation on the ii-th qubit. qi′={RY(θ)∣qi⟩i=1 CRY(θ)c,i∣qi′⟩i>1q_i' = \begin{cases}\mathbf{RY}(\theta)|q_i\rangle & i=1 \ \mathbf{CRY}(\theta)_{c,i}|q_i'\rangle & i>1\end{cases}
    5. If i=Li=L, we terminate here.
    6. Otherwise, we will now split the set of bitstrings into two parts S0=ψk∣ψk[i]=0S1=ψk∣ψk[i]=1S_0={\psi_k\mid\psi_k[i]=0} \qquad S_1={\psi_k\mid\psi_k[i]=1}
    7. For S0S_0, we frame qiq_i with NOT gates and prepare the state recursively with S0S_0 qi′′=X,Prep(i+1,S0,qi),X,∣qi′⟩q_i'' = \mathbf{X},\text{Prep}(i+1,S_0,q_i),\mathbf{X},|q_i'\rangle
    8. For S1S_1, we introduce an ancilla qubit aia_i and conditionally rotate it with CRY\mathbf{CRY} ∣ai⟩=CRY(θ)qi,ai∣0⟩|a_i\rangle = \mathbf{CRY}(\theta)_{q_i,a_i}|0\rangle
    9. We frame it with CX gates, and prepare the state recursively with S1S_1 qi′′′=CXqi,ai,Prep(i+1,S1,ai),CXqi,ai∣qi′′⟩q_i''' = \mathbf{CX}_{q_i,a_i},\text{Prep}(i+1,S_1,a_i),\mathbf{CX}_{q_i,a_i}|q_i''\rangle

image-7 Caption — Figure 10: Uniform state preparation at step ii, "Prep(S,i)\text{Prep}(S,i) conditioned on cc" — a recursive circuit block: qubit qiq_i gets an RY(θ)\mathbf{RY}(\theta), then is framed by X gates around two nested sub-blocks labeled "Prep(S0,i+1)(S_0,i{+}1) cond qiq_i" and "Prep(S1,i+1)(S_1,i{+}1) cond aia_i," with an ancilla qubit aia_i getting its own RY(θ)\mathbf{RY}(\theta) and X-framing to feed the second sub-block.

Example 2 — Uniform State Preparation

  • Example: Let's create a superposition for S=000,001,010,011,110,101S = {000,001,010,011,110,101}
  • We measure the probability of q1q_1 collapsing to 1 P1=2/6P_1 = 2/6
  • We therefore obtain the phase θ1=2arcsin⁡2/6=π/5\theta_1 = 2\arcsin\sqrt{2/6} = \pi/5
  • Rotate 1: ∣q1⟩=RY(π/5)∣0⟩|q_1\rangle = \mathbf{RY}(\pi/5)|0\rangle
  • We distinguish SS into S0=010,001,010,011S1=110,101S_0 = {010,001,010,011} \qquad S_1={110,101}
  • Frame-in 1/0: ∣q1⟩=X∣q1⟩|q_1\rangle = \mathbf{X}|q_1\rangle
  • For S0S_0, we observe that four possibilities correspond to 23−2+1=42^{3-2+1}=4, resulting in applying the controlled H on q2,q3q_2,q_3
  • Operation: ∣q2,q3⟩=CHq1,q2CHq1,q3∣q2,q3⟩|q_2,q_3\rangle = \mathbf{CH}_{q_1,q_2}\mathbf{CH}_{q_1,q_3}|q_2,q_3\rangle
  • Frame-out 1/0: ∣q1⟩=X∣q1⟩|q_1\rangle = \mathbf{X}|q_1\rangle
  • Ancilla 1: ∣a1⟩=CRY(π/2)q1,a1∣0⟩|a_1\rangle = \mathbf{CRY}(\pi/2)_{q_1,a_1}|0\rangle
  • Frame-in 1/1: ∣a1⟩=CXq1,a1|a_1\rangle = \mathbf{CX}_{q_1,a_1}
  • For S1S_1, the two possibilities have to be recursively converted P1=1/2P_1 = 1/2 resulting in the phase θ1=2arcsin⁡1/2=π/2\theta_1 = 2\arcsin\sqrt{1/2} = \pi/2
  • We distinguish SS into S0=101S1=110S_0 = {101} \qquad S_1={110}
  • Frame-in 2/0: ∣a1⟩=NOT∣a1⟩|a_1\rangle = \text{NOT}|a_1\rangle
  • For S0S_0, we observe that ∣q3⟩=∣1⟩|q_3\rangle=|1\rangle
  • Operation: ∣q3⟩=CNOTa1,q3∣q3⟩|q_3\rangle = \text{CNOT}_{a_1,q_3}|q_3\rangle
  • Frame-out 2/0: ∣a1⟩=NOT∣a1⟩|a_1\rangle = \text{NOT}|a_1\rangle
  • Ancilla 2: ∣a2⟩=CRY(π/2)a1,a2∣0⟩|a_2\rangle = \mathbf{CRY}(\pi/2)_{a_1,a_2}|0\rangle
  • Frame-in 2/1: ∣a2⟩=CNOTa1,a2∣a2⟩|a_2\rangle = \text{CNOT}_{a_1,a_2}|a_2\rangle
  • For S1S_1, we observe that ∣q3⟩=∣0⟩|q_3\rangle=|0\rangle
  • Operation: ∣q3⟩=CI∣a2,q3⟩∣q3⟩|q_3\rangle = \mathbf{CI}|a_2,q_3\rangle|q_3\rangle
  • Frame-in 2/1: ∣a2⟩=CNOTa1,a2∣a2⟩|a_2\rangle = \text{CNOT}_{a_1,a_2}|a_2\rangle
  • Frame-out 1/1: ∣a1⟩=CXq1,a1|a_1\rangle = \mathbf{CX}_{q_1,a_1}

image-8 Caption — Figure 11: Example of uniform state preparation for S=000,001,010,011,101,110S={000,001,010,011,101,110}. Pink dashed boxes denote a recursive conversion, while green boxes represent a termination case; three qubits results in a recursion depth of three. The full circuit nests Prep(000,001,010,011,2,q1)\text{Prep}({000,001,010,011},2,q_1) and Prep(101,110,2,a1)\text{Prep}({101,110},2,a_1) inside the outer Prep(000,001,010,011,101,110,1,None)\text{Prep}({000,001,010,011,101,110},1,\text{None}), with Prep(101,3,q2)\text{Prep}({101},3,q_2) and Prep(110,3,a2)\text{Prep}({110},3,a_2) nested one level deeper still.

Exercise 4.3 — Uniform State Preparation

Prepare a uniform quantum state from each of the following bitstring sets.

  1. 00,01,10,11{00,01,10,11}
  2. 01,10,11{01,10,11}
  3. 000,100,101,110,111{000,100,101,110,111}
  4. 000,001,110,111{000,001,110,111}
  5. 000,001,010,100,110,111{000,001,010,100,110,111}
  6. 0000,0010,0100,1001,1010,1100{0000,0010,0100,1001,1010,1100}
  7. 0000,0101,1000,1010,1011,1100{0000,0101,1000,1010,1011,1100}
  8. 0000,0011,0101,1000,1001,1010{0000,0011,0101,1000,1001,1010}

3. Measurement

State Measurement

  • Observing the final quantum state of a quantum algorithm
  • Type 1: Analytical solution
  • Type 2: Projective measurement
  • Type 3: State expectation
  • Type 4: Variance
  • Type 5: Simulation

image-9 Caption — Figure 12: Projective measurement. A "wave superposition" splits at each stage into a projection onto u1,u2,u3,…u_1,u_2,u_3,\dots (sent to a Detector) and a rejection (carried forward to the next projection), illustrated as a cascade of triangular prisms.

Type 1: Analytical Solution

  • To observe the final state theoretically by emulating a quantum algorithm
  • PennyLane: qml.state()
  • In reality, measured quantum states may deviate from the analytical solution due to quantum decoherence (i.e. noise)
  • Any quantum algorithm has to be evaluated by
    • Computing the analytical solution; this is the expected output
    • Running on an actual quantum computer to measure noise sensitivity of the algorithm

(Figure 13: a photograph of a physical quantum computer.)

Example 3 (Analytical Solution)

import pennylane as qml
from pennylane import numpy as np
 
dev = qml.device('default.qubit', wires=[0, 1])
 
@qml.qnode(dev)
def circuit(theta: float):
 
   qml.RY(theta, wires=[0])
   qml.CNOT(wires=[0,1])
   qml.RZ(theta, wires=[1])
 
   # Analytical solution
   return qml.state()
 
print(circuit(np.pi / 3))

Expected output: [3−3i4003+i4]⊤\begin{bmatrix}\dfrac{3-\sqrt3 i}{4} & 0 & 0 & \dfrac{\sqrt3+i}{4}\end{bmatrix}^{\top}

[0.75-0.433j    0.    0.      0.433+0.25j]

Type 2: Projective Measurement

  • To observe a quantum state with laser-beam projection
  • We can measure a qubit ∣ψ⟩=a∣0⟩+b∣1⟩|\psi\rangle=a|0\rangle+b|1\rangle by collapsing it, i.e. projecting it on a set of unitary vectors O=∣u1⟩,…,∣uN⟩\mathbf{O}={|u_1\rangle,\dots,|u_N\rangle}

proj⁡∣uk⟩∣ψ⟩=⟨ψ∣uk⟩⟨uk∣uk⟩∣uk⟩ =(∣ψ⟩⊤∣uk⟩)∣uk⟩ =(acˉk+bdˉk)∣uk⟩\begin{aligned} \operatorname{proj}_{|u_k\rangle}|\psi\rangle &= \frac{\langle\psi|u_k\rangle}{\langle u_k|u_k\rangle}|u_k\rangle \ &= \left(|\psi\rangle^{\top}|u_k\rangle\right)|u_k\rangle \ &= \left(a\bar c_k+b\bar d_k\right)|u_k\rangle \end{aligned}

where each $|u_k\rangle = c_k|0\rangle+d_k|1\rangle$ is called an **observable**
  • In measurement, the qubit is destroyed; i.e. measurement is irreversible

(Figure 14: the same projective-measurement idea drawn on a Bloch-sphere-like ellipse, with ∣ψ⟩|\psi\rangle projected onto an observable ∣uk⟩|u_k\rangle.)

Information Extraction

  • When decomposed into observables ∣uk⟩=ck∣0⟩+dk∣1⟩|u_k\rangle=c_k|0\rangle+d_k|1\rangle, we can measure the probability for each of them as follows

P(uk∣ψ)=∣acˉk+bdˉk∣2P(u_k|\psi) = |a\bar c_k+b\bar d_k|^2

(Figure 15: the measurement-gate symbol — a small box with a meter icon on a wire.)

  • Information extraction: We collapse a qubit on pure states U=∣0⟩,∣1⟩\mathbf{U}={|0\rangle,|1\rangle}

P(0∣ψ)=∣a∣2P(1∣ψ)=∣b∣2\boxed{P(0|\psi) = |a|^2} \qquad \boxed{P(1|\psi) = |b|^2}

This is a distribution of binary values
  • PennyLane: qml.probs()

(Figure 16: the measurement-gate symbol with a control-style output line.)

📝 Instructor's annotation: next to Figure 16, wrote "binary output is used as a control" — flagging that the classical bit this measurement produces can feed straight into a conditional gate (the subject of the Dynamic Circuit section below).

Example 3 (Projective Measurement)

import pennylane as qml
from pennylane import numpy as np
 
dev = qml.device('default.qubit', wires=[0, 1])
 
@qml.qnode(dev)
def circuit(theta: float):
 
   qml.RY(theta, wires=[0])
   qml.CNOT(wires=[0,1])
   qml.RZ(theta, wires=[1])
 
   # Projective measurement
   return qml.probs()
 
print(circuit(np.pi / 3))

Expected output: [0.75000.25]⊤\begin{bmatrix}0.75 & 0 & 0 & 0.25\end{bmatrix}^{\top}

[0.75    0.    0.    0.25]

Information Extraction via Quantum Operator

  • From Chapter 1, any quantum operator U\mathbf{U} can be eigen-decomposed into

U=∑k=1Nλk∣uk⟩⟨uk∣\mathbf{U} = \sum_{k=1}^{N}\lambda_k|u_k\rangle\langle u_k|

where $(\lambda_k,|u_k\rangle)_{k=1}^{N} = \operatorname{eigen}(\mathbf{U})$
  • Pauli-Z Gate

Z=[10\0−1]\mathbf{Z} = \begin{bmatrix}1&0\0&-1\end{bmatrix}

Its eigen-pairs are $(1,|0\rangle)$ and $(-1,|1\rangle)$
  • ∣0⟩|0\rangle and ∣1⟩|1\rangle are called Z-bases (annotated: "computational base")

  • Pauli-X Gate

X=[01\10]\mathbf{X} = \begin{bmatrix}0&1\1&0\end{bmatrix}

Its eigen-pairs are $(1,|+\rangle)$ and $(-1,|-\rangle)$
  • ∣+⟩|+\rangle and ∣−⟩|-\rangle are called X-bases (annotated: "superposition bases")

  • Pauli-Y Gate

Y=[0−i\i0]\mathbf{Y} = \begin{bmatrix}0&-i\i&0\end{bmatrix}

Its eigen-pairs are $(1,|{+i}\rangle)$ and $(-1,|{-i}\rangle)$
  • ∣+i⟩|{+i}\rangle and ∣−i⟩|{-i}\rangle are called Y-bases (annotated: "circular bases")

📝 Instructor's annotation: under each of the three "…-bases" labels, wrote the matching earlier name for that basis — "computational base" under Z-bases, "superposition bases" under X-bases, and "circular bases" under Y-bases — tying this terminology back to Chapter 2/3's naming.

Type 3: Multi-Shot Measurement with Expectation

  • Since quantum computing is probabilistic, we sometimes repeat an experiment to obtain the distribution of outcomes
  • The expectation of quantum operator U\mathbf{U} on a state ∣ψ⟩|\psi\rangle is

⟨U(ψ)⟩=⟨ψ∣U∣ψ⟩=∑k=1Kλk∣⟨uk∣ψ⟩∣2\boxed{\left\langle \mathbf{U}^{(\psi)}\right\rangle = \langle\psi|\mathbf{U}|\psi\rangle = \sum_{k=1}^{K}\lambda_k|\langle u_k|\psi\rangle|^2}

where $(\lambda_k,|u_k\rangle)$ is the $k$-th eigen-pair of $\mathbf{U}$
  • In actual experiments, each state is a binary string sampled from ∣ψ⟩|\psi\rangle
  • The expectation is approximated by

⟨U(ψ)⟩=Es∼∣ψ⟩[∑k=1Kλk∣⟨uk∣sj⟩∣2]≈1N∑j=1N∑k=1Kλk∣⟨uk∣sj⟩∣2\left\langle \mathbf{U}^{(\psi)}\right\rangle = \mathbb{E}_{s\sim|\psi\rangle}\left[\sum_{k=1}^{K}\lambda_k\left|\langle u_k|s_j\rangle\right|^2\right] \approx \frac{1}{N}\sum_{j=1}^{N}\sum_{k=1}^{K}\lambda_k\left|\langle u_k|s_j\rangle\right|^2

where $N$ is an adequately large number

(Figure 17: the expectation-measurement gate symbol, labeled ⟨U⟩\langle\mathbf{U}\rangle.)

Expectation on Pauli Projectors

  • Suppose ∣ψ⟩=a∣0⟩+b∣1⟩|\psi\rangle=a|0\rangle+b|1\rangle
  • Pauli-Z Projector: Positive value implies more chance of pointing to the ∣0⟩|0\rangle hemisphere

⟨Z(ψ)⟩=1⟨0∣ψ⟩−1⟨1∣ψ⟩=∣a∣2−∣b∣2\langle\mathbf{Z}^{(\psi)}\rangle = 1\langle0|\psi\rangle - 1\langle1|\psi\rangle = |a|^2-|b|^2

  • Pauli-X Projector: Positive value implies more chance of pointing to the positive real hemisphere

⟨X(ψ)⟩=1⟨+∣ψ⟩−1⟨−∣ψ⟩=∣a+b∣2−∣a−b∣2\langle\mathbf{X}^{(\psi)}\rangle = 1\langle{+}|\psi\rangle - 1\langle{-}|\psi\rangle = |a+b|^2-|a-b|^2

  • Pauli-Y Projector: Positive value implies more chance of pointing to the positive imaginary hemisphere

⟨Y(ψ)⟩=1⟨+i∣ψ⟩−1⟨−i∣ψ⟩=∣a+bi∣2−∣a−bi∣2\langle\mathbf{Y}^{(\psi)}\rangle = 1\langle{+i}|\psi\rangle - 1\langle{-i}|\psi\rangle = |a+bi|^2-|a-bi|^2

Table 3: Implication of expectation — projection on each axis

GatesPositiveNegative
Pauli-Z$0\rangle$
Pauli-XPos. realNeg. real
Pauli-YPos. imgNeg. img

Joint Expectation as Non-Destructive Projection

  • Projection onto a single axis is irreversible, because it destroys, or decoheres, the quantum state throughout the complex vector space
  • However, joint projection on multiple axes is reversible, because it makes a projection onto a subspace; therefore it does not decohere the quantum state
  • For example, there are four possible axes for a two-qubit system: ∣00⟩,∣01⟩,∣10⟩,∣11⟩|00\rangle,|01\rangle,|10\rangle,|11\rangle, and we choose to project a quantum state onto only two eigen-states ∣00⟩|00\rangle and ∣11⟩|11\rangle
  • Joint projection on two states ∣ψ1⟩|\psi_1\rangle on projector U1\mathbf{U}_1 and ∣ψ2⟩|\psi_2\rangle on U2\mathbf{U}_2 is

P(ψ1,ψ2∣U1,U2)=⟨U1(ψ1)⟩⟨U2(ψ2)⟩P(\psi_1,\psi_2|\mathbf{U}_1,\mathbf{U}_2) = \left\langle\mathbf{U}_1^{(\psi_1)}\right\rangle\left\langle\mathbf{U}_2^{(\psi_2)}\right\rangle

  • We can compute the joint probability, a.k.a. joint projection, for residing in each positive side

P(ψ1,…,ψN∣U1,…,UN)=∏k=1N⟨Uk(ψk)⟩\boxed{P(\psi_1,\dots,\psi_N|\mathbf{U}_1,\dots,\mathbf{U}_N) = \prod_{k=1}^{N}\left\langle\mathbf{U}_k^{(\psi_k)}\right\rangle}

where each state $|\psi_k\rangle$ is measured on the quantum operator $\mathbf{U}_k$

Mixing and Matching of Expectations

  • PennyLane:
    • qml.expval(op=qml.Z(wires=…))
    • qml.expval(op=qml.X(wires=…))
    • qml.expval(op=qml.Y(wires=…))
  • PennyLane (a product of several operators on possibly different wires):
return qml.expval(
    qml.Op1(wires=...)
    @ qml.Op2(wires=...)
    @ qml.Op3(wires=...)
    ...
    @ qml.OpN(wires=...)
)
where each `qml.Op`$_k$ is one of the projectors X, Y, and Z
  • Linear combination of projectors:
return qml.expval(
    0.5 * qml.X(wires=[0])
    + 0.1 * qml.Y(wires=[1])
    + 0.4 * qml.Z(wires=[2])
)
Also: `qml.ops.LinearCombination(coeffs, projectors)`
  • Returning several expectations is also possible in PennyLane:
return (
    qml.expval(qml.Op1(wires=...)),
    qml.expval(qml.Op2(wires=...)),
    qml.expval(qml.Op3(wires=...)),
    ...
)

Type 4: Variance

  • In multi-shot measurement, it is useful to measure the dispersion in an outcome distribution
  • The variance of quantum operator U\mathbf{U} on a state ∣ψ⟩|\psi\rangle is

Var⁡[U(ψ)]=⟨(U2)(ψ)⟩−⟨U(ψ)⟩2=⟨ψ∣U2∣ψ⟩−⟨ψ∣U∣ψ⟩2\boxed{\operatorname{Var}\left[\mathbf{U}^{(\psi)}\right] = \left\langle(\mathbf{U}^2)^{(\psi)}\right\rangle - \left\langle\mathbf{U}^{(\psi)}\right\rangle^2 = \langle\psi|\mathbf{U}^2|\psi\rangle - \langle\psi|\mathbf{U}|\psi\rangle^2}

  • PennyLane:
    • qml.var(op=qml.Z(wires=…))
    • qml.var(op=qml.X(wires=…))
    • qml.var(op=qml.Y(wires=…))
  • With the expectation and variance on the same quantum operator U\mathbf{U}, we can altogether compare any two given qubits by addressing two questions:
    • Which one goes to which extreme?
    • Which one has a more consistent or volatile distribution?
  • When measuring a qubit with several quantum operators, the one with the lowest variance is the most consistent

Example 3 (Expectation & Variance)

import pennylane as qml
from pennylane import numpy as np
 
dev = qml.device('default.qubit', wires=[0, 1])
 
@qml.qnode(dev)
def circuit(theta: float):
 
    qml.RY(theta, wires=[0])
    qml.CNOT(wires=[0,1])
    qml.RZ(theta, wires=[1])
 
    # Expectation of each wire
    return ( qml.expval(qml.Z(wires=[0])), qml.expval(qml.Z(wires=[1])),
             qml.var(qml.Z(wires=[0])), qml.var(qml.Z(wires=[1])) )
 
print(circuit(np.pi / 3))

Expected output: (0.5, 0.5, 0.75, 0.75)

(0.5, 0.5, 0.75, 0.75)

Type 5: Simulation

  • Simulation is based on sampling mixed states from their probability distribution
  • For a superposition ∣ψ⟩=α1∣ψ1⟩+⋯+αK∣ψK⟩|\psi\rangle = \alpha_1|\psi_1\rangle+\dots+\alpha_K|\psi_K\rangle, the probability distribution for each mixed state ∣ψk⟩|\psi_k\rangle is

P(ψk∣ψ)=∣αk∣2P(\psi_k|\psi) = |\alpha_k|^2

  • We can then sample a mixed state by ψ(j)∼P(⋅∣ψ)\psi^{(j)}\sim P(\cdot|\psi)

  • Suppose we have two superpositions ∣ψ⟩=α1∣ψ1⟩+⋯+αK∣ψK⟩|\psi\rangle=\alpha_1|\psi_1\rangle+\dots+\alpha_K|\psi_K\rangle and ∣ϕ⟩=β1∣ϕ1⟩+⋯+βM∣ϕM⟩|\phi\rangle=\beta_1|\phi_1\rangle+\dots+\beta_M|\phi_M\rangle. The probability distribution of their tensor product ∣ψ⟩⊗∣ϕ⟩|\psi\rangle\otimes|\phi\rangle is

P(ψjϕk∣ψ,ϕ)=∣αjβk∣2P(\psi_j\phi_k|\psi,\phi) = |\alpha_j\beta_k|^2

  • We can sample a tensor product by ψjϕk∼P(⋅∣ψ,ϕ)\psi_j\phi_k \sim P(\cdot|\psi,\phi)

(Figure 18: an illustrative photo of a die, captioned "Quantum dice roll.")

Simulation Methods

  • By specifying the option shots in the instruction qml.device, we enable the simulation mode
  • Two simulation methods: step-by-step and end-to-end
  • Step-by-step sampling: draw a set of NN bitstrings from the probability distribution repeatedly
    • shots = the number of shots for step-by-step sampling
    • PennyLane: qml.sample()
    • Output: a list of qubit sequences
  • End-to-end simulation: draw NN bitstrings from the probability distribution only once
    • shots = the number of shots for step-by-step sampling
    • PennyLane: qml.counts()
    • Output: a frequency table for each possible bitstring

Example 3 (Sampling)

import pennylane as qml
from pennylane import numpy as np
 
# Step-by-step simulation by 10-shots sampling
dev = qml.device('default.qubit', wires=[0, 1], shots=10)   # shots = number of samples
 
@qml.qnode(dev)
def circuit(theta: float):
 
   qml.RY(theta, wires=[0])
   qml.CNOT(wires=[0,1])
   qml.RZ(theta, wires=[1])
 
   # Sampling
   return qml.sample()
 
print(circuit(np.pi / 3))

Result ∼ 75% of ∣00⟩|00\rangle and ∼ 25% of ∣11⟩|11\rangle:

[ [0 0], [0 0], [0 0], [0 0], [1 1] ]

Example 3 (End-to-End Simulation)

import pennylane as qml
from pennylane import numpy as np
 
# End-to-end simulation with 1000 shots
dev = qml.device('default.qubit', wires=[0, 1])   # shots = number of samples
 
@qml.qnode(dev, shots=1000)
def circuit(theta: float):
 
   qml.RY(theta, wires=[0])
   qml.CNOT(wires=[0,1])
   qml.RZ(theta, wires=[1])
 
   # Simulation
   return qml.counts()
 
print(circuit(np.pi / 3))

Result ∼ 75% of ∣00⟩|00\rangle and ∼ 25% of ∣11⟩|11\rangle:

{'00': 738, '11': 262}

Density Matrix

📝 Instructor's annotation: wrote "(noisy state)" directly under the "Density Matrix" title.

  • Density matrix: a unified form of pure states and mixed states
  • A density matrix for ∣ψ⟩=a∣0⟩+b∣1⟩|\psi\rangle=a|0\rangle+b|1\rangle is

R(ψ)=[∣a∣2abˉ aˉb∣b∣2]\boxed{\mathbf{R}^{(\psi)} = \begin{bmatrix}|a|^2 & a\bar b \ \bar a b & |b|^2\end{bmatrix}}

- The diagonal entries are the probability of $|0\rangle$ and $|1\rangle$, respectively
- The off-diagonal entries are phase coherence
  • Trace: sum of the diagonal line

tr⁡(A)=∑k=1NAk,k\operatorname{tr}(\mathbf{A}) = \sum_{k=1}^{N}A_{k,k}

  • Property: Quantum state ∣ψ⟩|\psi\rangle is pure if and only if tr⁡([R(ψ)]2)=1\operatorname{tr}\left(\left[\mathbf{R}^{(\psi)}\right]^2\right)=1
  • Expectation of quantum operator U\mathbf{U} on a state ∣ψ⟩|\psi\rangle can be rewritten as

⟨U(ψ)⟩=tr⁡(R(ψ)U)\left\langle\mathbf{U}^{(\psi)}\right\rangle = \operatorname{tr}\left(\mathbf{R}^{(\psi)}\mathbf{U}\right)

  • Example: ⟨Zψ⟩=tr⁡(R(ψ)Z) =tr⁡([∣a∣2abˉbarab∣b∣2][10\0−1]) =∣a∣2−∣b∣2\begin{aligned} \left\langle\mathbf{Z}^{\psi}\right\rangle &= \operatorname{tr}\left(\mathbf{R}^{(\psi)}\mathbf{Z}\right) \ &= \operatorname{tr}\left(\begin{bmatrix}|a|^2&a\bar b\\bar a b&|b|^2\end{bmatrix}\begin{bmatrix}1&0\0&-1\end{bmatrix}\right) \ &= |a|^2-|b|^2 \end{aligned}

Reconstructing a Pure State from a Density Matrix

  1. With the expectations of all Pauli projectors, we can reconstruct a qubit ∣ψ⟩|\psi\rangle in terms of a density matrix

R=12(I+⟨X⟩X+⟨Y⟩Y+⟨Z⟩Z)=12[1+⟨Z⟩⟨X⟩−i⟨Y⟩ ⟨X⟩+i⟨Y⟩1−⟨Z⟩]\mathbf{R} = \frac{1}{2}\left(\mathbf{I}+\langle X\rangle\mathbf{X}+\langle Y\rangle\mathbf{Y}+\langle Z\rangle\mathbf{Z}\right) = \frac{1}{2}\begin{bmatrix}1+\langle Z\rangle & \langle X\rangle-i\langle Y\rangle \ \langle X\rangle+i\langle Y\rangle & 1-\langle Z\rangle\end{bmatrix}

  1. Check if tr⁡(R2)=1\operatorname{tr}(\mathbf{R}^2)=1. If so, the qubit will be pure and we can proceed.
    1. We eigen-decompose R\mathbf{R} into eigen-pairs (λk,∣ψk⟩)k=1N=eigen⁡(R)(\lambda_k,|\psi_k\rangle)_{k=1}^{N} = \operatorname{eigen}(\mathbf{R})
    2. If any λk=1\lambda_k=1, its eigenvector ∣ψk⟩|\psi_k\rangle becomes a state vector of R\mathbf{R}.
  2. Otherwise, it is a mixed state and the density matrix is already the best description for this qubit.

Exercise 4.4 — Measurement and Density Matrix

Convert the following qubits into equivalent density matrices.

  1. ∣0⟩|0\rangle
  2. ∣1⟩|1\rangle
  3. i∣0⟩i|0\rangle
  4. −∣1⟩-|1\rangle
  5. 12(∣0⟩+∣1⟩)\frac{1}{\sqrt2}(|0\rangle+|1\rangle)
  6. 15(3∣0⟩+4∣1⟩)\frac{1}{5}(3|0\rangle+4|1\rangle)
  7. 12(∣0⟩+i∣1⟩)\frac{1}{\sqrt2}(|0\rangle+i|1\rangle)
  8. 15(−i∣0⟩+2∣1⟩)\frac{1}{\sqrt5}(-i|0\rangle+2|1\rangle)
  9. 12(cis⁡π4∣0⟩+cis⁡π3∣1⟩)\frac{1}{\sqrt2}\left(\operatorname{cis}\frac{\pi}{4}|0\rangle+\operatorname{cis}\frac{\pi}{3}|1\rangle\right)
  10. 45cis⁡2π3∣0⟩+35cis⁡π3∣1⟩\frac{4}{5}\operatorname{cis}\frac{2\pi}{3}|0\rangle+\frac{3}{5}\operatorname{cis}\frac{\pi}{3}|1\rangle

Compute the projective measurements ⟨X⟩\langle X\rangle, ⟨Y⟩\langle Y\rangle, and ⟨Z⟩\langle Z\rangle from these density matrices, and check if they are pure states or mixed states.

  1. [10\01]\begin{bmatrix}1&0\0&1\end{bmatrix}
  2. [10\0−1]\begin{bmatrix}1&0\0&-1\end{bmatrix}
  3. 12[11\11]\frac{1}{\sqrt2}\begin{bmatrix}1&1\1&1\end{bmatrix}
  4. 12[1i\-i1]\frac{1}{\sqrt2}\begin{bmatrix}1&i\-i&1\end{bmatrix}
  5. 1Z[1.51−i\1+i0.5]\frac{1}{Z}\begin{bmatrix}1.5&1-i\1+i&0.5\end{bmatrix}, where ZZ is a normalizing factor

4. Dynamic Circuit

Mid-Circuit Measurement

  • We can introduce if-clauses and loops into a quantum algorithm, which means the quantum circuit dynamically changes w.r.t. the current state
  • Mid-circuit measurement (MCM): A quantum state is measured in the middle of the algorithm, and optionally it can be reset
  • Output: classical bit 0,1{0,1}
  • MCM can be done by taking a quick sneak peek of a qubit nondestructively, and estimating the actual quantum state — the result may be noisy
  • MCM is used in combination with the if-clause to form a conditional statement or a loop

Table 4: Mid-circuit measurement

InstructionDescription
qml.measure(wires=…)Measure the classical bit of the specified wire.
qml.measure(wires=…, reset=True)Measure the classical bit of the specified wire and reset that wire to $
qml.measure(wires=…, postselect=n)Measure the classical bit of the specified wire. If it is NOT equal to the postselected value, terminate this circuit run.

Conditional Operator

  • We can perform quantum operators conditioned on the outcome of the measurement
  • PennyLane: qml.cond(bool_expr, opr)(params)
  • If the Boolean expression is true, then the operator is applied on the parameters
  • We can form a complex Boolean expression using & (and), | (or), and not (negation)
InstructionDescription
qml.cond(bool, true_fn=…)(params)If the condition is true, true_fn is called with the specified parameters.
qml.cond(bool, true_fn=…, false_fn=…)(params)If the condition is true, true_fn is called with the specified parameters. Otherwise, false_fn is called with the same parameters.
qml.cond(bool, true_fn=…, false_fn=…, elifs=…)(params)The option elifs is a list of (cond, opr), where each pair is a condition and its operation in the else-if part.

Condition Operator

  • We can emulate the if-clause in quantum computing by treating the condition as a classical control bit

image-10 Caption — Figure 19: Quantum algorithm with mid-circuit measurement and conditional operators. Qubit 1 gets H\mathbf{H} then is measured to classical bit m0m_0; wire 2 gets RY(θ)\mathbf{RY}(\theta) if m0=0m_0=0 or RX(θ)\mathbf{RX}(\theta) if m0=1m_0=1, and is then itself measured to m1m_1; wire 3 gets RX(θ)\mathbf{RX}(\theta) if m1=1m_1=1 or RZ(θ)\mathbf{RZ}(\theta) if m1=0m_1=0.

Example 4 — Conditional Operator

dev = qml.device('default.qubit', wires=[0, 1, 2])
 
@qml.qnode(dev, shots=500)
def circuit(theta: float):
    qml.Hadamard(wires=[0])         # wire-0 is |+>
 
   m0 = qml.measure(wires=[0])      # m0 is either 0 or 1
   qml.cond(m0 == 0,                # If m0 == 0:
       true_fn=qml.RY,              #     Y-rotate wire-1 by theta
       false_fn=qml.RX              # Else: X-rotate wire-1 by theta
   )(theta, wires=[1])              # (Common parameters)
 
   m1 = qml.measure(wires=[1])      # m1 is either 0 or 1
   qml.cond(m1 == 1,                # If m1 == 1
       true_fn=qml.RX,              #     X-rotate wire-2 by theta
       false_fn=qml.RZ              # Else: Z-rotate wire-2 by theta
   )(theta, wires=2)                # (Common parameters)
 
   return qml.counts()
 
# Result = {'000': 125, '001': 50, '010': 50, '100': 150, '101': 50, '110': 75}
# N.B. It sums to 500.
print(circuit(np.pi / 3))

Postselection

  • Postselection: If the measurement is not equal to the specified value, we will discard this circuit run

image-11 Caption — Figure 20: Quantum algorithm with postselection and condition on external parameter θ\theta. Qubit 1 gets H\mathbf{H}; the CNOT-linked qubit 2 is measured with postselect=∣1⟩|1\rangle (runs where it comes out otherwise are discarded); wire 3 gets RY(θ)\mathbf{RY}(\theta) if θ≤π/2\theta\leq\pi/2 or RX(θ)\mathbf{RX}(\theta) if θ>π/2\theta>\pi/2 — this condition is on the external parameter θ\theta itself, not a measurement outcome.

Example 5 — Postselection

dev = qml.device('default.qubit', wires=[0, 1, 2])
 
@qml.qnode(dev, shots=500)
def circuit(theta: float):
 
   qml.Hadamard(wires=[0])
   qml.CNOT(wires=[0, 1])          # Bell state |Psi+>
   qml.RX(theta, wires=[1])        # X-rotate wire-1 by theta
 
   # Postselection: If wire-1 is not 1, terminate this circuit run
   m = qml.measure(wires=[1], postselect=1)
 
   qml.cond(theta > np.pi / 2,     # If theta > pi / 2:
       true_fn=qml.RX,             #     X-rotate wire-2 by theta
       false_fn=qml.RY             # Else: Y-rotate wire-2 by theta
   )(theta, wires=[2])             # (Common parameters)
 
   return qml.counts()
 
# Result: {'010': 40, '011': 10, '110': 150, '111': 50}.
# N.B. It does not sum to 500 due to the postselection.
print(circuit(np.pi / 3))

Looping in PennyLane

  • For-loop: Decorator @qml.for_loop(start, stop, step)
  • While-loop: Decorator @qml.while_loop(cond_fn)
  • The decorated function becomes the loop's body, where its parameters and returned values connect between each iteration
  • Once you declare a function decorated with these decorators, you have to actually call it to run the loop — you are encouraged to modularize and parameterize your loops

image-12 Caption — Figures 21–22: For-loop and while-loop in quantum circuits. Both show qubit 1 repeatedly getting RY(θ0)\mathbf{RY}(\theta_0) with θ0←θ0/2\theta_0\leftarrow\theta_0/2 each pass, and qubit 2 repeatedly getting RY(θ1)\mathbf{RY}(\theta_1) with θ1←θ1/3\theta_1\leftarrow\theta_1/3 each pass. Figure 21's loop runs "for i=1i=1 to 1010"; Figure 22's loop runs "while θ0>10−3\theta_0>10^{-3} and θ1>10−3\theta_1>10^{-3}."

Example 6 — For-Loop

dev = qml.device('default.qubit', wires=[0, 1])
 
@qml.qnode(dev, shots=500)
def circuit_for(iter_no: int, theta0: float, theta1: float):
 
   @qml.for_loop(0, iter_no, 1)          # for i in range(0, iter_no, 1):
   def loop(i, theta0, theta1):          #     (This is the loop's body)
       qml.RY(theta0, wires=[0])         #     Y-rotate wire-0 by theta0
       qml.RY(theta1, wires=[1])         #     X-rotate wire-1 by theta1
       theta0_new = theta0 / 2           #     Update theta0
       theta1_new = theta1 / 3           #     Update theta1
       return theta0_new, theta1_new     #     Pass theta0 and theta1 to the next iteration
 
   loop(theta0, theta1)                  # Actually run the loop
   qml.CNOT(wires=[0, 1])
   return qml.counts()
 
circuit_for(10, np.pi / 4, np.pi / 4)

Example 7 — While-Loop

dev = qml.device('default.qubit', wires=[0, 1])
 
@qml.qnode(dev, shots=500)
def circuit_while(iter_no: int, theta0: float, theta1: float):
 
   @qml.while_loop(                       # while:
       lambda t0, t1:                     #     (Terminal condition)
           t0 > 1e-3 and t1 > 1e-3        #     theta0 > 1e-3 and theta1 > 1e-3
   )                                      #
   def loop(theta0, theta1):              #     (This is the loop's body)
       qml.RY(theta0, wires=[0])          #     Y-rotate wire-0 by theta0
       qml.RY(theta1, wires=[1])          #     Y-rotate wire-1 by theta1
       theta0_new = theta0 / 2            #     Update theta0
       theta1_new = theta1 / 3            #     Update theta1
       return theta0_new, theta1_new      #     Pass theta0 and theta1 to the next iteration
 
   loop(theta0, theta1)                   # Actually run the loop
   qml.CNOT(wires=[0, 1])
   return qml.counts()
 
circuit_while(10, np.pi / 4, np.pi / 4)

Exercise 4.5 — Dynamic Circuits

Develop PennyLane programs to compute the following states. Let each ∣xk⟩|x_k\rangle be the kk-th wire.

  1. Compute ∏k=110RX(θ/2k)∣x0⟩\prod_{k=1}^{10}\mathbf{RX}(\theta/2^k)|x_0\rangle, for some θ>0\theta>0.
  2. Let ∣y0⟩=∏k=15RX(θ/2k)∣x0⟩∣y1⟩=∏k=15RZ(θ/3k)∣x1⟩|y_0\rangle = \prod_{k=1}^{5}\mathbf{RX}(\theta/2^k)|x_0\rangle \qquad |y_1\rangle = \prod_{k=1}^{5}\mathbf{RZ}(\theta/3^k)|x_1\rangle Compute CNOT∣y0,y1⟩\text{CNOT}|y_0,y_1\rangle, for some θ>0\theta>0.
  3. Compute CCX∣y0,y1,0⟩\text{CCX}|y_0,y_1,0\rangle, where each ∣yj⟩=∏k=15RX((2k+j)θ)|y_j\rangle = \prod_{k=1}^{5}\mathbf{RX}((2k+j)\theta), for some θ>0\theta>0.
  4. Compute CCX∣y0,y1,0⟩\text{CCX}|y_0,y_1,0\rangle, where each ∣yj⟩=∏k=1NRX(θ/22k+j)|y_j\rangle = \prod_{k=1}^{N}\mathbf{RX}(\theta/2^{2k+j}), for some θ>0\theta>0, and each phase θ/22k+j>10−4\theta/2^{2k+j}>10^{-4}. The number NN varies depending on this condition.
  5. Compute the GHZ state as illustrated below.

image-13 Caption — Figure 23: GHZ entangled state — qubit 1 gets H\mathbf{H}, then a chain of CNOTs cascades from qubit 1 into qubit 2, qubit 2 into qubit 3, and so on down to qubit NN, producing (∣0⟩⊗N+∣1⟩⊗N)/2(|0\rangle^{\otimes N}+|1\rangle^{\otimes N})/\sqrt2.

  1. Compute the following quantum circuit. Each Rk\mathbf{R}_k is a phase shift gate with phase 2π/2k2\pi/2^k. [Reminder: Phase shift gate is qml.PhaseShift(angle, wires=…) in PennyLane] Rk=P(2π/2k)\mathbf{R}_k = \mathbf{P}(2\pi/2^k)
  2. Compute the following circuit for the quantum Fourier transform. Note that the computation for each ∣yk⟩|y_k\rangle can be seen as a modularized loop in Question 6.
  3. Compute the following circuit for the quantum inverse Fourier transform.

image-14 Caption — Figures 24–26: Half QFT, (full) Quantum Fourier Transform, and Quantum Inverse Fourier Transform circuits.

  • Figure 24 (Half QFT): qubit 1 gets H\mathbf{H}, then controlled phase-shift gates R2,R3,…,Rk,…,RN\mathbf{R}_2,\mathbf{R}_3,\dots,\mathbf{R}_k,\dots,\mathbf{R}_N each controlled from qubits 2,3,…,N2,3,\dots,N respectively.
  • Figure 25 (QFT): the same H+R2⋯RN\mathbf{H}+\mathbf{R}_2\cdots\mathbf{R}_N pattern from Figure 24 is repeated on qubit 1, then on qubit 2 (one R\mathbf{R} fewer), then qubit 3, and so on down to qubit NN (which gets only H\mathbf{H}) — each row is a smaller copy of the row above it, shifted one qubit to the right.
  • Figure 26 (Inverse QFT): the exact mirror image of Figure 25, running right-to-left instead of left-to-right.

Conclusion

  • Non-Clifford set of quantum logic gates
  • Measurement of quantum states
  • Dynamic quantum circuits
  • Limitation: Quantum algorithm consisting of only Clifford gates can be efficiently simulated by classical computers, yielding no performance boost
  • Divergence: Quantum algorithms that include Non-Clifford gates cannot be efficiently simulated by classical computers, but still perform well on quantum computers

Target Learning Outcomes

Master Non-Clifford Quantum Operations

  • Differentiate between Clifford and non-Clifford gate sets and explain the significance of the Gottesman-Knill Theorem regarding classical simulation efficiency.
  • Demonstrate boolean universality by constructing classical logic gates (AND, OR, NOT, XOR, NAND) using the Toffoli (CCX) gate.
  • Apply generalized rotations using RX, RY, and RZ gates to manipulate qubit amplitudes, signs, and phases.
  • Synthesize universal unitaries by combining RZ, RX, and P gates to simulate any single-qubit operator.

Implement and Evaluate Measurement Strategies

  • Distinguish between five types of state measurement: analytical solutions, projective measurement, state expectation, variance, and simulation (sampling).
  • Calculate probabilities and expectations for quantum states using Pauli projectors (X, Y, Z) and eigen-decomposition.
  • Analyze outcome dispersion by computing the variance of quantum operators to determine the consistency of qubit distributions.
  • Execute quantum simulations in PennyLane using both step-by-step sampling and end-to-end frequency counting.

Model Quantum States via Density Matrices

  • Convert pure and mixed states into density matrix representations.
  • Verify state purity by applying the trace property tr⁡(ρ2)=1\operatorname{tr}(\rho^2)=1 for pure states versus tr⁡(ρ2)<1\operatorname{tr}(\rho^2)<1 for mixed states.
  • Reconstruct qubits from the expectations of Pauli projectors using the density matrix formula.

Design Dynamic and Algorithmic Quantum Circuits

  • Implement mid-circuit measurements (MCM) to extract classical information during an algorithm's execution.
  • Construct conditional logic using qml.cond to trigger quantum operators based on measurement outcomes or external parameters.
  • Apply postselection to refine algorithm results by discarding circuit runs that do not meet specific measurement criteria.
  • Develop iterative algorithms using for_loop and while_loop decorators to automate complex structures like the GHZ state and Quantum Fourier Transform (QFT).