Quantum Computation & Cryptography · Lecture 6

Lecture 6
Two-Qubit Gates, Bell States and Partial Measurement

Week 3  |  one gate that makes entanglement, and what happens when you look at half of it  |  reading: Watrous §2.2

Cartoon: two envelopes that always agreeTwo stick figures stand far apart, each opening an envelope. Each envelope holds a card showing the digit 1. The left figure says: we always match. The right figure answers: sure, but try asking a different question. we always match! sure, but try askinga different question. 11
agreeing when asked the same question is easy; the quantum part shows up when the questions differ
In this lecture
  1. CNOT: the gate that entangles
  2. The four Bell states
  3. Measuring part of a system
  4. Bell correlations
  5. Controlled gates, CZ and phase kickback
  6. SWAP
  7. Try it in code
  8. Summary and exercises

Lecture 5 ended with a limitation: gates applied to qubits separately, \(U\otimes V\), never change the Schmidt coefficients, so they can never create entanglement. This lecture introduces the gates that act on two qubits at once. By the end you should be able to apply CNOT, CZ, controlled-\(U\) and SWAP in either ordering convention, prepare all four Bell states, compute what happens when only some qubits are measured, and explain what is (and is not) quantum about the correlations of a Bell pair.

1 — CNOT: the gate that entangles

The classical reversible XOR gate maps a pair of bits \((c,t)\) to \((c,\,t\oplus c)\): it flips the target bit \(t\) exactly when the control bit \(c\) is 1. It is its own inverse, and it permutes the four bit pairs. Its quantum version acts this way on basis states and extends by linearity.

definition 6.1 — controlled-NOT

The CNOT gate with control qubit \(c\) and target qubit \(t\), written \(\mathrm{CX}_{c\to t}\) and in Qiskit cx(c, t), maps each basis state by flipping the target bit when the control bit is 1:

$$|\ldots c\ldots t\ldots\rangle\;\longmapsto\;|\ldots c\ldots (t\oplus c)\ldots\rangle .$$

In a circuit it is drawn as a dot on the control wire joined to \(\oplus\) on the target wire.

A permutation of basis vectors is a unitary matrix (its columns are distinct basis vectors), so CNOT is a legitimate gate. Its \(4\times4\) matrix depends on which tensor factor is the control, and this is where Lecture 5's ordering convention bites. With kets written \(|q_1q_0\rangle\), the two possibilities are

$$\mathrm{CX}_{1\to0}=|0\rangle\langle0|\otimes I+|1\rangle\langle1|\otimes X=\begin{pmatrix}1&0&0&0\\[2pt] 0&1&0&0\\[2pt] 0&0&0&1\\[2pt] 0&0&1&0\end{pmatrix},$$ $$\mathrm{CX}_{0\to1}=I\otimes|0\rangle\langle0|+X\otimes|1\rangle\langle1|=\begin{pmatrix}1&0&0&0\\[2pt] 0&0&0&1\\[2pt] 0&0&1&0\\[2pt] 0&1&0&0\end{pmatrix}.$$

The first is the matrix most textbooks call "CNOT", because they put the control on the left. In Qiskit it is cx(1, 0). The circuit you will write most often, cx(0, 1), is the second matrix. The projector forms explain the matrices: \(|1\rangle\langle1|\otimes X\) says "if the left qubit is 1, apply \(X\) to the right one", and similarly for the other.

idea — check a two-qubit matrix on one basis state

To tell which convention a CNOT matrix uses, look at the column for \(|01\rangle\) (the second column). If it is \(|11\rangle\), the control is qubit 0: \(q_0=1\) flipped \(q_1\). If it is \(|01\rangle\), the control is qubit 1, which is 0 here, so nothing happened. Two seconds of checking saves an hour of debugging.

Kets are written \(|q_1q_0\rangle\). Qiskit's cx(1, 0) (control qubit 1, target qubit 0) sends the basis state \(|10\rangle\) to .

Find the control bit first: in \(|q_1q_0\rangle=|10\rangle\), qubit 1 is the left digit. If it is 1, flip the other digit.
The control \(q_1=1\), so the target \(q_0\) flips: \(|10\rangle\mapsto|11\rangle\). This is the third column of \(\mathrm{CX}_{1\to0}\).
example 6.1 — CNOT makes entanglement

Compute \(\mathrm{CX}_{1\to0}\big(|+\rangle\otimes|0\rangle\big)\), and check that the result is entangled.

By linearity, apply CNOT to each term of \(|+\rangle|0\rangle=\tfrac{1}{\sqrt2}(|00\rangle+|10\rangle)\). The control is the left qubit. In \(|00\rangle\) it is 0, so nothing happens; in \(|10\rangle\) it is 1, so the right bit flips:

$$\mathrm{CX}_{1\to0}\,\frac{|00\rangle+|10\rangle}{\sqrt2}=\frac{|00\rangle+|11\rangle}{\sqrt2}=|\Phi^+\rangle .$$

The coefficient matrix is \(\tfrac{1}{\sqrt2}I\), with determinant \(\tfrac12\neq0\), so the output is entangled, in fact maximally (Lecture 5). A product state went in and an entangled state came out. The superposition on the control was essential: on a basis-state control, CNOT just permutes basis states, and product states stay product.

2 — The four Bell states

The four Bell states are

