I need some help on stim
, where I'm trying to compute expectation values of Pauli strings. Hopefully I did not overlook on the documentation an implementation of this method.
Problem Statement
Given a generic Pauli string $O$ acting on $N$ qubits and a stabilizer state $\rho$ on $N$ qubits, compute within stim
the expectation value
\begin{equation}
\langle O\rangle \equiv \mathrm{Tr}(O \rho).
\end{equation}
Since $O$ is a Pauli string, $\langle O\rangle \in \{0,+1,-1\}$.
Tentative Solution
To be concrete, within this question I fix $N=4$, and I want to compute $\mathrm{Tr}(X_1 Z_3 \rho)$. Furthermore, I specify I work on the c++
library, within which my state is contained in an instance of TableauSimulator
:
...
using namespace stim;
using namespace std;
mt19937_64 rng(1); // Random generator with SEED=1
MeasureRecord record; // Measurement records
TableauSimulator Psi(ref(rng),4,0,record);
// 0 is the unbiased-condition of the output sign for non-deterministic measurements
...
Now, I know I can apply a measurement gate MPP X1Z3
. If this measurement is deterministic (equivalently, if $X1Z3$ commute with $\rho$), the measurement readout gives $\langle O\rangle$, which is either $+1$ or $-1$.
If, instead, the measurement is non-deterministic (equivalently, if $X1Z3$ anticommute with $\rho$), $\langle O\rangle=0$.
Given the above, I tried the following. I initialize two new TableauSimulator
TableauSimulator PsiPlus = Psi;
TableauSimulator PsiMinus = Psi;
and applied to them PsiPlus
the gate MPP X1Z3
, whereas in PsiMinus
the gate MPP !X1Z3
. Then, since in general
\begin{equation}
\langle O\rangle = \mathrm{Tr}(O\rho) = \mathrm{Tr}\left(\frac{1+O}{2}\rho\right) -\mathrm{Tr}\left(\frac{1-O}{2}\rho\right) = p(+1) - p(-1),
\end{equation}
I expect the difference:
int aveO = PsiPlus.measurement_record.storage[last_entry]-PsiMinus.measurement_record.storage[last_entry];
where last_entry is the index of the last measurement (respectively of MPP X1Z3
for PsiPlus
and MPP !X1Z3
for PsiMinus
) should be the required value $\langle O\rangle$.
Problems There are problems with the above approach/ideas/implementation. It works if the measurement is deterministic, but it in general it doesn't for non-deterministic measurements (which is the main issue to solve). I think the reason is, since both states refer to the same random generator, the randomness in PsiPlus and PsiMinus are inequivalent, leading to different results (e.g. different internally drawn random numbers).
Furthermore, the operations required can be probably reduced.
Lastly, I conclude with a remark.
For single site measurements, the above issue do not figure in the present release of stim
, as there is a method TableauSimulator.is_deterministic_x
(and similarly for y,z) which should exactly check if the outcome of MX
(MY
,MZ
) is deterministic or not. If a similar method would be present for generic MPP
, probably a solution would still be easily implementable. In pseudo-code:
int aveO;
if (Psi.is_deterministic_mpp(`X1Z3`,{1,3}) {
TableauSimulator PsiM = Psi;
PsiM.apply_mpp(`X1Z3`,{1,3});
aveO = PsiM.measurement_record.storage[last_item];
}
Still, I'm not an expect in HPC, but probably there are smarter way to implement the computation of expectation values.