Skip to the content
Kets & Gates homeOpen the app

Lesson 4: Single-qubit gates

Gates as matrices and as turns of the Bloch sphere, and why every gate is unitary

about 30 min11 partsbest after lesson 3

Start the interactive lesson

The interactive version checks every answer, gives hints and saves your progress in this browser. This page is the same lesson on one page, with the answers folded away.

1 / 11 · Learn

Before you start

So far you have written states down and measured them. A gate changes a state without reading anything out, and every computation in the rest of this course is built from gates.

By the end of this lesson you can:

  • apply XX, ZZ and HH to a state by matrix multiplication;
  • read a gate's two columns as what it does to ∣0⟩\ket0 and ∣1⟩\ket1;
  • test whether a matrix is unitary, and say why every gate must be;
  • turn a circuit into a matrix product in the right order;
  • picture XX, ZZ and HH as half turns of the Bloch sphere.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

A qubit starts in ∣0⟩\ket0. One Hadamard gate HH would make a measurement in the 0/1 basis come out 0 or 1 with equal odds. Instead of measuring, you apply HH a second time, and only then measure in that basis. What is the probability of getting 0?

  1. A11
  2. B12\tfrac12
  3. C14\tfrac14
  4. D00
Show the answer

Answer: A. 11

H∣0⟩=∣+⟩H\ket0 = \ket+ and H∣+⟩=∣0⟩H\ket+ = \ket0, so HH∣0⟩=∣0⟩HH\ket0 = \ket0. A measurement in the 0/1 basis then gives 0 with certainty, so P(0)=1P(0) = 1.

Why not the others

  • B. A common picture is that HH randomizes a qubit, so a second HH leaves it random. But after one HH the qubit is in the definite state ∣+⟩\ket+, not a coin, and the second HH makes the two contributions to ∣1⟩\ket1 cancel. So HH∣0⟩=∣0⟩HH\ket0 = \ket0: the 50/50 appears only if you measure in the 0/1 basis between the gates.
  • C. It is tempting to multiply two 50/50 steps, 12×12\tfrac12 \times \tfrac12. But a gate does not add randomness step by step: it changes amplitudes, and amplitudes can cancel. Here the cancellation is complete, because HH=IHH = I.
  • D. This treats HH as a "square root of NOT", as if two of them made an XX and sent ∣0⟩\ket0 to ∣1⟩\ket1. They don't: HH=IHH = I, so the qubit ends in ∣0⟩\ket0, and the outcome is 0 every time.

Check yourself

A warm-up: make a guess before reading on. The lesson comes back to it.

Is there a single-qubit gate that turns every input state into ∣0⟩\ket0, whatever the input was?

  1. AYes: the matrix [1000]\begin{bmatrix}1&0\\0&0\end{bmatrix} keeps the ∣0⟩\ket0 part, and you rescale the result.
  2. BNo: every gate can be undone, so no gate can send two different inputs to the same output.
  3. CYes: a suitable fixed sequence of XX, ZZ and HH.
  4. DNo: gates never change the probabilities of the measurement outcomes 0 and 1; only measurements do.
Show the answer

Answer: B. No: every gate can be undone, so no gate can send two different inputs to the same output.

Gates are unitary, and a unitary always has an inverse. A gate that sent both ∣0⟩\ket0 and ∣1⟩\ket1 to ∣0⟩\ket0 would have no inverse. Resetting an unknown qubit takes a measurement in the 0/1 basis, followed by XX if the result was 1.

Why not the others

  • A. Keeping only the ∣0⟩\ket0 part is a natural first idea. But that matrix sends ∣1⟩\ket1 to the zero vector, which is not a state, and the rescaling factor depends on an input you may not know. A gate must keep every state normalized and be undoable, so it can never send two different inputs to the same output.
  • C. Each of XX, ZZ and HH can be undone, so any sequence of them can be undone too. A sequence that sent both ∣0⟩\ket0 and ∣1⟩\ket1 to ∣0⟩\ket0 could not be undone: from the output alone you could not tell which input you had.
  • D. Gates change probabilities all the time: H∣0⟩=∣+⟩H\ket0 = \ket+ turns a certain outcome 0 into a 50/50 split. The real obstacle is different: a gate is invertible, so it cannot merge two different inputs into one output.

2 / 11 · Learn

A gate is a linear map

The classical NOT gate swaps 0 and 1. Its quantum version, XX, does the same to the basis states: X∣0⟩=∣1⟩X\ket0 = \ket1 and X∣1⟩=∣0⟩X\ket1 = \ket0. Quantum gates are linear, so those two facts settle what XX does to every state:

X(α∣0⟩+β∣1⟩)=α∣1⟩+β∣0⟩.X(\alpha\ket0 + \beta\ket1) = \alpha\ket1 + \beta\ket0.

With states written as column vectors, a gate is a 2×22\times2 matrix, and you can read it straight off: the first column is the image of ∣0⟩\ket0, the second column is the image of ∣1⟩\ket1.

X=[0110],X[αβ]=[βα].X = \begin{bmatrix}0&1\\1&0\end{bmatrix},\qquad X\begin{bmatrix}\alpha\\ \beta\end{bmatrix} = \begin{bmatrix}\beta\\ \alpha\end{bmatrix}.

Refresher: Matrices acting on states Matrix times vector, and why order matters, if either is rusty.

Check yourself

A gate UU sends ∣0⟩↦12(∣0⟩+∣1⟩)\ket0 \mapsto \tfrac{1}{\sqrt2}(\ket0 + \ket1) and ∣1⟩↦12(i∣0⟩−i∣1⟩)\ket1 \mapsto \tfrac{1}{\sqrt2}(i\ket0 - i\ket1). Which matrix is UU?

  1. A12[11i−i]\tfrac{1}{\sqrt2}\begin{bmatrix}1 & 1\\ i & -i\end{bmatrix}
  2. B12[1i1−i]\tfrac{1}{\sqrt2}\begin{bmatrix}1 & i\\ 1 & -i\end{bmatrix}
  3. C12[111−1]\tfrac{1}{\sqrt2}\begin{bmatrix}1 & 1\\ 1 & -1\end{bmatrix}
Hints
  1. Which column does [10]\begin{bmatrix}1\\0\end{bmatrix} pick out when you multiply?
  2. Column 1 is U∣0⟩U\ket0 and column 2 is U∣1⟩U\ket1, written top to bottom.
Show the answer

Answer: B. 12[1i1−i]\tfrac{1}{\sqrt2}\begin{bmatrix}1 & i\\ 1 & -i\end{bmatrix}

Column 1 is U∣0⟩=12(1,1)U\ket0 = \tfrac{1}{\sqrt2}(1, 1) and column 2 is U∣1⟩=12(i,−i)U\ket1 = \tfrac{1}{\sqrt2}(i, -i). Multiplying by [10]\begin{bmatrix}1\\0\end{bmatrix} picks out column 1, and by [01]\begin{bmatrix}0\\1\end{bmatrix} column 2.

Why not the others

  • A. This writes the images as rows. Test it: multiplying by [10]\begin{bmatrix}1\\0\end{bmatrix} picks out the first column, 12(1,i)\tfrac{1}{\sqrt2}(1, i), which is not U∣0⟩U\ket0. Rows feel natural because we read across, but the multiplication rule puts each input's image down a column.
  • C. That is HH, whose second column is ∣−⟩\ket-. Here the image of ∣1⟩\ket1 is i∣−⟩i\ket-, so the second column is 12(i,−i)\tfrac{1}{\sqrt2}(i, -i). On ∣1⟩\ket1 alone the ii would be a global phase, but next to the first column it becomes a relative phase, so dropping it changes the gate.

Check yourself

Apply XX to 35∣0⟩−4i5∣1⟩\tfrac35\ket0 - \tfrac{4i}{5}\ket1. Enter the exact amplitudes of the result.

Hints
  1. Where does the amplitude of ∣1⟩\ket1 end up after XX?
  2. Linearity: X(α∣0⟩+β∣1⟩)=α∣1⟩+β∣0⟩X(\alpha\ket0 + \beta\ket1) = \alpha\ket1 + \beta\ket0.
Show the answer

Answer: −45 i∣0⟩+35∣1⟩-\frac{4}{5}\,i|0\rangle + \frac{3}{5}|1\rangle

XX swaps the entries of the column vector: X[3/5−4i/5]=[−4i/53/5]X\begin{bmatrix}3/5\\ -4i/5\end{bmatrix} = \begin{bmatrix}-4i/5\\ 3/5\end{bmatrix}, that is −4i5∣0⟩+35∣1⟩-\tfrac{4i}{5}\ket0 + \tfrac35\ket1.

3 / 11 · Learn

Z, H and the Bloch sphere

Two more single-qubit gates come up all the time. ZZ leaves ∣0⟩\ket0 alone and flips the sign of ∣1⟩\ket1. The Hadamard gate HH sends ∣0⟩\ket0 to ∣+⟩\ket+ and ∣1⟩\ket1 to ∣−⟩\ket-. Stack those images as columns:

Z=[100−1],H=12[111−1].Z = \begin{bmatrix}1&0\\0&-1\end{bmatrix},\qquad H = \frac{1}{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}.

On the Bloch sphere each of these gates is a half turn of the whole sphere: XX about the xx axis, ZZ about the zz axis, and HH about the axis halfway between xx and zz. A state on the rotation axis stays put; every other state moves.

Check yourself

What is X∣+⟩X\ket+?

  1. A∣+⟩\ket+
  2. B∣1⟩\ket1
  3. C∣−⟩\ket-
  4. D−∣+⟩-\ket+
Hints
  1. What does XX do to the two amplitudes of a state?
  2. XX swaps the amplitudes of ∣0⟩\ket0 and ∣1⟩\ket1. What are the two amplitudes of ∣+⟩\ket+?
Show the answer

Answer: A. ∣+⟩\ket+

∣+⟩\ket+ lies on the xx axis, the axis of XX's half turn, so it does not move. In vectors: XX swaps two equal entries.

Why not the others

  • B. XX acts as NOT on the basis states, but it does not send every state to ∣1⟩\ket1. By linearity X∣+⟩=12(X∣0⟩+X∣1⟩)=12(∣1⟩+∣0⟩)=∣+⟩X\ket+ = \tfrac{1}{\sqrt2}(X\ket0 + X\ket1) = \tfrac{1}{\sqrt2}(\ket1 + \ket0) = \ket+.
  • C. A natural guess is that NOT flips ++ into −-. But XX swaps the two amplitudes, and ∣+⟩\ket+ has equal amplitudes, so nothing changes: X12(∣0⟩+∣1⟩)=12(∣1⟩+∣0⟩)X\tfrac{1}{\sqrt2}(\ket0+\ket1) = \tfrac{1}{\sqrt2}(\ket1+\ket0). On the sphere ∣+⟩\ket+ sits on the axis of XX's half turn; turning ∣+⟩\ket+ into ∣−⟩\ket- is what ZZ does.
  • D. No sign appears: swapping two equal amplitudes 12\tfrac{1}{\sqrt2} gives back exactly ∣+⟩\ket+. A sign does appear for ∣−⟩\ket-: X∣−⟩=12(∣1⟩−∣0⟩)=−∣−⟩X\ket- = \tfrac{1}{\sqrt2}(\ket1 - \ket0) = -\ket-, the same physical state.

Check yourself

Using only the gate buttons, move the state from ∣+⟩\ket+ to ∣1⟩\ket1, the south pole.

Set the state on the Bloch sphere with the gates X, Z, H.

Hints
  1. Which gate takes ∣+⟩\ket+ to one of the poles?
  2. HH swaps ∣+⟩\ket+ and ∣0⟩\ket0; XX swaps the two poles.
Show the answer

Answer: ∣1⟩|1\rangle

Two routes work: HH then XX (∣+⟩→∣0⟩→∣1⟩\ket+ \to \ket0 \to \ket1), or ZZ then HH (∣+⟩→∣−⟩→∣1⟩\ket+ \to \ket- \to \ket1).

Aside: the Pauli Y gate

There is a fourth gate, Y=[0−ii0]Y = \begin{bmatrix}0&-i\\i&0\end{bmatrix}. Together XX, YY and ZZ are called the Pauli matrices. On the Bloch sphere YY is a half turn about the yy axis. You can check that Y=iXZY = iXZ, so for this course it adds nothing new.

4 / 11 · Learn

Why gates must be unitary

A gate has to turn every state into a state, so it must preserve length: ∣α∣2+∣β∣2=1\abs{\alpha}^2 + \abs{\beta}^2 = 1 before and after. For a matrix UU that requirement is exactly

U†U=I,U^\dagger U = I,

where the adjoint U†U^\dagger is UU transposed with every entry complex-conjugated. A matrix with this property is unitary. In practice you check the columns: each must have length 1, and the two must be orthogonal (conjugate the first column's entries before multiplying; see the inner-product refresher). A unitary can always be undone by applying U†U^\dagger. In the other direction, every unitary matrix is a valid gate. So there are infinitely many single-qubit gates, against one non-trivial classical one-bit gate, NOT.

Check yourself

Select every matrix that is a valid gate.

Select all that apply.

  1. A12[1ii1]\tfrac{1}{\sqrt2}\begin{bmatrix}1&i\\i&1\end{bmatrix}
  2. B[100−1]\begin{bmatrix}1&0\\0&-1\end{bmatrix}
  3. C[1101]\begin{bmatrix}1&1\\0&1\end{bmatrix}
  4. D[1000]\begin{bmatrix}1&0\\0&0\end{bmatrix}
Hints
  1. What length must each column have?
  2. A unitary has columns of length 1 that are orthogonal to each other.
Show the answer

Answers: A. 12[1ii1]\tfrac{1}{\sqrt2}\begin{bmatrix}1&i\\i&1\end{bmatrix}; B. [100−1]\begin{bmatrix}1&0\\0&-1\end{bmatrix}

Check the columns. In the first matrix each has squared length 12+12=1\tfrac12 + \tfrac12 = 1, and 12(1⋅i+(−i)⋅1)=0\tfrac12\big(1\cdot i + (-i)\cdot 1\big) = 0, so they are orthogonal. ZZ has columns (1,0)(1, 0) and (0,−1)(0, -1). The shear and the projector each have a second column whose length is not 1.

Why not the others

  • C. The shear can be undone, yet it is not unitary: its second column (1,1)(1, 1) has length 2\sqrt2, so it sends ∣1⟩\ket1 to an unnormalized vector. Being invertible is not enough; a gate must also preserve length.
  • D. This projector keeps the ∣0⟩\ket0 part and sends ∣1⟩\ket1 to the zero vector, which is not a state. It cannot be undone: 35∣0⟩+45∣1⟩\tfrac35\ket0 + \tfrac45\ket1 and 35∣0⟩−45∣1⟩\tfrac35\ket0 - \tfrac45\ket1 both come out as 35∣0⟩\tfrac35\ket0. It describes the outcome 0 of a measurement in the 0/1 basis, and a measurement is not a gate.

Check yourself

You know a qubit is in the state ∣+⟩\ket+. Can a gate turn it into ∣0⟩\ket0?

  1. ANo: only a measurement can turn a superposition into a basis state.
  2. BYes: the gate HH does it, since H∣+⟩=∣0⟩H\ket+ = \ket0.
  3. COnly half the time: measure in the 0/1 basis and hope for 0.
  4. DYes: apply [1000]\begin{bmatrix}1&0\\0&0\end{bmatrix} and multiply by 2\sqrt2.
Hints
  1. Which gate turns ∣+⟩\ket+ into a basis state?
  2. A gate is one fixed, invertible map. It can carry one known state anywhere; what it cannot do is send every state to the same place.
Show the answer

Answer: B. Yes: the gate HH does it, since H∣+⟩=∣0⟩H\ket+ = \ket0.

H∣+⟩=∣0⟩H\ket+ = \ket0. This is the contrast with your guess: a gate is one fixed, invertible map, so it can carry one known state to ∣0⟩\ket0 (or anywhere else), but it cannot send every state to the same place.

Why not the others

  • A. Gates turn superpositions into basis states all the time: H∣+⟩=12(∣0⟩+∣1⟩)+12(∣0⟩−∣1⟩)=∣0⟩H\ket+ = \tfrac12(\ket0 + \ket1) + \tfrac12(\ket0 - \ket1) = \ket0, with certainty and no measurement. What a gate cannot do is reset an unknown state, because it would have to send different inputs to the same output.
  • C. A measurement gives 0 only half the time, and you would then need XX after a 1. No measurement is needed here: because the state is known, the gate HH turns it into ∣0⟩\ket0 every time.
  • D. That does give ∣0⟩\ket0 for this one input. But the projector is not unitary, so it is not a gate, and the factor 2\sqrt2 only works because you already knew the input. On ∣1⟩\ket1 the same recipe gives the zero vector.
Aside: every gate from three rotations

Every single-qubit unitary can be written as a global phase times three rotations:

U=eiα[e−iβ/200eiβ/2]×[cos⁡γ2−sin⁡γ2sin⁡γ2cos⁡γ2]×[e−iδ/200eiδ/2]\begin{aligned} U = e^{i\alpha}&\begin{bmatrix}e^{-i\beta/2}&0\\0&e^{i\beta/2}\end{bmatrix}\\ \times&\begin{bmatrix}\cos\frac{\gamma}{2}&-\sin\frac{\gamma}{2}\\ \sin\frac{\gamma}{2}&\cos\frac{\gamma}{2}\end{bmatrix}\\ \times&\begin{bmatrix}e^{-i\delta/2}&0\\0&e^{i\delta/2}\end{bmatrix} \end{aligned}

with real α\alpha, β\beta, γ\gamma, δ\delta. Careful: here α\alpha and β\beta are angles, not the amplitudes of a state. For now you only need the idea that a gate can be any unitary.

5 / 11 · Learn

Order matters

In a circuit diagram time runs left to right: the gate drawn first acts first. In algebra the matrix written next to the ket acts first. So a circuit that applies XX and then HH is written

HX∣ψ⟩(read right to left).HX\ket\psi \qquad (\text{read right to left}).

The order changes the answer. Take ZZ and XX on ∣0⟩\ket0:

ZX∣0⟩=Z∣1⟩=−∣1⟩,XZ∣0⟩=X∣0⟩=∣1⟩.\begin{aligned} ZX\ket0 &= Z\ket1 = -\ket1,\\ XZ\ket0 &= X\ket0 = \ket1. \end{aligned}

These two differ only by an overall sign, because ZX=−XZZX = -XZ. For XX and HH the difference shows up in a measurement: on input ∣+⟩\ket+, "XX then HH" gives ∣0⟩\ket0, but "HH then XX" gives ∣1⟩\ket1.

Check yourself

Compute ZX∣1⟩ZX\ket1.

  1. A∣0⟩\ket0
  2. B−∣0⟩-\ket0
  3. C∣1⟩\ket1
  4. D−∣1⟩-\ket1
Hints
  1. In ZX∣1⟩ZX\ket1, which gate stands next to the ket, and so acts first?
  2. Work outward from the ket: find X∣1⟩X\ket1 first, then apply ZZ to that result.
Show the answer

Answer: A. ∣0⟩\ket0

ZX∣1⟩=Z(X∣1⟩)=Z∣0⟩=∣0⟩ZX\ket1 = Z(X\ket1) = Z\ket0 = \ket0. In the other order XZ∣1⟩=X(−∣1⟩)=−∣0⟩XZ\ket1 = X(-\ket1) = -\ket0: the two products differ by a sign because ZX=−XZZX = -XZ.

Why not the others

  • B. That applies ZZ first: XZ∣1⟩=X(−∣1⟩)=−∣0⟩XZ\ket1 = X(-\ket1) = -\ket0. In ZX∣1⟩ZX\ket1 the gate next to the ket acts first, so X∣1⟩=∣0⟩X\ket1 = \ket0 and then Z∣0⟩=∣0⟩Z\ket0 = \ket0. Here the slip costs only a sign, but for pairs such as XX and HH it changes what you measure.
  • C. XX moves ∣1⟩\ket1 to ∣0⟩\ket0, and ZZ never turns one basis state into the other, so the answer must be a multiple of ∣0⟩\ket0.
  • D. The minus sign is what ZZ does to ∣1⟩\ket1, but by the time ZZ acts, XX has already moved the qubit to ∣0⟩\ket0: ZX∣1⟩=Z∣0⟩=∣0⟩ZX\ket1 = Z\ket0 = \ket0.

Check yourself

A circuit applies XX, then HH, then ZZ to ∣ψ⟩\ket\psi. Which expression gives its output?

  1. AXHZ∣ψ⟩XHZ\ket\psi
  2. BX∣ψ⟩+H∣ψ⟩+Z∣ψ⟩X\ket\psi + H\ket\psi + Z\ket\psi
  3. CZHX∣ψ⟩ZHX\ket\psi
  4. DHZX∣ψ⟩HZX\ket\psi
Hints
  1. Which gate does the qubit meet first, and where must that gate stand in the product?
  2. The first gate stands next to the ket; each later gate is written to the left of the one before it.
Show the answer

Answer: C. ZHX∣ψ⟩ZHX\ket\psi

Read the circuit left to right and write the matrices right to left: Z(H(X∣ψ⟩))=ZHX∣ψ⟩Z\big(H(X\ket\psi)\big) = ZHX\ket\psi.

Why not the others

  • A. This writes the gates in the order they appear in the diagram. In algebra the matrix next to the ket acts first, so the first gate, XX, must stand on the right: ZHX∣ψ⟩ZHX\ket\psi. The diagram reads like text, which is why this slip is so common.
  • B. Gates applied one after another compose by multiplication, not addition: each gate acts on the output of the one before it.
  • D. XX is in the right place, next to the ket, but HH and ZZ are swapped. After XX the qubit meets HH, so HH comes next to the left, and ZZ, the last gate, stands on the far left.

6 / 11 · Explore

Two Hadamards

Predict, then see

Two circuits start from ∣0⟩\ket0. A: HH, then HH, then measure in the 0/1 basis. B: HH, measure in that basis, HH, measure again. Over 100 runs, how often does each give 0 at the end?

  1. AA: all 100 runs. B: about 50.
  2. BBoth all 100: the second HH undoes the first whether or not you look in between.
  3. CA: none, because two HH gates make an XX. B: about 50.
  4. DBoth about 50: HH randomizes the qubit, and doing it twice leaves it random.

In a sentence or two: why does a measurement between the two HH gates change the result?

Show what happens

What happens: A. A: all 100 runs. B: about 50.

After one HH the qubit is in ∣+⟩\ket+: a definite state, not a coin. The second HH sends ∣0⟩\ket0 to 12(∣0⟩+∣1⟩)\tfrac{1}{\sqrt2}(\ket0+\ket1) and ∣1⟩\ket1 to 12(∣0⟩−∣1⟩)\tfrac{1}{\sqrt2}(\ket0-\ket1), so the two contributions to ∣1⟩\ket1 cancel and the two contributions to ∣0⟩\ket0 add; this cancellation is interference, and it makes HH∣0⟩=∣0⟩HH\ket0 = \ket0. A measurement in the 0/1 basis in between replaces ∣+⟩\ket+ with ∣0⟩\ket0 or ∣1⟩\ket1 at random, so there is nothing left to interfere. The second HH then gives ∣+⟩\ket+ or ∣−⟩\ket-, and the final result is 50/50.

Why not the others

  • B. The second HH undoes the first only if nothing happens in between. A measurement in the 0/1 basis replaces ∣+⟩\ket+ by ∣0⟩\ket0 or ∣1⟩\ket1; HH then gives ∣+⟩\ket+ or ∣−⟩\ket-, each 50/50 in that basis. Looking changed the state.
  • C. HH is not a "square root of NOT": HH=IHH = I, not XX, so circuit A returns the qubit to ∣0⟩\ket0.
  • D. That is the picture of HH as a coin flip. If it were right, A and B would behave the same, but A gives 0 every time. Only the measurement in B turns the qubit into a coin.
The arithmetic
H∣+⟩=12(H∣0⟩+H∣1⟩)=12(∣0⟩+∣1⟩)+12(∣0⟩−∣1⟩)=∣0⟩.\begin{aligned} H\ket+ &= \tfrac{1}{\sqrt2}\big(H\ket0 + H\ket1\big)\\ &= \tfrac12(\ket0 + \ket1) + \tfrac12(\ket0 - \ket1)\\ &= \ket0. \end{aligned}

As matrices:

HH=12[111−1][111−1]=12[2002]=I.\begin{aligned} HH &= \tfrac12\begin{bmatrix}1&1\\1&-1\end{bmatrix}\begin{bmatrix}1&1\\1&-1\end{bmatrix}\\ &= \tfrac12\begin{bmatrix}2&0\\0&2\end{bmatrix} = I. \end{aligned}

Check yourself

As a half turn about the axis between xx and zz, HH swaps the xx and zz axes of the Bloch sphere. Use that, or multiply the matrices: which single gate equals HZHHZH?

  1. AHH
  2. BXX
  3. CZZ
  4. DII
Hints
  1. What does HZHHZH do to ∣0⟩\ket0? Apply the gates one at a time, starting from the right.
  2. H∣0⟩=∣+⟩H\ket0 = \ket+ and Z∣+⟩=∣−⟩Z\ket+ = \ket-. Apply the last HH, then do the same for ∣1⟩\ket1 and compare with XX, ZZ, HH and II.
Show the answer

Answer: B. XX

HZH∣0⟩=HZ∣+⟩=H∣−⟩=∣1⟩HZH\ket0 = HZ\ket+ = H\ket- = \ket1 and HZH∣1⟩=HZ∣−⟩=H∣+⟩=∣0⟩HZH\ket1 = HZ\ket- = H\ket+ = \ket0, which is exactly what XX does. In the same way HXH=ZHXH = Z.

Why not the others

  • A. Test it on ∣0⟩\ket0: HZH∣0⟩=HZ∣+⟩=H∣−⟩=∣1⟩HZH\ket0 = HZ\ket+ = H\ket- = \ket1, while H∣0⟩=∣+⟩H\ket0 = \ket+.
  • C. It is tempting to cancel the two HH gates, but they are not next to each other, and matrices do not commute in general. Use the sphere: the first HH swaps the xx and zz axes, so a half turn about zz between two HH gates acts as a half turn about xx.
  • D. Only copies of a gate standing side by side cancel. Test it on ∣0⟩\ket0: HZH∣0⟩=HZ∣+⟩=H∣−⟩=∣1⟩HZH\ket0 = HZ\ket+ = H\ket- = \ket1, so it is not the identity.

7 / 11 · Worked example

Worked example: a gate, column by column

Worked example

H on a general state

Find H∣ψ⟩H\ket\psi for ∣ψ⟩=32∣0⟩+12∣1⟩\ket\psi = \tfrac{\sqrt3}{2}\ket0 + \tfrac12\ket1, and the probability of outcome 0 when you measure it in the 0/1 basis.

  1. Use linearity

    By linearity H∣ψ⟩=32H∣0⟩+12H∣1⟩H\ket\psi = \tfrac{\sqrt3}{2}H\ket0 + \tfrac12 H\ket1: the first column of HH times α\alpha, plus the second column times β\beta.

    H∣ψ⟩=32⋅12[11]+12⋅12[1−1]\begin{aligned} H\ket\psi &= \frac{\sqrt3}{2}\cdot\frac{1}{\sqrt2}\begin{bmatrix}1\\1\end{bmatrix}\\ &\quad + \frac12\cdot\frac{1}{\sqrt2}\begin{bmatrix}1\\-1\end{bmatrix} \end{aligned}
  2. Add the columns

    H∣ψ⟩=122[3+13−1]≈[0.9660.259]\begin{aligned} H\ket\psi &= \frac{1}{2\sqrt2}\begin{bmatrix}\sqrt3 + 1\\ \sqrt3 - 1\end{bmatrix}\\ &\approx \begin{bmatrix}0.966\\ 0.259\end{bmatrix} \end{aligned}

    Check the length: (3+1)2+(3−1)28=88=1\frac{(\sqrt3+1)^2 + (\sqrt3-1)^2}{8} = \frac{8}{8} = 1, as a unitary guarantees.

  3. Your turn

    Your turn

    What is the probability of outcome 0 when H∣ψ⟩H\ket\psi is measured in the 0/1 basis? An exact expression or a decimal is fine.

    P(0)=  ?P(0) = \;?

    Hints
    1. Which entry of H∣ψ⟩H\ket\psi belongs to ∣0⟩\ket0?
    2. Square its magnitude: (3+1)2=4+23(\sqrt3+1)^2 = 4 + 2\sqrt3.
    Show the answer

    Answer: P(0)=0.933P(0) = 0.933

    P(0)=(3+122)2=4+238=2+34≈0.933P(0) = \left(\frac{\sqrt3+1}{2\sqrt2}\right)^2 = \frac{4 + 2\sqrt3}{8} = \frac{2+\sqrt3}{4} \approx 0.933.

    Common slips

    • 34\frac{3}{4}: 34\tfrac34 is P(0)P(0) for the input ∣ψ⟩\ket\psi. The question is about the state after HH, whose amplitude on ∣0⟩\ket0 is 3+122\tfrac{\sqrt3+1}{2\sqrt2}.
    • 12\frac{1}{2}: A common idea is that a Hadamard always produces 50/50 odds. That holds for the inputs ∣0⟩\ket0 and ∣1⟩\ket1, but this input already leans toward ∣0⟩\ket0, and HH moves it closer still. Square the top entry: (3+122)2\big(\tfrac{\sqrt3+1}{2\sqrt2}\big)^2.
  4. Your turn: same method, new gate

    Your turn

    Compute X(32∣0⟩+i2∣1⟩)X\big(\tfrac{\sqrt3}{2}\ket0 + \tfrac{i}{2}\ket1\big) and enter the exact amplitudes.

    Hints
    1. What are the two columns of XX?
    2. Column 1 is ∣1⟩\ket1 and column 2 is ∣0⟩\ket0, so the output is α∣1⟩+β∣0⟩\alpha\ket1 + \beta\ket0.
    Show the answer

    Answer: 12 i∣0⟩+32∣1⟩\frac{1}{2}\,i|0\rangle + \frac{\sqrt{3}}{2}|1\rangle

    The columns of XX are ∣1⟩\ket1 and ∣0⟩\ket0, so the output is 32∣1⟩+i2∣0⟩\tfrac{\sqrt3}{2}\ket1 + \tfrac i2\ket0: the vector (i2,32)\big(\tfrac i2, \tfrac{\sqrt3}{2}\big).

Here is the same method once more, with different numbers.

Another worked example

H on another state

  1. Use linearity

    H(35∣0⟩+45∣1⟩)=35⋅12[11]+45⋅12[1−1]\begin{aligned} &H\big(\tfrac35\ket0 + \tfrac45\ket1\big)\\ &\quad= \frac35\cdot\frac{1}{\sqrt2}\begin{bmatrix}1\\1\end{bmatrix}\\ &\qquad + \frac45\cdot\frac{1}{\sqrt2}\begin{bmatrix}1\\-1\end{bmatrix} \end{aligned}
  2. Add the columns

    =152[3+43−4]=[7/(52)−1/(52)]≈[0.990−0.141]\begin{aligned} &= \frac{1}{5\sqrt2}\begin{bmatrix}3 + 4\\ 3 - 4\end{bmatrix}\\ &= \begin{bmatrix}7/(5\sqrt2)\\ -1/(5\sqrt2)\end{bmatrix}\\ &\approx \begin{bmatrix}0.990\\ -0.141\end{bmatrix} \end{aligned}
  3. Probabilities

    In the 0/1 basis, P(0)=4950=0.98P(0) = \frac{49}{50} = 0.98 and P(1)=150P(1) = \frac{1}{50}. They add to 1, as they must.

  4. The same idea with X

    The columns of XX are ∣1⟩\ket1 and ∣0⟩\ket0, so it just moves the amplitudes: X(35∣0⟩+45∣1⟩)=45∣0⟩+35∣1⟩X\big(\tfrac35\ket0 + \tfrac45\ket1\big) = \tfrac45\ket0 + \tfrac35\ket1.

Check yourself

Compute H(12∣0⟩+32∣1⟩)H\big(\tfrac12\ket0 + \tfrac{\sqrt3}{2}\ket1\big) and enter the exact amplitudes.

Hints
  1. Which column of HH does α\alpha multiply, and which does β\beta?
  2. Linearity: H(α∣0⟩+β∣1⟩)=α∣+⟩+β∣−⟩H(\alpha\ket0 + \beta\ket1) = \alpha\ket+ + \beta\ket-.
  3. The top entry is α+β2\tfrac{\alpha+\beta}{\sqrt2} and the bottom entry is α−β2\tfrac{\alpha-\beta}{\sqrt2}.
Show the answer

Answer: 0.966∣0⟩−0.259∣1⟩0.966|0\rangle - 0.259|1\rangle

By linearity, H(α∣0⟩+β∣1⟩)=α∣+⟩+β∣−⟩=α+β2∣0⟩+α−β2∣1⟩H(\alpha\ket0 + \beta\ket1) = \alpha\ket+ + \beta\ket- = \tfrac{\alpha+\beta}{\sqrt2}\ket0 + \tfrac{\alpha-\beta}{\sqrt2}\ket1. With α=12\alpha = \tfrac12 and β=32\beta = \tfrac{\sqrt3}{2} this is 1+322∣0⟩+1−322∣1⟩\tfrac{1+\sqrt3}{2\sqrt2}\ket0 + \tfrac{1-\sqrt3}{2\sqrt2}\ket1, that is, the vector (1+322, 1−322)\big(\tfrac{1+\sqrt3}{2\sqrt2},\ \tfrac{1-\sqrt3}{2\sqrt2}\big).

8 / 11 · Practice

Practice

Check yourself

A qubit in ∣+⟩\ket+ goes through ZZ and then HH. Enter the final state.

Hints
  1. Which gate does the qubit meet first?
  2. Apply the gates one at a time, starting with the first one in the circuit. As a product the circuit reads right to left.
  3. After ZZ the state is ∣−⟩\ket-.
Show the answer

Answer: ∣1⟩|1\rangle

In algebra the circuit is HZ∣+⟩HZ\ket+. Work from the ket outward: Z∣+⟩=∣−⟩Z\ket+ = \ket-, then H∣−⟩=∣1⟩H\ket- = \ket1. The final state is ∣1⟩\ket1. (Any overall sign is accepted: it is the same physical state.)

Check yourself

Is M=12[11i−i]M = \tfrac{1}{\sqrt2}\begin{bmatrix}1 & 1\\ i & -i\end{bmatrix} a valid gate?

  1. AUnitary: a valid gate
  2. BNot unitary: a column does not have length 1
  3. CNot unitary: both columns have length 1, but they are not orthogonal
Hints
  1. What two things must be true of the columns of a unitary?
  2. Each column must have length 1, and the two must be orthogonal. For the inner product, conjugate the first column's entries before multiplying.
  3. Each column has squared length 12+12=1\tfrac12 + \tfrac12 = 1.
Show the answer

Answer: A. Unitary: a valid gate

It is unitary. Each column has squared length 12+12=1\tfrac12 + \tfrac12 = 1. With the conjugate, 12(1⋅1+(−i)(−i))=12(1−1)=0\tfrac12\big(1\cdot 1 + (-i)(-i)\big) = \tfrac12(1 - 1) = 0. Skipping the conjugate would give 1 instead.

Why not the others

  • B. Both tests pass here. Each column has squared length 12+12=1\tfrac12 + \tfrac12 = 1. With the conjugate, 12(1⋅1+(−i)(−i))=12(1−1)=0\tfrac12\big(1\cdot 1 + (-i)(-i)\big) = \tfrac12(1 - 1) = 0. Skipping the conjugate would give 1 instead.
  • C. Both tests pass here. Each column has squared length 12+12=1\tfrac12 + \tfrac12 = 1. With the conjugate, 12(1⋅1+(−i)(−i))=12(1−1)=0\tfrac12\big(1\cdot 1 + (-i)(-i)\big) = \tfrac12(1 - 1) = 0. Skipping the conjugate would give 1 instead.

Check yourself

Build a circuit that takes ∣0⟩\ket0 to ∣−⟩\ket-. Any global phase is fine.

Build it in the circuit builder (gates: X, Z, H).

Hints
  1. Which gate turns a basis state into a ±\pm state, or back?
  2. HH swaps ∣0⟩↔∣+⟩\ket0 \leftrightarrow \ket+ and ∣1⟩↔∣−⟩\ket1 \leftrightarrow \ket-; XX swaps ∣0⟩↔∣1⟩\ket0 \leftrightarrow \ket1; ZZ swaps ∣+⟩↔∣−⟩\ket+ \leftrightarrow \ket-.
  3. One route is XX then HH.
Show the answer

One circuit that works: X on q0, then H on q0.

From ∣0⟩\ket0: X∣0⟩=∣1⟩X\ket0 = \ket1, then H∣1⟩=∣−⟩H\ket1 = \ket-. Another route is HH then ZZ: H∣0⟩=∣+⟩H\ket0 = \ket+, then Z∣+⟩=∣−⟩Z\ket+ = \ket-.

9 / 11 · Explain

Explain it

Explain it

People often say that HH "randomizes" a qubit. If that were the whole story, why would HH∣0⟩HH\ket0 give ∣0⟩\ket0 with certainty? Explain what actually happens.

Show a model answer

H∣0⟩H\ket0 is the definite state ∣+⟩=12(∣0⟩+∣1⟩)\ket+ = \tfrac{1}{\sqrt2}(\ket0 + \ket1), not a coin that has secretly landed. When the second HH acts, the ∣0⟩\ket0 part contributes +12+\tfrac12 to the amplitude of ∣1⟩\ket1 and the ∣1⟩\ket1 part contributes −12-\tfrac12, so they cancel, while the two contributions to ∣0⟩\ket0 add up to 1. Randomness appears only when you measure, and it depends on the basis: ∣+⟩\ket+ gives 50/50 in the 0/1 basis but a certain result in the ± basis.

A good answer covers

  • Says that H∣0⟩H\ket0 is a definite state with amplitudes (∣+⟩\ket+), not a random mixture of 0 and 1.
  • Explains that the second HH makes the contributions to ∣1⟩\ket1 cancel and those to ∣0⟩\ket0 add (interference), so HH=IHH = I.
  • Says that randomness appears only at measurement, and depends on the basis measured in.

10 / 11 · Mastery check

Mastery check

Check yourself

A qubit starts in ∣1⟩\ket1. You apply HH, then HH again, then measure in the 0/1 basis. What is the probability of outcome 0?

  1. A11
  2. B12\tfrac12
  3. C14\tfrac14
  4. D00
Show the answer

Answer: D. 00

H∣1⟩=∣−⟩H\ket1 = \ket- and H∣−⟩=∣1⟩H\ket- = \ket1, so HH∣1⟩=∣1⟩HH\ket1 = \ket1. A measurement in the 0/1 basis then gives 1 with certainty, so P(0)=0P(0) = 0.

Why not the others

  • A. This treats HH as a "square root of NOT", as if two of them made an XX and sent ∣1⟩\ket1 to ∣0⟩\ket0. They don't: HH=IHH = I, so the qubit ends in ∣1⟩\ket1, and the outcome is 1 every time.
  • B. A common picture is that HH randomizes a qubit, so a second HH leaves it random. But after one HH the qubit is in the definite state ∣−⟩\ket-, not a coin, and the second HH makes the two contributions to ∣0⟩\ket0 cancel. So HH∣1⟩=∣1⟩HH\ket1 = \ket1: the 50/50 appears only if you measure in the 0/1 basis between the gates.
  • C. It is tempting to multiply two 50/50 steps, 12×12\tfrac12 \times \tfrac12. But a gate does not add randomness step by step: it changes amplitudes, and amplitudes can cancel. Here the cancellation is complete, because HH=IHH = I.

Check yourself

A gate UU sends ∣0⟩↦12(∣0⟩+i∣1⟩)\ket0 \mapsto \tfrac{1}{\sqrt2}(\ket0 + i\ket1) and ∣1⟩↦12(i∣0⟩+∣1⟩)\ket1 \mapsto \tfrac{1}{\sqrt2}(i\ket0 + \ket1). Compute U∣−⟩U\ket- and enter its exact amplitudes, keeping any global phase factor (this asks for the vector, not just the physical state).

Show the answer

Answer: 1−i2∣0⟩+−1+i2∣1⟩\frac{1-i}{2}|0\rangle + \frac{-1+i}{2}|1\rangle

By linearity U∣−⟩=12(U∣0⟩−U∣1⟩)=1−i2∣0⟩−1−i2∣1⟩U\ket- = \tfrac{1}{\sqrt2}\big(U\ket0 - U\ket1\big) = \tfrac{1-i}{2}\ket0 - \tfrac{1-i}{2}\ket1.

Check yourself

The expression XH∣1⟩XH\ket1 describes a two-gate circuit acting on ∣1⟩\ket1. Which state comes out, up to an overall sign?

  1. A∣−⟩\ket-
  2. B∣+⟩\ket+
  3. C∣0⟩\ket0
  4. D∣1⟩\ket1
Show the answer

Answer: A. ∣−⟩\ket-

The gate next to the ket acts first: H∣1⟩=∣−⟩H\ket1 = \ket-, then X∣−⟩=−∣−⟩X\ket- = -\ket-. (Got ∣+⟩\ket+? That is the other order.)

Read XH∣1⟩XH\ket1 right to left. First HH: H∣1⟩=∣−⟩H\ket1 = \ket-. Then XX: X∣−⟩=−∣−⟩X\ket- = -\ket-. Up to its sign this is ∣−⟩\ket-.

Why not the others

  • B. That reads the product left to right, applying XX first. The gate next to the ket acts first: H∣1⟩=∣−⟩H\ket1 = \ket-, then X∣−⟩=−∣−⟩X\ket- = -\ket-. In a circuit diagram HH would be drawn on the left.
  • C. Work outward from the ket, one gate at a time: H∣1⟩=∣−⟩H\ket1 = \ket-, then X∣−⟩=−∣−⟩X\ket- = -\ket-.
  • D. Work outward from the ket, one gate at a time: H∣1⟩=∣−⟩H\ket1 = \ket-, then X∣−⟩=−∣−⟩X\ket- = -\ket-.

Check yourself

A gate UU sends ∣1⟩\ket1 to ∣+⟩\ket+. Which of these could U∣0⟩U\ket0 be, up to a global phase?

  1. A∣0⟩\ket0
  2. B∣1⟩\ket1
  3. C∣−⟩\ket-
  4. D∣+⟩\ket+
Show the answer

Answer: C. ∣−⟩\ket-

U∣1⟩U\ket1 and U∣0⟩U\ket0 are the columns of UU. A unitary has orthogonal columns of length 1, and the only state on the list orthogonal to ∣+⟩\ket+ is ∣−⟩\ket-.

Why not the others

  • A. U∣1⟩U\ket1 and U∣0⟩U\ket0 are the two columns of UU, and a unitary has orthogonal columns. But ∣⟨+|0⟩∣=12\abs{\braket{+}{0}} = \tfrac{1}{\sqrt2}, not 0.
  • B. U∣1⟩U\ket1 and U∣0⟩U\ket0 are the two columns of UU, and a unitary has orthogonal columns. But ∣⟨+|1⟩∣=12\abs{\braket{+}{1}} = \tfrac{1}{\sqrt2}, not 0.
  • D. Then UU would send both ∣0⟩\ket0 and ∣1⟩\ket1 to the same state, and nothing could undo it: from the output you could not tell which input you had. A unitary keeps orthogonal inputs orthogonal.

Check yourself

Is there a single-qubit gate that turns every input state into ∣0⟩\ket0, whatever the input was?

  1. ANo: every gate can be undone, so no gate can send two different inputs to the same output.
  2. BYes: a suitable fixed sequence of XX, ZZ and HH.
  3. CYes: the matrix [1000]\begin{bmatrix}1&0\\0&0\end{bmatrix} keeps the ∣0⟩\ket0 part, and you rescale the result.
  4. DNo: gates never change the probabilities of the measurement outcomes 0 and 1; only measurements do.
Show the answer

Answer: A. No: every gate can be undone, so no gate can send two different inputs to the same output.

Gates are unitary, and a unitary always has an inverse. A gate that sent both ∣0⟩\ket0 and ∣1⟩\ket1 to ∣0⟩\ket0 would have no inverse. Resetting an unknown qubit takes a measurement in the 0/1 basis, followed by XX if the result was 1.

Why not the others

  • B. Each of XX, ZZ and HH can be undone, so any sequence of them can be undone too. A sequence that sent both ∣0⟩\ket0 and ∣1⟩\ket1 to ∣0⟩\ket0 could not be undone: from the output alone you could not tell which input you had.
  • C. Keeping only the ∣0⟩\ket0 part is a natural first idea. But that matrix sends ∣1⟩\ket1 to the zero vector, which is not a state, and the rescaling factor depends on an input you may not know. A gate must keep every state normalized and be undoable, so it can never send two different inputs to the same output.
  • D. Gates change probabilities all the time: H∣0⟩=∣+⟩H\ket0 = \ket+ turns a certain outcome 0 into a 50/50 split. The real obstacle is different: a gate is invertible, so it cannot merge two different inputs into one output.

11 / 11 · Recap

Recap

From memory

Before reading the summary, write down what you remember. Pulling it back from memory is what makes it last.

  1. How do you find the matrix of a gate from what it does to ∣0⟩\ket0 and ∣1⟩\ket1, and how do you then apply it to α∣0⟩+β∣1⟩\alpha\ket0 + \beta\ket1?

    Show the key points
    • Column 1 is the image of ∣0⟩\ket0; column 2 is the image of ∣1⟩\ket1.
    • By linearity the output is α\alpha times column 1 plus β\beta times column 2.
    • Equivalently, each output entry is a row of the matrix times the input column.
  2. What makes a matrix a valid gate, and why can no gate reset every state to ∣0⟩\ket0?

    Show the key points
    • U†U=IU^\dagger U = I: columns of length 1 that are orthogonal (conjugate before multiplying).
    • A unitary preserves normalization and can be undone by U†U^\dagger.
    • A reset would send different inputs to the same output, which cannot be undone; resetting needs a measurement.
  3. Why does measuring HH∣0⟩HH\ket0 in the 0/1 basis give the outcome 0 every time, and what changes if you measure between the two HH gates?

    Show the key points
    • H∣0⟩=∣+⟩H\ket0 = \ket+ is a definite state, not a coin.
    • The second HH makes the contributions to ∣1⟩\ket1 (+12+\tfrac12 and −12-\tfrac12) cancel: interference, so HH=IHH = I.
    • A measurement in the 0/1 basis in between replaces ∣+⟩\ket+ by ∣0⟩\ket0 or ∣1⟩\ket1, so the final result is 50/50.

Key ideas

  1. A gate acts linearly, so what it does to ∣0⟩\ket0 and ∣1⟩\ket1 fixes everything. Its matrix has those two images as its columns.

  2. XX swaps the amplitudes, ZZ flips the sign of the ∣1⟩\ket1 amplitude, and HH sends ∣0⟩→∣+⟩\ket0 \to \ket+ and ∣1⟩→∣−⟩\ket1 \to \ket-. On the Bloch sphere each is a half turn: about xx, about zz, and about the axis between xx and zz.

  3. Every gate is unitary, U†U=IU^\dagger U = I, and every unitary is a gate. A unitary keeps states normalized and can always be undone, so no gate resets an unknown state.

  4. Circuits run left to right; matrix products act right to left. "XX then HH" is HXHX. Most gates do not commute: ZX=−XZZX = -XZ, and HX∣+⟩=∣0⟩HX\ket+ = \ket0 while XH∣+⟩=∣1⟩XH\ket+ = \ket1.

  5. HH=IHH = I because the second HH makes amplitudes cancel (interference). Sandwiching between two HH gates swaps XX and ZZ: HZH=XHZH = X, HXH=ZHXH = Z.

Go further

Free resources for when you want more than this lesson covers.

Math refreshers

The toolkit topics this lesson leans on.

Start the interactive lesson