Skip to content

Non-deterministic sampling when seed is fixed but batch_size or reference sampling varies #104

@rafaelha

Description

@rafaelha

Tsim sampling is currently not fully deterministic with respect to the random seed. While setting a fixed seed is expected to produce reproducible results, the output also depends on the internal batch_size used during sampling, as well as whether compute_reference_X_sample=True is enabled.

This violates the common expectation that fixing the seed alone should guarantee reproducibility.

Minimal Example

c = tsim.Circuit("""
X_ERROR(0.01) 0 1
M 0 1
""")

sampler = c.compile_sampler(seed=0)
samples = sampler.sample(shots=100, batch_size=100)

sampler2 = c.compile_sampler(seed=0)
samples2 = sampler2.sample(shots=100, batch_size=10)

assert np.all(samples == samples2)  # Fails

The assertion fails even though the same seed is used. The assertion passes when batch_size is the same for both sample calls.

Observed Behavior

  • Changing batch_size changes the sampled results, even with a fixed seed.
  • When batch_size is not explicitly specified, Tsim selects it dynamically (e.g., based on available memory), which can lead to inconsistent results across runs or environments.
  • When compute_reference_X_sample=True, Tsim may internally increase the batch size (e.g., by 1), further affecting determinism.

Expected Behavior

  • Sampling should be fully deterministic given a fixed seed, regardless of: batch_size, internal batching strategy, whether reference samples are computed

Root cause

Tsim samples random numbers per f variable, for a whole batch at once

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions