-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add PRNG support with singleton pattern #7360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your contribution ... please address the comments
cirq-core/cirq/value/prng.py
Outdated
|
||
# Singleton generator instance for None input, created on demand. | ||
# Avoids creating many Generators if parse_prng(None) is called frequently. | ||
_NONE_PRNG_INSTANCE: np.random.Generator = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a global state is not something we want. a global generator will come with two negative side effects 1) it will break multiprocessing/multithreading 2) it will be a shared state that may result in correlated simulation results
creating new generators is fine
cirq-core/cirq/value/prng.py
Outdated
@@ -0,0 +1,88 @@ | |||
# Copyright 2024 The Cirq Developers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code is being depveloped in 2025, same goes for the other files
# Copyright 2024 The Cirq Developers | |
# Copyright 2025 The Cirq Developers |
This merge integrates upstream changes with our local PRNG updates, including: - Removed singleton PRNG pattern - Updated copyright year - Fixed formatting and linting issues
Closing this PR for now due to shifting priorities. If anyone wants to pick this up, should just be a few modifications. |
Fixes #6531
This PR introduces a new
PRNG_OR_SEED_LIKE
type and parse_prng function to improve random number generation in Cirq. The key changes are:New
PRNG_OR_SEED_LIKE
type:None
,np.random.Generator
, andnp.random.RandomState
RANDOM_STATE_OR_SEED_LIKE
Singleton pattern for
None
input:np.random.Generator
instance forNone
inputsImproved random number generation:
np.random.Generator
objectsnp.random.RandomState
conversionnp.random
module as inputFixes:
The implementation includes comprehensive tests covering:
None