I'm trying to wrap my head around the expectation argument in Qiskit's
VQE class, so to understand it I'm trying to create an ExpectationBase object to understand it better, but I'm getting nowhere. Can anyone give a hand?
The VQE computes the expectation values of the input operator with respect to the parameterized ansatz circuit. There are different ways to compute this expectation value, e.g. you could
- convert operator to a matrix, the ansatz circuit to a statevector and compute the dot-product, or
- convert the operator to Pauli strings, diagonalize the ansatz circuit in each basis of the Pauli strings and measure.
expectation argument allows you to choose the kind of expectation computation you want to use.
qiskit.aqua.operators.MatrixExpectationdoes the dot product
qiskit.aqua.operators.PauliExpectationdoes the Pauli string transform (this is what you would do on real quantum hardware)
qiskit.aqua.operators.AerPauliExpectationuses a special expectation value from Qiskit's Aer element
A bit more advanced:
If you were to create your own kind of expectation computation you can check how
PauliExpectation are implemented. They require some knowledge of how the operator flow in Aqua works though. In brief, the
ExpectationValue provides a
convert method that maps an operator expression that realizes the expectation value (something like
OperatorMeasurement @ StateFn) to a new expression that implements your expectation value scheme. For
MatrixExpectation this means converting all operators to matrices and for
PauliExpectation this means converting the operator to Pauli strings and applying the right basis transformations to the ansatz circuit.