Skip to main content
KetQat

Appearance

Lesson 550 min

Multiple qubits and entanglement

Tensor products, two-qubit gates, Bell states, and why entanglement is not correlation alone.

After this lesson you can

  • Build multi-qubit states and operators with tensor products under the course's qubit-ordering convention
  • Write the matrices of CX, CZ and SWAP and apply them to two-qubit states
  • Prepare the four Bell states with a circuit and verify them by calculation
  • Decide whether a two-qubit pure state is entangled with the Schmidt (rank or determinant) test
  • Explain, using the CHSH inequality, what distinguishes entanglement from classical correlation, and state the classical and Tsirelson bounds
  • Prove the no-cloning theorem

Read first: Measurement and probability

From one qubit to two

Two classical bits have four possible values, 00, 01, 10 and 11. Two qubits have one complex amplitude for each of them:

∣ψ⟩=c00∣00⟩+c01∣01⟩+c10∣10⟩+c11∣11⟩,∑x,y∣cxy∣2=1.\ket\psi = c_{00}\ket{00} + c_{01}\ket{01} + c_{10}\ket{10} + c_{11}\ket{11}, \qquad \sum_{x,y}\abs{c_{xy}}^2 = 1 .

With nn qubits there are 2n2^n amplitudes. This exponential growth is both the promise of quantum computing and the reason it is hard to simulate, which the next lesson quantifies. In this lesson we learn how to build such states from single qubits, how two-qubit gates act on them, and what new phenomenon appears: entanglement. Background for the whole lesson is in Nielsen and Chuang, Sections 1.3, 2.1.7 and 2.6.

Tensor products and the ordering convention

If qubit 0 is in ∣a⟩=(a0,a1)T\ket a = (a_0, a_1)^T and qubit 1 is in ∣b⟩=(b0,b1)T\ket b = (b_0, b_1)^T, the joint state is their tensor product

∣a⟩⊗∣b⟩=(a0b0a0b1a1b0a1b1),\ket a\otimes\ket b = \begin{pmatrix} a_0 b_0\\ a_0 b_1\\ a_1 b_0\\ a_1 b_1\end{pmatrix},

also written ∣a⟩∣b⟩\ket a\ket b. For basis states this gives ∣x⟩⊗∣y⟩=∣xy⟩\ket x\otimes\ket y = \ket{xy}. Convention used in this course: in ∣q0q1…qn−1⟩\ket{q_0q_1\dots q_{n-1}} the leftmost label is qubit 0, and qubit 0 is the most significant bit of the index. So ∣01⟩\ket{01} is entry 1 and ∣10⟩\ket{10} is entry 2 of the vector, and numpy.kron(a, b) produces exactly this layout when a is qubit 0. Some libraries use the opposite (little-endian) convention, in which qubit 0 is the least significant bit; Qiskit does, and the KetQat SDK's teleportation product states its own order in its output. Whenever you compare numbers across tools, translate the ordering first.

Operators combine the same way. A gate UU on qubit 0 and nothing on qubit 1 is U⊗IU\otimes I, a 4×44\times4 matrix, and (U⊗V)(∣a⟩⊗∣b⟩)=U∣a⟩⊗V∣b⟩(U\otimes V)(\ket a\otimes\ket b) = U\ket a\otimes V\ket b. For example, (H⊗I)∣00⟩=∣+⟩∣0⟩=(∣00⟩+∣10⟩)/2(H\otimes I)\ket{00} = \ket+\ket0 = (\ket{00} + \ket{10})/\sqrt2.

Two-qubit gates

The workhorse is the controlled-NOT, CX0→1\mathrm{CX}_{0\to1}: it flips the target (qubit 1) when the control (qubit 0) is 1, so ∣xy⟩↦∣x,y⊕x⟩\ket{x y}\mapsto\ket{x, y\oplus x}. In our ordering,

