Week 2 | gates are rotations of the sphere; Lab 1 launches | reading: Watrous §1.2 (unitary operations)
Lecture 3 told us what a qubit is: a point on the Bloch sphere. This lecture is about what we can do to one. By the end you should be able to apply the standard single-qubit gates by hand, see each one as a rotation of the Bloch sphere, compose gates in the right order, measure in the X and Y bases using only a standard-basis measurement, and write, simulate and sample your first Qiskit circuit.
Between measurements, a qubit evolves by a gate: a \(2\times2\) matrix \(U\) that turns the state \(|\psi\rangle\) into \(U|\psi\rangle\). Which matrices are allowed? The new state must again be a state, whatever the old one was, so \(U\) must preserve the length of every vector. Lecture 2 showed that the matrices preserving lengths are exactly the unitary ones.
A single-qubit gate is a \(2\times2\) unitary matrix, \(U^\dagger U=I\). Applying it takes \(|\psi\rangle\) to \(U|\psi\rangle\), and it can always be undone by applying \(U^\dagger\).
We draw computations as circuits. A horizontal wire carries a qubit from left to right, boxes on the wire are gates, and a meter symbol is a standard-basis measurement. Time runs left to right on the page, but matrices act on the vector to their right. So the circuit order and the matrix order are reversed:
$$|\psi\rangle\;\text{—}\,\boxed{A}\,\text{—}\,\boxed{B}\,\text{—}\,\boxed{C}\,\text{—}\qquad\longleftrightarrow\qquad C\,B\,A\,|\psi\rangle .$$This reversal causes more lost points than any other single thing in the first weeks. The first gate you meet on the wire is the rightmost matrix, the one that touches \(|\psi\rangle\) first. When in doubt, compute one gate at a time: apply \(A\) to the state, then \(B\) to the result, and so on.
A circuit applies \(S\) first, then \(H\), then \(X\). As a single matrix acting on \(|\psi\rangle\) it is .
Most circuits in this course are built from a handful of named gates. You met the Pauli matrices and \(H\) in Lecture 2 as examples of unitary and Hermitian matrices; now they get jobs.
| gate | matrix | on basis states | nickname |
|---|---|---|---|
| \(X\) | \(\displaystyle \begin{pmatrix}0&1\\1&0\end{pmatrix}\) | \(|0\rangle\leftrightarrow|1\rangle\) | bit flip, NOT |
| \(Z\) | \(\displaystyle \begin{pmatrix}1&0\\0&-1\end{pmatrix}\) | \(|0\rangle\mapsto|0\rangle,\ |1\rangle\mapsto-|1\rangle\) | phase flip |
| \(Y\) | \(\displaystyle \begin{pmatrix}0&-i\\i&0\end{pmatrix}\) | \(|0\rangle\mapsto i|1\rangle,\ |1\rangle\mapsto-i|0\rangle\) | both flips |
| \(H\) | \(\displaystyle \tfrac{1}{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}\) | \(|0\rangle\mapsto|+\rangle,\ |1\rangle\mapsto|-\rangle\) | Hadamard |
| \(S\) | \(\displaystyle \begin{pmatrix}1&0\\0&i\end{pmatrix}\) | \(|1\rangle\mapsto i|1\rangle\) | quarter phase |
| \(T\) | \(\displaystyle \begin{pmatrix}1&0\\0&e^{i\pi/4}\end{pmatrix}\) | \(|1\rangle\mapsto e^{i\pi/4}|1\rangle\) | eighth phase |
\(S\) and \(T\) belong to the family of phase gates \(P(\lambda)=\mathrm{diag}(1,e^{i\lambda})\), with \(S=P(\pi/2)\), \(T=P(\pi/4)\) and \(Z=P(\pi)\). Their inverses are \(S^\dagger=P(-\pi/2)\) and \(T^\dagger=P(-\pi/4)\). A phase gate leaves \(|0\rangle\) and \(|1\rangle\) alone up to a phase, so it never changes standard-basis probabilities. It changes only the relative phase, which Lecture 3 showed is exactly the physical part.
You apply one gate to \(|+\rangle\) and then measure in the standard basis. Which gate changes the outcome probabilities?
Compute \(Z|+\rangle\), \(X|{+i}\rangle\) and \(Y|+\rangle\), and identify each result, up to global phase, as one of the six cardinal states.
\(Z\) negates the second amplitude, so \(Z|+\rangle=\tfrac{1}{\sqrt2}(|0\rangle-|1\rangle)=|-\rangle\). \(X\) swaps the amplitudes:
$$X|{+i}\rangle=\frac{1}{\sqrt2}\begin{pmatrix}i\\[2pt] 1\end{pmatrix}=\frac{i}{\sqrt2}\begin{pmatrix}1\\[2pt] -i\end{pmatrix}=i\,|{-i}\rangle .$$Up to the global phase \(i\), that is \(|{-i}\rangle\). Finally,
$$Y|+\rangle=\frac{1}{\sqrt2}\begin{pmatrix}-i\\[2pt] i\end{pmatrix}=-i\cdot\frac{1}{\sqrt2}\begin{pmatrix}1\\[2pt] -1\end{pmatrix}=-i\,|-\rangle ,$$which is \(|-\rangle\) up to phase. Each gate sent a cardinal state to its antipode. The next section explains why: a half turn about an axis sends every point on the perpendicular great circle to the opposite side.
The unitary \(2\times2\) matrices look like an abstract algebraic family. They have a concrete geometric meaning: each one rigidly rotates the Bloch sphere. The building blocks are rotations about the three coordinate axes.
For an angle \(t\), the rotation gates about the \(x\), \(y\) and \(z\) axes are
$$R_x(t)=e^{-itX/2},\qquad R_y(t)=e^{-itY/2},\qquad R_z(t)=e^{-itZ/2}.$$More generally, for a unit vector \(\vec n=(n_x,n_y,n_z)\), \(R_{\vec n}(t)=e^{-it\,(n_xX+n_yY+n_zZ)/2}\).
An exponential of a matrix looks alarming, but Lecture 2's spectral theorem makes it concrete.
If \(\sigma\) is any of \(X,Y,Z\) (or any \(n_xX+n_yY+n_zZ\) with \(|\vec n|=1\)), then
$$e^{-it\sigma/2}=\cos\frac t2\,I-i\sin\frac t2\,\sigma .$$In particular
$$R_x(t)=\begin{pmatrix}\cos\frac t2 & -i\sin\frac t2\\[4pt] -i\sin\frac t2 & \cos\frac t2\end{pmatrix},$$ $$R_y(t)=\begin{pmatrix}\cos\frac t2 & -\sin\frac t2\\[4pt] \sin\frac t2 & \cos\frac t2\end{pmatrix},$$ $$R_z(t)=\begin{pmatrix}e^{-it/2} & 0\\[4pt] 0 & e^{it/2}\end{pmatrix}.$$\(\sigma\) is Hermitian with eigenvalues \(\pm1\), so by the spectral theorem \(\sigma=P_+-P_-\), where \(P_\pm\) are the projectors onto its eigenvectors and \(P_++P_-=I\). Applying the function \(f(\lambda)=e^{-it\lambda/2}\) to the eigenvalues:
$$\begin{aligned}e^{-it\sigma/2}&=e^{-it/2}P_++e^{it/2}P_-\\[4pt] &=\cos\tfrac t2\,(P_++P_-)-i\sin\tfrac t2\,(P_+-P_-)\\[4pt] &=\cos\tfrac t2\,I-i\sin\tfrac t2\,\sigma .\end{aligned}$$Substituting \(\sigma=X,Y,Z\) gives the three matrices.
Apply \(R_x(\pi/3)\) to \(|0\rangle\) and measure in the standard basis. What is \(P(1)\)?
Why are these called rotations? Look at \(R_z\) acting on a state in standard form.
For every state \(\cos\tfrac\theta2|0\rangle+e^{i\varphi}\sin\tfrac\theta2|1\rangle\),
$$R_z(\lambda)\Big(\cos\tfrac\theta2|0\rangle+e^{i\varphi}\sin\tfrac\theta2|1\rangle\Big)=e^{-i\lambda/2}\Big(\cos\tfrac\theta2|0\rangle+e^{i(\varphi+\lambda)}\sin\tfrac\theta2|1\rangle\Big).$$Up to global phase, \(\theta\) is unchanged and \(\varphi\) increases by \(\lambda\): the Bloch vector turns by \(\lambda\) about the \(z\) axis, counterclockwise when viewed from above.
\(R_z(\lambda)=\mathrm{diag}(e^{-i\lambda/2},e^{i\lambda/2})\) multiplies the first amplitude by \(e^{-i\lambda/2}\) and the second by \(e^{i\lambda/2}=e^{-i\lambda/2}e^{i\lambda}\). Factor out \(e^{-i\lambda/2}\).
Up to global phase, \(R_z(3\pi/2)\,|-\rangle\) is .
The same holds for every axis, which is the central fact of single-qubit geometry.
\(R_{\vec n}(t)\) rotates the Bloch sphere by the angle \(t\) about the axis \(\vec n\), by the right-hand rule. Conversely, every single-qubit gate equals \(e^{i\alpha}R_{\vec n}(t)\) for some global phase \(\alpha\), axis \(\vec n\) and angle \(t\).
Comparing matrices identifies the named gates as rotations, up to global phases that we may ignore:
$$\begin{aligned}X&=i\,R_x(\pi), & Y&=i\,R_y(\pi), & Z&=i\,R_z(\pi),\\[4pt] H&=i\,R_{\vec n_H}(\pi), & S&=e^{i\pi/4}R_z(\tfrac\pi2), & T&=e^{i\pi/8}R_z(\tfrac\pi4),\end{aligned}$$Here \(\vec n_H=\tfrac{1}{\sqrt2}(1,0,1)\) is the diagonal axis halfway between \(x\) and \(z\).
The Hadamard is the surprising one. It is a half turn about the diagonal axis halfway between \(x\) and \(z\), which is why it swaps the \(z\) axis with the \(x\) axis, and so swaps \(|0\rangle,|1\rangle\) with \(|+\rangle,|-\rangle\). You can see this from \(H=\tfrac{1}{\sqrt2}(X+Z)\) and Proposition 4.1.
The rotation gates carry \(t/2\) in their matrices for the same reason states carry \(\theta/2\): angles on the sphere are double the angles between vectors. That has a strange consequence. A full turn, \(t=2\pi\), gives \(R_{\vec n}(2\pi)=\cos\pi\,I=-I\). The sphere is back where it started, but the state vector has picked up a minus sign. For a single qubit that sign is an invisible global phase. In Lecture 6, when the rotation is controlled by another qubit, the same sign becomes a relative phase, and it becomes visible.
For every state, \(R_x(2\pi)|\psi\rangle\) equals , and a single-qubit measurement tell the result apart from \(|\psi\rangle\).
A sequence of gates is a sequence of rotations, and the Bloch vector traces a path over the sphere. Because rotations about different axes do not commute, the order matters.
Apply \(H\), then \(T\), then \(H\) to \(|1\rangle\). Track the state after each gate, find \(P(0)\) at the end, and describe the path on the sphere.
The matrix is \(HTH\) (reversed order, though here the sequence is a palindrome). Step by step:
$$|1\rangle\;\xrightarrow{\ H\ }\;\frac{1}{\sqrt2}\begin{pmatrix}1\\[2pt] -1\end{pmatrix}=|-\rangle\;\xrightarrow{\ T\ }\;\frac{1}{\sqrt2}\begin{pmatrix}1\\[2pt] -e^{i\pi/4}\end{pmatrix}\;\xrightarrow{\ H\ }\;\frac12\begin{pmatrix}1-e^{i\pi/4}\\[2pt] 1+e^{i\pi/4}\end{pmatrix}.$$Using \(|1\mp e^{i\lambda}|^2=2\mp2\cos\lambda\),
$$P(0)=\frac{2-2\cos(\pi/4)}{4}=\frac{1-\cos(\pi/4)}{2}=\sin^2\frac\pi8\approx0.146,$$ $$P(1)=\cos^2\frac\pi8\approx0.854 .$$On the sphere: the first \(H\) swings the south pole on a half turn about the diagonal axis to \(|-\rangle\) at \(-x\); \(T\) slides it \(45^\circ\) along the equator; the second \(H\) swings it on another half turn to \((0,\,0.707,\,-0.707)\). The check: \(P(0)=(1+z)/2=(1-0.707)/2=0.146\), by Lecture 3's Proposition 3.3.
Theorem 4.3 says each gate is one rotation, but about an arbitrary axis. For hardware and for proofs it is often more convenient to use only rotations about fixed axes.
Every single-qubit gate can be written as
$$U=e^{i\alpha}\,R_z(\beta)\,R_y(\gamma)\,R_z(\delta)$$for real numbers \(\alpha,\beta,\gamma,\delta\). Any orientation of the sphere can be reached by a turn about \(z\), a tilt about \(y\), and another turn about \(z\).
Find the \(z\) coordinate of the Bloch vector of \(R_z(\pi/4)\,R_y(\pi/3)\,R_z(\pi/5)\,|0\rangle\).
Check that \(H=e^{i\pi/2}\,R_y(\tfrac\pi2)\,R_z(\pi)\).
From Proposition 4.1, \(R_z(\pi)=\mathrm{diag}(-i,\,i)\) and \(\displaystyle R_y(\tfrac\pi2)=\tfrac{1}{\sqrt2}\begin{pmatrix}1&-1\\1&1\end{pmatrix}\). Multiplying,
$$R_y(\tfrac\pi2)R_z(\pi)=\frac{1}{\sqrt2}\begin{pmatrix}1&-1\\[2pt] 1&1\end{pmatrix}\begin{pmatrix}-i&0\\[2pt] 0&i\end{pmatrix}=\frac{1}{\sqrt2}\begin{pmatrix}-i&-i\\[2pt] -i&i\end{pmatrix}=-i\,H,$$and \(e^{i\pi/2}(-i)=i\cdot(-i)=1\). Geometrically: a half turn about \(z\) followed by a quarter tilt about \(y\) lands the sphere in the same orientation as a half turn about the Hadamard axis.
A \(2\times2\) unitary has four real parameters: a global phase and three rotation angles. The global phase does nothing physical, so a single-qubit gate is really just an orientation of a sphere. Real hardware implements a small native set, typically \(R_z\) at any angle plus a fixed \(\sqrt X\) pulse, and a compiler assembles every other gate from Euler angles. Lecture 7 goes further: the finite set \(\{H,T\}\) alone can approximate every single-qubit gate as closely as you like.
Sandwiching one gate between another and its inverse, \(UAU^\dagger\), is called conjugation. For Paulis it has a clean geometric meaning: conjugating the Pauli matrix for an axis by a gate gives the Pauli matrix for the rotated axis.
The phase gate \(S\), a quarter turn about \(z\), carries the \(x\) axis to the \(y\) axis, and correspondingly
$$S\,X\,S^\dagger=Y .$$Multiply out, with \(S=\mathrm{diag}(1,i)\) and \(S^\dagger=\mathrm{diag}(1,-i)\):
$$\begin{aligned}S X S^\dagger&=\begin{pmatrix}1&0\\[2pt] 0&i\end{pmatrix}\begin{pmatrix}0&1\\[2pt] 1&0\end{pmatrix}\begin{pmatrix}1&0\\[2pt] 0&-i\end{pmatrix}\\[4pt] &=\begin{pmatrix}0&1\\[2pt] i&0\end{pmatrix}\begin{pmatrix}1&0\\[2pt] 0&-i\end{pmatrix}=\begin{pmatrix}0&-i\\[2pt] i&0\end{pmatrix}=Y .\end{aligned}$$The general rule, which we will not prove here, is that if \(U\) rotates the sphere so that axis \(\vec n\) goes to axis \(\vec m\), then \(U\,(\vec n\cdot\vec\sigma)\,U^\dagger=\vec m\cdot\vec\sigma\).
\(S\) is a quarter turn about \(z\), counterclockwise from above. So \(S\,Y\,S^\dagger\) equals .
Here are the identities worth memorising. Each one is a statement about rotations, and HW2 asks you to prove and interpret the Hadamard ones.
| identity | what it says on the sphere |
|---|---|
| \(H^2=I\) | two half turns about the same axis undo each other |
| \(S^2=Z,\quad T^2=S,\quad T^8=I\) | quarter turns and eighth turns about \(z\) add up |
| \(HXH=Z,\quad HZH=X,\quad HYH=-Y\) | the Hadamard swaps the \(x\) and \(z\) axes and flips \(y\) (HW2) |
| \(SXS^\dagger=Y\) | a quarter turn about \(z\) carries \(x\) to \(y\) |
| \(XZ=-ZX,\quad XY=iZ\) | Paulis anticommute; two half turns about perpendicular axes make a half turn about the third |
Quantum hardware can only measure in the standard basis. To measure in any other basis \(\{|b_0\rangle,|b_1\rangle\}\), we rotate the sphere so that the basis we want lands on the standard one, and then measure.
Let \(U\) be a gate with \(U|b_0\rangle=|0\rangle\) and \(U|b_1\rangle=|1\rangle\). Applying \(U\) and then measuring in the standard basis gives outcome \(k\) with probability \(|\langle b_k|\psi\rangle|^2\), exactly as if we had measured \(|\psi\rangle\) in the basis \(\{|b_0\rangle,|b_1\rangle\}\).
Since \(U\) is unitary, \(U|b_k\rangle=|k\rangle\) means \(|b_k\rangle=U^\dagger|k\rangle\). Moving \(U\) across the bracket,
$$P(k)=|\langle k|U\psi\rangle|^2=|\langle U^\dagger k|\psi\rangle|^2=|\langle b_k|\psi\rangle|^2 .$$For the X basis we need \(U|+\rangle=|0\rangle\) and \(U|-\rangle=|1\rangle\), and \(H\) does it. For the Y basis we first turn \(|{\pm i}\rangle\) back to \(|\pm\rangle\) with \(S^\dagger\), a quarter turn about \(z\) the other way, and then apply \(H\). In circuit order that is \(S^\dagger\) then \(H\); as a matrix, \(U=HS^\dagger\).
A classmate wants to measure in the Y basis. Which recipe, written in circuit order (left gate first), makes outcome 0 mean \(+i\) and outcome 1 mean \(-i\)?
The state \(|\psi\rangle=0.8|0\rangle+0.6i|1\rangle\) is measured with the X-basis circuit, and a fresh copy with the Y-basis circuit. Find \(P(+)\) and \(P(+i)\).
Predict the outcome statistics when \(|{+i}\rangle\) is measured with the X-basis circuit and with the Y-basis circuit.
X basis: \(H|{+i}\rangle=\tfrac12\big((1+i)|0\rangle+(1-i)|1\rangle\big)\), so \(P(0)=P(1)=\tfrac{|1\pm i|^2}{4}=\tfrac12\), a fair coin. That fits the sphere: \(|{+i}\rangle\) is on the \(y\) axis, at right angles to the X basis. Y basis: \(S^\dagger|{+i}\rangle=\tfrac{1}{\sqrt2}(|0\rangle+(-i)(i)|1\rangle)=|+\rangle\), and then \(H|+\rangle=|0\rangle\), so outcome 0, meaning \(+i\), with probability 1.
In BB84 (Lecture 20) the receiver decides, qubit by qubit, whether to measure in the Z basis or the X basis. In hardware that decision is simply whether or not to put an \(H\) in front of the meter. An eavesdropper faces the same choice without knowing the sender's basis. When they guess wrong, Proposition 4.6 says their result is a coin toss and their measurement leaves the qubit rotated onto the wrong axis, which is the disturbance the legitimate parties look for.
from qiskit import QuantumCircuit from qiskit.quantum_info import Statevector from qiskit.primitives import StatevectorSampler qc = QuantumCircuit(1) qc.x(0) # start from |1> qc.h(0) qc.t(0) qc.h(0) print(qc.draw()) psi = Statevector.from_instruction(qc) # exact state (no measurement) print(psi.probabilities().round(4)) qc.measure_all() # now add a measurement sampler = StatevectorSampler(seed=7) result = sampler.run([qc], shots=1000).result()[0] print(result.data.meas.get_counts())
┌───┐┌───┐┌───┐┌───┐
q: ┤ X ├┤ H ├┤ T ├┤ H ├
└───┘└───┘└───┘└───┘
[0.1464 0.8536]
{'1': 847, '0': 153}
This is Example 4.2 on a simulator. Statevector.from_instruction gives the exact answer; the StatevectorSampler returns what a perfect quantum computer would, namely shots. The meas in result.data.meas is the name measure_all gives the classical register.
import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.quantum_info import Statevector
theta = Parameter("theta")
qc = QuantumCircuit(1)
qc.ry(theta, 0) # R_y(theta)|0> = cos(theta/2)|0> + sin(theta/2)|1>
for t in [0, np.pi/3, np.pi/2, np.pi]:
bound = qc.assign_parameters({theta: t})
p1 = Statevector.from_instruction(bound).probabilities()[1]
print(f"theta = {t:.4f} P(1) = {p1:.4f} sin^2(theta/2) = {np.sin(t/2)**2:.4f}")
# measuring |+i> in the Y basis: rotate with S-dagger then H, then read in Z
y = QuantumCircuit(1)
y.rx(-np.pi/2, 0) # R_x(-pi/2)|0> = |+i>
y.sdg(0); y.h(0) # Y-basis measurement rotation
print(Statevector.from_instruction(y).probabilities().round(4))
theta = 0.0000 P(1) = 0.0000 sin^2(theta/2) = 0.0000 theta = 1.0472 P(1) = 0.2500 sin^2(theta/2) = 0.2500 theta = 1.5708 P(1) = 0.5000 sin^2(theta/2) = 0.5000 theta = 3.1416 P(1) = 1.0000 sin^2(theta/2) = 1.0000 [1. 0.]
A Parameter lets one circuit stand for a whole family; assign_parameters fills in a value. Give parameters plain ASCII names like "theta", which print and export cleanly. The last line is Example 4.4: \(|{+i}\rangle\) always reads \(+i\) in the Y basis.
Goal. Set up the toolchain and treat measurement as the statistics problem it is.
Statevector probabilities with sampled counts.Hardware budget: at most 20 seconds of machine time, out of your 10 minutes per month. Debug everything on the simulator first and send one clean job to hardware. Save your job ID in the notebook.
Next time: two qubits, tensor products, and entanglement. HW2 is due at the start of Week 3; Lab 1 is due in ten days.
StatevectorSampler).all numbers and figures in these notes are computed in python (verify_L04.py) and follow the page theme