Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions tensornetwork/backends/base_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,32 @@ def eigsh_lanczos(self,
raise NotImplementedError(
"Backend '{}' has not implemented eighs_lanczos.".format(self.name))

def addition(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
"""
Return the default multiplication of `tensor`.
A backend can override such implementation.
Args:
tensor1: A tensor.
tensor2: A tensor.
Returns:
Tensor
"""
raise NotImplementedError(
"Backend '{}' has not implemented addition.".format(self.name))

def subtraction(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
"""
Return the default multiplication of `tensor`.
A backend can override such implementation.
Args:
tensor1: A tensor.
tensor2: A tensor.
Returns:
Tensor
"""
raise NotImplementedError(
"Backend '{}' has not implemented subtraction.".format(self.name))

def multiply(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
"""
Return the default multiplication of `tensor`.
Expand All @@ -404,6 +430,19 @@ def multiply(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
raise NotImplementedError(
"Backend '{}' has not implemented multiply.".format(self.name))

def divide(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
"""
Return the default divide of `tensor`.
A backend can override such implementation.
Args:
tensor1: A tensor.
tensor2: A tensor.
Returns:
Tensor
"""
raise NotImplementedError(
"Backend '{}' has not implemented divide.".format(self.name))

def index_update(self, tensor: Tensor, mask: Tensor,
assignee: Tensor) -> Tensor:
"""
Expand Down
9 changes: 9 additions & 0 deletions tensornetwork/backends/numpy/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,18 @@ def eigsh_lanczos(self,
eigenvectors.append(state / self.np.linalg.norm(state))
return eigvals[0:numeig], eigenvectors

def addition(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 + tensor2

def subtraction(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 - tensor2

def multiply(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 * tensor2

def divide(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 / tensor2

def index_update(self, tensor: Tensor, mask: Tensor,
assignee: Tensor) -> Tensor:
t = self.np.copy(tensor)
Expand Down
56 changes: 54 additions & 2 deletions tensornetwork/backends/numpy/numpy_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,67 @@ def test_eigsh_lanczos_raises():


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 2),
pytest.param(1., np.ones((1, 2, 3)), 2*np.ones((1, 2, 3))),
pytest.param(2.*np.ones(()), 1., 3.*np.ones((1, 2, 3))),
pytest.param(2.*np.ones(()), 1.*np.ones((1, 2, 3)), 3.*np.ones((1, 2, 3))),
])
def test_addition(a, b, expected):
backend = numpy_backend.NumPyBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.addition(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 0),
pytest.param(2., 1.*np.ones((1, 2, 3)), 1.*np.ones((1, 2, 3))),
pytest.param(np.ones((1, 2, 3)), 1., np.zeros((1, 2, 3))),
pytest.param(np.ones((1, 2, 3)), np.ones((1, 2, 3)), np.zeros((1, 2, 3))),
])
def test_subtraction(a, b, expected):
backend = numpy_backend.NumPyBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.subtraction(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 1),
pytest.param(2., 1.*np.ones((1, 2, 3)), 2.*np.ones((1, 2, 3))),
pytest.param(np.ones((1, 2, 3)), 1., np.ones((1, 2, 3))),
pytest.param(np.ones((1, 2, 3)), np.ones((1, 2, 3)), np.ones((1, 2, 3))),
pytest.param(2. * np.ones(()), np.ones((1, 2, 3)), 2. * np.ones((1, 2, 3))),
])
def test_multiply(a, b, expected):
backend = numpy_backend.NumPyBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.multiply(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(2., 2., 1.),
pytest.param(2., 0.5*np.ones((1, 2, 3)), 4.*np.ones((1, 2, 3))),
pytest.param(np.ones(()), 2., 0.5*np.ones((1, 2, 3))),
pytest.param(np.ones(()), 2.*np.ones((1, 2, 3)), 0.5*np.ones((1, 2, 3))),
])
def test_divide(a, b, expected):
backend = numpy_backend.NumPyBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.divide(tensor1, tensor2)

np.testing.assert_allclose(backend.multiply(tensor1, tensor2), expected)
np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


def find(which, vector):
Expand Down
9 changes: 9 additions & 0 deletions tensornetwork/backends/pytorch/pytorch_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,18 @@ def eigsh_lanczos(self,
eigenvectors.append(state / self.torch.norm(state))
return eigvals[0:numeig], eigenvectors

def addition(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 + tensor2

def subtraction(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 - tensor2

def multiply(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 * tensor2

def divide(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 / tensor2

def index_update(self, tensor: Tensor, mask: Tensor,
assignee: Tensor) -> Tensor:
#make a copy
Expand Down
48 changes: 46 additions & 2 deletions tensornetwork/backends/pytorch/pytorch_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,59 @@ def test_eigsh_lanczos_raises():


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 2),
pytest.param(np.ones((1, 2, 3)), np.ones((1, 2, 3)), 2.*np.ones((1, 2, 3))),
])
def test_addition(a, b, expected):
backend = pytorch_backend.PyTorchBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.addition(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 0),
pytest.param(np.ones((1, 2, 3)), np.ones((1, 2, 3)), np.zeros((1, 2, 3))),
])
def test_subtraction(a, b, expected):
backend = pytorch_backend.PyTorchBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.subtraction(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 1),
pytest.param(np.ones((1, 2, 3)), np.ones((1, 2, 3)), np.ones((1, 2, 3))),
pytest.param(2. * np.ones(()), np.ones((1, 2, 3)), 2. * np.ones((1, 2, 3))),
])
def test_multiply(a, b, expected):
backend = pytorch_backend.PyTorchBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.multiply(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(2., 2., 1.),
pytest.param(np.ones(()), 2.*np.ones((1, 2, 3)), 0.5*np.ones((1, 2, 3))),
])
def test_divide(a, b, expected):
backend = pytorch_backend.PyTorchBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.divide(tensor1, tensor2)

np.testing.assert_allclose(backend.multiply(tensor1, tensor2), expected)
np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


def test_eigh():
Expand Down
9 changes: 9 additions & 0 deletions tensornetwork/backends/shell/shell_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,20 @@ def eigsh_lanczos(self,
raise ValueError(
'`A` has no attribut shape adn no `initial_state` is given.')

def addition(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
raise NotImplementedError("Shell tensor has not implemented addition( + )")

def subtraction(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
raise NotImplementedError("Shell tensor has not implemented subtraction( - )")

def multiply(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
a = np.ones(tensor1.shape)
b = np.ones(tensor2.shape)
return ShellTensor((a * b).shape)

def divide(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
raise NotImplementedError("Shell tensor has not implemented add( / )")

def index_update(self, tensor: Tensor, mask: Tensor,
assignee: Tensor) -> Tensor:
return ShellTensor(tensor.shape)
Expand Down
9 changes: 9 additions & 0 deletions tensornetwork/backends/tensorflow/tensorflow_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,18 @@ def eigsh_lanczos(self,
raise NotImplementedError(
"Backend '{}' has not implemented eighs_lanczos.".format(self.name))

def addition(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 + tensor2

def subtraction(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 - tensor2

def multiply(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 * tensor2

def divide(self, tensor1: Tensor, tensor2: Tensor) -> Tensor:
return tensor1 / tensor2

def index_update(self, tensor: Tensor, mask: Tensor,
assignee: Tensor) -> Tensor:
#returns a copy (unfortunately)
Expand Down
48 changes: 46 additions & 2 deletions tensornetwork/backends/tensorflow/tensorflow_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,59 @@ def test_conj():


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 2),
pytest.param(2.*np.ones(()), 1.*np.ones((1, 2, 3)), 3.*np.ones((1, 2, 3))),
])
def test_addition(a, b, expected):
backend = tensorflow_backend.TensorFlowBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.addition(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 0),
pytest.param(np.ones((1, 2, 3)), np.ones((1, 2, 3)), np.zeros((1, 2, 3))),
])
def test_subtraction(a, b, expected):
backend = tensorflow_backend.TensorFlowBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.subtraction(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(1, 1, 1),
pytest.param(np.ones((1, 2, 3)), np.ones((1, 2, 3)), np.ones((1, 2, 3))),
pytest.param(2. * np.ones(()), np.ones((1, 2, 3)), 2. * np.ones((1, 2, 3))),
])
def test_multiply(a, b, expected):
backend = tensorflow_backend.TensorFlowBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.multiply(tensor1, tensor2)

np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("a, b, expected", [
pytest.param(2., 2., 1.),
pytest.param(np.ones(()), 2.*np.ones((1, 2, 3)), 0.5*np.ones((1, 2, 3))),
])
def test_divide(a, b, expected):
backend = tensorflow_backend.TensorFlowBackend()
tensor1 = backend.convert_to_tensor(a)
tensor2 = backend.convert_to_tensor(b)
result = backend.divide(tensor1, tensor2)

np.testing.assert_allclose(backend.multiply(tensor1, tensor2), expected)
np.testing.assert_allclose(result, expected)
assert tensor1.dtype == tensor2.dtype == result.dtype


@pytest.mark.parametrize("dtype", [tf.float64, tf.complex128])
Expand Down
Loading