From 1f14c9c155c242910a8113ce79463b771b1817b5 Mon Sep 17 00:00:00 2001 From: Morax Date: Wed, 12 Aug 2026 06:31:29 +0200 Subject: [PATCH] Use local RNG in MultiTensor tests Give each randomized MultiTensor test a fresh, reproducible NumPy Generator instead of consuming the legacy global random state. This is an incremental step toward removing the suite-wide seed without destabilizing unrelated tests. --- .../representability/_multitensor_test.py | 83 ++++++++++--------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/src/openfermion/contrib/representability/_multitensor_test.py b/src/openfermion/contrib/representability/_multitensor_test.py index 40fb398c1..cc230664b 100644 --- a/src/openfermion/contrib/representability/_multitensor_test.py +++ b/src/openfermion/contrib/representability/_multitensor_test.py @@ -6,10 +6,15 @@ from openfermion.contrib.representability._dualbasis import DualBasis, DualBasisElement -def test_tmap(): - a = np.random.random((5, 5)) - b = np.random.random((4, 4)) - c = np.random.random((3, 3)) +@pytest.fixture(name='rng') +def rng_fixture() -> np.random.Generator: + return np.random.default_rng(0) + + +def test_tmap(rng): + a = rng.random((5, 5)) + b = rng.random((4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -23,13 +28,13 @@ def test_tmap(): assert np.allclose(iterated_tensor.data, tmp_tensors[idx]) -def test_multitensor_init(): +def test_multitensor_init(rng): """ Testing the generation of a multitensor object with random tensors """ - a = np.random.random((5, 5)) - b = np.random.random((4, 4)) - c = np.random.random((3, 3)) + a = rng.random((5, 5)) + b = rng.random((4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -43,10 +48,10 @@ def test_multitensor_init(): assert np.isclose(mt.vec_dim, 5**2 + 4**2 + 3**2) -def test_multitensor_offsetmap(): - a = np.random.random((5, 5, 5, 5)) - b = np.random.random((4, 4, 4)) - c = np.random.random((3, 3)) +def test_multitensor_offsetmap(rng): + a = rng.random((5, 5, 5, 5)) + b = rng.random((4, 4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -55,10 +60,10 @@ def test_multitensor_offsetmap(): assert mt.off_set_map == {'a': 0, 'b': 5**4, 'c': 5**4 + 4**3} -def test_vectorize_test(): - a = np.random.random((5, 5)) - b = np.random.random((4, 4)) - c = np.random.random((3, 3)) +def test_vectorize_test(rng): + a = rng.random((5, 5)) + b = rng.random((4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -67,9 +72,9 @@ def test_vectorize_test(): vec = np.vstack((vec, ct.vectorize())) assert np.allclose(vec, mt.vectorize_tensors()) - a = np.random.random((5, 5, 5, 5)) - b = np.random.random((4, 4, 4)) - c = np.random.random((3, 3)) + a = rng.random((5, 5, 5, 5)) + b = rng.random((4, 4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -79,10 +84,10 @@ def test_vectorize_test(): assert np.allclose(vec, mt.vectorize_tensors()) -def test_add_dualelement(): - a = np.random.random((5, 5, 5, 5)) - b = np.random.random((4, 4, 4)) - c = np.random.random((3, 3)) +def test_add_dualelement(rng): + a = rng.random((5, 5, 5, 5)) + b = rng.random((4, 4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -107,9 +112,9 @@ def test_add_dualelement(): assert isinstance(mt.dual_basis[1], DualBasisElement) -def test_multitensor_init_isolation(): +def test_multitensor_init_isolation(rng): # Test that different MultiTensor instances don't share the same dual_basis. - a = np.random.random((2, 2)) + a = rng.random((2, 2)) at = Tensor(tensor=a, name='a') mt1 = MultiTensor([at]) mt2 = MultiTensor([at]) @@ -122,10 +127,10 @@ def test_multitensor_init_isolation(): assert len(mt2.dual_basis) == 0 -def test_synthesis_element(): - a = np.random.random((5, 5)) - b = np.random.random((4, 4)) - c = np.random.random((3, 3)) +def test_synthesis_element(rng): + a = rng.random((5, 5)) + b = rng.random((4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -147,10 +152,10 @@ def test_synthesis_element(): assert [at.data[0, 1], at.data[1, 0]] == [at(0, 1), at(1, 0)] -def test_synthesis_dualbasis(): - a = np.random.random((5, 5)) - b = np.random.random((4, 4)) - c = np.random.random((3, 3)) +def test_synthesis_dualbasis(rng): + a = rng.random((5, 5)) + b = rng.random((4, 4)) + c = rng.random((3, 3)) at = Tensor(tensor=a, name='a') bt = Tensor(tensor=b, name='b') ct = Tensor(tensor=c, name='c') @@ -168,7 +173,7 @@ def test_synthesis_dualbasis(): assert c.shape == (1, 1) -def test_dual_basis_element(): +def test_dual_basis_element(rng): de = DualBasisElement() de_2 = DualBasisElement() db_0 = de + de_2 @@ -177,7 +182,7 @@ def test_dual_basis_element(): assert isinstance(db_1, DualBasis) dim = 2 - opdm = np.random.random((dim, dim)) + opdm = rng.random((dim, dim)) opdm = (opdm.T + opdm) / 2 opdm = Tensor(tensor=opdm, name='opdm') rdm = MultiTensor([opdm]) @@ -206,9 +211,9 @@ def generate_dual_basis_element(i, j): assert np.allclose(test_oqdm.reshape((dim, dim)), np.eye(dim) - opdm.data) -def test_cover_make_offset_dict(): - a = np.random.random((5, 5)) - b = np.random.random((4, 4)) - c = np.random.random((3, 3)) +def test_cover_make_offset_dict(rng): + a = rng.random((5, 5)) + b = rng.random((4, 4)) + c = rng.random((3, 3)) with pytest.raises(TypeError): _ = MultiTensor.make_offset_dict([a, b, c])