Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit 80dbe5a

Browse files
authored
Symmetric matmul (#875)
* test * add test for matmul * add matmul * test added * linting
1 parent 23a96b3 commit 80dbe5a

2 files changed

Lines changed: 68 additions & 3 deletions

File tree

tensornetwork/backends/symmetric/symmetric_backend.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,7 @@ def sign(self, tensor: Tensor) -> Tensor:
680680

681681
def pivot(self, tensor: Tensor, pivot_axis: int = -1) -> Tensor:
682682
raise NotImplementedError("Symmetric backend doesn't support pivot.")
683+
def matmul(self, tensor1: Tensor, tensor2: Tensor):
684+
if (tensor1.ndim != 2) or (tensor2.ndim != 2):
685+
raise ValueError("inputs to `matmul` have to be matrices")
686+
return tensor1 @ tensor2

tensornetwork/backends/symmetric/symmetric_backend_test.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,15 +1543,15 @@ def test_sign():
15431543
tensor = get_tensor(R=4, num_charges=1, dtype=np.float64)
15441544
backend = symmetric_backend.SymmetricBackend()
15451545
res = backend.sign(tensor)
1546-
np.testing.assert_allclose(res.data, np.sign(tensor.data))
1546+
np.testing.assert_allclose(res.data, np.sign(tensor.data))
15471547

15481548
def test_abs():
15491549
tensor = get_tensor(R=4, num_charges=1, dtype=np.float64)
15501550
backend = symmetric_backend.SymmetricBackend()
15511551
res = backend.abs(tensor)
1552-
np.testing.assert_allclose(res.data, np.abs(tensor.data))
1552+
np.testing.assert_allclose(res.data, np.abs(tensor.data))
1553+
15531554

1554-
15551555
@pytest.mark.parametrize('dtype', [np.float64, np.complex128])
15561556
@pytest.mark.parametrize('x0', [True, False])
15571557
@pytest.mark.parametrize('ncv', [None, 40])
@@ -1644,3 +1644,64 @@ def test_gmres_raises():
16441644
backend.gmres(lambda x: x, b, x0=mps, tol=-0.001)
16451645
with pytest.raises(ValueError, match="atol = "):
16461646
backend.gmres(lambda x: x, b, x0=mps, atol=-0.001)
1647+
1648+
1649+
1650+
@pytest.mark.parametrize("dtype", np_tensordot_dtypes)
1651+
@pytest.mark.parametrize("num_charges", [1, 2])
1652+
def test_matmul(dtype, num_charges):
1653+
np.random.seed(10)
1654+
backend = symmetric_backend.SymmetricBackend()
1655+
D = 100
1656+
c1 = BaseCharge(
1657+
np.random.randint(-5, 6, (D, num_charges)),
1658+
charge_types=[U1Charge] * num_charges)
1659+
c2 = BaseCharge(
1660+
np.random.randint(-5, 6, (D, num_charges)),
1661+
charge_types=[U1Charge] * num_charges)
1662+
c3 = BaseCharge(
1663+
np.random.randint(-5, 6, (D, num_charges)),
1664+
charge_types=[U1Charge] * num_charges)
1665+
charges1 = [c1, c2]
1666+
charges2 = [c2, c3]
1667+
flows1 = [False, True]
1668+
flows2 = [False, True]
1669+
inds1 = [Index(charges1[n], flows1[n]) for n in range(2)]
1670+
inds2 = [Index(charges2[n], flows2[n]) for n in range(2)]
1671+
A = BlockSparseTensor.random(indices=inds1, dtype=dtype)
1672+
B = BlockSparseTensor.random(indices=inds2, dtype=dtype)
1673+
1674+
actual = backend.matmul(A, B)
1675+
expected = A @ B
1676+
np.testing.assert_allclose(expected.data, actual.data)
1677+
assert np.all([
1678+
charge_equal(expected._charges[n], actual._charges[n])
1679+
for n in range(len(actual._charges))
1680+
])
1681+
1682+
1683+
def test_matmul_raises():
1684+
dtype = np.float64
1685+
num_charges = 1
1686+
np.random.seed(10)
1687+
backend = symmetric_backend.SymmetricBackend()
1688+
D = 100
1689+
c1 = BaseCharge(
1690+
np.random.randint(-5, 6, (D, num_charges)),
1691+
charge_types=[U1Charge] * num_charges)
1692+
c2 = BaseCharge(
1693+
np.random.randint(-5, 6, (D, num_charges)),
1694+
charge_types=[U1Charge] * num_charges)
1695+
c3 = BaseCharge(
1696+
np.random.randint(-5, 6, (D, num_charges)),
1697+
charge_types=[U1Charge] * num_charges)
1698+
charges1 = [c1, c2, c3]
1699+
charges2 = [c2, c3]
1700+
flows1 = [False, True, False]
1701+
flows2 = [False, True]
1702+
inds1 = [Index(charges1[n], flows1[n]) for n in range(3)]
1703+
inds2 = [Index(charges2[n], flows2[n]) for n in range(2)]
1704+
A = BlockSparseTensor.random(indices=inds1, dtype=dtype)
1705+
B = BlockSparseTensor.random(indices=inds2, dtype=dtype)
1706+
with pytest.raises(ValueError, match="inputs to"):
1707+
_ = backend.matmul(A, B)

0 commit comments

Comments
 (0)