$$|\Phi^\pm\rangle=\frac{|00\rangle\pm|11\rangle}{\sqrt2},\qquad |\Psi^\pm\rangle=\frac{|01\rangle\pm|10\rangle}{\sqrt2}.$$

Each is maximally entangled: its coefficient matrix is \(\tfrac{1}{\sqrt2}\) times a unitary, so both Schmidt coefficients are \(\tfrac{1}{\sqrt2}\). The letter tells you whether the two bits agree (\(\Phi\)) or differ (\(\Psi\)) in the standard basis; the sign is the relative phase. One circuit makes all four.

Tick every state that equals one of the four Bell states up to a global phase.

A global phase multiplies every amplitude by the same number. Compare each state with \(|\Phi^\pm\rangle\) and \(|\Psi^\pm\rangle\) term by term.
\(\tfrac1{\sqrt2}(|10\rangle-|01\rangle)=-|\Psi^-\rangle\) and \(\tfrac1{\sqrt2}(-|00\rangle-|11\rangle)=-|\Phi^+\rangle\). Being maximally entangled is not enough: the relative phase must be \(+1\) or \(-1\).
The Bell-state circuit and its four outputsA two-qubit circuit drawn with qubit 0 on top. Qubit 0 starts in |x> and qubit 1 in |y>. A Hadamard gate acts on qubit 0, then a CNOT with control qubit 0 and target qubit 1. A table lists the outputs: input y x = 00 gives Phi+ = (|00> + |11>)/sqrt2, 01 gives Phi- = (|00> - |11>)/sqrt2, 10 gives Psi+ = (|01> + |10>)/sqrt2, and 11 gives -Psi- = (|10> - |01>)/sqrt2.q0|x>q1|y>Hh(0)cx(0, 1)input |y x>output|00>Phi+ = (|00> + |11>)/sqrt2|01>Phi- = (|00> - |11>)/sqrt2|10>Psi+ = (|01> + |10>)/sqrt2|11>-Psi- = (|10> - |01>)/sqrt2
The Bell circuit in Qiskit's drawing convention: \(H\) on qubit 0, then cx(0, 1). Feeding in the four basis states \(|y\,x\rangle\) (qubit 1 in \(|y\rangle\), qubit 0 in \(|x\rangle\)) produces the four Bell states, the last one with an overall minus sign.
proposition 6.1 — the Bell circuit

For bits \(x,y\), the circuit \(\mathrm{CX}_{0\to1}(I\otimes H)\) maps \(|y\,x\rangle\) to \(\tfrac{1}{\sqrt2}\big(|y\,0\rangle+(-1)^x|\bar y\,1\rangle\big)\), where \(\bar y=1-y\). In particular

$$|00\rangle\mapsto|\Phi^+\rangle,\qquad |01\rangle\mapsto|\Phi^-\rangle,\qquad |10\rangle\mapsto|\Psi^+\rangle,\qquad |11\rangle\mapsto-|\Psi^-\rangle .$$

\(H\) on qubit 0 turns \(|y\rangle|x\rangle\) into \(\tfrac{1}{\sqrt2}\big(|y\rangle|0\rangle+(-1)^x|y\rangle|1\rangle\big)\), because \(H|x\rangle=\tfrac{1}{\sqrt2}(|0\rangle+(-1)^x|1\rangle)\). Then cx(0, 1) leaves the first term alone (qubit 0 is 0) and flips qubit 1 in the second (qubit 0 is 1), giving \(\tfrac{1}{\sqrt2}\big(|y\,0\rangle+(-1)^x|\bar y\,1\rangle\big)\). Substituting the four values of \((y,x)\): \((0,0)\) gives \(\tfrac{1}{\sqrt2}(|00\rangle+|11\rangle)\); \((0,1)\) gives \(\tfrac{1}{\sqrt2}(|00\rangle-|11\rangle)\); \((1,0)\) gives \(\tfrac{1}{\sqrt2}(|10\rangle+|01\rangle)\); and \((1,1)\) gives \(\tfrac{1}{\sqrt2}(|10\rangle-|01\rangle)=-|\Psi^-\rangle\).

The circuit is unitary and sends the orthonormal basis \(\{|00\rangle,|01\rangle,|10\rangle,|11\rangle\}\) to the four Bell states (up to a sign), so the Bell states also form an orthonormal basis of \(\mathbb C^4\), the Bell basis. Lecture 8 puts it to work in teleportation and superdense coding. Build it yourself in the sandbox.

Two-qubit sandbox

Add gates to a two-qubit circuit (qubit 0 on top) starting from \(|00\rangle\). The bars show \(|c_{ij}|^2\) for each basis state, labelled \(|q_1q_0\rangle\), with each amplitude printed above its bar. The readout gives the state and says whether it is entangled. The measure buttons measure one qubit with the rule of Section 3 (outcome drawn at random) and show the collapsed state. "Bell" loads \(H\) on qubit 0 followed by cx(0, 1).

3 — Measuring part of a system

Real algorithms rarely measure every qubit at once. Often we measure one register, look at the answer, and keep computing with the rest. Measuring some qubits of a joint state needs one more rule. It is the natural one: keep the terms consistent with what you saw, and renormalise.

rule 6.2 — partial measurement

