Skip to content

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

Closed
wants to merge 16 commits into from

Conversation

RevanthGundala
Copy link
Contributor

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:

  1. New PRNG_OR_SEED_LIKE type:

    • Supports None, np.random.Generator, and np.random.RandomState
    • Provides better type safety compared to RANDOM_STATE_OR_SEED_LIKE
  2. Singleton pattern for None input:

    • Uses a singleton np.random.Generator instance for None inputs
    • Reduces memory usage and prevents multiple Generator instances
  3. Improved random number generation:

    • Always returns np.random.Generator objects
    • Better handling of np.random.RandomState conversion
    • No support for np.random module as input
  4. Fixes:

    • Addresses multiprocessing issues by avoiding shared random state
    • Improves type safety and code maintainability
    • Ensures consistent random number generation across processes

The implementation includes comprehensive tests covering:

  • Singleton behavior for None
  • Integer seeding consistency
  • Type validation
  • RandomState conversion
  • Equality testing for different input types

@RevanthGundala RevanthGundala requested review from vtomole and a team as code owners May 20, 2025 17:34
@github-actions github-actions bot added the size: M 50< lines changed <250 label May 20, 2025
Copy link
Collaborator

@NoureldinYosri NoureldinYosri left a 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


# 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
Copy link
Collaborator

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

@@ -0,0 +1,88 @@
# Copyright 2024 The Cirq Developers
Copy link
Collaborator

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

Suggested change
# Copyright 2024 The Cirq Developers
# Copyright 2025 The Cirq Developers

@NoureldinYosri NoureldinYosri self-assigned this Jun 4, 2025
@github-actions github-actions bot added size: L 250< lines changed <1000 and removed size: M 50< lines changed <250 labels Jun 17, 2025
This merge integrates upstream changes with our local PRNG updates, including:
- Removed singleton PRNG pattern
- Updated copyright year
- Fixed formatting and linting issues
@RevanthGundala
Copy link
Contributor Author

Closing this PR for now due to shifting priorities. If anyone wants to pick this up, should just be a few modifications.

@RevanthGundala RevanthGundala deleted the prngs branch June 21, 2025 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update cirq.RANDOM_STATE_OR_SEED_LIKE to support np.random.Generator
2 participants