# Why does adding a 50/50 Z error after every initialization, reset and measurement account for randomness of outcomes in Stim?

Stim generates multiple (say N many) noisy samples by first collecting one reference sample, then running a Pauli frame simulator N times to track which errors may flip which measurements and eventually combining the N flip patterns with the same reference sample N times to produce N noisy samples. The reference sample is collected by simulating the circuit with no errors.

A relevant StackExchange answer also includes:

For example, on thousand qubit circuits with millions of measurements, Stim can take a few seconds to produce its reference sample. But then it starts spitting out thousands of circuit samples per second. What it's doing is pretending noiseless runs always produce the reference sample, except it's perturbed by the noise in the circuit. A key thing here is that the distinction between different choices of reference sample is removed by adding a 50/50 Z error after every reset and measurement. The set of noiseless samples forms a group and you sample uniformly from that group via these 50/50 Z errors.

Why can Stim pretend that noiseless runs always produce the reference sample and so add a 50/50 Z error after every reset and measurement to make up for the randomness of outcomes when sampling the noiseless circuit N many times? After a measurement for example, it may be that adding this occasional 50/50 Z error accounts for the probability of having either of two eigenstates as the post-measurement states. What if, the probabilities were not 50/50 though?

Thank you!

You can think of the 50/50 randomness of a measurement that anticommutes with the stabilizers as coming from the fact that you can apply the any one of the stabilizers to the state, as an operation, without changing the state. But if the measurement anticommutes with the stabilizers, this application would flip the measurement. It's isomorphic to the single qubit case: if you know the Z observable, you are completely uncertain about the X observable, and one way to see that is to notice that applying Z has no effect on the state but applying Z gate right before an X basis measurement would invert its result. You can see this isomorphism must hold because for any anti-commuting pair of Pauli products $$A, B$$ (e.g. one being the measured observable and the other being a stabilizer of the state), you can find a Clifford operation C such that $$CAC^\dagger = X_1$$ and $$CBC^\dagger = Z_1$$. Conjugating everything by a unitary just corresponds to doing analysis in a different basis, so this proves the general case must behave identical to the simple single qubit case.