The Bell–CHSH inequality is a fence that every local hidden variable theory must respect. Quantum mechanics climbs over it, but only up to a precise height called the Tsirelson bound. In this post we compute that height numerically with a semidefinite program (SDP), using the Navascués–Pironio–Acín (NPA) hierarchy, and then explore how the bound changes when we reweight the inequality.
1. The Problem
Alice and Bob each choose between two $\pm1$-valued observables, $A_0, A_1$ and $B_0, B_1$. With correlators $E_{ij}=\langle A_i B_j\rangle$, the CHSH expression is
$$
S = E_{00} + E_{01} + E_{10} - E_{11}.
$$
- Local hidden variable models obey $|S|\le 2$.
- Quantum mechanics obeys $|S|\le 2\sqrt{2}$ (Tsirelson bound).
- No-signalling theories can reach $|S|\le 4$.
We also study a weighted CHSH family with two parameters $\alpha,\beta\ge 0$:
$$
S(\alpha,\beta)=\alpha,(E_{00}+E_{01})+\beta,(E_{10}-E_{11}).
$$
For this family the classical bound is $2\max(\alpha,\beta)$, and Tsirelson’s vector construction gives the analytic quantum bound
$$
S_Q(\alpha,\beta)=2\sqrt{\alpha^{2}+\beta^{2}}.
$$
This gives us an exact formula to check the SDP against.
2. The NPA Hierarchy in a Nutshell
Choose a finite list of operator words ${w_i}$ built from $A_0,A_1,B_0,B_1$. These operators satisfy
$$
A_i^2=B_j^2=\mathbb{1},\qquad [A_i,B_j]=0 .
$$
Define the moment matrix
$$
\Gamma_{ij}=\langle \psi|,w_i^{\dagger}w_j,|\psi\rangle .
$$
Every quantum realisation produces a $\Gamma$ that is positive semidefinite, has unit diagonal, and whose entries are equal whenever the underlying operator products reduce to the same word. The NPA relaxation drops everything else:
$$
\max_{\Gamma}\ \sum_{ij} c_{ij},\langle A_iB_j\rangle
\quad\text{s.t.}\quad \Gamma\succeq 0,\ \ \Gamma_{\mathbb 1\mathbb 1}=1,\ \ \Gamma_{ij}=\Gamma_{kl}\ \text{if}\ w_i^\dagger w_j \equiv w_k^\dagger w_l .
$$
Each level of the hierarchy uses longer words and gives a tighter outer approximation of the quantum set:

For CHSH, already the first level is tight, so every level returns $2\sqrt2$.
3. The Complete Code
1 | import time |
4. Code Walkthrough
4.1 Operator algebra
reduce_word implements $X_i^2=\mathbb 1$ with a stack: whenever the same letter appears twice in a row, both copies cancel. Alice’s and Bob’s letters commute, so a word is stored as a pair (alice_sequence, bob_sequence) and each half is reduced independently.
party_words(L) lists all reduced words for one party up to length $L$. With two observables these are just alternating strings: $\mathbb 1, A_0, A_1, A_0A_1, A_1A_0, A_0A_1A_0,\dots$
build_words(level) then forms every product of an Alice word and a Bob word whose total length is at most the level. The special level "1+AB" adds the four products $A_iB_j$ to the level-1 list, giving a $9\times 9$ moment matrix.
4.2 Moment keys
The entry $\Gamma_{uv}=\langle u^\dagger v\rangle$ depends only on the reduced form of $u^\dagger v$. The function moment_key computes that reduced word, and canon identifies a word with its reversed (adjoint) version, since we work with the real relaxation. Two matrix entries share one SDP variable exactly when their keys coincide. This is how the linear constraints $\Gamma_{ij}=\Gamma_{kl}$ are enforced without writing a single equality by hand.
4.3 Building the SDP
make_npa assigns one scalar variable (an entry of y) to each distinct key. A sparse $0/1$ matrix S maps y to the flattened $\Gamma$, and cp.reshape turns the result into a square matrix. Because $\Gamma$ is symmetric by construction, the reshape order is irrelevant. The constraints are:
Gamma >> 0, which is the positive-semidefiniteness condition,y[identity] == 1, which is the normalisation $\langle\mathbb 1\rangle=1$.
The unit diagonal comes for free, because $u^\dagger u$ always reduces to the identity.
The Bell coefficients live in a cp.Parameter, so the objective $\sum_{ij}c_{ij}\langle A_iB_j\rangle$ is affine in the variables for any value of the parameter.
4.4 Speed-up for the parameter scan
A naive scan would rebuild and re-canonicalise a new problem for each of the 121 grid points. Here the problem is built once, and only C.value changes between solves. Because the parameter multiplies a variable in a disciplined-parametrized (DPP) way, cvxpy reuses the compiled problem and only re-runs the solver. The sparse map S also keeps the matrix construction cheap at higher levels.
4.5 Validation
Three independent checks surround the SDP:
- A brute-force search over the 16 deterministic strategies gives the classical bound $2$.
- An explicit two-qubit strategy with the state $|\Phi^+\rangle$ and measurement angles $0,\pi/2$ (Alice) and $\pm\pi/4$ (Bob) gives $4\cdot\tfrac{1}{\sqrt2}=2\sqrt2$, so the upper bound from the SDP is actually attained.
- The analytic formula $2\sqrt{\alpha^2+\beta^2}$ is compared with the SDP over the whole grid.
4.6 Geometry
At level 1 the matrix $\Gamma$ is a Gram matrix of unit vectors, $\Gamma_{ij}=\vec v_i\cdot\vec v_j$. An eigendecomposition $\Gamma=V V^{T}$ recovers those vectors, and we plot the three dominant components in 3D. This is Tsirelson’s construction made visible.
5. Console Output
Solver: CLARABEL
level | matrix size | #moments | CHSH max | time [s]
----------------------------------------------------------
1 | 5x5 | 11 | 2.828427 | 0.043
1+AB | 9x9 | 17 | 2.828427 | 0.025
2 | 13x13 | 31 | 2.828427 | 0.022
3 | 25x25 | 61 | 2.828427 | 0.074
Tsirelson bound 2*sqrt(2) = 2.828427
Optimal correlators (level 1):
E00 = 0.707107
E01 = 0.707107
E10 = 0.707107
E11 = -0.707107
1/sqrt(2) = 0.707107
Classical bound (brute force) : 2.000000
Explicit two-qubit strategy : 2.828427
Weighted CHSH grid: 121 SDPs solved in 0.62 s
Max |SDP - 2*sqrt(a^2+b^2)| over the grid = 2.64e-08
Reading the console output
- The table shows the CHSH maximum at every level of the hierarchy. All four entries should equal $2\sqrt2\approx 2.828427$ to solver precision, which confirms that the first level already captures the full quantum set for this scenario.
- The optimal correlators come out as $|E_{ij}|=1/\sqrt2$, with $E_{11}$ carrying the opposite sign, so that $S = 4\cdot\tfrac{1}{\sqrt2}=2\sqrt2$.
- The explicit qubit strategy reproduces the same number. Since the NPA value is an upper bound and the qubit strategy is a lower bound, they sandwich the true quantum maximum.
- The final line reports the largest deviation between the SDP and the closed form $2\sqrt{\alpha^2+\beta^2}$ over all 121 grid points.
6. Result Image