Let \(|\psi\rangle=\sum_x c_x|x\rangle\) be an \(n\)-qubit state, with \(x\) running over bit strings \(x_{n-1}\cdots x_0\). Measuring only qubit \(k\) in the standard basis gives the outcome \(m\in\{0,1\}\) with probability

$$p(m)=\sum_{x:\;x_k=m}|c_x|^2 ,$$

and leaves the \(n\)-qubit state

$$|\psi_m\rangle=\frac{1}{\sqrt{p(m)}}\sum_{x:\;x_k=m}c_x|x\rangle .$$

Measuring several qubits is the same with "\(x_k=m\)" replaced by agreement on all of them. Equivalently, one can measure them one at a time.

Rule 6.2 is part of the measurement postulate (Watrous §2.2 states it in slightly different notation), but two consistency checks show it is the only sensible choice. First, measuring qubit \(k\) and then the others must reproduce the Born rule for measuring everything, and it does: outcome \(x\) needs \(x_k\) first, with probability \(p(x_k)\), and then \(x\) from \(|\psi_{x_k}\rangle\), with probability \(|c_x|^2/p(x_k)\); the product is \(|c_x|^2\). Second, on a product state \(|a\rangle\otimes|b\rangle\), measuring the right qubit gives \(m\) with probability \(|b_m|^2\) and leaves \(|a\rangle\otimes|m\rangle\): the unmeasured qubit is untouched, as it should be.

Let \(|\psi\rangle=\tfrac12\big(|00\rangle-|01\rangle+\sqrt2\,|10\rangle\big)\), kets \(|q_1q_0\rangle\). Only qubit 0 is measured. What is the probability of outcome 0?

Qubit 0 is the right digit. Add \(|c_x|^2\) over the terms whose right digit is 0.
The terms with \(q_0=0\) are \(|00\rangle\) and \(|10\rangle\), so \(p(0)=\tfrac14+\tfrac24=\tfrac34\). Grouping by the left digit instead would give \(\tfrac12\).
example 6.2 — measuring either qubit of a two-qubit state

Let \(|\psi\rangle=\tfrac13\big(|00\rangle+2|01\rangle+2|11\rangle\big)\). Find the outcome probabilities and post-measurement states when (a) qubit 1 is measured, and (b) qubit 0 is measured instead.

(a) Qubit 1 is the left digit. The terms with \(q_1=0\) are \(|00\rangle\) and \(|01\rangle\); the only term with \(q_1=1\) is \(|11\rangle\). So

$$p(0)=\frac{1+4}{9}=\frac59,\qquad |\psi_0\rangle=\frac{|00\rangle+2|01\rangle}{\sqrt5}=|0\rangle\otimes\frac{|0\rangle+2|1\rangle}{\sqrt5},$$ $$p(1)=\frac49,\qquad |\psi_1\rangle=|11\rangle .$$

(b) Now group by the right digit. Only \(|00\rangle\) has \(q_0=0\), so

$$\begin{aligned}p(0)&=\frac19, & |\psi_0\rangle&=|00\rangle,\\[4pt] p(1)&=\frac89, & |\psi_1\rangle&=\frac{2|01\rangle+2|11\rangle}{\sqrt8}=|+\rangle\otimes|1\rangle .\end{aligned}$$

Each time the probabilities add to 1, and each post-measurement state has the measured qubit in the state that was seen. Notice that in (a), outcome 0 leaves qubit 0 in the superposition \(\tfrac{1}{\sqrt5}(|0\rangle+2|1\rangle)\): measuring one qubit does not force the other to a basis state unless the two were correlated.

Measuring qubit 1 of (|00> + 2|01> + 2|11>)/3Left: bars of the four squared amplitudes, 1/9 for |00>, 4/9 for |01>, 0 for |10> and 4/9 for |11>. The bars with q1 = 0 are one colour and those with q1 = 1 another. Arrows lead to two outcomes. Outcome q1 = 0 happens with probability 5/9 and leaves bars 1/5 and 4/5 on |00> and |01>. Outcome q1 = 1 happens with probability 4/9 and leaves the single bar 1 on |11>.before: |c_ij|^2blue: q1 = 0, red: q1 = 11/9|00>4/9|01>0|10>4/9|11>q1 = 0 (prob 5/9)renormalised1/5|00>4/5|01>|10>|11>q1 = 1 (prob 4/9)renormalised|00>|01>0|10>1|11>measureq1or
Example 6.2(a) as bar charts. Measuring qubit 1 splits the four squared amplitudes into the terms with \(q_1=0\) and those with \(q_1=1\). The chosen group's total is the outcome probability; the group is then rescaled to total 1 and the other group is discarded.
example 6.3 — three qubits, measuring the middle one

Let \(|\psi\rangle=\tfrac12\big(\sqrt2\,|000\rangle+|011\rangle+|110\rangle\big)\), with kets written \(|q_2q_1q_0\rangle\). Measure qubit 1. What are the outcomes, and what is the state of qubits 2 and 0 afterwards?

Qubit 1 is the middle digit. It is 0 only in \(|000\rangle\), and 1 in \(|011\rangle\) and \(|110\rangle\). So

$$p(0)=\frac{2}{4}=\frac12,\quad |\psi_0\rangle=|000\rangle;$$ $$p(1)=\frac{1+1}{4}=\frac12,\quad |\psi_1\rangle=\frac{|011\rangle+|110\rangle}{\sqrt2}.$$

