quantumlib / Stim

A fast stabilizer circuit library.
Apache License 2.0
329 stars 95 forks source link

Feature Request: Enhanced ramdom seed api #759

Closed Fadelis98 closed 5 months ago

Fadelis98 commented 5 months ago

problem description

In the current version, the samplers only takes in the ramdom seed at initialization, so if I want to reproduce a result at the third sample of the sampler, I could not skip the first 2 samples. It doesn't matter when I just want the 3rd sample, while it would be problematic if want a very late sample.

expected improvement

allow user to pass a rng state when sampling, e.g.

import random

rng_state = random.getstate()[1]
sampler = dem.compile_sampler()
syndromes,logical_flips,_ = sampler.sample(shots=num_shots, rng_key = rng_state)

So that the user can manage and trace the rng state by themselves if necessary.

Strilanc commented 5 months ago

Unfortunately, stim internally doesn't use the python random number generator. In fact, the simulation code is pure C++ with no knowledge of python at all. So this would be an absolutely enormous refactor to implement.

Also, entropy generation is one of the most performance sensitive parts of the code; incurring the overhead of a python call for each random number is likely to seriously hurt performance.

Because of these two reasons, I am not going to implement this change. I like the suggestion. It's not an unreasonable idea. It's just too much work for too little benefit.