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

Commit 8451862

Browse files
authored
Add power function and test to backend (#846)
* Add power function and test to backend * Update power method docstring.
1 parent a27f269 commit 8451862

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

tensornetwork/backends/abstract_backend.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,3 +988,26 @@ def deserialize_tensor(self, s: str) -> Tensor:
988988
raise NotImplementedError(
989989
"Backend '{}' has not implemented deserialize_tensor.".format(
990990
self.name))
991+
992+
def power(self, a: Tensor, b: Union[Tensor, float]) -> Tensor:
993+
"""
994+
Returns the exponentiation of tensor a raised to b.
995+
If b is a tensor, then the exponentiation is element-wise
996+
between the two tensors, with a as the base and b as the power.
997+
Note that a and b must be broadcastable to the same shape if
998+
b is a tensor.
999+
If b is a scalar, then the exponentiation is each value in a
1000+
raised to the power of b.
1001+
1002+
Args:
1003+
a: The tensor containing the bases.
1004+
b: The tensor containing the powers; or a single scalar as the power.
1005+
1006+
Returns:
1007+
The tensor that is each element of a raised to the
1008+
power of b. Note that the shape of the returned tensor
1009+
is that produced by the broadcast of a and b.
1010+
"""
1011+
raise NotImplementedError(
1012+
f"Backend {self.name} has not implemented power.")
1013+

tensornetwork/backends/backend_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,8 @@ def test_pivot_not_implemented():
436436
backend = AbstractBackend()
437437
with pytest.raises(NotImplementedError):
438438
backend.pivot(np.ones((2, 2)))
439+
440+
def test_power_not_implemented():
441+
backend = AbstractBackend()
442+
with pytest.raises(NotImplementedError):
443+
backend.power(np.array([1, 2]), np.array([1, 2]))

0 commit comments

Comments
 (0)