In \(|\psi_1\rangle\) the middle qubit is 1 in both terms, so it factors out, and qubits 2 and 0 are left in \(\tfrac{1}{\sqrt2}\big(|0\rangle_2|1\rangle_0+|1\rangle_2|0\rangle_0\big)\), which is \(|\Psi^+\rangle\) on that pair. With probability \(\tfrac12\), measuring the middle qubit left the outer two maximally entangled with each other. Measurement is not only something that destroys entanglement; in the right circumstances it can concentrate it where you want it.

The most famous case is a Bell pair. Measure qubit 0 of \(|\Phi^+\rangle\). By Rule 6.2 the outcome is 0 or 1 with probability \(\tfrac12\) each, and the state becomes \(|00\rangle\) or \(|11\rangle\): qubit 1 now has a definite value, equal to the outcome, however far away it is. Does that send a message? No. Before or after the measurement, someone holding only qubit 1 who measures it sees 0 or 1 with probability \(\tfrac12\) each; nothing they can observe on their own changes. The correlation becomes visible only when the two sides compare notes over an ordinary channel. This is the no-signalling property, and we will prove it in general with density matrices (Lecture 18).

Alice and Bob share \(|\Phi^+\rangle\). Alice measures her qubit and gets 1. Bob has not heard from her. Which statement is true?

Separate two questions: what is the state after Alice's outcome, and what can Bob observe on his own, averaged over the outcomes he does not know?
The state collapses to \(|11\rangle\), but from Bob's side it is \(|00\rangle\) or \(|11\rangle\) with probability \(\tfrac12\) each, which is the same 50/50 he had before. That is no-signalling.

4 — Bell correlations

Suppose Alice holds qubit 1 and Bob holds qubit 0 of a Bell pair, and each measures in the same basis: both in Z, both in X, or both in Y (Lecture 4: measure in X by applying \(H\) first, in Y by applying \(S^\dagger\) then \(H\)). The table gives the probability that their two bits agree, computed exactly from the states.

stateboth measure Zboth measure Xboth measure Y
\(|\Phi^+\rangle\)110
\(|\Phi^-\rangle\)101
\(|\Psi^+\rangle\)011
\(|\Psi^-\rangle\)000

In every case each person's own bit is a fair coin, yet the pair is perfectly correlated (agreement 1) or perfectly anticorrelated (agreement 0). For \(|\Phi^+\rangle\) the X column can be read off from the identity

$$|\Phi^+\rangle=\frac{|{+}{+}\rangle+|{-}{-}\rangle}{\sqrt2},$$

which you can check by expanding the right-hand side: in the X basis the state looks exactly as it does in the Z basis. The singlet \(|\Psi^-\rangle\) is anticorrelated in every basis; it is the state used in most textbook discussions of Bell's theorem.

proposition 6.3 — product states cannot do this

If \(|\psi\rangle=|a\rangle\otimes|b\rangle\) is a product state and a Z measurement of both qubits always gives agreeing bits, then the outcome is not random: it is \(00\) with certainty or \(11\) with certainty.

The outcome probabilities are \(p_{ij}=|a_i|^2|b_j|^2\), so \(p_{00}\,p_{11}=|a_0a_1b_0b_1|^2=p_{01}\,p_{10}\). Always agreeing means \(p_{01}=p_{10}=0\), hence \(p_{00}\,p_{11}=0\): one of the two agreeing outcomes has probability 0 and the other has probability 1.

So "random but always equal" is impossible for unentangled quantum states. Is it impossible classically? No, and it is important to be honest about this. Put a card reading 0 in two envelopes, or 1 in both, by a fair coin toss, and mail one to Alice and one to Bob: they agree every time and each sees a fair coin. That reproduces the Z column. If each envelope carries two identical answers, one for "Z?" and one for "X?", it reproduces the X column too. Perfect agreement in a few fixed bases is shared randomness, nothing more.

What no envelope scheme can reproduce appears when Alice and Bob measure along different directions. For \(|\Phi^+\rangle\), measuring along directions in the x–z plane of the Bloch sphere at angles \(a\) and \(b\) from the z-axis, the agreement probability is \(\cos^2\!\big(\tfrac{a-b}{2}\big)\), which is about 0.854 at \(a-b=\pi/4\). Lecture 8 proves (the CHSH inequality) that these values, for suitable angles, are beyond every classical model with pre-agreed answers. The widget lets you collect the data.

Alice and Bob share \(|\Phi^+\rangle\). Alice measures along the z-axis (\(a=0\)) and Bob along the direction in the x–z plane at \(b=2\pi/3\) from the z-axis. What is the probability that their bits agree?

Use \(\cos^2\!\big(\tfrac{a-b}{2}\big)\). Remember to halve the angle between the two directions.
\(\cos^2(\pi/3)=\tfrac14\). At \(120^\circ\) apart they disagree three times out of four, even though each bit on its own is still a fair coin.

Bell correlation experiment

Choose a Bell state and a measurement for each side: Z, X, Y, or a direction in the x–z plane at angle \(\theta\) from the z-axis. Each click runs more shots. The bars are sampled counts; the tick marks are exact probabilities. Compare the agreement fraction with \(\cos^2\!\big(\tfrac{\theta_A-\theta_B}{2}\big)\) for \(|\Phi^+\rangle\).

crypto connection — correlations you can check

In the E91 key-distribution protocol (Lecture 20), Alice and Bob share Bell pairs and each measures in a randomly chosen basis. Rounds where the bases match give identical key bits, exactly the Z and X columns above. Rounds where they differ are not wasted: they are used to test the CHSH inequality. A value above the classical limit certifies that the pairs are genuinely entangled, and by Lecture 5's monogamy remark, that nobody else holds a copy of the answers. The envelope model is precisely what an eavesdropper who pre-arranged the outcomes would be running, and the test catches it.

5 — Controlled gates, CZ and phase kickback

CNOT applies \(X\) to the target when the control is 1. Any single-qubit gate can be controlled in the same way.

definition 6.2 — controlled-U

For a single-qubit gate \(U\), the controlled-\(U\) gate with control qubit 1 and target qubit 0 is

$$C_U=|0\rangle\langle0|\otimes I+|1\rangle\langle1|\otimes U=\begin{pmatrix}I&0\\[2pt] 0&U\end{pmatrix},$$

a block-diagonal \(4\times4\) unitary: it applies \(U\) to qubit 0 exactly on the part of the state where qubit 1 is \(|1\rangle\). With the roles swapped it is \(I\otimes|0\rangle\langle0|+U\otimes|1\rangle\langle1|\). In Qiskit, any gate object has a controlled version, for example qc.append(SGate().control(1), [c, t]) with SGate imported from qiskit.circuit.library, and the common ones have their own methods: cx, cy, cz, ch, cp, crz.

The controlled-Z gate is the simplest of all:

$$\mathrm{CZ}=\begin{pmatrix}I&0\\[2pt] 0&Z\end{pmatrix}=\mathrm{diag}(1,1,1,-1).$$

It multiplies \(|11\rangle\) by \(-1\) and does nothing else. That description does not mention which qubit is the control, and indeed CZ is symmetric: controlling on qubit 1 or on qubit 0 gives the same matrix. That is why it is drawn as two dots joined by a line.

Kets are written \(|q_1q_0\rangle\). CZ is applied to \(|1\rangle|+\rangle\) (qubit 1 in \(|1\rangle\), qubit 0 in \(|+\rangle\)). The result is

Write \(|1\rangle|+\rangle=\tfrac1{\sqrt2}\big(|10\rangle+|11\rangle\big)\) and apply \(\mathrm{diag}(1,1,1,-1)\).
Only the \(|11\rangle\) term changes sign, giving \(\tfrac1{\sqrt2}\big(|10\rangle-|11\rangle\big)=|1\rangle|-\rangle\): qubit 0 becomes \(|-\rangle\).
Three two-qubit circuit identitiesTop left: CZ drawn as two joined dots is symmetric, so a CZ with control on qubit 0 equals a CZ with control on qubit 1. Top right: CZ equals a CNOT with Hadamard gates before and after on the target wire. Bottom: SWAP, drawn as two crosses joined by a line, equals three CNOTs whose control alternates: control qubit 1, then control qubit 0, then control qubit 1.(a) CZ is symmetricq0q1=cz(0,1) = cz(1,0)(b) CZ = (H on target) CNOT (H on target)=HH(c) SWAP = three CNOTsq0q1=cx(1,0)cx(0,1)cx(1,0)|a b>-> |a, a xor b>-> |b, a xor b>-> |b a>(kets written |q1 q0>)
Three circuit identities. (a) CZ has no preferred control. (b) Conjugating the target of a CNOT by \(H\) turns it into CZ (Proposition 6.4). (c) SWAP is three CNOTs with alternating controls (Proposition 6.6).
proposition 6.4 — CZ from CNOT

\(\mathrm{CZ}=(I\otimes H)\,\mathrm{CX}_{1\to0}\,(I\otimes H)\). Equivalently, since CZ is symmetric, \(\mathrm{CZ}=(H\otimes I)\,\mathrm{CX}_{0\to1}\,(H\otimes I)\): Hadamards on both sides of the target turn a CNOT into a CZ.

Here \(I\otimes H\) is block diagonal with blocks \(H,H\), and \(\mathrm{CX}_{1\to0}\) is block diagonal with blocks \(I,X\). Block-diagonal matrices multiply block by block, so

$$\begin{aligned}(I\otimes H)\,\mathrm{CX}_{1\to0}\,(I\otimes H)&=\begin{pmatrix}H&0\\[2pt] 0&H\end{pmatrix}\begin{pmatrix}I&0\\[2pt] 0&X\end{pmatrix}\begin{pmatrix}H&0\\[2pt] 0&H\end{pmatrix}\\[4pt] &=\begin{pmatrix}H^2&0\\[2pt] 0&HXH\end{pmatrix}=\begin{pmatrix}I&0\\[2pt] 0&Z\end{pmatrix}=\mathrm{CZ},\end{aligned}$$

using \(H^2=I\) and \(HXH=Z\). If you prefer to see all sixteen entries, the first two factors multiply to

$$\mathrm{CX}_{1\to0}\,(I\otimes H)=\frac{1}{\sqrt2}\begin{pmatrix}1&1&0&0\\[2pt] 1&-1&0&0\\[2pt] 0&0&1&-1\\[2pt] 0&0&1&1\end{pmatrix},$$

and multiplying on the left by \(I\otimes H\) gives \(\mathrm{diag}(1,1,1,-1)\). The second form follows by exchanging the names of the two qubits, which turns \(\mathrm{CX}_{1\to0}\) into \(\mathrm{CX}_{0\to1}\), \(I\otimes H\) into \(H\otimes I\), and leaves CZ unchanged.

Symmetry of CZ has a surprising consequence. In CZ neither qubit is "the control", so a CNOT, which is CZ in disguise, must also be able to act "backwards". That is phase kickback.

theorem 6.5 — phase kickback

Let \(|u\rangle\) be an eigenvector of \(U\), with \(U|u\rangle=e^{i\lambda}|u\rangle\). Then, for any control state,

$$C_U\Big(\big(\alpha|0\rangle+\beta|1\rangle\big)\otimes|u\rangle\Big)=\big(\alpha|0\rangle+e^{i\lambda}\beta|1\rangle\big)\otimes|u\rangle .$$

The target is unchanged, and the control has had the phase gate \(P(\lambda)=\mathrm{diag}(1,e^{i\lambda})\) applied to it.

By linearity, \(C_U\) sends \(\alpha|0\rangle|u\rangle\) to itself and \(\beta|1\rangle|u\rangle\) to \(\beta|1\rangle\,U|u\rangle=e^{i\lambda}\beta|1\rangle|u\rangle\). The scalar \(e^{i\lambda}\) can be moved onto either factor of the tensor product (Proposition 5.1), and we move it onto the control. Adding the two terms gives the claim.

The controlled phase gate \(C_{P(\pi/3)}\) (control qubit 1) is applied to \(|+\rangle\otimes|1\rangle\). The control qubit is then measured in the X basis. What is the probability of the outcome \(+\)?

\(|1\rangle\) is an eigenvector of \(P(\pi/3)\) with eigenvalue \(e^{i\pi/3}\). By Theorem 6.5 the control becomes \(\tfrac1{\sqrt2}(|0\rangle+e^{i\pi/3}|1\rangle)\); then \(P(+)=\big|\tfrac{1+e^{i\pi/3}}{2}\big|^2\).
\(P(+)=\tfrac{1+\cos(\pi/3)}{2}=\tfrac34\). The target never changed, yet the control's X statistics did: the phase was kicked back.
example 6.4 — the CNOT that flips its control

Apply \(\mathrm{CX}_{1\to0}\) (control qubit 1) to \(|+\rangle\otimes|-\rangle\).

The target is \(|-\rangle\), an eigenvector of \(X\) with eigenvalue \(-1=e^{i\pi}\), since \(X|-\rangle=\tfrac{1}{\sqrt2}(|1\rangle-|0\rangle)=-|-\rangle\). By Theorem 6.5 the control receives \(P(\pi)=Z\):

$$\mathrm{CX}_{1\to0}\big(|+\rangle|-\rangle\big)=\big(Z|+\rangle\big)|-\rangle=|-\rangle|-\rangle .$$

The "target" did not change at all, and the "control" did. Which qubit controls which depends on the basis you look in. Deutsch's algorithm (Lecture 9) and phase estimation (Lecture 14) are built on exactly this effect.

Kickback also settles the two loose ends from Lecture 4, where we saw that \(R_z(\lambda)\) and \(P(\lambda)\) differ only by a global phase, and that a full turn \(R_{\vec n}(2\pi)=-I\) is invisible on one qubit. Controlling a gate turns its global phase into a relative phase on the control:

$$C_{e^{i\alpha}U}=\begin{pmatrix}I&0\\[2pt] 0&e^{i\alpha}U\end{pmatrix}=\big(P(\alpha)\otimes I\big)\,C_U .$$

So \(C_{R_z(\lambda)}=\big(P(-\lambda/2)\otimes I\big)\,C_{P(\lambda)}\): the two controlled gates differ by a phase gate on the control, which is observable. That is why Qiskit keeps both crz and cp. And a controlled full turn is

$$C_{R_{\vec n}(2\pi)}=C_{-I}=P(\pi)\otimes I=Z\otimes I ,$$

a \(Z\) gate on the control. The minus sign that a single qubit could never detect now flips the relative phase of another qubit, and an interference experiment on the control will see it.

The gate \(iI\) does nothing observable to one qubit. Its controlled version \(C_{iI}\), with control qubit 1, equals .

Use \(C_{e^{i\alpha}U}=\big(P(\alpha)\otimes I\big)\,C_U\) with \(U=I\), and write \(i=e^{i\alpha}\).
\(i=e^{i\pi/2}\) and \(C_I=I\), so \(C_{iI}=P(\pi/2)\otimes I=S\otimes I\): the global phase of the target became a relative phase on the control.

6 — SWAP

The SWAP gate exchanges the states of two qubits: \(\mathrm{SWAP}\,|a\rangle|b\rangle=|b\rangle|a\rangle\). On basis states it fixes \(|00\rangle\) and \(|11\rangle\) and exchanges \(|01\rangle\) with \(|10\rangle\), so

$$\mathrm{SWAP}=\begin{pmatrix}1&0&0&0\\[2pt] 0&0&1&0\\[2pt] 0&1&0&0\\[2pt] 0&0&0&1\end{pmatrix}.$$

SWAP is applied to \(\big(0.6|0\rangle+0.8|1\rangle\big)\otimes|0\rangle\) (qubit 1 on the left). Qubit 0 is then measured. What is the probability of outcome 1?

SWAP moves the state of qubit 1 onto qubit 0. Which single-qubit state does qubit 0 hold afterwards?
After SWAP the state is \(|0\rangle\otimes(0.6|0\rangle+0.8|1\rangle)\), so qubit 0 gives 1 with probability \(0.8^2=0.64\).
proposition 6.6 — SWAP from three CNOTs

\(\mathrm{SWAP}=\mathrm{CX}_{1\to0}\,\mathrm{CX}_{0\to1}\,\mathrm{CX}_{1\to0}\) (and also with the roles of the qubits exchanged).

Both sides are linear, so it is enough to compare them on the basis states \(|a\,b\rangle\), with qubit 1 in \(|a\rangle\) and qubit 0 in \(|b\rangle\). Reading the circuit left to right: the first CNOT replaces \(b\) by \(a\oplus b\); the second replaces \(a\) by \(a\oplus(a\oplus b)=b\); the third replaces \(a\oplus b\) by \((a\oplus b)\oplus b=a\):

$$|a\,b\rangle\;\mapsto\;|a,\;a\oplus b\rangle\;\mapsto\;|b,\;a\oplus b\rangle\;\mapsto\;|b\,a\rangle .$$

This is the classical trick for swapping two variables without a temporary one, run on qubits.

idea — why SWAPs cost so much on hardware

On most superconducting chips a two-qubit gate is only available between neighbouring qubits. To apply a CNOT between distant qubits, the compiler moves their states next to each other with SWAPs, and each SWAP costs three CNOTs. Two-qubit gates are also the noisiest operations on current devices, typically ten times or more worse than single-qubit gates, so routing can dominate the error budget of a circuit. Lecture 19 returns to this when we run circuits on IBM hardware.

7 — Try it in code

tool — a Bell pair: state, matrix, and samples (tested with qiskit 2.5)
import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector, Operator
from qiskit.primitives import StatevectorSampler

def probs(sv, qargs=None):                    # tidy printing of probabilities_dict()
    return {str(k): round(float(v), 4) for k, v in sv.probabilities_dict(qargs).items()}

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)                                   # control qubit 0, target qubit 1
print(qc.draw())
print(probs(Statevector.from_instruction(qc)))

cx01 = QuantumCircuit(2); cx01.cx(0, 1)
print(Operator(cx01).data.real.astype(int))   # rows/columns in the order 00, 01, 10, 11

qc.measure_all()
counts = StatevectorSampler(seed=11).run([qc], shots=1000).result()[0].data.meas.get_counts()
print(dict(sorted(counts.items())))
     ┌───┐
q_0: ┤ H ├──■──
     └───┘┌─┴─┐
q_1: ─────┤ X ├
          └───┘
{'00': 0.5, '11': 0.5}
[[1 0 0 0]
 [0 0 0 1]
 [0 0 1 0]
 [0 1 0 0]]
{'00': 520, '11': 480}

The drawing puts qubit 0 on top, as in our figures. The matrix is \(\mathrm{CX}_{0\to1}\) from Section 1, with its telltale second column. The 1000 shots contain only 00 and 11, in a ratio consistent with \(\tfrac12\) each (Lecture 1: the standard error with 1000 shots is about 0.016).

tool — marginal probabilities and a partial measurement
import numpy as np
from qiskit.quantum_info import Statevector

def probs(sv, qargs=None):
    return {str(k): round(float(v), 4) for k, v in sv.probabilities_dict(qargs).items()}

psi = Statevector(np.array([1, 2, 0, 2]) / 3)      # (|00> + 2|01> + 2|11>)/3, Example 6.2
print(probs(psi, [1]))                             # distribution of qubit 1 alone
print(probs(psi, [0]))                             # distribution of qubit 0 alone

psi.seed(1)                                        # fix the random outcome, for reproducibility
outcome, post = psi.measure([1])                   # measure qubit 1 only
print(outcome, post.data.real.round(4))
{'0': 0.5556, '1': 0.4444}
{'0': 0.1111, '1': 0.8889}
0 [0.4472 0.8944 0.     0.    ]

Passing a list of qubits to probabilities_dict gives the distribution of just those qubits: \(\tfrac59,\tfrac49\) and \(\tfrac19,\tfrac89\), as in Example 6.2. Statevector.measure([1]) performs Rule 6.2: here it drew outcome 0 and returned \(\tfrac{1}{\sqrt5}(|00\rangle+2|01\rangle)\), since \(1/\sqrt5\approx0.4472\).

8 — Summary and exercises

summary

Next time: circuits as a model of computation, reversible classical logic inside quantum circuits, and universal gate sets. HW3 (Lectures 5 and 6) is due at the start of Week 4.

