qml.labs.trotter_error.RealspaceMatrix¶
- class RealspaceMatrix(states, modes, blocks=None)[source]¶
Bases:
FragmentImplements a dictionary of
RealspaceSumobjects.This can be used to represent the fragments of a vibronic Hamiltonian given by, Eq. 3 of arXiv:2411.13669,
\[V_{i,j} = \lambda_{i,j} + \sum_{r} \phi^{(1)}_{i,j,r} Q_r + \sum_{r,s} \phi^{(2)}_{i,j,r,s} Q_r Q_s + \sum_{r,s,t} \phi^{(3)}_{i,j,r,s,t} Q_r Q_s Q_t + \dots,\]where the dictionary is indexed by tuples \((i, j)\) and the values are
RealspaceSumobjects representing the operator \(V_{i,j}\).- Parameters:
states (int) – the number of electronic states
modes (int) – the number of vibrational modes
blocks (Dict[Tuple[int, int], RealspaceSum) – a dictionary representation of the block matrix
Example
>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceSum, RealspaceCoeffs, RealspaceMatrix >>> import numpy as np >>> n_states = 1 >>> n_modes = 5 >>> op1 = RealspaceOperator(n_modes, (), RealspaceCoeffs(np.array(1))) >>> op2 = RealspaceOperator(n_modes, ("Q"), RealspaceCoeffs(np.array([1, 2, 3, 4, 5]), label="phi")) >>> rs_sum = RealspaceSum(n_modes, [op1, op2]) >>> RealspaceMatrix(n_states, n_modes, {(0, 0): rs_sum}) RealspaceMatrix({(0, 0): RealspaceSum((RealspaceOperator(5, (), 1), RealspaceOperator(5, 'Q', phi[idx0])))})
Methods
apply(state)Apply the
RealspaceMatrixto an inputVibronicHOon the right.block(row, col)Return the
RealspaceSumobject located at the(row, col)entry of theRealspaceMatrix.expectation(left, right)Return the expectation value of a state.
get_coefficients([threshold])Return a dictionary containing the coefficients of the
RealspaceSummatrix(gridpoints[, sparse, basis])Return a matrix representation of the operator.
norm(params)Returns an upper bound on the spectral norm of the operator.
set_block(row, col, rs_sum)Set the value of the block indexed at
(row, col).zero(states, modes)Return a
RealspaceMatrixrepresentation of the zero operator.- apply(state)[source]¶
Apply the
RealspaceMatrixto an inputVibronicHOon the right.- Parameters:
state (VibronicHO) – a vibronic wavefunction
- Returns:
the result of applying the
RealspaceMatrixtostate- Return type:
Example
>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceSum, RealspaceCoeffs, RealspaceMatrix >>> from pennylane.labs.trotter_error import HOState, VibronicHO >>> import numpy as np >>> n_states = 1 >>> n_modes = 3 >>> gridpoints = 2 >>> op1 = RealspaceOperator(n_modes, (), RealspaceCoeffs.coeffs(np.array(1), label="lambda")) >>> op2 = RealspaceOperator(n_modes, ("Q"), RealspaceCoeffs.coeffs(np.array([1, 2, 3, 4, 5]), label="phi")) >>> rs_sum = RealspaceSum(n_modes, [op1, op2]) >>> vib_matrix = RealspaceMatrix(n_states, n_modes, {(0, 0): rs_sum}) >>> state_dict = {(1, 0, 0): 1, (0, 1, 1): 1} >>> state = HOState.from_dict(n_modes, gridpoints, state_dict) >>> VibronicHO(n_states, n_modes, gridpoints, [state]) VibronicHO([HOState(modes=3, gridpoints=2, <Compressed Sparse Row sparse array of dtype 'int64' with 2 stored elements and shape (8, 1)> Coords Values (3, 0) 1 (4, 0) 1)])
- block(row, col)[source]¶
Return the
RealspaceSumobject located at the(row, col)entry of theRealspaceMatrix.- Parameters:
row (int) – the row of the index
col (int) – the column of the index
- Returns:
- the
RealspaceSumobject indexed at
(row, col)
- the
- Return type:
Example
>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceSum, RealspaceCoeffs, RealspaceMatrix >>> import numpy as np >>> n_states = 1 >>> n_modes = 5 >>> op1 = RealspaceOperator(n_modes, (), RealspaceCoeffs(np.array(1))) >>> op2 = RealspaceOperator(n_modes, ("Q"), RealspaceCoeffs(np.array([1, 2, 3, 4, 5]), label="phi")) >>> rs_sum = RealspaceSum(n_modes, [op1, op2]) >>> RealspaceMatrix(n_states, n_modes, {(0, 0): rs_sum}).block(0, 0) RealspaceSum((RealspaceOperator(5, (), 1), RealspaceOperator(5, 'Q', phi[idx0])))
- expectation(left, right)¶
Return the expectation value of a state. The type of
stateis determined by each class inheriting fromFragment.- Parameters:
left (AbstractState) – the state to be multiplied on the left of the
Fragmentright (AbstractState) – the state to be multiplied on the right of the
Fragment
- Returns:
the expectation value obtained by applying
Fragmentto the given states- Return type:
float
- get_coefficients(threshold=0.0)[source]¶
Return a dictionary containing the coefficients of the
RealspaceSum- Parameters:
threshold (float) – tolerance to return coefficients whose magnitude is greater than
threshold- Returns:
a dictionary whose keys are the indices of the
RealspaceMatrixand whose values are dictionaries obtained byget_coefficients()- Return type:
Dict
Example
>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceSum, RealspaceCoeffs, RealspaceMatrix >>> import numpy as np >>> n_states = 1 >>> n_modes = 5 >>> op1 = RealspaceOperator(n_modes, ("Q"), RealspaceCoeffs(np.array([1, 2, 3, 4, 5]), label="phi")) >>> op2 = RealspaceOperator(n_modes, ("P"), RealspaceCoeffs(np.array([1, 2, 3, 4, 5]), label="chi")) >>> rs_sum = RealspaceSum(n_modes, [op1, op2]) >>> RealspaceMatrix(n_states, n_modes, {(0, 0): rs_sum}).get_coefficients() {(0, 0): {'Q': {(0,): 1.0, (1,): 2.0, (2,): 3.0, (3,): 4.0, (4,): 5.0}, 'P': {(0,): 1.0, (1,): 2.0, (2,): 3.0, (3,): 4.0, (4,): 5.0}}}
- matrix(gridpoints, sparse=False, basis='realspace')[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
realspaceandharmonicsparse (bool) – if
Truereturns 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, RealspaceSum, RealspaceCoeffs, RealspaceMatrix >>> import numpy as np >>> n_states = 1 >>> n_modes = 5 >>> op1 = RealspaceOperator(n_modes, (), RealspaceCoeffs(np.array(1))) >>> op2 = RealspaceOperator(n_modes, ("Q"), RealspaceCoeffs(np.array([1, 2, 3, 4, 5]), label="phi")) >>> rs_sum = RealspaceSum(n_modes, [op1, op2]) >>> RealspaceMatrix(n_states, n_modes, {(0, 0): rs_sum}).matrix(2) [[-25.58680776 0. 0. ... 0. 0. 0. ] [ 0. -16.72453851 0. ... 0. 0. 0. ] [ 0. 0. -18.49699236 ... 0. 0. 0. ] ... [ 0. 0. 0. ... -6.0898154 0. 0. ] [ 0. 0. 0. ... 0. -7.86226925 0. ] [ 0. 0. 0. ... 0. 0. 1. ]]
- norm(params)[source]¶
Returns an upper bound on the spectral norm of the operator.
- Parameters:
params (dict[str, Union[int, bool]]) –
The dictionary of parameters. The supported parameters are
gridpoints(int): the number of gridpoints used to discretize the operatorsparse(bool): IfTrue, use optimizations for sparse operators. Defaults toFalse.
- Returns:
an upper bound on the spectral norm of the operator
- Return type:
float
Example
>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceSum, RealspaceCoeffs, RealspaceMatrix >>> import numpy as np >>> n_states = 1 >>> n_modes = 5 >>> op1 = RealspaceOperator(n_modes, (), RealspaceCoeffs(np.array(1))) >>> op2 = RealspaceOperator(n_modes, ("Q"), RealspaceCoeffs(np.array([1, 2, 3, 4, 5]), label="phi")) >>> rs_sum = RealspaceSum(n_modes, [op1, op2]) >>> params = {"gridpoints": 2, "sparse": True} >>> RealspaceMatrix(n_states, n_modes, {(0, 0): rs_sum}).norm(params) 27.586807763582737
- set_block(row, col, rs_sum)[source]¶
Set the value of the block indexed at
(row, col).- Parameters:
row (int) – the row of the index
col (int) – the column of the index
rs_sum (RealspaceSum) – the
RealspaceSumobject to stored in index(row, col)
- Returns:
None
>>> from pennylane.labs.trotter_error import RealspaceOperator, RealspaceSum, RealspaceCoeffs, RealspaceMatrix >>> import numpy as np >>> n_states = 2 >>> n_modes = 5 >>> op1 = RealspaceOperator(n_modes, (), RealspaceCoeffs(np.array(1))) >>> op2 = RealspaceOperator(n_modes, ("Q"), RealspaceCoeffs(np.array([1, 2, 3, 4, 5]), label="phi")) >>> rs_sum = RealspaceSum(n_modes, [op1, op2]) >>> vib = RealspaceMatrix(n_states, n_modes, {(0, 0): rs_sum}) >>> vib RealspaceMatrix({(0, 0): RealspaceSum((RealspaceOperator(5, (), 1), RealspaceOperator(5, 'Q', phi[idx0])))}) >>> vib.set_block(1, 1, rs_sum) >>> vib RealspaceMatrix({(0, 0): RealspaceSum((RealspaceOperator(5, (), 1), RealspaceOperator(5, 'Q', phi[idx0]))), (1, 1): RealspaceSum((RealspaceOperator(5, (), 1), RealspaceOperator(5, 'Q', phi[idx0])))})
- classmethod zero(states, modes)[source]¶
Return a
RealspaceMatrixrepresentation of the zero operator.- Parameters:
states (int) – the number of electronic states
modes (int) – the number of vibrational modes
- Returns:
a
RealspaceMatrixonstateselectronic states andmodesvibrational modes such that all coefficients are zero- Return type: