Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions src/openfermion/contrib/representability/_multitensor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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])
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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])
Expand Down Expand Up @@ -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])
Loading