exercises 6
  1. Apply Qiskit's cx(0, 1) to \(|01\rangle\), to \(|+\rangle\otimes|1\rangle\) and to \(|1\rangle\otimes|+\rangle\) (kets written \(|q_1q_0\rangle\)). Which outputs are entangled?
  2. Let \(|\psi\rangle=\tfrac{1}{\sqrt6}\big(|00\rangle+|01\rangle+2|11\rangle\big)\). Find the outcome probabilities and post-measurement states when qubit 0 is measured, and again when qubit 1 is measured instead. Write each post-measurement state as a product where possible.
  3. Show that \((H\otimes H)\,\mathrm{CX}_{0\to1}\,(H\otimes H)=\mathrm{CX}_{1\to0}\): conjugating both qubits by \(H\) reverses a CNOT. (Hint: use Proposition 6.4 twice rather than multiplying \(4\times4\) matrices.)
  4. Apply the controlled-\(S\) gate, with control qubit 1 and target qubit 0, to \(|+\rangle\otimes|1\rangle\). Which single-qubit state does the control end up in?
  5. Show that \(|\Phi^+\rangle=\tfrac{1}{\sqrt2}\big(|{+i}\rangle|{-i}\rangle+|{-i}\rangle|{+i}\rangle\big)\), and use it to explain the Y column of the correlation table for \(|\Phi^+\rangle\).
  6. Show that \(\mathrm{SWAP}\,(|a\rangle\otimes|b\rangle)=|b\rangle\otimes|a\rangle\) for all single-qubit states \(|a\rangle,|b\rangle\), not just basis states. Is SWAP an entangling gate?
  1. cx(0, 1) flips \(q_1\) when \(q_0=1\). So \(|01\rangle\mapsto|11\rangle\). For \(|+\rangle\otimes|1\rangle\), qubit 0 is 1 in both terms, so \(X\) acts on qubit 1: \(X|+\rangle=|+\rangle\), and the state is unchanged. For \(|1\rangle\otimes|+\rangle=\tfrac{1}{\sqrt2}(|10\rangle+|11\rangle)\), the second term becomes \(|01\rangle\), giving \(\tfrac{1}{\sqrt2}(|10\rangle+|01\rangle)=|\Psi^+\rangle\). Only the last is entangled.
  2. Qubit 0 (right digit): only \(|00\rangle\) has \(q_0=0\), so $$p(0)=\frac16,\quad |\psi_0\rangle=|00\rangle;$$ $$p(1)=\frac{1+4}{6}=\frac56,\quad |\psi_1\rangle=\frac{|01\rangle+2|11\rangle}{\sqrt5}=\frac{|0\rangle+2|1\rangle}{\sqrt5}\otimes|1\rangle .$$ Qubit 1 (left digit): $$p(0)=\frac{1+1}{6}=\frac13,\quad |\psi_0\rangle=\frac{|00\rangle+|01\rangle}{\sqrt2}=|0\rangle\otimes|+\rangle;$$ $$p(1)=\frac46=\frac23,\quad |\psi_1\rangle=|11\rangle .$$
  3. Proposition 6.4 with the qubits in each role gives \(\mathrm{CX}_{0\to1}=(H\otimes I)\,\mathrm{CZ}\,(H\otimes I)\) and \(\mathrm{CX}_{1\to0}=(I\otimes H)\,\mathrm{CZ}\,(I\otimes H)\) (multiply each identity by the Hadamards on both sides, using \(H^2=I\)). Then $$\begin{aligned}(H\otimes H)\,\mathrm{CX}_{0\to1}\,(H\otimes H)&=(H\otimes H)(H\otimes I)\,\mathrm{CZ}\,(H\otimes I)(H\otimes H)\\[4pt] &=(I\otimes H)\,\mathrm{CZ}\,(I\otimes H)=\mathrm{CX}_{1\to0},\end{aligned}$$ because \((H\otimes H)(H\otimes I)=H^2\otimes H=I\otimes H\) by Proposition 5.1. In the X basis the control and target of a CNOT trade places, which is Example 6.4 seen from the other side.
  4. The target \(|1\rangle\) is an eigenvector of \(S\) with eigenvalue \(i=e^{i\pi/2}\). By Theorem 6.5 the control receives \(P(\pi/2)=S\): the output is \(S|+\rangle\otimes|1\rangle=\tfrac{1}{\sqrt2}(|0\rangle+i|1\rangle)\otimes|1\rangle=|{+i}\rangle\otimes|1\rangle\).
  5. With \(|{\pm i}\rangle=\tfrac{1}{\sqrt2}(|0\rangle\pm i|1\rangle)\), $$|{+i}\rangle|{-i}\rangle=\tfrac12\big(|00\rangle-i|01\rangle+i|10\rangle+|11\rangle\big),$$ $$|{-i}\rangle|{+i}\rangle=\tfrac12\big(|00\rangle+i|01\rangle-i|10\rangle+|11\rangle\big).$$ Adding and dividing by \(\sqrt2\) gives \(\tfrac{1}{\sqrt2}(|00\rangle+|11\rangle)=|\Phi^+\rangle\). In the Y basis the state is an equal superposition of the two disagreeing outcomes \((+i,-i)\) and \((-i,+i)\), so Y measurements on both sides always disagree: agreement probability 0.
  6. Write \(|a\rangle|b\rangle=\sum_{i,j}a_ib_j|ij\rangle\). SWAP sends \(|ij\rangle\) to \(|ji\rangle\), so by linearity the result is \(\sum_{i,j}a_ib_j|ji\rangle=\sum_{i,j}b_ja_i|ji\rangle=|b\rangle|a\rangle\). SWAP maps every product state to a product state, so it creates no entanglement, even though it cannot be written as \(U\otimes V\). (It is still useful: it moves entanglement around.)

Reading and sources

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