Skip to content

Different seeds can give identical samples (noise seed has only 30 bits) #261

Closed

Description

@rafaelha

Hi! A quick one that's probably worth settling before 1.0.0, since the fix changes seeded samples.

The sampler turns the user's seed into the noise seed with only 30 bits of entropy (src/tsim/sampler.py):

channel_seed = int(np.random.default_rng(seed).integers(0, 2**30))

seed=None also draws just 30 bits (integers(0, 2**30)). By the birthday bound, a sweep over seeds 0…N has about N²/2³¹ colliding pairs. Seeds 0–300,000, for example, contain 46 pairs that give the same noise. For a noise-only circuit those pairs return bit-identical samples:

import numpy as np, stim, tsim

c = tsim.Circuit(str(stim.Circuit.generated(
    "surface_code:rotated_memory_z", distance=3, rounds=3,
    after_clifford_depolarization=0.01)))
a = c.compile_detector_sampler(seed=37430).sample(100_000)
b = c.compile_detector_sampler(seed=43909).sample(100_000)
np.array_equal(a, b)   # True: both seeds map to channel seed 644760479

With statevector components, the noise columns still match and only the component outcomes differ. Runs that combine several seeds, or treat them as independent, end up double-counting shots.

Possible fix: pass the full seed through, e.g. seed the channel sampler from np.random.default_rng([seed, 0]) or np.random.SeedSequence(seed).spawn(...), and give seed=None a 64-bit draw. That changes the samples you get for a given seed, so it's nicer to do before 1.0.0 than after.

Reproduces on main (e3a465b) and on #259 (a9af140).

🤖 Generated with Claude Code

Activity

  1. added a commit that references this issue on Nov 4, 2026
    40b7423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions