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 productqiskit.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
A bit more advanced:
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.
-
$\begingroup$ That's exactly what I want with the PauliExpectation: I want to have a custom circuit to act after the ansatz and then calculate the expectation, but the inner workings of the operator flow in aqua mess me up. Can you do a trivial example that I can follow after? $\endgroup$ – Bidon Sep 1 '20 at 16:09
-
$\begingroup$ Why not just add the circuit to the input ansatz? $\endgroup$ – Cryoris Sep 1 '20 at 17:15
-
$\begingroup$ Because then all the pauliexpectation circuits would apply to that ansatz, and I want to add that new circuit to the final rotation layer to make measurements $\endgroup$ – Bidon Sep 1 '20 at 17:19
-
$\begingroup$ 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 ownreplacement_fn
by changing that L80 in the PauliExpectation (you could add that as option in the initializer or so). $\endgroup$ – Cryoris Sep 1 '20 at 17:30