CX0→1=(1000010000010010),CZ=diag(1,1,1,−1),SWAP=(1000001001000001).\mathrm{CX}_{0\to1} = \begin{pmatrix}1&0&0&0\\0&1&0&0\\0&0&0&1\\0&0&1&0\end{pmatrix},\qquad \mathrm{CZ} = \mathrm{diag}(1,1,1,-1),\qquad \mathrm{SWAP} = \begin{pmatrix}1&0&0&0\\0&0&1&0\\0&1&0&0\\0&0&0&1\end{pmatrix}.

CZ\mathrm{CZ} multiplies ∣11⟩\ket{11} by −1-1 and does nothing else; it is symmetric in the two qubits, so "control" and "target" are interchangeable. Three identities, each checkable by multiplying 4×44\times4 matrices, connect them: CZ=(I⊗H) CX0→1 (I⊗H)\mathrm{CZ} = (I\otimes H)\,\mathrm{CX}_{0\to1}\,(I\otimes H); CX1→0=(H⊗H) CX0→1 (H⊗H)\mathrm{CX}_{1\to0} = (H\otimes H)\,\mathrm{CX}_{0\to1}\,(H\otimes H); and SWAP is three alternating CX gates. On basis states CX acts like the classical reversible XOR. On superpositions it does something no classical gate does.

Bell states

Apply HH to qubit 0 and then CX to ∣00⟩\ket{00}:

Quantum circuit on 2 wires (q0, q1). step 1: H on q0. step 2: CNOT, control q0, target q1. step 3: measure q0; measure q1.q0q1H
Preparing the Bell state |Φ+⟩ from |00⟩
∣00⟩→H⊗I∣00⟩+∣10⟩2→CX∣00⟩+∣11⟩2=∣Φ+⟩.\ket{00} \xrightarrow{H\otimes I} \frac{\ket{00} + \ket{10}}{\sqrt2} \xrightarrow{\mathrm{CX}} \frac{\ket{00} + \ket{11}}{\sqrt2} = \ket{\Phi^+}.

Starting instead from ∣01⟩\ket{01}, ∣10⟩\ket{10} and ∣11⟩\ket{11} gives the other three Bell states:

Input Output
∣00⟩\ket{00} ∣Φ+⟩=(∣00⟩+∣11⟩)/2\ket{\Phi^+} = (\ket{00} + \ket{11})/\sqrt2
∣10⟩\ket{10} ∣Φ−⟩=(∣00⟩−∣11⟩)/2\ket{\Phi^-} = (\ket{00} - \ket{11})/\sqrt2
∣01⟩\ket{01} ∣Ψ+⟩=(∣01⟩+∣10⟩)/2\ket{\Psi^+} = (\ket{01} + \ket{10})/\sqrt2
∣11⟩\ket{11} ∣Ψ−⟩=(∣01⟩−∣10⟩)/2\ket{\Psi^-} = (\ket{01} - \ket{10})/\sqrt2

They form an orthonormal basis of the two-qubit space, because they are the image of the computational basis under a unitary.

Measure both qubits of ∣Φ+⟩\ket{\Phi^+} in the computational basis: outcomes 00 and 11 each occur with probability 1/21/2, and 01 and 10 never. Measure only qubit 0 and get, say, 1: the state-update rule leaves ∣11⟩\ket{11}, so qubit 1 now gives 1 with certainty. Each qubit on its own looks like a fair coin, but the two coins always agree.

Product states and entangled states

A two-qubit pure state is a product state if it can be written ∣a⟩⊗∣b⟩\ket a\otimes\ket b, and entangled otherwise. Here is a complete test for two qubits. Arrange the amplitudes in a 2×22\times2 matrix, rows indexed by qubit 0 and columns by qubit 1:

C=(c00c01c10c11).C = \begin{pmatrix} c_{00} & c_{01}\\ c_{10} & c_{11}\end{pmatrix}.

Claim. ∣ψ⟩\ket\psi is a product state if and only if CC has rank 1, which for a non-zero 2×22\times2 matrix means det⁡C=c00c11−c01c10=0\det C = c_{00}c_{11} - c_{01}c_{10} = 0.

