qml.math.scatter_element_add¶
- scatter_element_add(tensor, index, value, like=None, *, indices_are_sorted=False, unique_indices=False)[source]¶
In-place addition of a multidimensional value over various indices of a tensor.
- Parameters:
tensor (tensor_like[float]) – Tensor to add the value to
index (tuple or list[tuple]) – Indices to which to add the value
value (float or tensor_like[float]) – Value to add to
tensor
like (str) – Manually chosen interface to dispatch to.
- Keyword Arguments:
indices_are_sorted=False (bool) – If
True
, jax will assume that the indices are in ascending order. Required to beTrue
with catalyst.unique_indices=False (bool) – If
True
, jax will assume each index is unique. Required to beTrue
with catalyst.
- Returns:
The tensor with the value added at the given indices.
- Return type:
tensor_like[float]
Example
>>> tensor = torch.tensor([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]) >>> index = (1, 2) >>> value = -3.1 >>> qml.math.scatter_element_add(tensor, index, value) tensor([[ 0.1000, 0.2000, 0.3000], [ 0.4000, 0.5000, -2.5000]])
If multiple indices are given, in the form of a list of tuples, the
k
th tuple is interpreted to contain thek
th entry of all indices:>>> indices = [(1, 0), (2, 1)] # This will modify the entries (1, 2) and (0, 1) >>> values = torch.tensor([10, 20]) >>> qml.math.scatter_element_add(tensor, indices, values) tensor([[ 0.1000, 20.2000, 0.3000], [ 0.4000, 0.5000, 10.6000]])