Skip to content

Add support for numpy.random.Generator #8374

Description

@arettig

Is your feature request related to a use case or problem? Please explain

np.RandomState has been effectively deprecated by numpy but it is still used throughout much of Cirq. Additionally, when multiple threads are used, RandomState also becomes a shared object, causing blocking between threads.

Describe the solution you would prefer

We should instead use numpy.random.Generator, the modern approach. We should provide a separate path for generating random numbers with Generators and ensure multithreading support. The old RandomState based paths may then be deprecated. Per previous work on this issue, the general approach should be:

  • A new type such as PRNG_OR_SEED_LIKE should be added, allowing for either a generator or a seed.
  • parse_random_generator should be added, which returns a np.random.Generator.
  • Existing calls to parse_random_state within cirq should be replaced with parse_random_generator.
  • parse_random_state should be deprecated.

How urgent is this for you? Is it blocking important work?

P2 – we should do it in the next couple of quarters

Provide additional information or context (e.g., screenshots)

This is an amalgamation of the discussion and conclusions reached within several issues and PRs: #6566, #6531, #6321, #8184

Related: #6567

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

good first issueThis issue can be resolved by someone who is not familiar with the codebase. A good starting issue.kind/feature-requestDescribes new functionalitykind/healthFor CI/testing/release process/refactoring/technical debt itemstriage/discussNeeds decision / discussion, bring these up during Cirq Cynque

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions