Passing a random number generator to simulator/sampler methods should VS maintaing an internal random state #6567
Description
All Cirq simulators maintain an internal np.random.RandomState
Cirq/cirq-core/cirq/sim/sparse_simulator.py
Line 130 in e1b03ef
This is fine when running in a single thread, however we are starting to have more places where use multiprocessing/multithreads (e.g. using multiprocessing.Pool
, concurrent.futures.ThreadPoolExecutor
, or other multiprocessing/multithreading libraries) and in these cases this internal random state negatively affects the simulations in two ways
- the internal random state becomes a shared state that causes the different threads/processes blocked on each other.
- the results of simulations become correlated.
Suggested solution: Start to prefer passing prng
s to methods/functions over maitaining an internal state. this prng
should be an np.random.Generator
instead of an np.random.RandomState
so that we get a spawn
method to use when starting threads/processes.
related: #6531