SWAP test and destructive SWAP test
Measure an observable of a quantum circuit
If we want to measure some observable
which
$$ \begin{align} U^{\dagger} O U = D \end{align} $$Measure X
Thus measure
Measure Y
Thus measure
Destructive SWAP test
The overlap between $\rho$ and $\sigma$ is
$$ \begin{align} \mathrm{Tr}[\rho\sigma] =& \sum_{j k} \rho_{j k} \sigma_{k j} = \sum_{pq}\sum_{j k} \delta_{p j}\delta_{qk}\rho_{pq} \sigma_{k j} = \sum_{pqjk} (SWAP)_{qj,pk} (\rho\otimes \sigma)_{pk, qj}\\ =& \mathrm{Tr}\left[ SWAP \cdot \rho\otimes \sigma \right] \end{align} $$and
$$ \begin{align} (H \otimes I\cdot CNOT) \cdot (SWAP)\cdot (CNOT \cdot H\otimes I) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1 \end{pmatrix} \end{align} $$$$ \begin{align} \mathrm{Tr}[\rho\sigma] = P_{00} + P_{01} + P_{10} - P_{11} = 1 - 2 P_{11} \end{align} $$Reference
arXiv:2011.01382v1: Hybrid quantum-classical algorithms and quantum error mitigation