Proof. For a product state, cxy=axbyc_{xy} = a_x b_y, so C=a⃗ b⃗TC = \vec a\,\vec b^{T} is an outer product and has rank 1. Conversely, a rank-1 matrix is an outer product a⃗ b⃗T\vec a\,\vec b^T of two non-zero vectors, which is the coefficient matrix of ∣a⟩⊗∣b⟩\ket a\otimes\ket b. □\square

The general version is the Schmidt decomposition. The singular value decomposition C=U diag(s0,s1) VTC = U\,\mathrm{diag}(s_0, s_1)\,V^T gives

∣ψ⟩=s0 ∣u0⟩∣v0⟩+s1 ∣u1⟩∣v1⟩,s0≥s1≥0,s02+s12=1,\ket\psi = s_0\,\ket{u_0}\ket{v_0} + s_1\,\ket{u_1}\ket{v_1},\qquad s_0\ge s_1\ge0,\quad s_0^2+s_1^2 = 1,

with orthonormal {∣uk⟩}\{\ket{u_k}\} for qubit 0 and {∣vk⟩}\{\ket{v_k}\} for qubit 1. The Schmidt coefficients sks_k do not depend on which local bases you start from. The state is a product state exactly when s1=0s_1 = 0, and ∣det⁡C∣=s0s1\abs{\det C} = s_0 s_1. When s0=s1=1/2s_0 = s_1 = 1/\sqrt2, as for every Bell state, the state is maximally entangled.

Worked example

  1. 12(∣00⟩+∣01⟩+∣10⟩+∣11⟩)\tfrac12(\ket{00} + \ket{01} + \ket{10} + \ket{11}): det⁡C=14−14=0\det C = \tfrac14 - \tfrac14 = 0, a product state. Indeed it is ∣+⟩∣+⟩\ket+\ket+.
  2. 12(∣00⟩+∣01⟩+∣10⟩−∣11⟩)\tfrac12(\ket{00} + \ket{01} + \ket{10} - \ket{11}): det⁡C=−14−14=−12≠0\det C = -\tfrac14 - \tfrac14 = -\tfrac12 \neq 0, entangled. Its Schmidt coefficients are (1/2,1/2)(1/\sqrt2, 1/\sqrt2), because s0s1=∣det⁡C∣=1/2s_0 s_1 = \abs{\det C} = 1/2 and s02+s12=1s_0^2 + s_1^2 = 1 force s0=s1s_0 = s_1. A single sign change turned a product state into a maximally entangled one.
  3. ∣Φ+⟩\ket{\Phi^+}: C=I/2C = I/\sqrt2, det⁡C=1/2\det C = 1/2, maximally entangled.
import numpy as np

H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
CX = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])  # control q0, target q1
X, Z = np.array([[0, 1], [1, 0]]), np.diag([1, -1])

phi_plus = CX @ np.kron(H, np.eye(2)) @ np.array([1, 0, 0, 0])
print(np.round(phi_plus, 4))                                      # [0.7071 0. 0. 0.7071]
print(np.linalg.svd(phi_plus.reshape(2, 2), compute_uv=False))    # Schmidt coefficients
print(np.linalg.svd((np.array([1, 1, 1, -1]) / 2).reshape(2, 2), compute_uv=False))

def A(t):
    return np.cos(t) * Z + np.sin(t) * X

def E(psi, a, b):
    return psi @ np.kron(A(a), A(b)) @ psi    # real amplitudes here, so no conjugate needed

a0, a1, b0, b1 = 0, np.pi / 2, np.pi / 4, -np.pi / 4
S = E(phi_plus, a0, b0) + E(phi_plus, a0, b1) + E(phi_plus, a1, b0) - E(phi_plus, a1, b1)
print(round(S, 6), round(2 * np.sqrt(2), 6))                      # 2.828427 2.828427

Both Schmidt computations print [0.70710678 0.70710678]. The last lines compute the CHSH value, explained next.

Entanglement is more than correlation

Perfectly correlated outcomes are not, by themselves, evidence of anything quantum. Put two identical coins in two envelopes, both heads or both tails with probability 1/21/2, and send them to Alice and Bob: their results always agree, exactly like computational-basis measurements of ∣Φ+⟩\ket{\Phi^+}. The difference appears only when the parties can choose between different measurements. This was Bell's insight (Bell, 1964); the form used in experiments is due to Clauser, Horne, Shimony and Holt (CHSH, 1969).

The game. Alice chooses one of two measurements, labelled a0a_0 or a1a_1, and Bob one of b0b_0 or b1b_1, independently and after the qubits have been separated. Each measurement returns ±1\pm1. Let E(a,b)E(a,b) be the average of the product of their outcomes, and

S=E(a0,b0)+E(a0,b1)+E(a1,b0)−E(a1,b1).S = E(a_0,b_0) + E(a_0,b_1) + E(a_1,b_0) - E(a_1,b_1).

Classical bound (theorem). Suppose each outcome is fixed in advance by some shared information λ\lambda (possibly random), and Alice's outcome does not depend on Bob's choice or vice versa. For each λ\lambda there are then predetermined values A0,A1,B0,B1∈{±1}A_0, A_1, B_0, B_1\in\{\pm1\}, and

A0B0+A0B1+A1B0−A1B1=A0(B0+B1)+A1(B0−B1).A_0B_0 + A_0B_1 + A_1B_0 - A_1B_1 = A_0(B_0 + B_1) + A_1(B_0 - B_1).

One of the brackets is 0 and the other is ±2\pm2, so the expression is ±2\pm2. Averaging over λ\lambda cannot leave [−2,2][-2, 2], so ∣S∣≤2\abs S\le2. The exercise confirms by enumeration that the 16 deterministic strategies reach exactly 2.

Quantum value. Let Alice and Bob share ∣Φ+⟩\ket{\Phi^+} and measure the observable A(θ)=cos⁡θ Z+sin⁡θ XA(\theta) = \cos\theta\,Z + \sin\theta\,X, a measurement along an axis at angle θ\theta in the xx–zz plane of the Bloch sphere. Since ⟨Φ+∣Z⊗Z∣Φ+⟩=⟨Φ+∣X⊗X∣Φ+⟩=1\bra{\Phi^+}Z\otimes Z\ket{\Phi^+} = \bra{\Phi^+}X\otimes X\ket{\Phi^+} = 1 and the mixed terms vanish,

E(a,b)=cos⁡acos⁡b+sin⁡asin⁡b=cos⁡(a−b).E(a,b) = \cos a\cos b + \sin a\sin b = \cos(a-b).

With a0=0a_0 = 0, a1=π/2a_1 = \pi/2, b0=π/4b_0 = \pi/4, b1=−π/4b_1 = -\pi/4, every term has magnitude cos⁡(π/4)=1/2\cos(\pi/4) = 1/\sqrt2 and the signs line up: S=4/2=22≈2.83>2S = 4/\sqrt2 = 2\sqrt2 \approx 2.83 > 2. No assignment of predetermined outcomes can reproduce these statistics.

Tsirelson's bound (theorem, stated without proof). No quantum state and no choice of quantum measurements gives ∣S∣>22\abs S > 2\sqrt2 (Tsirelson, 1980). The value we just found is therefore the quantum maximum. The exercise checks, on random states and angles, that product states stay within 2 and no state exceeds 222\sqrt2; such checks are consistent with the theorems but do not prove them.

What a violation does and does not show. A measured S>2S > 2 rules out every model in which outcomes are predetermined by shared information and are independent of the distant party's choice of setting. Experiments must close "loopholes" (for example, detecting too few particles, or settings that are not chosen independently and fast enough). As one published example, Hensen et al. reported in 2015 a CHSH test free of the detection and locality loopholes, with S=2.42±0.20S = 2.42 \pm 0.20 from 245 trials (Hensen et al., 2015), a value quoted from that paper. A violation does not let Alice send a message to Bob: each of Bob's outcomes is a fair coin whatever Alice chooses, and the correlations become visible only when the two lists of results are brought together through an ordinary channel.

No-cloning

Classical information can be copied. Quantum information cannot.

