Skip to content

Commit 46ec3fd

Browse files
authored
Fix memory overflow in sparse matrix stress test (#1529)
fix `test_matrix_stress`: reduce size to avoid tensor memory error
1 parent 7bbec3b commit 46ec3fd

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

qualtran/bloqs/block_encoding/sparse_matrix_test.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,23 +244,25 @@ def test_counts():
244244
)
245245

246246

247+
def random_banded_matrix(n: int, bandsize: int, *, rs: np.random.RandomState):
248+
f = lambda: rs.randint(0, 10) / 10
249+
data = np.zeros((2**n, 2**n))
250+
for i in range(2**n):
251+
for d in range(-bandsize, bandsize + 1):
252+
j = i + d
253+
if 0 <= j < 2**n:
254+
data[i, j] = f()
255+
return data
256+
257+
247258
@pytest.mark.slow
248-
def test_matrix_stress():
259+
@pytest.mark.parametrize(("n", "bandsize"), [(2, 1), (3, 1)])
260+
def test_matrix_stress(n: int, bandsize: int):
249261
rs = np.random.RandomState(1234)
250-
f = lambda: rs.randint(0, 10) / 10
251-
data = [
252-
[f(), f(), f(), 0.0, 0.0, 0.0, 0.0, 0.0],
253-
[f(), f(), f(), f(), 0.0, 0.0, 0.0, 0.0],
254-
[f(), f(), f(), f(), f(), 0.0, 0.0, 0.0],
255-
[0.0, f(), f(), f(), f(), f(), 0.0, 0.0],
256-
[0.0, 0.0, f(), f(), f(), f(), f(), 0.0],
257-
[0.0, 0.0, 0.0, f(), f(), f(), f(), f()],
258-
[0.0, 0.0, 0.0, 0.0, f(), f(), f(), f()],
259-
[0.0, 0.0, 0.0, 0.0, 0.0, f(), f(), f()],
260-
]
261-
n = 3
262-
row_oracle = SymmetricBandedRowColumnOracle(n, bandsize=2)
263-
col_oracle = SymmetricBandedRowColumnOracle(n, bandsize=2)
262+
data = random_banded_matrix(n, bandsize, rs=rs)
263+
264+
row_oracle = SymmetricBandedRowColumnOracle(n, bandsize=bandsize)
265+
col_oracle = SymmetricBandedRowColumnOracle(n, bandsize=bandsize)
264266
entry_oracle = ExplicitEntryOracle(system_bitsize=n, data=np.array(data), entry_bitsize=7)
265267
bloq = SparseMatrix(row_oracle, col_oracle, entry_oracle, eps=0)
266268
alpha = bloq.alpha

0 commit comments

Comments
 (0)