I am trying to understand the syntax of Stim detector and measurements.

Consider the following code (from the official pedagogic example)

import stim
circuit = stim.Circuit()

# First, the circuit will initialize a Bell pair.
circuit.append("H", [0])
circuit.append("CNOT", [0, 1])

# Then, the circuit will measure both qubits of the Bell pair in the Z basis.
circuit.append("M", [0, 1])

sampler = circuit.compile_sampler()

# Indicate the two previous measurements are supposed to consistently agree.
circuit.append("DETECTOR", [stim.target_rec(-1), stim.target_rec(-2)])

The first print returns:

 [[False False]
 [ True  True]
 [False False]
 [False False]
 [ True  True]
 [ True  True]
 [False False]
 [False False]
 [False False]
 [False False]]

Question 1: What does the syntax stim.target_rec(-x) precisely means?

I know it is supposed to refer to the x'th last measurement outcome, but how are the measurement outcome precisely stored? I didn't find where it is explained (the function description in "python_api_reference_vDev.md" does not really help understanding it). To illustrate my confusion: a naïve interpretation of the first print would indicate that the x'th last measurement outcome is a 2D array containing the outcome of the measurement of the 1st and 2nd qubit respectively. This interpretation is incompatible with the notation [stim.target_rec(-1), stim.target_rec(-2)] because this last notation mentions the two qubits outcome separately.

Question 2: More generally: is there a kind of general documentation of Stim? Outside of the function descriptions in "python_api_reference_vDev.md" A kind of wiki that enters a bit more in the details?


1 Answer 1


There is no requirement for a simulator to store that record in any particular way; it just needs to be the case that you can refer to it by index. The Semantics -> State Space section of the stim file format spec just says that the measurement record is a list of the measurements that have occurred.

Different simulator stores the measurement record in different ways.

  • The tableau simulator stim.TableauSimulator is backed by the C++ class stim::TableauSimulator<W> has a field MeasureRecord measurement_record where MeasureRecord is a class with a field std::vector<bool> storage which is where the measurement bits are put.

  • The flip simulator stim.FlipSimulator is backed by the C++ class stim::FrameSimulator<W> has a field MeasureRecordBatch<W> m_record where MeasureRecordBatch<W> has a field simd_bit_table<W> storage where simd_bit_table<W> is a pointer to a buffer plus some width/height parameters. This simulator runs hundreds or thousands of shots in parallel, and the tableau is storing flip results in this way so that it's possible to compute detectors across multiple shots in a single-instruction-multiple-data style using the _mm256_xor_si256 instruction.

  • The measurement sampler runs the tableau simulator against a noiseless version of the circuit to get a measurement record forming the reference sample. It then runs the flip simulator against the noisy circuit, and xors the propagated flips into the reference sample in order to get samples. So in this case the measurement record is sort of spread across two locations (the reference sample and the flips) with the "true" record being the xor of those two locations.

  • The detector sampler skips the whole reference sample thing, because detectors are defined in terms of being-flipped-compared-to-noiseless as opposed to in terms of an absolute measurement-is-0-or-1. So in the detector sampler the measurement record technically isn't ever even computed, only the measurement flips are computed, but you can't tell because what's being output can't depend on the details that are skipped.

But like I said, none of this is actually relevant to how you use measurement records in the circuit. In fact these underlying details have changed over time, while preserving the semantic behavior exposed to the user.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.