Theorem (no-cloning). There is no unitary UU and fixed "blank" state ∣e⟩\ket e such that U(∣ψ⟩⊗∣e⟩)=∣ψ⟩⊗∣ψ⟩U(\ket\psi\otimes\ket e) = \ket\psi\otimes\ket\psi for every state ∣ψ⟩\ket\psi. (Wootters and Zurek, 1982; Dieks, 1982.)

Proof. Suppose such a UU copies two states ∣ψ⟩\ket\psi and ∣ϕ⟩\ket\phi. Unitaries preserve inner products, so

⟨ϕ∣ψ⟩ ⟨e∣e⟩=(⟨ϕ∣⟨e∣)U†U(∣ψ⟩∣e⟩)=(⟨ϕ∣⟨ϕ∣)(∣ψ⟩∣ψ⟩)=⟨ϕ∣ψ⟩2.\braket{\phi}{\psi}\,\braket{e}{e} = \big(\bra\phi\bra e\big)U^\dagger U\big(\ket\psi\ket e\big) = \big(\bra\phi\bra\phi\big)\big(\ket\psi\ket\psi\big) = \braket{\phi}{\psi}^2 .

With ⟨e∣e⟩=1\braket{e}{e}=1 this says x=x2x = x^2 for x=⟨ϕ∣ψ⟩x = \braket\phi\psi, so x=0x = 0 or x=1x = 1. A cloner can therefore copy only states that are identical or orthogonal; it cannot copy both ∣0⟩\ket0 and ∣+⟩\ket+, whose overlap is 1/21/\sqrt2. □\square

You can see the failure concretely. CX copies basis states: ∣x⟩∣0⟩↦∣x⟩∣x⟩\ket{x}\ket{0}\mapsto\ket{x}\ket{x}. Feed it ∣+⟩∣0⟩\ket+\ket0 and linearity gives ∣Φ+⟩\ket{\Phi^+}, not ∣+⟩∣+⟩\ket+\ket+; the "copy" is entangled with the original instead. No-cloning is why quantum error correction cannot simply keep backup copies, and why teleportation, in the lesson on introductory quantum algorithms, must destroy the original.

Common mistakes

  • Mixing ordering conventions. In this course ∣10⟩\ket{10} means qubit 0 is 1 and is vector index 2. A little-endian library puts the same state at index 1.
  • Testing entanglement by looking at correlations in one basis. Classically correlated mixtures show the same computational-basis correlations as ∣Φ+⟩\ket{\Phi^+}. For pure two-qubit states use the Schmidt test; to rule out classical models experimentally, use a Bell inequality with several settings.
  • Reading a CHSH violation as faster-than-light signalling. Each party's local statistics are independent of the other's setting.
  • Treating 222\sqrt2 as a number found numerically. That no quantum strategy exceeds it is a theorem (Tsirelson); random sampling can only fail to find a counterexample.
  • Claiming CX copies qubits. It copies computational-basis values, not states.

Summary

  • nn qubits have 2n2^n amplitudes; tensor products build states and operators, with qubit 0 as the most significant bit in this course.
  • CX, CZ and SWAP are the basic two-qubit gates; HH then CX turns ∣00⟩\ket{00} into ∣Φ+⟩\ket{\Phi^+}.
  • A two-qubit pure state is entangled iff its coefficient matrix has rank 2 (det⁡C≠0\det C\neq0); the Schmidt coefficients quantify it.
  • Local predetermined models obey ∣S∣≤2\abs S\le2; quantum mechanics reaches, and never exceeds, 222\sqrt2.
  • Unknown quantum states cannot be cloned.

The exercise asks you to build the Bell states, implement the Schmidt test and compute CHSH values, including the classical maximum by enumeration. Next, Quantum circuits and simulation builds a state-vector simulator for many qubits.

Exercises

Run these on your own computer. Each comes with automated checks: python -m pytest test_exercise.py. Nothing you write is sent to KetQat.

Bell states, the Schmidt test and the CHSH value

intermediate · about 45 min · needs numpy