7. Reading the Figure
Top left: 3D surface. The coloured surface is the quantum maximum computed by the SDP over $(\alpha,\beta)\in[0,1]^2$. The red mesh is the classical bound $2\max(\alpha,\beta)$. The two coincide along the axes, where the Bell expression degenerates to a trivially satisfiable one, and they separate most strongly along the diagonal $\alpha=\beta$, where the surface peaks at $2\sqrt2$ against a classical $2$. The gap between the surface and the mesh is the room available for quantum non-locality.
Top centre: Tsirelson vectors. The four arrows are the vectors $\vec a_0,\vec a_1,\vec b_0,\vec b_1$ extracted from $\Gamma$. $\vec a_0$ and $\vec a_1$ are orthogonal, and likewise $\vec b_0$ and $\vec b_1$. Each $\vec b_j$ bisects the two Alice vectors, at $45^\circ$ to each. That is exactly the geometry that maximises
$$
\sum_{j}\bigl|c_{0j}\vec a_0+c_{1j}\vec a_1\bigr| ,
$$
and it mirrors the measurement angles of the optimal qubit strategy.
Top right: moment matrix. The block of correlators between Alice’s and Bob’s observables shows entries of magnitude $0.71\approx 1/\sqrt2$. The entries between $A_0$ and $A_1$, and between $B_0$ and $B_1$, are zero, which is the orthogonality seen in the 3D picture. The unit diagonal is the normalisation.
Bottom left: slice at $\beta=1$. Green dots (SDP) lie on the black analytic curve $2\sqrt{\alpha^2+1}$ and stay above the dashed classical line for every $\alpha<1$. At $\alpha=1$ the curve reaches $2\sqrt2$.
Bottom centre: bounds on one axis. The bars compare the classical value ($2$), the four NPA levels, the explicit qubit strategy, and the no-signalling maximum ($4$, attained by the Popescu–Rohrlich box). The NPA bars and the qubit bar have the same height, namely the Tsirelson bound, which sits strictly between the classical and no-signalling values.
Bottom right: numerical error. The map of $\log_{10}|\mathrm{SDP}-2\sqrt{\alpha^2+\beta^2}|$ shows that the SDP matches the analytic bound to solver tolerance everywhere on the grid, so the SDP and the closed form agree across the whole parameter range.
8. Takeaways
- The NPA hierarchy turns the question “what is the largest Bell violation quantum mechanics allows?” into a sequence of SDPs with moment matrices that are positive semidefinite and carry the algebraic identifications between words.
- For CHSH, level 1 is already exact, and the optimum $2\sqrt2$ is reached by a maximally entangled two-qubit state.
- Building the SDP once with a
cp.Parametermakes parameter scans cheap, and the same code can be pointed at other two-input, two-output Bell expressions simply by changing the coefficient matrixC.








is the partial trace over the two-dimensional output factor, and the constraint is exactly trace preservation, $\sum_jR_j^\dagger R_j=I_{16}$.
for every pair $(j,k)$ at once. The same function scores every strategy, which keeps the comparison fair: the uncoded qubit uses $R=I$ with $A_k=K_k$, and the coded cases use $16\times2$ operators $A_k$.








