From ab5372aa3e58fade1cc9f6f2f830a1a3f8f7ba71 Mon Sep 17 00:00:00 2001 From: Charles Yuan Date: Mon, 26 Aug 2024 15:40:17 -0700 Subject: [PATCH] Remove possible nondeterminism in block encoding tests (#1342) * Switch `LinearCombination` test to `RandomState` * Also fix `SparseMatrix` test --- .../block_encoding/linear_combination_test.py | 23 ++++----- .../block_encoding/sparse_matrix_test.py | 51 +++++++++---------- 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/qualtran/bloqs/block_encoding/linear_combination_test.py b/qualtran/bloqs/block_encoding/linear_combination_test.py index efb7d0209..903761167 100644 --- a/qualtran/bloqs/block_encoding/linear_combination_test.py +++ b/qualtran/bloqs/block_encoding/linear_combination_test.py @@ -201,21 +201,16 @@ def test_linear_combination_approx5(lambd): ) -random.seed(1234) - - -def gen_test(): - n = random.randint(3, 6) - bitsize = random.randint(1, 3) - gates = [MatrixGate.random(bitsize, random_state=1234) for _ in range(n)] - lambd = [random.uniform(-10, 10) for _ in range(n)] - return gates, lambd - - @pytest.mark.slow -@pytest.mark.parametrize('gates,lambd', [gen_test() for _ in range(10)]) -def test_linear_combination_approx_random(gates, lambd): - run_gate_test(gates, lambd, lambd_bits=9, atol=0.02) +def test_linear_combination_approx_random(): + random_state = np.random.RandomState(1234) + + for _ in range(10): + n = random_state.randint(3, 6) + bitsize = random_state.randint(1, 3) + gates = [MatrixGate.random(bitsize, random_state=random_state) for _ in range(n)] + lambd = [random.uniform(-10, 10) for _ in range(n)] + run_gate_test(gates, lambd, lambd_bits=9, atol=0.02) def test_linear_combination_signal_state(): diff --git a/qualtran/bloqs/block_encoding/sparse_matrix_test.py b/qualtran/bloqs/block_encoding/sparse_matrix_test.py index e9fd0213c..324e20a50 100644 --- a/qualtran/bloqs/block_encoding/sparse_matrix_test.py +++ b/qualtran/bloqs/block_encoding/sparse_matrix_test.py @@ -103,36 +103,33 @@ def test_sparse_matrix_tensors(): np.testing.assert_allclose(from_gate, from_tensors) -rs = np.random.RandomState(1234) - - -def gen_test(): - n = rs.randint(1, 3) - N = 2**n - data = rs.rand(N, N) - return n, data - - @pytest.mark.slow -@pytest.mark.parametrize( - "n,data", [(1, [[0.0, 0.25], [1 / 3, 0.467]])] + [gen_test() for _ in range(10)] -) -def test_explicit_entry_oracle(n, data): - row_oracle = TopLeftRowColumnOracle(n) - col_oracle = TopLeftRowColumnOracle(n) - entry_oracle = ExplicitEntryOracle(n, data=data, entry_bitsize=10) - bloq = SparseMatrix(row_oracle, col_oracle, entry_oracle, eps=0) +def test_explicit_entry_oracle(): + rs = np.random.RandomState(1234) - alpha = bloq.alpha - bb = BloqBuilder() - system = bb.add_register("system", n) - ancilla = cast(Soquet, bb.add(IntState(0, n + 1))) - system, ancilla = bb.add_t(bloq, system=system, ancilla=ancilla) - bb.add(IntEffect(0, n + 1), val=ancilla) - bloq = bb.finalize(system=system) + def gen_test(): + n = rs.randint(1, 3) + N = 2**n + data = rs.rand(N, N) + return n, data - from_tensors = bloq.tensor_contract() * alpha - np.testing.assert_allclose(data, from_tensors, atol=0.003) + tests = [(1, [[0.0, 0.25], [1 / 3, 0.467]])] + [gen_test() for _ in range(10)] + for n, data in tests: + row_oracle = TopLeftRowColumnOracle(n) + col_oracle = TopLeftRowColumnOracle(n) + entry_oracle = ExplicitEntryOracle(n, data=data, entry_bitsize=10) + bloq = SparseMatrix(row_oracle, col_oracle, entry_oracle, eps=0) + + alpha = bloq.alpha + bb = BloqBuilder() + system = bb.add_register("system", n) + ancilla = cast(Soquet, bb.add(IntState(0, n + 1))) + system, ancilla = bb.add_t(bloq, system=system, ancilla=ancilla) + bb.add(IntEffect(0, n + 1), val=ancilla) + bloq = bb.finalize(system=system) + + from_tensors = bloq.tensor_contract() * alpha + np.testing.assert_allclose(data, from_tensors, atol=0.003) topleft_matrix = [