Skip to content

Use local random seeds in slater determinant tests#1308

Open
mzl2233 wants to merge 1 commit into
quantumlib:mainfrom
mzl2233:fix/local-random-seeds
Open

Use local random seeds in slater determinant tests#1308
mzl2233 wants to merge 1 commit into
quantumlib:mainfrom
mzl2233:fix/local-random-seeds

Conversation

@mzl2233
Copy link
Copy Markdown

@mzl2233 mzl2233 commented May 14, 2026

This updates the Slater determinant Gaussian-state tests to keep their random choices local to the test case. The Hamiltonian construction now receives explicit per-test seeds, and the excited-state orbital selections use a local NumPy generator instead of the module-level random state, which makes this module less dependent on the global pytest seed while preserving deterministic coverage. Verified with python3 -m compileall -q src/openfermion/circuits/slater_determinants_test.py, python3 -m pytest src/openfermion/circuits/slater_determinants_test.py -q -k 'JWGetGaussianStateTest', and git diff --check. Part of #1060.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 14, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the JWGetGaussianStateTest class to use reproducible random number generation by introducing numpy.random.default_rng and SeedSequence. Feedback from the reviewer indicates that calling spawn(1) inside loops causes the same seed to be generated for every iteration, which reduces test diversity. Additionally, the reviewer pointed out that maintaining both a Generator and a SeedSequence with the same initial seed is redundant and could lead to correlated random streams, suggesting instead that a single generator be used for all random choices.

def setUp(self):
self.n_qubits_range = range(2, 10)
self.rng = numpy.random.default_rng(5387)
self.hamiltonian_seed_sequence = numpy.random.SeedSequence(5387)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The SeedSequence is redundant here because self.rng (a Generator) can be used to produce seeds for the Hamiltonian construction. Additionally, using the same seed (5387) for both default_rng and SeedSequence can lead to correlated random streams. It is recommended to use a single generator for all random choices in the test to ensure independence and simplify the code.

Suggested change
self.hamiltonian_seed_sequence = numpy.random.SeedSequence(5387)
# self.hamiltonian_seed_sequence is not needed if we use self.rng

Comment on lines +148 to +149
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, True, seed=hamiltonian_seed)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Calling spawn(1) inside the loop returns the same child SeedSequence in every iteration, which results in the same hamiltonian_seed being used for all values of n_qubits. This reduces the diversity of the test cases. Use the existing self.rng to generate a unique seed for each iteration.

Suggested change
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, True, seed=hamiltonian_seed)
hamiltonian_seed = int(self.rng.integers(0, 2**32))
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, True, seed=hamiltonian_seed)

Comment on lines +171 to +172
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, False, seed=hamiltonian_seed)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Calling spawn(1) inside the loop returns the same child SeedSequence in every iteration, which results in the same hamiltonian_seed being used for all values of n_qubits. Use the existing self.rng to generate a unique seed for each iteration.

Suggested change
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, False, seed=hamiltonian_seed)
hamiltonian_seed = int(self.rng.integers(0, 2**32))
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, False, seed=hamiltonian_seed)

Comment on lines +194 to +195
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, True, seed=hamiltonian_seed)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Calling spawn(1) inside the loop returns the same child SeedSequence in every iteration, which results in the same hamiltonian_seed being used for all values of n_qubits. Use the existing self.rng to generate a unique seed for each iteration.

Suggested change
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, True, seed=hamiltonian_seed)
hamiltonian_seed = int(self.rng.integers(0, 2**32))
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, True, seed=hamiltonian_seed)

Comment on lines +227 to +228
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, False, seed=hamiltonian_seed)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Calling spawn(1) inside the loop returns the same child SeedSequence in every iteration, which results in the same hamiltonian_seed being used for all values of n_qubits. Use the existing self.rng to generate a unique seed for each iteration.

Suggested change
hamiltonian_seed = self.hamiltonian_seed_sequence.spawn(1)[0].generate_state(1)[0]
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, False, seed=hamiltonian_seed)
hamiltonian_seed = int(self.rng.integers(0, 2**32))
quadratic_hamiltonian = random_quadratic_hamiltonian(n_qubits, False, seed=hamiltonian_seed)

@mhucka mhucka added the area/tests Involves testing and test cases label May 16, 2026
@mhucka mhucka self-assigned this May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tests Involves testing and test cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants