The circuit sampling code doesn't have an option for this, but the detector error model sampling code does. stim.CompiledDemSampler.sample has a return_errors
argument that you can set to True
.
circuit = stim.Circuit(...)
dem = circuit.detector_error_model()
sampler = dem.compile_sampler()
dets, obs, errs = sampler.sample(shots=1024, return_errors=True)
The reason this isn't supported for circuits is because many error mechanisms have multiple possible symptoms so it would be more complex to index all possible symptoms. In principle it can be done, I just didn't want to have to pick and explain an indexing convention. Also, there are more redundancies where different errors produce the same detection events and perturbations of the observable.
For the machine learning use case, you can also consider injecting magical measurements of the stabilizers and the logical observable. As long as you don't use these magical measurements in any DETECTOR or OBSERVABLE_INCLUDE instruction, and we're not talking about a gauge code where the measurements keep anticommuting with the system state, the decoding problem should stay the same. But now you can use the magical measurements as a ground truth for the current state of the stabilizers and the observable at any one time.