diff --git a/check/pytest b/check/pytest index 24f792899cf..e63bf6c07cf 100755 --- a/check/pytest +++ b/check/pytest @@ -33,6 +33,9 @@ if [ "$PYTHON_VERSION" -lt "37" ]; then PYTEST_ARGS+=("--ignore=cirq-rigetti") fi +: ${CIRQ_TESTING_RANDOM_SEED=$(git log -1 --pretty="%ct")} +export CIRQ_TESTING_RANDOM_SEED + if [ -z "${ACTUALLY_QUIET}" ]; then pytest "${PYTEST_ARGS[@]}" RESULT=$? diff --git a/check/pytest-changed-files b/check/pytest-changed-files index fd623377b18..9b57b1bf7bd 100755 --- a/check/pytest-changed-files +++ b/check/pytest-changed-files @@ -77,4 +77,7 @@ fi source dev_tools/pypath +: ${CIRQ_TESTING_RANDOM_SEED=$(git log -1 --pretty="%ct")} +export CIRQ_TESTING_RANDOM_SEED + pytest ${rest} "${changed[@]}" diff --git a/cirq-core/cirq/contrib/acquaintance/gates_test.py b/cirq-core/cirq/contrib/acquaintance/gates_test.py index 099aa4721db..cff4f361360 100644 --- a/cirq-core/cirq/contrib/acquaintance/gates_test.py +++ b/cirq-core/cirq/contrib/acquaintance/gates_test.py @@ -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 @@ -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)) ] diff --git a/cirq-core/cirq/ops/common_gates_test.py b/cirq-core/cirq/ops/common_gates_test.py index 2e64e0d2940..d593be664f0 100644 --- a/cirq-core/cirq/ops/common_gates_test.py +++ b/cirq-core/cirq/ops/common_gates_test.py @@ -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): diff --git a/cirq-core/cirq/protocols/json_serialization_test.py b/cirq-core/cirq/protocols/json_serialization_test.py index e102cebfdda..51244fd22c1 100644 --- a/cirq-core/cirq/protocols/json_serialization_test.py +++ b/cirq-core/cirq/protocols/json_serialization_test.py @@ -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( diff --git a/conftest.py b/conftest.py index e74e741ee54..099d4971cd1 100644 --- a/conftest.py +++ b/conftest.py @@ -12,6 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os +import random + +# allow CI execution of isolated_packages_test.py without numpy +try: + import numpy +except ImportError: + # coverage: ignore + numpy = None + def pytest_configure(config): # Ignore deprecation warnings in python code generated from our protobuf definitions. @@ -35,3 +45,10 @@ def pytest_addoption(parser): default=False, help="run Rigetti integration tests", ) + + +# skip seeding for unset or empty CIRQ_TESTING_RANDOM_SEED +if numpy is not None and os.environ.get('CIRQ_TESTING_RANDOM_SEED'): + rngseed = int(os.environ['CIRQ_TESTING_RANDOM_SEED']) + random.seed(rngseed) + numpy.random.seed(rngseed)