qrisp.operators.fermionic.FermionicOperator.expectation_value#
- FermionicOperator.expectation_value(state_prep, mapping_type='jordan_wigner', **measurement_kwargs)[source]#
The
expectation value
function allows to estimate the expectation value of a Hamiltonian for a state that is specified by a preparation procedure. This preparation procedure can be supplied via a Python function that returns a QuantumVariable.Note that this method measures the hermitized version of the operator:
\[H = (O + O^\dagger)/2\]- Parameters:
- state_prepcallable
A function returning a QuantumVariable. The expectation of the Hamiltonian for the state of this QuantumVariable will be measured. The state preparation function can only take classical values as arguments. This is because a quantum value would need to be copied for each sampling iteration, which is prohibited by the no-cloning theorem.
- mapping_typestr, optional
The strategy on how to map the FermionicOperator to a QubitOperator. Default is
jordan_wigner
.- measurement_kwargsdict, optional
The keyword arguments of
QubitOperator.expectation_value
.
- Returns:
- callable
A function returning an array containing the expectaion value.
Examples
We define a Fermionic Hamiltonian, and measure its expectation value for the state of a QuantumFloat.
We prepare the state
\[\ket{\psi_{\theta}} = (\cos(\theta)\ket{0}+\sin(\theta)\ket{1})^{\otimes 2}\]from qrisp import * from qrisp.operators import a,c import numpy as np def state_prep(theta): qv = QuantumFloat(2) ry(theta,qv) return qv
And compute the expectation value of the Hamiltonion \(H=a_0^{\dagger}a_1+a_1^{\dagger}a_0\) for the state \(\ket{\psi_{\theta}}\)
H = c(0)*a(1) + c(1)*a(0) ev_function = H.expectation_value(state_prep) print(ev_function(np.pi/2)) # Yields: 0.5027499999999724
Similiarly, expectation values can be calculated with Jasp
@jaspify(terminal_sampling=True) def main(): H = c(0)*a(1) + c(1)*a(0) ev_function = H.expectation_value(state_prep) return ev_function(np.pi/2) print(main()) # Yields: 0.5027499999999724