qml.labs.trotter_error.RealspaceSum

class RealspaceSum(modes, ops)[source]

Bases: Fragment

Represents a linear combination of RealspaceOperator objects.

The RealspaceSum class can be used to represent a Hamiltonian that is built from a sum of RealspaceOperator objects. For example, the vibrational hamiltonian, adapted from Eq. 4 of arXiv:1703.09313,

\[\sum_i \frac{\omega_i}{2} P_i^2 + \sum_i \frac{\omega_i}{2} Q_i^2 + \sum_i \phi^{(1)}_i Q_i + \sum_{i,j} \phi^{(2)}_{ij} Q_i Q_j + \dots,\]

is a sum of terms where each term can be expressed by a RealspaceOperator.

Parameters:

Example

We can build the harmonic part of a vibrational Hamiltonian, \(\sum_i \frac{\omega_i}{2} P_i^2 + \sum_i \frac{\omega_i}{2} Q_i^2\), with the following code.

>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceCoeffs, RealspaceSum
>>> import numpy as np
>>> n_modes = 2
>>> freqs = np.array([1.23, 3.45]) / 2
>>> coeffs = RealspaceCoeffs(freqs, label="omega")
>>> rs_op1 = RealspaceOperator(n_modes, ("PP",), coeffs)
>>> rs_op2 = RealspaceOperator(n_modes, ("QQ",), coeffs)
>>> RealspaceSum(n_modes, [rs_op1, rs_op2])
RealspaceSum((RealspaceOperator(2, ('PP',), omega[idx0]), RealspaceOperator(2, ('QQ',), omega[idx0])))

apply(state)

Apply the RealspaceSum to an input HOState object.

expectation(left, right)

Return the expectation value of a state.

get_coefficients([threshold])

Return a dictionary containing the non-zero coefficients of the RealspaceSum.

matrix(gridpoints[, basis, sparse])

Return a matrix representation of the RealspaceSum.

norm(params)

Returns an upper bound on the spectral norm of the operator.

zero(modes)

Returns a RealspaceOperator representing the zero operator

apply(state)[source]

Apply the RealspaceSum to an input HOState object.

expectation(left, right)

Return the expectation value of a state. The type of state is determined by each class inheriting from Fragment.

Parameters:
  • left (AbstractState) – the state to be multiplied on the left of the Fragment

  • right (AbstractState) – the state to be multiplied on the right of the Fragment

Returns:

the expectation value obtained by applying Fragment to the given states

Return type:

float

get_coefficients(threshold=0.0)[source]

Return a dictionary containing the non-zero coefficients of the RealspaceSum.

Parameters:

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

Returns:

a dictionary whose keys correspond to the RealspaceOperators in the sum, and whose

values are dictionaries obtained by get_coefficients()

Return type:

Dict

Example

>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceCoeffs, RealspaceSum
>>> import numpy as np
>>> n_modes = 2
>>> freqs = np.array([1.23, 3.45])
>>> coeffs = RealspaceCoeffs(freqs, label="omega")
>>> rs_op1 = RealspaceOperator(n_modes, ("PP",), coeffs)
>>> rs_op2 = RealspaceOperator(n_modes, ("QQ",), coeffs)
>>> RealspaceSum(n_modes, [rs_op1, rs_op2]).get_coefficients()
{('PP',): {(0,): 1.23, (1,): 3.45}, ('QQ',): {(0,): 1.23, (1,): 3.45}}
matrix(gridpoints, basis='realspace', sparse=False)[source]

Return a matrix representation of the RealspaceSum.

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

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

  • sparse (bool) – if True returns a sparse matrix, otherwise 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, RealspaceSum
>>> import numpy as np
>>> n_modes = 2
>>> freqs = np.array([1.23, 3.45])
>>> coeffs = RealspaceCoeffs(freqs, label="omega")
>>> rs_op1 = RealspaceOperator(n_modes, ("PP",), coeffs)
>>> rs_op2 = RealspaceOperator(n_modes, ("QQ",), coeffs)
>>> RealspaceSum(n_modes, [rs_op1, rs_op2]).matrix(2)
[[22.05398043+0.00000000e+00j -5.41924733+6.63666389e-16j
  -1.93207948+2.36611495e-16j  0.        +0.00000000e+00j]
 [-5.41924733-6.63666389e-16j 11.21548577+0.00000000e+00j
   0.        +0.00000000e+00j -1.93207948+2.36611495e-16j]
 [-1.93207948-2.36611495e-16j  0.        +0.00000000e+00j
  18.18982146+0.00000000e+00j -5.41924733+6.63666389e-16j]
 [ 0.        +0.00000000e+00j -1.93207948-2.36611495e-16j
  -5.41924733-6.63666389e-16j  7.35132681+0.00000000e+00j]]
norm(params)[source]

Returns an upper bound on the spectral norm of the operator.

Parameters:

params (Dict) –

The dictionary of parameters. The supported parameters are

  • gridpoints (int): the number of gridpoints used to discretize the operator

  • sparse (bool): If True, use optimizations for sparse operators. Defaults to False.

Returns:

an upper bound on the spectral norm of the operator

Return type:

float

Example

>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceCoeffs, RealspaceSum
>>> import numpy as np
>>> n_modes = 2
>>> freqs = np.array([1.23, 3.45])
>>> coeffs = RealspaceCoeffs(freqs, label="omega")
>>> rs_op1 = RealspaceOperator(n_modes, ("PP",), coeffs)
>>> rs_op2 = RealspaceOperator(n_modes, ("QQ",), coeffs)
>>> params = {"gridpoints": 2, "sparse": True}
>>> RealspaceSum(n_modes, [rs_op1, rs_op2]).norm(params)
29.405307237600457
classmethod zero(modes)[source]

Returns a RealspaceOperator representing the zero operator

Parameters:

modes (int) – the number of vibrational modes (needed for consistency with arithmetic operations)

Returns:

a representation of the zero operator

Return type:

RealspaceOperator