You will build two-qubit states with tensor products and gates, decide whether a two-qubit state is entangled from its Schmidt coefficients, and compute the CHSH combination of correlations for quantum states and for every deterministic classical strategy.

Ordering convention. As in the lesson, ∣q0q1⟩\ket{q_0q_1} has qubit 0 on the left and qubit 0 is the most significant bit: the amplitude of ∣q0q1⟩\ket{q_0q_1} sits at index 2q0+q12q_0 + q_1. numpy.kron(a, b) follows this convention when a is qubit 0.

Functions to implement (in starter.py)

Function Contract
kron(*factors) Tensor product of any number of vectors or matrices, first factor = qubit 0.
bell_state(name) name in "phi+", "phi-", "psi+", "psi-". Build the state by applying HH on qubit 0 and then CX0→1\mathrm{CX}_{0\to1} to the right basis state ∣xy⟩\ket{xy}; return a length-4 complex array.
schmidt_coefficients(psi) The two Schmidt coefficients of a normalised two-qubit state, in decreasing order (non-negative reals).
is_entangled(psi, tol=1e-9) True when the second Schmidt coefficient exceeds tol.
chsh_value(psi, a0, a1, b0, b1) S=E(a0,b0)+E(a0,b1)+E(a1,b0)−E(a1,b1)S = E(a_0,b_0) + E(a_0,b_1) + E(a_1,b_0) - E(a_1,b_1) with E(a,b)=⟨ψ∣A(a)⊗A(b)∣ψ⟩E(a,b) = \bra\psi A(a)\otimes A(b)\ket\psi and A(θ)=cos⁡θ Z+sin⁡θ XA(\theta) = \cos\theta\, Z + \sin\theta\, X.
classical_chsh_max() The largest value of a0b0+a0b1+a1b0−a1b1a_0b_0 + a_0b_1 + a_1b_0 - a_1b_1 over all 16 assignments a0,a1,b0,b1∈{+1,−1}a_0, a_1, b_0, b_1 \in \{+1, -1\}, found by enumeration.

What the checks verify

  • kron reproduces the ordering convention (∣01⟩\ket{01} is index 1, ∣10⟩\ket{10} index 2).
  • The four Bell states equal their explicit vectors (up to the stated signs) and are orthonormal.
  • Schmidt coefficients are (1/2,1/2)(1/\sqrt2, 1/\sqrt2) for Bell states, (1,0)(1, 0) for random product states, and always have squares summing to 1; the rank test agrees with the determinant test det⁡C≠0\det C \neq 0 from the lesson.
  • chsh_value gives 222\sqrt2 for ∣Φ+⟩\ket{\Phi^+} at the angles in the lesson and matches the closed form E(a,b)=cos⁡(a−b)E(a,b) = \cos(a-b) for ∣Φ+⟩\ket{\Phi^+}.
  • Random product states never exceed ∣S∣=2\abs S = 2, and random states never exceed Tsirelson's value 222\sqrt2 (checked on seeded random samples; these are consistency checks of the theorems, not proofs).
  • The classical deterministic maximum is exactly 2.

Run the checks

python -m pip install numpy
python -m pytest test_exercise.py
Terminal
python -m pip install numpy

starter.py

Python
"""Bell states, the Schmidt test and the CHSH value (KetQat Commons, Foundations).

Ordering: |q0 q1>, qubit 0 is the most significant bit (index 2*q0 + q1).
"""
import numpy as np

X = np.array([[0, 1], [1, 0]], dtype=complex)
Z = np.array([[1, 0], [0, -1]], dtype=complex)
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2)
CX = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]], dtype=complex)  # control 0, target 1


def kron(*factors):
    """Tensor product of vectors or matrices; the first factor is qubit 0."""
    raise NotImplementedError


def bell_state(name):
    """Return the Bell state "phi+", "phi-", "psi+" or "psi-" built as CX (H x I) |xy>."""
    raise NotImplementedError


def schmidt_coefficients(psi):
    """Return the two Schmidt coefficients of a two-qubit state in decreasing order."""
    raise NotImplementedError


