Skip to content

Commit

Permalink
Remove possible nondeterminism in block encoding tests (#1342)
Browse files Browse the repository at this point in the history
* Switch `LinearCombination` test to `RandomState`

* Also fix `SparseMatrix` test
  • Loading branch information
charlesyuan314 authored Aug 26, 2024
1 parent c0e1ca2 commit ab5372a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
23 changes: 9 additions & 14 deletions qualtran/bloqs/block_encoding/linear_combination_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
51 changes: 24 additions & 27 deletions qualtran/bloqs/block_encoding/sparse_matrix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit ab5372a

Please sign in to comment.