qml.labs.trotter_error.perturbation_error

perturbation_error(product_formula, fragments, states, order, timestep=1.0, num_workers=1, backend='serial', parallel_mode='state')[source]

Computes the perturbation theory error using the effective Hamiltonian \(\hat{\epsilon} = \hat{H}_{eff} - \hat{H}\) for a given product formula.

For a state \(\left| \psi \right\rangle\) the perturbation theory error is given by the expectation value \(\left\langle \psi \right| \hat{\epsilon} \left| \psi \right\rangle\).

Parameters:
  • product_formula (ProductFormula) – the ProductFormula used to obtain the effective Hamiltonian

  • fragments (Sequence[Fragments]) – the set of Fragment objects to compute the perturbation error from

  • states (Sequence[AbstractState]) – the states to compute expectation values from

  • delta (float) – time step for the trotter error operator.

  • num_workers (int) – the number of concurrent units used for the computation. Default value is set to 1.

  • backend (string) – the executor backend from the list of supported backends. Available options : “mp_pool”, “cf_procpool”, “cf_threadpool”, “serial”, “mpi4py_pool”, “mpi4py_comm”. Default value is set to “serial”.

  • parallel_mode (str) – the mode of parallelization to use. Options are “state” or “commutator”. “state” parallelizes the computation of expectation values per state, while “commutator” parallelizes the application of commutators to each state. Default value is set to “state”.

Returns:

the list of expectation values computed from the Trotter error operator and the input states

Return type:

List[float]

Example

>>> import numpy as np
>>> from pennylane.labs.trotter_error import HOState, ProductFormula, vibrational_fragments, perturbation_error
>>>
>>> frag_labels = [0, 1, 1, 0]
>>> frag_coeffs = [1/2, 1/2, 1/2, 1/2]
>>> pf = ProductFormula(frag_labels, coeffs=frag_coeffs)
>>>
>>> n_modes = 2
>>> r_state = np.random.RandomState(42)
>>> freqs = r_state.random(n_modes)
>>> taylor_coeffs = [
>>>     np.array(0),
>>>     r_state.random(size=(n_modes, )),
>>>     r_state.random(size=(n_modes, n_modes)),
>>>     r_state.random(size=(n_modes, n_modes, n_modes))
>>> ]
>>> frags = dict(enumerate(vibrational_fragments(n_modes, freqs, taylor_coeffs)))
>>>
>>> gridpoints = 5
>>> state1 = HOState(n_modes, gridpoints, {(0, 0): 1})
>>> state2 = HOState(n_modes, gridpoints, {(1, 1): 1})
>>>
>>> errors = perturbation_error(pf, frags, [state1, state2], order=3)
>>> print(errors)
[0.9189251160920877j, 4.797716682426847j]
>>>
>>> errors = perturbation_error(pf, frags, [state1, state2], order=3, num_workers=4, backend="mp_pool", parallel_mode="commutator")
>>> print(errors)
[0.9189251160920877j, 4.797716682426847j]