# How to compute projective probabilities of a given measurement outcome using Stim?

I am looking to calculate the projective probabilities for each measurement outcome in a Clifford circuit and then output its corresponding tableau state. I am performing the measurement in the Z basis. The measurements should not be implementing postselections.

I have a similar code in QuTiP which gives a statevector after a projective measurement instead of Tableau representation.

for statename in [ '+x', '+z'] :
print('For an initial state',statename,':')
psi = states[statename]
M = measurement.measurement_statistics(psi,sigmaz())
for i in range(3) :
print('M[%d] = ' % i)
print(M[i])
print()

For an initial state +x :
M[0] =
[-1.  1.]
M[1] =
[Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[ 0.]
[-1.]]
Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[-1.]
[ 0.]]                                                      ]
M[2] =
[0.4999999999999998, 0.5000000000000001]

For an initial state +z :
M[0] =
[-1.  1.]
M[1] =
[Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[ 0.]
[-1.]]
Quantum object: dims = [[2], [1]], shape = (2, 1), type = ket
Qobj data =
[[-1.]
[ 0.]]                                                      ]
M[2] =
[0, 1.0]


Here M[2] gives the measurement probabilities of each outcome and M[1] states the corresponding statevectors. I want to obtain a Tableau state after performing a measurement and its corresponding probabilities. It may not list all the possible measurement outcome but instead only its output Tableau state and the corresponding probability. No noise, reset or postselection like situation are considered here.

Any help will be greatly appreciated. Thanks

• How complicated are these circuits? Do the measurements all happen in one layer, or are they spread throughout? Can the circuit contain noise channels whose effects you want to affect the probabilities? Are there mid-circuit resets? Aug 22, 2022 at 21:08
• @CraigGidney , I am trying to simulate the random Clifford circuit with measurement taking place on single qubits in each layer with probability p. No, there is no reset or noise channels which has any affect on the probabilities. Aug 23, 2022 at 6:29
• Is the probability of previous measurements being different supposed to be accounted for in the expectation, or is it assumed they are fixed when computing the probability? Aug 23, 2022 at 8:01
• @CraigGidney No I am not taking into account the classical probability p. I just want to obtain the simple quantum projective probabilities of any given quantum state. In here all the quantities are fixed. Sorry if I am not so clear. I think my objective is very simple. I just want to perform a quantum measurement on a given qubit state and then obtain its outcome i.e. both the output statevector and the probability with which I can obtain it for a single realization. I don't want to perform any statistics on them. All of this happens in a single realization. Aug 23, 2022 at 20:39
• I believe you that your case is simple, but it's important how it's simple for what will work and what won't. Perhaps the easiest way to make this clear is to include an example circuit and an example expected output. Aug 23, 2022 at 20:41

If you sample the final state of a stim Circuit twice, getting state $$|A\rangle$$ and state $$|B\rangle$$, it will be the case that there exists some tensor product of Pauli operations $$P$$ such that $$P|A\rangle = |B\rangle$$. So once you know any one possible output state, you can switch your focus to "relative to this state, what different values of $$P$$ might I sample from this circuit?".
There are two sources of these $$P$$ flippers. Explicit noise channels and Heisenberg uncertainty. An explicit channel would be like X_ERROR(0.1) 5 or DEPOLARIZE(0.2) 6. Heisenberg uncertainty is the fact that operations like M 55 should be thought of as being followed by fully randomizing the commuting Pauli, so M 55 is intrinsically followed by Z_ERROR(0.5) 55. In a circuit without noise, all the $$P$$ values you see in the final state are from different combinations of these Z_ERROR(0.5) terms propagating to the end of the circuit.
So, one viable strategy is to identify all of these Heisenberg uncertainties and translate them from the time they apply to the time at the end of the circuit, where they correspond to 50/50 Pauli flips of the result. You then perform Gaussian elimination to remove the redundant ones, get a set of $$m$$ generators $$P_0$$, $$P_1$$, ..., $$P_{m-1}$$ with the property that the final state is always of the form $$P_0^{b_0} \ldots {P_{m-1}}^{b_{m-1}} |A\rangle$$ where each $$b_k$$. In fact, if there's no explicit noise in the circuit, then picking the value of each $$b_k$$ uniformly at random this is the same as sampling from the circuit.
An alternative strategy, for noiseless circuits, is to use the fact that you know the solution looks like $$P_0^{b_0} \ldots {P_{m-1}}^{b_{m-1}} |A\rangle$$ with uniformly random $$b_k$$ and just back-infer the generators from a lot of samples. If, given a $$|B\rangle$$ that happens to equal some $$P$$ times $$|A\rangle$$, you can figure out the value of $$P$$, then you are basically set. Sample the circuit, and convert that sample into a sampled flipper $$P_s$$. Check if $$P_s$$ is a product of your existing flipper generators or not. If it is a product, discard it. If it isn't, add it as a new generator. Repeat this process until you haven't seen a new product for 40 straight attempts. There is now a less than one in a trillion chance that your current set of generators is missing a generator present in the circuit.