Skip to main content

MeasurementSampler

Struct MeasurementSampler 

Source
pub struct MeasurementSampler { /* private fields */ }
Expand description

Fast repeated measurement sampling from a compiled Stim circuit.

An analyzed stabilizer circuit whose measurements can be sampled quickly. The sampler uses a noiseless reference sample, collected from the circuit using Stim’s Tableau simulator during initialization, as a baseline for deriving more samples using an error-propagation simulator.

This is the Rust equivalent of Python’s stim.CompiledMeasurementSampler. Obtain one via Circuit::compile_sampler.

§Examples

let circuit: stim::Circuit = "X 0\nM 0 1".parse().unwrap();
let mut sampler = circuit.compile_sampler(false);
let results = sampler.sample(4);
assert_eq!(results.ncols(), 2);
assert_eq!(results.nrows(), 4);
// Qubit 0 was flipped by X, qubit 1 was not.
assert!(results[[0, 0]]);
assert!(!results[[0, 1]]);

Implementations§

Source§

impl MeasurementSampler

Source

pub fn num_measurements(&self) -> u64

Creates a measurement sampler for the given circuit.

The sampler uses a noiseless reference sample, collected from the circuit using Stim’s Tableau simulator during initialization, as a baseline for deriving more samples using an error-propagation simulator.

§Arguments
  • circuit - The stabilizer circuit to sample measurements from.
  • skip_reference_sample - When true, the reference sample is set to all zeros instead of being collected from the circuit. This means returned results represent whether each measurement was flipped, rather than actual measurement outcomes. Useful when you only care about error propagation or when you know the all-zero result is a valid noiseless outcome. Computing the reference sample is the most expensive part of initialization, so skipping it is an effective optimization.
  • seed - Deterministically seeds the random number generator. Making the exact same series of calls on the same machine with the same Stim version will produce the same results. Results are not guaranteed to be consistent across Stim versions, SIMD widths, or varying shot counts.
§Examples

Returns the number of measurement bits produced per shot.

This equals the total number of M (and similar measurement) operations in the compiled circuit.

Source

pub fn sample_bit_packed(&mut self, shots: u64) -> Vec<u8>

Samples measurement data in bit-packed form.

Returns a flat Vec<u8> where each shot occupies ceil(num_measurements / 8) bytes. Within each byte, bits are packed in little-endian order, matching Stim’s b8 data layout: the bit for measurement m in shot s is at result[s * row_bytes + m / 8] >> (m % 8) & 1.

Source

pub fn sample(&mut self, shots: u64) -> Array2<bool>

Samples a batch of measurement results as an unpacked boolean matrix.

Returns an Array2<bool> with shape (shots, num_measurements). The bit for measurement m in shot s is at result[[s, m]].

§Examples
let circuit: stim::Circuit = "X 0\nM 0".parse().unwrap();
let mut sampler = circuit.compile_sampler(false);
assert_eq!(sampler.sample(3), ndarray::array![[true], [true], [true]]);
Source

pub fn sample_write( &mut self, shots: u64, filepath: impl AsRef<Path>, format_name: ShotDataFormat, ) -> Result<()>

Samples measurements from the circuit and writes them directly to a file.

This is more memory-efficient than calling sample and then writing the result, because the data is streamed to disk without ever being fully materialized in memory.

§Arguments
  • shots - The number of times to sample every measurement.
  • filepath - The file path to write results to.
  • format_name - The output format.
§Errors

Returns a StimError if the file path is not valid UTF-8 or the file cannot be written.

§Examples
let circuit: stim::Circuit = "X 0\nM 0".parse().unwrap();
let path = std::env::temp_dir().join("stim-rs-measurement-sampler-write.01");
let mut sampler = circuit.compile_sampler(false);
sampler
    .sample_write(2, &path, stim::ShotDataFormat::Bits01)
    .unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), "1\n1\n");
std::fs::remove_file(path).unwrap();

Trait Implementations§

Source§

impl Debug for MeasurementSampler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.