qml.math.einsum

einsum(indices, *operands, like=None, optimize=None)[source]

Evaluates the Einstein summation convention on the operands.

Parameters
  • indices (str) – Specifies the subscripts for summation as comma separated list of subscript labels. An implicit (classical Einstein summation) calculation is performed unless the explicit indicator ‘->’ is included as well as subscript labels of the precise output form.

  • *operands (tuple[tensor_like]) – The tensors for the operation.

Returns

The calculation based on the Einstein summation convention.

Return type

tensor_like

Examples

>>> a = np.arange(25).reshape(5,5)
>>> b = np.arange(5)
>>> c = np.arange(6).reshape(2,3)

Trace of a matrix:

>>> qml.math.einsum('ii', a)
60

Extract the diagonal (requires explicit form):

>>> qml.math.einsum('ii->i', a)
array([ 0,  6, 12, 18, 24])

Sum over an axis (requires explicit form):

>>> qml.math.einsum('ij->i', a)
array([ 10,  35,  60,  85, 110])

Compute a matrix transpose, or reorder any number of axes:

>>> np.einsum('ij->ji', c)
array([[0, 3],
       [1, 4],
       [2, 5]])

Matrix vector multiplication:

>>> np.einsum('ij,j', a, b)
array([ 30,  80, 130, 180, 230])

Contents

Using PennyLane

Development

API

Internals