The Quantum Delsarte Bound and a Stabilizer Code Search
How many logical qubits can we protect, and how well, using a given number of physical qubits? An $[[n,k,d]]$ quantum code encodes $k$ logical qubits into $n$ physical qubits and corrects any error acting on up to $\lfloor (d-1)/2 \rfloor$ qubits. Two questions drive code design:
- Upper bounds. Which triples $(n,k,d)$ are impossible?
- Construction. For the triples that are not ruled out, can we actually find a code?
In this article we answer both with a single concrete example. First, we use the quantum Delsarte (quantum linear programming) bound to compute, for every $n \le 16$ and $k < n$, the largest distance $d$ that is not excluded. Second, we run a random stabilizer-code search for $[[5,1,d]]$ and check that the code it finds is consistent with the LP constraints.
1. Mathematical Background
1.1 Weight enumerators of a stabilizer code
Let $\mathcal{S}$ be the stabilizer group of an $[[n,k]]$ stabilizer code, so $|\mathcal{S}| = 2^{n-k}$. Let $\mathcal{N}$ be its normalizer, so $|\mathcal{N}| = 2^{n+k}$. Define

where $\mathrm{wt}(E)$ is the number of qubits on which the Pauli operator $E$ acts non-trivially. Then $A_0 = B_0 = 1$ and
$$
\sum_{j=0}^{n} A_j = 2^{n-k}, \qquad \sum_{j=0}^{n} B_j = 2^{n+k}.
$$
1.2 Quantum MacWilliams identity
With $K = 2^k$, the two enumerators are linked by
$$
B_j = \frac{K}{2^n} \sum_{i=0}^{n} Q_j(i), A_i, \qquad
Q_j(i) = \sum_{s=0}^{j} (-1)^s, 3^{,j-s} \binom{i}{s}\binom{n-i}{j-s},
$$
where $Q_j(i)$ is the quaternary Krawtchouk polynomial.
1.3 The shadow enumerator
Rains showed that the following quantity must also be non-negative:
$$
S_j = \frac{K}{2^n} \sum_{i=0}^{n} (-1)^i, Q_j(i), A_i ;\ge; 0 .
$$
1.4 The linear program
If an $[[n,k,d]]$ stabilizer code exists, the vector $A = (A_0,\dots,A_n)$ must satisfy:
$$
\begin{aligned}
& A_0 = 1, \qquad \sum_{j} A_j = 2^{n-k}, \qquad A_j \ge 0,\
& B_j = A_j \quad (1 \le j \le d-1) \qquad \text{(no undetectable error of weight} < d \text{ outside } \mathcal{S}),\
& B_j \ge A_j \quad (j \ge d),\
& S_j \ge 0 \quad (0 \le j \le n).
\end{aligned}
$$
All of these constraints are linear in $A$. If the system is infeasible, no $[[n,k,d]]$ code exists. This is the quantum Delsarte (quantum LP) bound. The condition is necessary but not sufficient, so feasibility does not guarantee existence.
Feasibility is monotone in $d$: raising $d$ only adds constraints. For each $(n,k)$ we can therefore increase $d$ until the LP first becomes infeasible. The last feasible value is $d_{\mathrm{LP}}(n,k)$.
We compare it with the quantum Singleton bound:
$$
n - k ;\ge; 2(d-1) \quad\Longleftrightarrow\quad d \le \left\lfloor \frac{n-k}{2} \right\rfloor + 1 .
$$
1.5 The code search
Every $n$-qubit Pauli operator (up to phase) is a vector $(x,|,z) \in \mathbb{F}_2^{2n}$. Two Paulis commute exactly when their symplectic product vanishes:
$$
\langle (x|z),(x’|z’) \rangle = x\cdot z’ + z\cdot x’ \equiv 0 \pmod 2 .
$$
A stabilizer code is an $(n-k)$-dimensional isotropic subspace of $\mathbb{F}_2^{2n}$. We build one at random as follows:
- Pick a Pauli that commutes with all earlier generators and is not already in their span.
- Repeat until we have $n-k$ generators.
- Compute $A_j$ and $B_j$ by direct enumeration.
- Read off the distance as the minimum weight in $\mathcal{N}\setminus\mathcal{S}$.
For $[[5,1]]$ the famous perfect code $[[5,1,3]]$ should appear. Its enumerators are
$$
A(z) = 1 + 15 z^4, \qquad B(z) = 1 + 30 z^3 + 15 z^4 + 18 z^5 .
$$
2. The Concrete Problem
Task A (bound). For every $2 \le n \le 16$ and $0 \le k \le n-1$, determine
$$
d_{\mathrm{LP}}(n,k) = \max{, d : \text{the LP above is feasible} ,}
$$
and compare it with the Singleton bound.
Task B (search). Sample 4000 random $[[5,1]]$ stabilizer codes, record their distances, and take the best one. Then verify that its enumerators satisfy the quantum MacWilliams identity and the shadow inequalities, and that the LP is feasible for $d=3$ and infeasible for $d=4$.
3. Full Source Code
1 | import numpy as np |
4. Code Walkthrough
4.1 Krawtchouk matrices
krawtchouk_matrix(n) evaluates $Q_j(i)$ directly from its definition, using Python’s exact integers and math.comb. comb returns $0$ when the lower index exceeds the upper, so the boundary cases of the double binomial sum need no special handling. The matrices are computed once for each $n$ and cached in KRAW, so the thousands of LP calls never recompute them.
4.2 The LP feasibility test
lp_feasible(n, k, d) builds exactly the constraints of Section 1.4 in matrix form.
Bmatis $\frac{K}{2^n} Q$, so $B = \texttt{Bmat},A$.Smatis the same matrix with the alternating sign $(-1)^i$ applied column-wise, which gives the shadow enumerator.- The equality block contains $A_0 = 1$, $\sum_j A_j = 2^{n-k}$, and $B_j - A_j = 0$ for $1 \le j \le d-1$.
- The inequality block contains $A_j - B_j \le 0$ for $j \ge d$, and $-S_j \le 0$ for all $j$.
- Each row is divided by its largest absolute coefficient. Without this, entries range from $1$ to about $10^{9}$ and the solver’s tolerances become unreliable.
- The objective is identically zero, because we only ask whether a feasible point exists. HiGHS reports status $0$ for a feasible problem, and any other status (in particular status $2$, infeasible) is treated as “no code”.
4.3 Speed
The expensive-looking part is the double loop over $(n,k)$ together with an inner loop over $d$. Three choices keep it fast:
- The Krawtchouk matrices are cached, as above.
- The inner loop stops at the first infeasible $d$, which is valid because feasibility is monotone in $d$.
- Each LP has at most $17$ variables, and HiGHS solves it in milliseconds.
The code search is vectorized with NumPy. All $4^n$ Pauli operators are tabulated once as bit vectors. A Pauli is encoded as the integer X | (Z << n), so multiplication of Paulis (ignoring phase) is a single XOR, and commutation with a generator is one matrix–vector product modulo $2$.
4.4 Random stabilizer code
random_stabilizer_code adds generators one at a time:
allowedmarks all Paulis that commute with every generator chosen so far.np.isin(codes, elems)removes those already in the generated group.- A random candidate $g$ is drawn from what remains. A candidate always exists while fewer than $n$ generators have been chosen, because the normalizer is strictly larger than the stabilizer.
- After choosing $g$,
allowedis intersected with the commutant of $g$, and the groupelemsis doubled by XOR with the code of $g$. - At the end,
in_Smarks the stabilizer andin_N(the finalallowedmask) marks the normalizer.np.bincountover the weights gives $A_j$ and $B_j$. - The distance is the minimum weight over $\mathcal{N}\setminus\mathcal{S}$.
No rejection sampling is needed, so every trial succeeds.
4.5 Consistency checks
After the search, the best code is tested against the theory:
- the quantum MacWilliams identity, $B = \frac{K}{2^n} Q A$ (the maximum error should be at rounding level),
- the inequality $B_j \ge A_j$,
- non-negativity of the shadow enumerator,
- LP feasibility at the found distance $d$ and, as a sanity check, infeasibility at $d+1$.
4.6 The figure
All plots are drawn in one figure with a $2 \times 3$ grid, so the output is a single image.
5. Execution Results
Quantum LP upper bound d_LP(n, k)
n \ k | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
---------------------------------------------------------
2 | 2 1 . . . . . . . . . . . . . .
3 | 3 1 1 . . . . . . . . . . . . .
4 | 4 2 2 1 . . . . . . . . . . . .
5 | 5 3 2 1 1 . . . . . . . . . . .
6 | 6 3 2 2 2 1 . . . . . . . . . .
7 | 7 3 2 2 2 1 1 . . . . . . . . .
8 | 8 3 3 3 2 2 2 1 . . . . . . . .
9 | 9 3 3 3 2 2 2 1 1 . . . . . . .
10 | 10 4 4 3 3 2 2 2 2 1 . . . . . .
11 | 11 5 4 3 3 3 2 2 2 1 1 . . . . .
12 | 12 5 4 4 4 3 3 2 2 2 2 1 . . . .
13 | 13 5 4 4 4 4 3 3 2 2 2 1 1 . . .
14 | 14 5 5 5 4 4 4 3 3 2 2 2 2 1 . .
15 | 15 5 5 5 5 4 4 4 3 3 2 2 2 1 1 .
16 | 16 6 6 5 5 5 4 4 4 3 3 2 2 2 2 1
LP feasible for [[5,1,3]] : True
LP feasible for [[5,1,4]] : False
LP solution A_j for [[5,1,3]] : [ 1. 0. 0. 0. 15. -0.]
Random search over 4000 stabilizer codes with n=5, k=1
distance d = 1 : 2321 codes (58.02 %)
distance d = 2 : 1663 codes (41.58 %)
distance d = 3 : 16 codes (0.40 %)
Best code distance : 3
A_j (stabilizer) : [ 1 0 0 0 15 0]
B_j (normalizer) : [ 1 0 0 30 15 18]
MacWilliams max error : 0.0
min (B_j - A_j) : 0
min shadow S_j : 0.0
LP feasible at d=3 : True
LP feasible at d=4 : False

6. Interpreting the Results
6.1 The 3D bar chart of $d_{\mathrm{LP}}(n,k)$ (top left)
The horizontal axes are the number of physical qubits $n$ and logical qubits $k$, and the height of each bar is $d_{\mathrm{LP}}(n,k)$. Two trends are visible:
- Along the $n$ axis the bars rise: more physical qubits allow a larger distance.
- Along the $k$ axis the bars fall: encoding more logical qubits into the same $n$ qubits costs protection.
This is the basic rate–distance trade-off of quantum coding. The LP bound shows exactly where the wall of impossibility lies.
6.2 The gap to the Singleton bound (top middle)
Here each bar is the amount by which the LP bound beats the quantum Singleton bound, $\lfloor (n-k)/2 \rfloor + 1 - d_{\mathrm{LP}}$. Where a bar is zero, the LP adds nothing beyond Singleton. Where a bar is positive, the LP has excluded distances that Singleton still allowed. The Singleton bound is a counting argument that ignores the finer structure of the weight distribution. The LP uses the full MacWilliams and shadow structure, so it is never weaker, and it becomes noticeably stronger as $n$ grows.
6.3 Heatmap and line plots (top right and bottom left)
The heatmap gives the exact integer values of $d_{\mathrm{LP}}$ and shows the staircase shape of the feasible region. In the line plot, each color is one value of $k$. The solid curve is the LP bound and the dashed curve of the same color is Singleton. The solid curve lies on or below the dashed one everywhere, and the distance between them is the extra information the LP provides.
6.4 The best code found (bottom middle)
The orange bars are the stabilizer enumerator $A_j$ and the blue bars are the normalizer enumerator $B_j$. For the perfect $[[5,1,3]]$ code we expect
$$
A = (1,0,0,0,15,0), \qquad B = (1,0,0,30,15,18).
$$
The total of $A$ is $16 = 2^{4}$ and the total of $B$ is $64 = 2^{6}$, as required. Because $B_j = A_j$ for $j = 1, 2$ and $B_3 = 30 > 0 = A_3$, there are weight-3 logical operators. This is exactly the statement that the distance is $3$. The console check that the MacWilliams error is at rounding level confirms that these enumerators are exactly the ones the LP constraints describe.
6.5 The distance histogram (bottom right)
Most random $[[5,1]]$ stabilizer codes have a small distance, and only a small fraction reach $d = 3$. The LP bound tells us that $d = 4$ is impossible, so $d = 3$ is optimal. Without that certificate, a random search could never tell whether a better code might still be hiding.
7. Conclusion
Linear programming gives a systematic way to explore the landscape of quantum codes:
- The constraints $B_j = A_j$ for $j < d$, $B_j \ge A_j$, and $S_j \ge 0$, together with the quantum MacWilliams identity, define a small LP for each triple $(n,k,d)$.
- Infeasibility of this LP proves that no $[[n,k,d]]$ code exists, and the table $d_{\mathrm{LP}}(n,k)$ summarizes all such certificates for $n \le 16$.
- The random stabilizer search complements the bound by constructing actual codes, and for $[[5,1]]$ it meets the bound with the perfect five-qubit code.
The same pipeline extends naturally to larger parameters, to non-additive codes by dropping the integrality of $A_j$, and to sharper bounds by adding further constraints to the LP.

















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$.



