Quantum Computation & Cryptography · Lecture 4

Lecture 4
Single-Qubit Gates and Measuring in Other Bases

Week 2  |  gates are rotations of the sphere; Lab 1 launches  |  reading: Watrous §1.2 (unitary operations)

Cartoon: turning a globe instead of looking at it from a new angleA stick figure turns a globe on a stand with one hand. A camera fixed on a tripod points at the globe. A speech bubble says: the camera can only look one way, so I turn the world instead. the camera only looks one way,so I turn the world instead. measure (Z)
hardware measures in one basis only; gates rotate every other basis onto it
In this lecture
  1. Gates and circuits
  2. The single-qubit gate zoo
  3. Gates are rotations of the Bloch sphere
  4. Composing gates
  5. Every gate is three rotations
  6. Conjugation and a few identities
  7. Measuring in other bases
  8. Try it in code
  9. Lab 1 launch
  10. Summary and exercises

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.

1 — Gates and circuits

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.

definition 4.1 — single-qubit gate

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 .$$
idea — read circuits forwards, write matrices backwards

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 .

The first gate on the wire is the rightmost matrix, the one that touches \(|\psi\rangle\) first.
\(X\,H\,S\,|\psi\rangle\): \(S\) acts first, so it sits next to the state, and \(X\) acts last, so it is leftmost.

2 — The single-qubit gate zoo

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.

gatematrixon basis statesnickname
\(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?

Phase gates never change standard-basis probabilities. Which gate in the list mixes the two amplitudes together?
\(H|+\rangle=|0\rangle\), which reads 0 with certainty. The phase gates \(S,T,Z\) only change the relative phase, which a standard-basis measurement cannot see.
example 4.1 — applying gates by hand

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.

3 — Gates are rotations of the Bloch sphere

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.

definition 4.2 — rotation gates

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.

proposition 4.1 — rotation gates in closed form

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)\)?

The first column of \(R_x(t)\) is \(\big(\cos\tfrac t2,\ -i\sin\tfrac t2\big)\). Remember the half angle, and that \(|-i|=1\).
\(P(1)=\big|-i\sin\tfrac\pi6\big|^2=\tfrac14\). Using \(\sin^2\tfrac\pi3=\tfrac34\) instead is the classic half-angle slip.

Why are these called rotations? Look at \(R_z\) acting on a state in standard form.

proposition 4.2 — \(R_z(\lambda)\) turns the sphere about the \(z\) axis

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 .

\(|-\rangle\) has \(\varphi=\pi\). Add \(\lambda\) and reduce modulo \(2\pi\).
\(\varphi=\pi+\tfrac{3\pi}2=\tfrac{5\pi}2\equiv\tfrac\pi2\), which is \(|{+i}\rangle\): three quarter turns about \(z\) from \(-x\) land on \(+y\).

The same holds for every axis, which is the central fact of single-qubit geometry.

theorem 4.3 — every gate is a rotation (statement)

\(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.

Single-qubit gates as rotations of the Bloch sphereA Bloch sphere with its x, y and z axes, plus the Hadamard axis halfway between x and z. A legend lists each gate's rotation: X is a half turn about x, Y a half turn about y, Z a half turn about z, H a half turn about the axis (x + z)/sqrt(2), S a quarter turn about z, and T an eighth of a turn about z.xyzH axis|0>|1>|+>|+i>gaterotationXhalf turn about xYhalf turn about yZhalf turn about zHhalf turn about H axisSquarter turn about zTeighth turn about zRx(t)angle t about xRy(t)angle t about yRz(t)angle t about z
Each standard gate is a rotation about an axis of the Bloch sphere. The Pauli gates are half turns about the coordinate axes, \(H\) is a half turn about the dashed diagonal axis, and the phase gates \(S\) and \(T\) are a quarter turn and an eighth of a turn about \(z\).
idea — half angles again, and a full turn that is not the identity

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\).

Proposition 4.1 with \(t=2\pi\): \(\cos\pi\,I-i\sin\pi\,X\).
\(R_x(2\pi)=\cos\pi\,I=-I\). The sign is a global phase, so no single-qubit experiment can see it; it matters only once the rotation is controlled.

4 — Composing gates

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.

example 4.2 — H, then T, then H, starting from \(|1\rangle\)

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.

