qml.labs.trotter_error.RealspaceCoeffs

class RealspaceCoeffs(tensor, label=None)[source]

Bases: object

Lightweight representation of a tensor of coefficients.

The RealspaceCoeffs object is initialized with an array and can be used to represent coefficients of a real space operator. A real space operator is constrcuted from position and momentum operators, e.g., Eq. 4 of arXiv:1703.09313 which represents a vibrational Hamiltonian.

Parameters:
  • tensor (ndarray) – a numpy tensor

  • label (string) – name of the tensor

Examples

>>> import numpy as np
>>> from pennylane.labs.trotter_error import RealspaceCoeffs
>>> coeffs = np.array([[1, 0], [0, 1]])
>>> rs_coeffs = RealspaceCoeffs(coeffs, label="alpha")
>>> rs_coeffs.shape
(2, 2)

The RealspaceCoeffs object allows arithmetic operations such as addition, subtraction, multiplication and matrix multiplication. Printing the resulting objects displays the expression that is used to compute each entry of the tensor.

>>> coeffs1 = RealspaceCoeffs(np.array([[1, 0], [0, 1]]), label="alpha")
>>> coeffs2 = RealspaceCoeffs(np.array([[2, 1], [1, 3]]), label="beta")
>>> expr1 = coeffs1 + 2 * coeffs2
>>> coeffs3 = RealspaceCoeffs(np.array([3, 2]), label="omega")
>>> expr2 = expr1 @ coeffs3
>>> expr2
((alpha[idx0,idx1]) + (2 * (beta[idx0,idx1]))) * (omega[idx2])

is_zero

Determine if the RealspaceCoeffs objects represents the zero tensor.

shape

Return the shape of the tensor.

is_zero

Determine if the RealspaceCoeffs objects represents the zero tensor.

Returns:

returns True when the tensor is zero, otherwise returns False

Return type:

bool

shape

Return the shape of the tensor.

nonzero([threshold])

Return the nonzero coefficients in a dictionary.

nonzero(threshold=0.0)[source]

Return the nonzero coefficients in a dictionary.

Parameters:

threshold (float) – tolerance to return coefficients with magnitude greater than threshold

Returns:

a dictionary representation of the coefficient tensor

Return type:

dict

Example

>>> from pennylane.labs.trotter_error import RealspaceCoeffs
>>> import numpy as np
>>> node = RealspaceCoeffs(np.array([[1, 0, 0, 1], [0, 0, 1, 1]]), label="alpha")
>>> node.nonzero()
{(0, 0): 1, (0, 3): 1, (1, 2): 1, (1, 3): 1}