Finding the Best Observable to Detect a Given Entangled State
Entanglement is a resource, but how do you certify that a lab-prepared state is actually entangled? Full state tomography is expensive. An entanglement witness is a cheaper alternative: a single observable $W$ whose expectation value is non-negative on every separable state and negative on at least one entangled state.
$$
\mathrm{Tr}(W\sigma)\ge 0\quad \forall,\sigma\in\mathrm{SEP},\qquad \mathrm{Tr}(W\rho)<0 ;\Rightarrow; \rho \text{ is entangled.}
$$
Measuring one number is enough to certify entanglement. The catch is that a witness built naively from the state you intended to prepare can fail on the noisy state you actually have. In this article we optimize the witness for a given entangled state and show that the intuitive choice is not the best one.
1. The Problem Setup
We consider two qubits and a family of pure states with a Schmidt angle $\theta$ and a relative phase $\varphi$:
$$
|\psi(\theta,\varphi)\rangle=\cos\theta,|00\rangle+e^{i\varphi}\sin\theta,|11\rangle .
$$
The state we want to certify is this pure state mixed with white noise:
$$
\rho(p)=p,|\psi_0\rangle\langle\psi_0|+(1-p),\frac{\mathbb{I}_4}{4},\qquad \theta_0=\frac{\pi}{6},;; \varphi_0=\frac{\pi}{2},;; p=0.6 .
$$
Witness family
For every $(\theta,\varphi)$ define

This is a valid witness. For any separable state $\sigma$, the overlap with a pure state is bounded by the largest squared Schmidt coefficient, $\langle\psi|\sigma|\psi\rangle\le\lambda_{\max}^2$, hence $\mathrm{Tr}(W\sigma)\ge0$.
Fair comparison through normalization
Different witnesses have different scales, so we normalize each one by its trace:
$$
\tilde W=\frac{W}{\mathrm{Tr},W},\qquad \mathrm{Tr},W=4\lambda_{\max}^2-1 .
$$
The optimization problem
$$
\min_{\theta,\varphi};\mathrm{Tr}!\left[\tilde W(\theta,\varphi),\rho\right]
=\min_{\theta,\varphi};\frac{\lambda_{\max}^2(\theta)-p,|\langle\psi(\theta,\varphi)|\psi_0\rangle|^2-\frac{1-p}{4}}{4\lambda_{\max}^2(\theta)-1}.
$$
The overlap has a closed form, which lets us evaluate the whole landscape quickly:
$$
\langle\psi(\theta,\varphi)|\psi_0\rangle=\cos\theta\cos\theta_0+\sin\theta\sin\theta_0,e^{i(\varphi_0-\varphi)} .
$$
A reference answer from the PPT criterion
For two qubits, a state is entangled if and only if its partial transpose $\rho^{T_B}$ has a negative eigenvalue. For our state,
$$
\lambda_{\min}!\left(\rho^{T_B}\right)=\frac{1-p}{4}-\frac{p}{2}\sin 2\theta_0 ,
$$
which becomes negative exactly when
$$
p>\frac{1}{1+2\sin 2\theta_0}=\frac{1}{1+\sqrt3}\approx 0.366 .
$$
The witness $|v\rangle\langle v|^{T_B}$ built from the negative eigenvector $|v\rangle$ attains this value, so it is the benchmark any witness can be compared against.
2. The Complete Python Code
Everything is in a single cell. The heavy 3D landscape is computed with the vectorized closed form instead of a double loop over matrices, and a consistency check confirms that it agrees with the explicit matrix calculation.
1 | import numpy as np |
3. Detailed Code Walkthrough
Building blocks
psi_vec(theta, phi)returns the four-component vector $\cos\theta,|00\rangle+e^{i\varphi}\sin\theta,|11\rangle$ in the basis ${|00\rangle,|01\rangle,|10\rangle,|11\rangle}$. Index 0 is $|00\rangle$ and index 3 is $|11\rangle$.witness(theta, phi)implements $W=\lambda_{\max}^2\mathbb{I}-|\psi\rangle\langle\psi|$. The constantais the larger of $\cos^2\theta$ and $\sin^2\theta$, which guarantees that no separable state gives a negative value.normalized_valuecomputes $\mathrm{Tr}(W\rho)/\mathrm{Tr}(W)$. Dividing by the trace puts every witness on the same scale, so the comparison is fair.target_statebuilds the noisy state $\rho(p)$ as a $4\times4$ density matrix.
Partial transpose and the PPT benchmark
partial_transpose reshapes the matrix into a tensor $\rho_{ab,a’b’}$ with shape (2,2,2,2), swaps the two indices of the second qubit with transpose(0, 3, 2, 1), and reshapes it back. ppt_min_eigenvalue then returns the smallest eigenvalue of $\rho^{T_B}$. Because $\mathrm{Tr}(|v\rangle\langle v|^{T_B}\rho)=\langle v|\rho^{T_B}|v\rangle$, this eigenvalue equals the value attained by the optimal PPT-type witness, and it serves as the ground truth.
Optimization
optimize_witness minimizes $\mathrm{Tr}(\tilde W\rho)$ over $(\theta,\varphi)$ with Nelder–Mead. The landscape is periodic in $\varphi$ and has a kink at $\theta=\pi/4$, where $\lambda_{\max}^2$ switches branches. A single starting point could get stuck, so the function launches $3\times4=12$ starting points and keeps the best result. Nelder–Mead needs no gradients, which suits the non-smooth point at $\theta=\pi/4$.
Speed-up for the landscape
Evaluating the witness matrix at every point of a $120\times180$ grid would mean 21,600 matrix constructions. value_grid instead uses the analytic overlap
$$
\mathrm{Tr}(W\rho)=\lambda_{\max}^2-p,|\langle\psi|\psi_0\rangle|^2-\frac{1-p}{4},
$$
evaluated for the whole mesh by NumPy broadcasting. The line that prints the “Consistency check” compares this closed form against the explicit matrix version at one point and should show a value near zero.
Noise sweep
The loop over ps repeats the optimization for 33 noise levels and records three curves: the witness built from the target state, the Bell-type witness $(\theta=\pi/4)$, and the numerically optimized witness. The helper crossing finds the value of $p$ at which each curve crosses zero by linear interpolation. That crossing is the detection threshold of that witness.
4. Console Output
State: p = 0.60, theta0 = pi/6, phi0 = pi/2 Tr(W rho) with the target-state witness : +0.025000 Tr(W rho) with the Bell-type witness : -0.159808 Tr(W rho) with the optimized witness : -0.159808 Min eigenvalue of rho^(T_B) (PPT bound) : -0.159808 Optimal theta = 0.785398 (pi/4 = 0.785398) Optimal phi = 1.570796 (phi0 = 1.570796) Consistency check (matrix vs. analytic): 0.00e+00 Detection threshold, target-state witness : p > 0.667 Detection threshold, Bell-type witness : p > 0.366 Detection threshold, optimized witness : p > 0.366 Exact entanglement threshold (PPT) : p > 0.366
What the numbers mean
For the state with $p=0.6$, the target-state witness uses $\lambda_{\max}^2=\cos^2\theta_0=0.75$ and $\mathrm{Tr},W=2$. Its value is
$$
\mathrm{Tr}(\tilde W\rho)=\frac{0.75-\left[p+\frac{1-p}{4}\right]}{2}=\frac{0.75-0.70}{2}=+0.025>0 ,
$$
so it fails to detect the entanglement, even though the state is entangled. The Bell-type witness at $\theta=\pi/4$ has $\lambda_{\max}^2=1/2$ and $\mathrm{Tr},W=1$, and gives
$$
\mathrm{Tr}(\tilde W\rho)=\frac12-\left[p,\frac{1+\sin 2\theta_0}{2}+\frac{1-p}{4}\right]\approx-0.1598<0 ,
$$
which is a clear violation. The optimizer lands on $\theta^\ast=\pi/4$, $\varphi^\ast=\varphi_0$, and its value coincides with the PPT lower bound
$$
\lambda_{\min}!\left(\rho^{T_B}\right)=\frac{1-p}{4}-\frac{p}{2}\sin 2\theta_0\approx-0.1598 .
$$
The three detection thresholds printed at the end confirm the same picture. The target-state witness needs $p>2/3$, while the optimized witness reaches the exact entanglement threshold $p>1/(1+\sqrt3)\approx0.366$.
5. Graphs
All four panels are produced by one figure.

Top left: the 3D witness landscape
The surface shows $\mathrm{Tr}(\tilde W\rho)$ over the witness parameters $(\theta,\varphi)$. Blue regions are negative, meaning the witness detects entanglement. Red regions are positive, meaning it does not. The green point is the witness built from the target state, $(\theta_0,\varphi_0)$, and it lies in the positive region. The black point is the true minimum, which sits at the deepest part of the blue valley. The valley is centered on the phase $\varphi=\varphi_0$, so the phase must match the state exactly, while the Schmidt angle should be pushed towards the maximally entangled value $\pi/4$. The kink along $\theta=\pi/4$ comes from the $\max(\cdot)$ in $\lambda_{\max}^2$.
Top right: the contour map
This is the top view of the same landscape. The black line marks $\mathrm{Tr}=0$, and everything inside it is a successful witness. The green point lies outside the contour, so the intuitive witness fails. The black point lies near the center of the negative region. The elongated shape of the contour shows that detection is sensitive to the phase $\varphi$ and tolerant to moderate changes in $\theta$ around $\pi/4$.
Bottom left: detection power versus noise
Each curve shows the witness value as the noise level changes. A witness certifies entanglement wherever its curve is below zero. The green curve (target-state witness) crosses zero at $p\approx0.667$, the orange curve (Bell-type witness) at $p\approx0.366$, and the blue curve (optimized witness) also at $p\approx0.366$. The dashed vertical line marks the exact PPT threshold. The optimized witness reaches this limit, which no witness can beat for two qubits. The target-state witness wastes almost half of the detectable range.
Bottom right: slices along $\theta$
Here the phase is fixed at $\varphi_0$ and $\theta$ is scanned for several noise strengths. Every curve has its minimum at the dashed line $\theta=\pi/4$, not at the dotted line $\theta_0$. As $p$ decreases, the curves rise and the dip below zero shrinks. Near $p=0.4$ the minimum is only just negative, and it reaches zero at the threshold $p\approx0.366$.
6. Conclusion
A witness tailored to the state you intended to prepare is not necessarily the best witness for the state you actually hold. For the weakly entangled state $\theta_0=\pi/6$ mixed with white noise, the target-state witness detects entanglement only for $p>2/3$. The optimized witness, with $\theta=\pi/4$ and the phase matched to $\varphi_0$, detects it all the way down to $p\approx0.366$, which is the exact boundary given by the PPT criterion.
The same strategy extends to larger systems. One parametrizes a family of valid witnesses, evaluates $\mathrm{Tr}(\tilde W\rho)$ for the state at hand, and minimizes over the family. In higher dimensions, where the PPT criterion is no longer sufficient, this optimization over witnesses becomes the practical tool for certifying entanglement.




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









. The dark-to-bright scale makes solver-level errors visible.