Bloch-sphere path of H, then T, then H, starting from |1>Starting at the south pole |1>, the first H swings the state on a half turn about the Hadamard axis to |->, on the equator at the negative x axis. T then turns it an eighth of a turn about z, along the equator to (-0.71, -0.71, 0). The second H swings it on another half turn about the Hadamard axis to the final point (0, 0.71, -0.71), below the equator. There P(0) = (1 + z)/2 = 0.146.xyzstart |1>after H: |->after Tend1st H: half turn, H axisT: 45 deg about z2nd H: half turn, H axisend: (0, 0.707, -0.707)P(0) = (1 + z)/2 = 0.146= sin^2(pi/8)
The path of Example 4.2, seen from slightly below the equator so that the whole path is on the near side. Each arc is computed by applying the gate's rotation continuously, from angle 0 to its full angle. The two \(H\) arcs are half circles around the diagonal Hadamard axis, and the \(T\) arc is a short slide along the equator.

Circuit sandbox

Choose a starting state, then add gates. Each gate animates as a rotation of the Bloch vector, and the readout shows the exact state (with its global phase removed) and \(P(0)\). "example 4.2" loads the circuit above. Try \(H,X,H\) and compare it with \(Z\).

5 — Every gate is three rotations

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.

theorem 4.4 — Euler (Z-Y-Z) decomposition (statement)

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\).

Turns about \(z\) never change the \(z\) coordinate. Only the tilt \(R_y(\pi/3)\) matters, and it moves the north pole to \(\theta=\pi/3\).
\(R_z(\pi/5)\) leaves \(|0\rangle\) alone up to phase, \(R_y(\pi/3)\) tilts it to \(\theta=\pi/3\), and \(R_z(\pi/4)\) spins it without changing the height: \(z=\cos\tfrac\pi3=\tfrac12\).
example 4.3 — the Hadamard in Euler form

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.

idea — four numbers, and why a few gates are enough

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.

6 — Conjugation and a few identities

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.

proposition 4.5 — conjugation moves the 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 .

Use the rule after Proposition 4.5: find where the quarter turn sends the \(+y\) axis.
A quarter turn about \(z\) carries \(+y\) to \(-x\), so \(SYS^\dagger=-X\). (Multiplying the matrices out confirms it.)

Here are the identities worth memorising. Each one is a statement about rotations, and HW2 asks you to prove and interpret the Hadamard ones.

identitywhat 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

7 — Measuring in other bases

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.

proposition 4.6 — rotate, 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\)?

First turn \(|{\pm i}\rangle\) back to \(|\pm\rangle\) about the \(z\) axis, then use the X-basis recipe.
\(S^\dagger\) sends \(|{\pm i}\rangle\) to \(|\pm\rangle\), and \(H\) sends those to \(|0\rangle,|1\rangle\). As a matrix this is \(HS^\dagger\).
Circuits for measuring in the Z, X and Y basesThree one-qubit circuits. Z basis: measure directly. X basis: apply H, then measure; outcome 0 means +, outcome 1 means -. Y basis: apply S dagger, then H, then measure; outcome 0 means +i and outcome 1 means -i.Z basis|psi>0 = |0>, 1 = |1>X basis|psi>H0 = |+>, 1 = |->Y basis|psi>S†H0 = |+i>, 1 = |-i>rotate the basis you want onto the standard basis, then measure
Measuring in the three standard bases with a standard-basis meter. Outcome 0 means the first basis vector (\(|0\rangle\), \(|+\rangle\) or \(|{+i}\rangle\)) and outcome 1 means the second.

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)\).

By Proposition 4.6, \(P(b)=|\langle b|\psi\rangle|^2\). Conjugate the bra: \(\langle{+i}|=\tfrac1{\sqrt2}(1\;\;-i)\).
\(P(+)=\tfrac{|0.8+0.6i|^2}{2}=\tfrac12\), and \(P(+i)=\tfrac{|0.8+(-i)(0.6i)|^2}{2}=\tfrac{1.4^2}{2}=0.98\). The state is near \(+y\) on the sphere, so only the Y measurement is nearly certain.
example 4.4 — \(|{+i}\rangle\) in two bases

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.

crypto connection — a basis choice is a single gate

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.

8 — Try it in code

tool — a first circuit: build, simulate, sample (tested with qiskit 2.5)
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.

tool — parameters, and a Y-basis measurement
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.

9 — Lab 1 launch: Hello, qubit

lab 1 — simulation, sampling, and your first hardware run (due in 10 days)

Goal. Set up the toolchain and treat measurement as the statistics problem it is.

  1. Environment. Install or verify Qiskit 2.x, Qiskit Aer and Qiskit IBM Runtime; log in with your classroom account.
  2. Circuits. Prepare \(|0\rangle\), \(|+\rangle\), \(|{+i}\rangle\) and \(R_y(\theta)|0\rangle\). Compare exact Statevector probabilities with sampled counts.
  3. Statistics. For 100, 1,000 and 10,000 shots, compute \(\hat p\) with normal and Wilson 95% intervals, and check that the interval width falls like \(1/\sqrt n\) (Lecture 1).
  4. Curve fitting. Sweep \(\theta\) from 0 to \(\pi\), estimate \(P(1)\), and fit \(\sin^2(\theta/2)\) by least squares.
  5. Hardware. Run the \(\theta\)-sweep (9 points, 1,000 shots each) on an IBM backend and overlay it on your simulation. Are the deviations inside your intervals? If not, what might cause them? (Lecture 19 returns to this.)

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.

10 — Summary and exercises

summary

Next time: two qubits, tensor products, and entanglement. HW2 is due at the start of Week 3; Lab 1 is due in ten days.

exercises 4
  1. Compute \(Y|0\rangle\), \(Y|1\rangle\) and \(Z|{+i}\rangle\). Identify each result, up to phase, as a cardinal state, and explain each answer as a rotation.
  2. Show that \(S^2=Z\) and \(T^2=S\), and deduce \(T^4\) and \(T^8\).
  3. Write the matrix of \(R_x(\pi/2)\) and apply it to \(|0\rangle\). Which cardinal state do you get? Check your answer with the right-hand rule on the sphere.
  4. Show that the phase gate \(P(\lambda)=\mathrm{diag}(1,e^{i\lambda})\) and \(R_z(\lambda)\) differ only by a global phase. Can any single-qubit experiment tell them apart?
  5. Design a circuit that measures in the basis \(|b_0\rangle=\cos\tfrac\pi8|0\rangle+\sin\tfrac\pi8|1\rangle\), \(|b_1\rangle=-\sin\tfrac\pi8|0\rangle+\cos\tfrac\pi8|1\rangle\). (Hint: \(R_y(t)|0\rangle=\cos\tfrac t2|0\rangle+\sin\tfrac t2|1\rangle\).)
  6. Show that \(XZ=-ZX\) and \(XY=iZ\). Interpret \(XY=iZ\) on the sphere.
  1. \(Y|0\rangle=i|1\rangle\), \(Y|1\rangle=-i|0\rangle\), and \(Z|{+i}\rangle=\tfrac{1}{\sqrt2}(|0\rangle-i|1\rangle)=|{-i}\rangle\). \(Y\) is a half turn about \(y\), which swaps the poles. \(Z\) is a half turn about \(z\), which carries \(+y\) to \(-y\).
  2. \(S^2=\mathrm{diag}(1,i^2)=Z\) and \(T^2=\mathrm{diag}(1,e^{i\pi/2})=S\). Hence \(T^4=S^2=Z\) and \(T^8=Z^2=I\): eight eighth-turns make a full turn.
  3. From Proposition 4.1, $$R_x(\tfrac\pi2)=\frac{1}{\sqrt2}\begin{pmatrix}1&-i\\[2pt] -i&1\end{pmatrix},\qquad R_x(\tfrac\pi2)|0\rangle=\frac{1}{\sqrt2}\begin{pmatrix}1\\[2pt] -i\end{pmatrix}=|{-i}\rangle .$$ With your right thumb along \(+x\), your fingers carry \(+z\) toward \(-y\), and a quarter turn lands exactly on \(-y\), which is \(|{-i}\rangle\).
  4. \(R_z(\lambda)=\mathrm{diag}(e^{-i\lambda/2},e^{i\lambda/2})=e^{-i\lambda/2}\,\mathrm{diag}(1,e^{i\lambda})=e^{-i\lambda/2}P(\lambda)\). A global phase is undetectable in any single-qubit experiment (Proposition 3.1, since it rides along through later gates). The difference will matter once these gates are controlled by a second qubit.
  5. \(R_y(\pi/4)\) sends \(|0\rangle\mapsto|b_0\rangle\) and \(|1\rangle\mapsto|b_1\rangle\) (its columns are \(|b_0\rangle,|b_1\rangle\)). So \(U=R_y(\pi/4)^\dagger=R_y(-\pi/4)\) satisfies \(U|b_k\rangle=|k\rangle\). The circuit is: apply \(R_y(-\pi/4)\), then measure; outcome \(k\) means \(|b_k\rangle\).
  6. Multiplying gives $$XZ=\begin{pmatrix}0&-1\\[2pt] 1&0\end{pmatrix}=-ZX,\qquad XY=\begin{pmatrix}i&0\\[2pt] 0&-i\end{pmatrix}=iZ .$$ On the sphere, a half turn about \(y\) followed by a half turn about \(x\) is a half turn about \(z\). Try it with a book: flip it end over end, then side to side, and it has turned half a revolution flat on the table.

Reading and sources

all numbers and figures in these notes are computed in python (verify_L04.py) and follow the page theme