# sum¶

Graph.sum(input_tensor, axis=None, keepdims=False, *, name=None)

Sum the elements in a tensor (or a list of tensors with the same shape) along one or multiple of its axes.

Parameters
• input_tensor (np.ndarray or Tensor or list[Tensor]) – The tensor whose elements you want to sum. If you pass a list of tensors, they must all have the same shape, and are interpreted as being stacked along a new first dimension (for example, if you pass two 2D tensors of shape (3, 4), the result is equivalent to passing the stacked 3D tensor of shape (2, 3, 4)).

• axis (int or list[int] or tuple[int], optional) – The dimension or dimensions along which you want to sum the tensor. Defaults to None, in which case this node sums along all axes of the tensor.

• keepdims (bool, optional) – Whether or not to retain summed axes in the output tensor. If True, each dimension in axis has size 1 in the result; otherwise, the dimensions in axis are removed from the result. Defaults to False.

• name (str, optional) – The name of the node.

Returns

The tensor obtained by summing the input tensor along the specified axes (or, if axis was None, the tensor obtained by summing the input tensor along all of the specified axes).

Return type

Tensor

einsum()

Tensor contraction via Einstein summation convention.

Examples

>>> x = np.array([1, 2, 3])
>>> y = np.array([[1, 2, 3], [4, 5, 6]])


Sum elements of an array.

>>> graph.sum(x, 0, name="sum_a")
<Tensor: name="sum_a", operation_name="sum", shape=()>
>>> result = qctrl.functions.calculate_graph(graph=graph, output_node_names=["sum_a"])
>>> result.output["sum_a"]["value"]
6


Sum elements of a 2D array along its first dimension.

>>> graph.sum(y, 0, name="sum_b")
<Tensor: name="sum_b", operation_name="sum", shape=(3,)>
>>> result = qctrl.functions.calculate_graph(graph=graph, output_node_names=["sum_b"])
>>> result.output["sum_b"]["value"]
array([5, 7, 9])


Sum elements of a 2D array along its second dimension.

>>> graph.sum(y, 1, name="sum_c")
<Tensor: name="sum_c", operation_name="sum", shape=(2,)>
>>> result = qctrl.functions.calculate_graph(graph=graph, output_node_names=["sum_c"])
>>> result.output["sum_c"]["value"]
array([ 6, 15])


Sum elements of a 2D array along its first and second dimension.

>>> graph.sum(y, [0, 1], name="sum_d")
<Tensor: name="sum_d", operation_name="sum", shape=()>
>>> result = qctrl.functions.calculate_graph(graph=graph, output_node_names=["sum_d"])
>>> result.output["sum_d"]["value"]
21