Skip to content

Fix #1060: Remove global random seed from tests#11

Draft
mhucka wants to merge 1 commit intomainfrom
jules-fix-1060-6998816404824291497
Draft

Fix #1060: Remove global random seed from tests#11
mhucka wants to merge 1 commit intomainfrom
jules-fix-1060-6998816404824291497

Conversation

@mhucka
Copy link
Copy Markdown
Owner

@mhucka mhucka commented Mar 13, 2026

Removed the global seed from conftest and fixed the randomly parametrized inputs.


PR created automatically by Jules for task 6998816404824291497 started by @mhucka

Following best practices mentioned in issue quantumlib#1060, the global `np.random.seed`
and `random.seed` calls from the conftest.py `set_random_seed` fixture
have been completely removed.

To fix the resulting pytest-xdist "Different tests were collected" errors
arising from dynamically random generated lists passed to
`@pytest.mark.parametrize`, those test generation logics have been updated
to either use locally scoped seeds, explicitly predefined test sets, or fixed inputs.
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where tests relied on global random seeds and dynamically generated parameters, leading to potential non-reproducibility. By removing the global seed and replacing random parameter generation with fixed values or localized seeding, the changes ensure that tests are deterministic and consistently verifiable.

Highlights

  • Removed Global Random Seed: The global random seed fixture in conftest.py was removed, eliminating a source of non-determinism across tests.
  • Fixed Randomly Parametrized Inputs: All instances where test parameters were generated using random functions (e.g., np.random.randint, np.random.rand, random.uniform, random.sample) have been replaced with fixed, explicit values or explicitly seeded random number generators within the test functions themselves.
Changelog
  • src/openfermion/circuits/gates/fermionic_simulation_test.py
    • Replaced np.random.randint with fixed integer seeds for test_interaction_operator_interconversion.
    • Replaced np.random.rand with a predefined list of float tuples for test_weights_and_exponent.
    • Replaced np.random.uniform calls with fixed complex and real numbers for test_zero_weights.
    • Replaced np.random.uniform calls with fixed complex array and real number pairs for test_cubic_fermionic_simulation_gate_consistency_docstring.
    • Replaced np.random.rand with a predefined list of float tuples for test_quartic_fermionic_simulation_decompose.
    • Replaced np.random.uniform calls with fixed complex array and real number pairs for test_quartic_fermionic_simulation_unitary.
    • Replaced np.random.uniform calls with fixed complex array and real number pairs for test_quartic_fermionic_simulation_apply_unitary.
  • src/openfermion/conftest.py
    • Removed the set_random_seed pytest fixture that globally set random.seed(0) and np.random.seed(0).
  • src/openfermion/hamiltonians/general_hubbard_test.py
    • Replaced random.uniform calls with fixed float values for test_fermi_hubbard_square_special_general_equivalence.
    • Modified test_fermi_hubbard_square_lattice_random_parameters to accept explicit seed values and set random.seed(seed) internally, rather than generating random parameters in the decorator.
  • src/openfermion/testing/random_test.py
    • Replaced random.randrange with a predefined list of fixed integer seed values for test_random_interaction_operator_term.
  • src/openfermion/utils/lattice_test.py
    • Replaced random.sample with fixed lists of integer dimensions for test_spin.
    • Replaced random.sample with fixed lists of integer dimensions for test_hubbard_square_lattice_1xd.
    • Replaced random.sample with fixed integer pairs for test_hubbard_square_lattice_neighbors.
    • Replaced random.sample with fixed lists of integer dimensions for test_hubbard_square_lattice_2xd.
    • Replaced randomly generated dictionary kwargs with fixed dictionaries for test_hubbard_square_lattice_repr.
Activity
  • PR created automatically by Jules for task 6998816404824291497 started by @mhucka.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@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 commendably removes the global random seed from conftest.py to improve test reproducibility, replacing random parameter generation with deterministic, hardcoded values across several test files. The changes are mostly correct and follow good testing practices. However, I've identified one instance where a for loop was inadvertently left in place after hardcoding values, leading to redundant test executions. My specific comment provides details on how to address this.

Comment on lines 264 to 270
[
(
(np.random.uniform(-5, 5) + 1j * np.random.uniform(-5, 5), np.random.uniform(-5, 5)),
np.random.uniform(-5, 5),
(1.2 + 3.4j, 2.1),
-1.5,
)
for _ in range(5)
],
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The list comprehension with for _ in range(5) on line 269, combined with the now hard-coded values, will cause the test test_quadratic_fermionic_simulation_gate_unitary to run 5 times with the exact same parameters. This is redundant and inefficient. You should either provide different sets of hard-coded parameters for each run (as you've done elsewhere in this file) or remove the loop to run the test just once.

    [
        ((1.2 + 3.4j, 2.1), -1.5)
    ],

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant