Skip to content

Commit

Permalink
Make test ordering and parametrization reproducible (quantumlib#4788)
Browse files Browse the repository at this point in the history
Implement deterministic ordering of pytest parameters.
Add CIRQ_TESTING_RANDOM_SEED environment variable for seeding
random numbers generation in random and numpy.random modules.
Use commit epoch time as the default seed to have a non-constant,
but deterministic seeding.

NB: Use empty CIRQ_TESTING_RANDOM_SEED to prevent seeding, for example,
    CIRQ_TESTING_RANDOM_SEED="" check/pytest

This implements quantumlib#4787
  • Loading branch information
pavoljuhas authored Jan 7, 2022
1 parent c3edbeb commit f40cb10
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
5 changes: 2 additions & 3 deletions cirq/contrib/acquaintance/gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from string import ascii_lowercase as alphabet
from typing import Optional, Sequence, Tuple

import numpy as np
import numpy
import pytest

import cirq
Expand Down Expand Up @@ -232,9 +232,8 @@ def test_swap_network_init_error():
cca.SwapNetworkGate((3,))


rng = np.random.default_rng()
part_lens_and_acquaintance_sizes = [
[[l + 1 for l in rng.poisson(size=n_parts, lam=lam)], rng.poisson(4)]
[[l + 1 for l in numpy.random.poisson(size=n_parts, lam=lam)], numpy.random.poisson(4)]
for n_parts, lam in product(range(2, 20, 3), range(1, 4))
]

Expand Down
8 changes: 4 additions & 4 deletions cirq/ops/common_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,10 +1042,10 @@ def test_ixyz_circuit_diagram():
@pytest.mark.parametrize(
'theta,exp',
[
{sympy.Symbol("theta"), 1 / 2},
{np.pi / 2, 1 / 2},
{np.pi / 2, sympy.Symbol("exp")},
{sympy.Symbol("theta"), sympy.Symbol("exp")},
(sympy.Symbol("theta"), 1 / 2),
(np.pi / 2, 1 / 2),
(np.pi / 2, sympy.Symbol("exp")),
(sympy.Symbol("theta"), sympy.Symbol("exp")),
],
)
def test_rxyz_exponent(theta, exp):
Expand Down
13 changes: 8 additions & 5 deletions cirq/protocols/json_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,14 @@ def _list_public_classes_for_tested_modules():
# to remove DeprecationWarning noise during test collection
with warnings.catch_warnings():
warnings.simplefilter("ignore")
return [
(mod_spec, o, n)
for mod_spec in MODULE_TEST_SPECS
for (o, n) in mod_spec.find_classes_that_should_serialize()
]
return sorted(
(
(mod_spec, n, o)
for mod_spec in MODULE_TEST_SPECS
for (n, o) in mod_spec.find_classes_that_should_serialize()
),
key=lambda mno: (str(mno[0]), mno[1]),
)


@pytest.mark.parametrize(
Expand Down

0 comments on commit f40cb10

Please sign in to comment.