Skip to content

Remove global random seed used for testing #1060

Open

Description

@mhucka

PR #1056 modified the pytest configuration to set a global random number seed, to deal with flaky tests that failed sporadically because some numerical comparisons exceeded tolerances undpredictably. Setting a global seed is not good practice. The seeds for test scenarios should be revised:

  • Avoid global seeds. It's generally better to avoid setting a global seed for all tests. Global seeds can cause unexpected behavior when different parts of the code rely on random numbers.

  • Use numpy.random.default_rng. It's considered better to create a Generator instance with a seed at the beginning of each test or test suite. This ensures that each test has its own independent random state.

  • Pass Generator objects. Pass the Generator object around as an argument. This makes the random number generation explicit and avoids relying on global state.

Activity

  1. added
    no QC knowledge neededDoes not require knowledge of quantum computing
    P2Priority: medium priority
    on Aug 30, 2025
  2. matti-sarjala commented on May 14, 2026

    @matti-sarjala
    Contributor

    A safe way to split this might be to avoid removing the global seed in the first PR.

    Possible incremental path:

    1. Identify tests that currently rely on implicit global randomness.
    2. Convert one small test module to use local numpy.random.default_rng(seed) instances.
    3. Pass the Generator explicitly where helper functions need randomness.
    4. Confirm that the converted module still passes when run repeatedly.
    5. Only after enough modules are converted, remove the global pytest seed as a final cleanup PR.

    This would reduce the risk of turning one global-seed cleanup into many unrelated flaky-test investigations.

    A minimal first PR could target just one test file and follow the invariant:

    “randomness used by a test should be local, explicit, and reproducible from the test itself.”

  3. removed
    P2Priority: medium priority
    on Aug 12, 2026
  4. fzlzjerry commented on Aug 12, 2026

    @fzlzjerry
    Contributor

    I'll take the first incremental slice in _multitensor_test.py: replace its uses of the legacy np.random global state with a fresh, deterministically seeded numpy.random.Generator fixture passed into each randomized test. I'll leave the global fixture in place so unrelated modules are not destabilized, and run this module repeatedly with varied pytest-randomly seeds.

  5. dhanvanthbalaji commented on Sep 29, 2026

    @dhanvanthbalaji

    Hi @mhucka! I'm a student looking to contribute to OpenFermion for a class project. I'd love to take an incremental slice of this issue by updating one of the test files. Could I be assigned to work on a test file?

  6. SripriyaDevarapalli commented on Oct 4, 2026

    @SripriyaDevarapalli

    Hi! I've addressed this issue by removing the autouse test fixture that reset Python's and NumPy's global random seeds before every test.

    The collection-time seeds have been retained to preserve deterministic test parameterization. I ran the RNG-related tests, with all 190 passing. The full test suite reported 2,223 passed, 66 skipped, and one failure related to the unsupported forkserver multiprocessing context on Windows with Python 3.14.

    I've submitted the changes in PR #1458. I'd appreciate any feedback or suggestions for improvement. Thank you!

  7. added
    e: experiencedExperience: needs familiarity with OpenFermion development
    on Oct 9, 2026
  8. SripriyaDevarapalli commented on Oct 12, 2026

    @SripriyaDevarapalli

    @mhucka can you please review my work and assign me this issue to work on it .
    Thank you !

  9. SripriyaDevarapalli commented on Oct 13, 2026

    @SripriyaDevarapalli

    @mhucka hey is there any update on my PR #1458 review , can you please let me know .

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: performanceArea: code performancea: pythonArea: Python codee: experiencedExperience: needs familiarity with OpenFermion developmentno QC knowledge neededDoes not require knowledge of quantum computing

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions