# How to build an ExpectationBase object?

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.

The expectation argument allows you to choose the kind of expectation computation you want to use.

• qiskit.aqua.operators.MatrixExpectation does the dot product
• qiskit.aqua.operators.PauliExpectation does the Pauli string transform (this is what you would do on real quantum hardware)
• qiskit.aqua.operators.AerPauliExpectation uses a special expectation value from Qiskit's Aer element

If you were to create your own kind of expectation computation you can check how MatrixExpectation or 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.
• Ah, so if you want to do it for PauliExpectation then the hacky way would be to add your circuit as last element of ComposedOp here: github.com/Qiskit/qiskit-aqua/blob/…. The correct way would be to pass your own replacement_fn by changing that L80 in the PauliExpectation (you could add that as option in the initializer or so). – Cryoris Sep 1 '20 at 17:30