I want to calculate the mean expectation value of an PauliSumOp within qiskit after I executed a QAOA Circuit. My approach is the following:
# Run and get counts
job=qiskit.execute(circuit,backend=simulator,shots=shots,optimization_level=0)
result = job.result().get_counts()
# Compute average expectation value of the observable H Ising
max_count=0
value=0
for string,count in result.items():
value+=count*sum([(~StateFn(string)@ op @ StateFn(string)).eval() for op in hamiltonian])
max_count+=count
expectation=value/max_count
Is this correct? I am especially concerned if StateFn(Bitstring)
is a valid usage of qiskit in terms of StateFn("0101")
=$|0101\rangle$
hamiltonian
variable? i.e. where you writefor op in hamiltonian
, could you show how you instantiate thatPauliSumOp
object? $\endgroup$max_cut = Maxcut(Graph)
qp = max_cut.to_quadratic_program()
hamiltonian=qp.to_ising()
Returning the hamiltonian gives in my example:PauliSumOp(SparsePauliOp(['ZZII', 'IZZI', 'ZIIZ', 'IZIZ', 'IIZZ'], coeffs=[0.5+0.j, 0.5+0.j, 0.5+0.j, 0.5+0.j, 0.5+0.j]), coeff=1.0)
$\endgroup$