def is_entangled(psi, tol=1e-9):
    """Return True when the second Schmidt coefficient exceeds tol."""
    raise NotImplementedError


def chsh_value(psi, a0, a1, b0, b1):
    """Return E(a0,b0) + E(a0,b1) + E(a1,b0) - E(a1,b1) with A(t) = cos(t) Z + sin(t) X."""
    raise NotImplementedError


def classical_chsh_max():
    """Return the maximum of a0 b0 + a0 b1 + a1 b0 - a1 b1 over a0, a1, b0, b1 in {+1, -1}."""
    raise NotImplementedError

test_exercise.py

Python
"""Checks for the bell-states-and-chsh exercise. Run: python -m pytest test_exercise.py"""
import importlib.util
import os
from pathlib import Path

def _load_target():
    name = os.environ.get("KETQAT_COMMONS_TARGET", "starter")
    path = Path(__file__).with_name(f"{name}.py")
    spec = importlib.util.spec_from_file_location(f"{Path(__file__).parent.name}_{name}", path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module

target = _load_target()

import numpy as np
import pytest

S2 = np.sqrt(2)
RNG = np.random.default_rng(1969)


def _random_qubit():
    v = RNG.normal(size=2) + 1j * RNG.normal(size=2)
    return v / np.linalg.norm(v)


def _random_two_qubit_state():
    v = RNG.normal(size=4) + 1j * RNG.normal(size=4)
    return v / np.linalg.norm(v)


def test_kron_follows_the_ordering_convention():
    zero, one = np.array([1, 0]), np.array([0, 1])
    assert np.allclose(target.kron(zero, one), [0, 1, 0, 0])  # |01> is index 1
    assert np.allclose(target.kron(one, zero), [0, 0, 1, 0])  # |10> is index 2
    assert np.allclose(target.kron(one, one, zero), np.eye(8)[6])  # |110> is index 6
    X = np.array([[0, 1], [1, 0]])
    # X on qubit 0 maps |00> to |10>
    assert np.allclose(target.kron(X, np.eye(2)) @ np.eye(4)[0], np.eye(4)[2])


@pytest.mark.parametrize(
    "name, vector",
    [
        ("phi+", [1, 0, 0, 1]),
        ("phi-", [1, 0, 0, -1]),
        ("psi+", [0, 1, 1, 0]),
        ("psi-", [0, 1, -1, 0]),
    ],
)
def test_bell_states_match_explicit_vectors(name, vector):
    assert np.allclose(target.bell_state(name), np.array(vector) / S2)


def test_bell_states_are_orthonormal():
    B = np.array([target.bell_state(n) for n in ("phi+", "phi-", "psi+", "psi-")])
    assert np.allclose(B.conj() @ B.T, np.eye(4))


def test_schmidt_coefficients():
    for n in ("phi+", "phi-", "psi+", "psi-"):
        assert np.allclose(target.schmidt_coefficients(target.bell_state(n)), [1 / S2, 1 / S2])
        assert target.is_entangled(target.bell_state(n))
    for _ in range(20):
        product = np.kron(_random_qubit(), _random_qubit())
        assert np.allclose(target.schmidt_coefficients(product), [1, 0], atol=1e-9)
        assert not target.is_entangled(product)
    for _ in range(20):
        psi = _random_two_qubit_state()
        s = target.schmidt_coefficients(psi)
        assert s[0] >= s[1] >= 0 and np.isclose(np.sum(np.square(s)), 1)
        # independent test from the lesson: entangled iff det C != 0, and |det C| = s0 * s1
        det = abs(np.linalg.det(psi.reshape(2, 2)))
        assert np.isclose(det, s[0] * s[1])
    assert not target.is_entangled(np.array([1, 1, 0, 0]) / S2)  # |0>|+>
    assert target.is_entangled(np.array([1, 1, 1, -1]) / 2)


def test_chsh_for_phi_plus():
    phi = np.array([1, 0, 0, 1]) / S2
    assert np.isclose(target.chsh_value(phi, 0, np.pi / 2, np.pi / 4, -np.pi / 4), 2 * S2)
    for _ in range(20):
        a0, a1, b0, b1 = RNG.uniform(-np.pi, np.pi, size=4)
        closed = np.cos(a0 - b0) + np.cos(a0 - b1) + np.cos(a1 - b0) - np.cos(a1 - b1)
        assert np.isclose(target.chsh_value(phi, a0, a1, b0, b1), closed)


def test_product_states_respect_the_classical_bound():
    worst = 0.0
    for _ in range(300):
        psi = np.kron(_random_qubit(), _random_qubit())
        worst = max(worst, abs(target.chsh_value(psi, *RNG.uniform(-np.pi, np.pi, size=4))))
    assert worst <= 2 + 1e-9


def test_no_state_exceeds_tsirelson():
    worst = 0.0
    for _ in range(300):
        psi = _random_two_qubit_state()
        worst = max(worst, abs(target.chsh_value(psi, *RNG.uniform(-np.pi, np.pi, size=4))))
    assert worst <= 2 * S2 + 1e-9


def test_classical_deterministic_maximum():
    assert target.classical_chsh_max() == 2

Check your understanding

8 questions. Pass with 70% of the points. Answers are checked on the server; retakes are allowed. Explanations appear once you pass or after three complete attempts.

  1. Using this course's convention (qubit 0 is the leftmost label and the most significant bit), at which index (counting from 0) of the length-8 state vector is the amplitude of ∣110⟩\ket{110} stored?

    1 pt
  2. Which of these two-qubit states is entangled?

    1 pt
  3. What is the smaller Schmidt coefficient of cos⁡π8∣00⟩+sin⁡π8∣11⟩\cos\frac\pi8\ket{00} + \sin\frac\pi8\ket{11}?

    1 pt
  4. What does CX0→1\mathrm{CX}_{0\to1} produce from ∣+⟩∣0⟩\ket+\ket0?

    1 pt
  5. For fixed values A0,A1,B0,B1∈{+1,−1}A_0, A_1, B_0, B_1\in\{+1,-1\}, which values can A0B0+A0B1+A1B0−A1B1A_0B_0 + A_0B_1 + A_1B_0 - A_1B_1 take?

    1 pt
  6. With your chsh_value and bell_state from the exercise, compute chsh_value(bell_state("phi+"), 0, numpy.pi/2, numpy.pi/6, -numpy.pi/6).

    1 pt

    Computed by running exercise “bell-states-and-chsh” on your computer.

  7. Select every true statement about the CHSH experiment.

    1 pt

    Select all that apply.

  8. For which pair of states does a unitary exist that copies both of them, U∣ψ⟩∣0⟩=∣ψ⟩∣ψ⟩U\ket\psi\ket0 = \ket\psi\ket\psi?

    1 pt

Sign in with GitHub to check answers and save progress. Reading stays free without an account.

References

  1. Michael A. Nielsen, Isaac L. Chuang (2010). Quantum Computation and Quantum Information (10th Anniversary Edition). Cambridge University Press. doi:10.1017/CBO9780511976667
  2. John S. Bell (1964). On the Einstein Podolsky Rosen paradox. Physics Physique Fizika 1, 195-200. doi:10.1103/PhysicsPhysiqueFizika.1.195
  3. John F. Clauser et al. (1969). Proposed experiment to test local hidden-variable theories. Physical Review Letters 23, 880-884. doi:10.1103/PhysRevLett.23.880
  4. Boris S. Tsirelson (1980). Quantum generalizations of Bell's inequality. Letters in Mathematical Physics 4, 93-100 (published under the transliteration Cirel'son). doi:10.1007/BF00417500
  5. Bas Hensen et al. (2015). Loophole-free Bell inequality violation using electron spins separated by 1.3 kilometres. Nature 526, 682-686. doi:10.1038/nature15759
  6. William K. Wootters, Wojciech H. Zurek (1982). A single quantum cannot be cloned. Nature 299, 802-803. doi:10.1038/299802a0
  7. Dennis Dieks (1982). Communication by EPR devices. Physics Letters A 92, 271-272. doi:10.1016/0375-9601(82)90084-6