Skip to content

Updated RANDOM_STATE_OR_SEED_LIKE to support np.random.Generator (#6531)#8184

Open
Prashik123 wants to merge 2 commits into
quantumlib:mainfrom
Prashik123:feature/6531-random-generator
Open

Updated RANDOM_STATE_OR_SEED_LIKE to support np.random.Generator (#6531)#8184
Prashik123 wants to merge 2 commits into
quantumlib:mainfrom
Prashik123:feature/6531-random-generator

Conversation

@Prashik123

Copy link
Copy Markdown

Closes #6531

Description

This PR updates cirq.RANDOM_STATE_OR_SEED_LIKE and cirq.value.parse_random_state to explicitly support np.random.Generator.

As discussed in the issue thread, this is Phase 1 of the implementation. It focuses strictly on the public normalization boundary and backward compatibility:

  • np.random.Generator is now a valid input and passes through unmodified.
  • Existing behavior for int, np.random.RandomState, None, and the global np.random module remains fully backward compatible.
  • Invalid seed-like objects now raise a clear ValueError specifying the accepted types.
  • Added specific tests in random_state_test.py to verify Generator support and the updated error handling.

Future PRs can now safely migrate internal call sites to prefer Generator for parallel-safe behavior.

Checklist

  • I have signed the Google CLA.
  • Existing tests pass locally (pytest).
  • New code is covered by tests.
  • Code passes linting (pylint) and type checks (mypy).

Note for reviewers: Gemini 3.1 Pro Extended was used strictly as a conversational tutor to help me understand the repository structure and configure my local Windows testing environment. All code changes submitted in this PR are my own original creation in full compliance with the CLA.

@arettig arettig self-assigned this Jul 8, 2026

@arettig arettig left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thanks for the PR. There are some things that need to be addressed here:

  • The code does not pass multiple checks (mypy, ruff, format-incremental). Please ensure these pass locally.
  • You state in the PR comment that you added tests but there are no tests added.
  • You should not use the closes keyword in the comment since this PR is only part of the overall issue.
  • Your change allows passing a generator to parse_random_state which will then be returned as a generator. Many places in the code assume the return of this function is a RandomState, creating numerous problems (which is what mypy is catching).

You will need to rethink your design a little - take a look at the previous PR and discussion for this issue: #6566. There was some discussion about whether to extend RANDOM_STATE_OR_SEED_LIKE (like you did) or create a new type. From the looks of it, it was decided a new type was necessary.

elif isinstance(random_state, int):
return np.random.RandomState(random_state)
elif isinstance(random_state, (np.random.RandomState, np.random.Generator)):
return random_state

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will return a np.random.Generator which will cause problems anywhere in Cirq that parse_random_state() is called

"""
if random_state is None:
# FIX: Explicitly allow the np.random module to pass through
if random_state is None or random_state is np.random:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.random is not an allowed type of random_state

@mhucka
mhucka requested a review from NoureldinYosri July 9, 2026 22:36
@NoureldinYosri

Copy link
Copy Markdown
Collaborator

@mhucka I think patching RANDOM_STATE_OR_SEED_LIKE is no longer desirable. I think creating a new path for generators while deprecating the random state one is more advisable. the RandomState class is effectively deprecated by numpy

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

Labels

size: S 10< lines changed <50

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

3 participants