Week 3 | one gate that makes entanglement, and what happens when you look at half of it | reading: Watrous §2.2
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.
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.
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:
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.
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
.
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.
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.
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.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.
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.
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?
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.
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?
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.
| state | both measure Z | both measure X | both measure Y |
|---|---|---|---|
| \(|\Phi^+\rangle\) | 1 | 1 | 0 |
| \(|\Phi^-\rangle\) | 1 | 0 | 1 |
| \(|\Psi^+\rangle\) | 0 | 1 | 1 |
| \(|\Psi^-\rangle\) | 0 | 0 | 0 |
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.
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?
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.
CNOT applies \(X\) to the target when the control is 1. Any single-qubit gate can be controlled in the same way.
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
\(\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.
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 \(+\)?
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
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 .
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?
\(\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.
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.
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).
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\).
cx(0, 1) is \(I\otimes|0\rangle\langle0|+X\otimes|1\rangle\langle1|\), and the textbook CNOT is Qiskit's cx(1, 0).cx(0, 1) sends \(|00\rangle,|01\rangle,|10\rangle,|11\rangle\) to \(|\Phi^+\rangle,|\Phi^-\rangle,|\Psi^+\rangle,-|\Psi^-\rangle\), an orthonormal basis of maximally entangled states.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.
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?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.probabilities_dict, measure) and the circuit library of controlled gates.all numbers and figures in these notes are computed in python (verify_L06.py) and follow the page theme