Skip to content

Commit ab5372a

Browse files
Remove possible nondeterminism in block encoding tests (#1342)
* Switch `LinearCombination` test to `RandomState` * Also fix `SparseMatrix` test
1 parent c0e1ca2 commit ab5372a

2 files changed

Lines changed: 33 additions & 41 deletions

File tree

qualtran/bloqs/block_encoding/linear_combination_test.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,21 +201,16 @@ def test_linear_combination_approx5(lambd):
201201
)
202202

203203

204-
random.seed(1234)
205-
206-
207-
def gen_test():
208-
n = random.randint(3, 6)
209-
bitsize = random.randint(1, 3)
210-
gates = [MatrixGate.random(bitsize, random_state=1234) for _ in range(n)]
211-
lambd = [random.uniform(-10, 10) for _ in range(n)]
212-
return gates, lambd
213-
214-
215204
@pytest.mark.slow
216-
@pytest.mark.parametrize('gates,lambd', [gen_test() for _ in range(10)])
217-
def test_linear_combination_approx_random(gates, lambd):
218-
run_gate_test(gates, lambd, lambd_bits=9, atol=0.02)
205+
def test_linear_combination_approx_random():
206+
random_state = np.random.RandomState(1234)
207+
208+
for _ in range(10):
209+
n = random_state.randint(3, 6)
210+
bitsize = random_state.randint(1, 3)
211+
gates = [MatrixGate.random(bitsize, random_state=random_state) for _ in range(n)]
212+
lambd = [random.uniform(-10, 10) for _ in range(n)]
213+
run_gate_test(gates, lambd, lambd_bits=9, atol=0.02)
219214

220215

221216
def test_linear_combination_signal_state():

qualtran/bloqs/block_encoding/sparse_matrix_test.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,36 +103,33 @@ def test_sparse_matrix_tensors():
103103
np.testing.assert_allclose(from_gate, from_tensors)
104104

105105

106-
rs = np.random.RandomState(1234)
107-
108-
109-
def gen_test():
110-
n = rs.randint(1, 3)
111-
N = 2**n
112-
data = rs.rand(N, N)
113-
return n, data
114-
115-
116106
@pytest.mark.slow
117-
@pytest.mark.parametrize(
118-
"n,data", [(1, [[0.0, 0.25], [1 / 3, 0.467]])] + [gen_test() for _ in range(10)]
119-
)
120-
def test_explicit_entry_oracle(n, data):
121-
row_oracle = TopLeftRowColumnOracle(n)
122-
col_oracle = TopLeftRowColumnOracle(n)
123-
entry_oracle = ExplicitEntryOracle(n, data=data, entry_bitsize=10)
124-
bloq = SparseMatrix(row_oracle, col_oracle, entry_oracle, eps=0)
107+
def test_explicit_entry_oracle():
108+
rs = np.random.RandomState(1234)
125109

126-
alpha = bloq.alpha
127-
bb = BloqBuilder()
128-
system = bb.add_register("system", n)
129-
ancilla = cast(Soquet, bb.add(IntState(0, n + 1)))
130-
system, ancilla = bb.add_t(bloq, system=system, ancilla=ancilla)
131-
bb.add(IntEffect(0, n + 1), val=ancilla)
132-
bloq = bb.finalize(system=system)
110+
def gen_test():
111+
n = rs.randint(1, 3)
112+
N = 2**n
113+
data = rs.rand(N, N)
114+
return n, data
133115

134-
from_tensors = bloq.tensor_contract() * alpha
135-
np.testing.assert_allclose(data, from_tensors, atol=0.003)
116+
tests = [(1, [[0.0, 0.25], [1 / 3, 0.467]])] + [gen_test() for _ in range(10)]
117+
for n, data in tests:
118+
row_oracle = TopLeftRowColumnOracle(n)
119+
col_oracle = TopLeftRowColumnOracle(n)
120+
entry_oracle = ExplicitEntryOracle(n, data=data, entry_bitsize=10)
121+
bloq = SparseMatrix(row_oracle, col_oracle, entry_oracle, eps=0)
122+
123+
alpha = bloq.alpha
124+
bb = BloqBuilder()
125+
system = bb.add_register("system", n)
126+
ancilla = cast(Soquet, bb.add(IntState(0, n + 1)))
127+
system, ancilla = bb.add_t(bloq, system=system, ancilla=ancilla)
128+
bb.add(IntEffect(0, n + 1), val=ancilla)
129+
bloq = bb.finalize(system=system)
130+
131+
from_tensors = bloq.tensor_contract() * alpha
132+
np.testing.assert_allclose(data, from_tensors, atol=0.003)
136133

137134

138135
topleft_matrix = [

0 commit comments

Comments
 (0)