qml.labs.trotter_error.RealspaceOperator

class RealspaceOperator(modes, ops, coeffs)[source]

Bases: object

Represents a linear combination of a product of position and momentum operators. The RealspaceOperator class can be used to represent components of a vibrational Hamiltonian, e.g., the following sum over a product of two position operators \(Q\):

\[\sum_{i,j=1}^n \phi_{i,j}Q_i Q_j,\]

where \(\phi_{i, j}\) represents the coefficient and is a constant.

Parameters:
  • modes (int) – the number of vibrational modes

  • ops (Sequence[str]) – a sequence representation of the position and momentum operators

  • coeffs (RealspaceCoeffs) – an expression tree which evaluates the entries of the coefficient tensor

Example

This example uses RealspaceOperator to build the operator \(\sum_{i,j=1}^2 \phi_{i,j}Q_i Q_j\). The operator represents a sum over 2 modes for the position operators \(Q_iQ_j\).

>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceCoeffs
>>> import numpy as np
>>> n_modes = 2
>>> ops = ("Q", "Q")
>>> coeffs = RealspaceCoeffs(np.array([[1, 0], [0, 1]]), label="phi")
>>> RealspaceOperator(n_modes, ops, coeffs)
RealspaceOperator(5, ('Q', 'Q'), phi[idx0,idx1])

get_coefficients([threshold])

Return the non-zero coefficients in a dictionary.

matrix(gridpoints[, basis, sparse])

Return a matrix representation of the operator.

zero(modes)

Returns a RealspaceOperator representing the zero operator.

get_coefficients(threshold=0.0)[source]

Return the non-zero coefficients in a dictionary.

Parameters:

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

Returns:

a dictionary whose keys are the nonzero indices, and values are the coefficients

Return type:

Dict[Tuple[int], float]

Example

>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceCoeffs
>>> import numpy as np
>>> n_modes = 2
>>> ops = ("Q", "Q")
>>> coeffs = RealspaceCoeffs(np.array([[1, 0], [0, 1]]), label="phi")
>>> RealspaceOperator(n_modes, ops, coeffs).get_coefficients()
{(0, 0): 1, (1, 1): 1}
matrix(gridpoints, basis='realspace', sparse=False)[source]

Return a matrix representation of the operator.

Parameters:
  • gridpoints (int) – the number of gridpoints used to discretize the position or momentum operators

  • basis (str) – the basis of the matrix, available options are realspace and harmonic

  • sparse (bool) – if True returns a sparse matrix, otherwise returns a dense matrix

Returns:

the matrix representation of the RealspaceOperator

Return type:

Union[ndarray, scipy.sparse.csr_array]

Example

>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceCoeffs
>>> import numpy as np
>>> n_modes = 2
>>> ops = ("Q", "Q")
>>> coeffs = RealspaceCoeffs(np.array([[1, 0], [0, 1]]), label="phi")
>>> RealspaceOperator(n_modes, ops, coeffs).matrix(2)
[[6.28318531 0.         0.         0.        ]
 [0.         3.14159265 0.         0.        ]
 [0.         0.         3.14159265 0.        ]
 [0.         0.         0.         0.        ]]
classmethod zero(modes)[source]

Returns a RealspaceOperator representing the zero operator.

Parameters:

modes (int) – the number of vibrational modes

Returns:

a representation of the zero operator

Return type:

RealspaceOperator