Following UT course on Quantum Machine Learning, they have a notebook on QAOA. As part of their lecture, they perform QAOA using Qiskit. Unfortunately, it seems the new versions of Qiskit have changed a lot since then and the code is not working anymore.
Specifically, the instructions on the optimization / evaluation procedure say the following:
We now create a function
evaluate_circuit that takes a single vector
gamma_beta (the concatenation of
beta) and returns $\langle H_c \rangle = \langle \psi | H_c | \psi \rangle$ where $\psi$ is defined by the circuit created with the function above.
In their code,
evaluate_circuit looks like this:
def evaluate_circuit(beta_gamma): n = len(beta_gamma)//2 circuit = create_circuit(beta_gamma[:n], beta_gamma[n:]) return np.real(Hc.eval("matrix", circuit, get_aer_backend('statevector_simulator')))
So basically they're calculating the expected value of $H_c$ (the hamiltonian we expect to evolve to at the end) with respect to $\psi$, which in this case is constructed using the function create_circuit (which simulates the evolution from a superposition state based on the applicacion of Hc and Hm operators with angles beta and gamma, respectively):
def create_circuit(beta, gamma): circuit_evolv = sum([evolve(Hc, beta[i], qr) + evolve(Hm, gamma[i], qr) for i in range(p)], evolve(identity, 0, qr)) circuit = circuit_init + circuit_evolv return circuit
The problem here is that $H_c$ in this case is an object of WeightedPauliOperator, and it doesn't seem to have the function eval anymore.
What's the best way to calculate $\langle \psi | H_c | \psi \rangle$ using WeightedPauliOperator objects?