I'm trying to learn QAOA and how to apply it to a complex combinatorial problem. But for the purpose of this question I'll use the common example MaxCut.
I'd like to know if I've set up my cost Hamiltonian correctly. To check that, would I would do for MaxCut, is set up the circuit using only the cost Hamiltonian, and then set the initial states to of all qubits such that they're in the ground state. (In the following example of MaxCut, the ground state is 00011
, as you'll see from the graph G
).
Then, I would expect that all of the counts outputted by running the state simulator would correspond to the ground state given as input. However, when I set the initial state as 00000
, running the following code ALSO returns all counts in the state given as input. So, I'm guessing this is not the proper way to debug my cost Hamiltonian. The code to reproduce this behavior is below:
G = nx.Graph()
G.add_edges_from([[0,3],[0,4],[1,3],[1,4],[2,3],[2,4]])
def append_zz_term(qc, q1, q2, gamma):
qc.cx(q1, q2)
qc.rz(2*gamma, q2)
qc.cx(q1, q2)
def get_cost_operator_circuit(G, gamma):
N = G.number_of_nodes()
qc = QuantumCircuit(N, N)
for i, j in G.edges():
qc.barrier()
append_zz_term(qc, i, j, gamma)
return qc
def invert_counts(counts):
return {k[::-1]:v for k, v in counts.items()}
N = G.number_of_nodes()
qc = QuantumCircuit(N,N)
# Removing the following line ALSO returns all counts in the initial state 00000, which is NOT the ground state
qc.x([3,4])
qc = qc.compose(get_cost_operator_circuit(G, np.pi/3))
qc.barrier(range(N))
qc.measure(range(N), range(N))
qc.draw()
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1000)
result = job.result()
print(invert_counts(result.get_counts()))
This tells me that this isn't the right way to debug my cost Hamiltonian. What would be the proper way to check if the cost Hamiltonian represents the objective function faithfully?