# Optimising Angle Code in Qiskit - Using the QAOAVarForm

I’ve got a bit of an elongated plea for aid here!
I’ve got some code that finds the optimum angles for QAOA on the Maximum Independent Set Problem. It does however seem about ~10x slower than related code that solves the Maxcut Problem.
The issue I think arises from the fact I use QAOAVarForm.construct_circuit() to generate my circuit whereas the Maxcut program ‘manually’ places in the RX, RZ gates, etc. I’ve made two notebooks that compare the two approaches side by side in-depth:

(I’ve also included an image from snakeviz which shows that the problem is coming from pauli_trotter_evolution script. And for that matter the cProfile Data for this too, under the filename ‘Benchmarking_with_COBYLA’)

Here is the engine of my poorly performing code (this is certainly not all the functions required to make it work, those can be found in the notebooks!). Am I doing something incredibly inefficient? Is there a way that speeds up my code? Or would the best be to just manually create the circuit?

def quantum_operator_for_graph(Graph,model_name):
'''
Generates the quanutm object to be passed to the optimal angles function
'''
qp.from_docplex(new_docplex_generator(Graph,model_name)) # Putting in Graph
quantum_operator, offset = qp.to_ising()
return quantum_operator

def get_qaoa_circuit_sv(var_form,p, theta):
'''
Supplies the circuit from var_form to be used in the optimisation stage of the program
'''
var_qc = var_form.construct_circuit(theta)  # cost operator put in first, then after p the mixer p angles
return var_qc

def get_black_box_objective_sv(G,p,var_form,seed =10):
backend = Aer.get_backend('statevector_simulator')
def f(theta):
quantum_circuit = get_qaoa_circuit_sv(var_form,p,theta)
statevector = execute(quantum_circuit, backend, seed_simulator=seed).result().get_statevector()
# return the energy
return f

def get_optimal_angles(Graph,p, quantum_operator, initial_starting_points,seed):
'''
This performs the classical-quantum interchange, improving the values of beta and gamma by reducing the value of
< beta, gamma | -  C | beta, gamma > (Note Negative Sign). Returns the best angles found and the objective value this refers to.
Starting points for the angles are randomly distributed across the interval specified.
'''
var_form = QAOAVarForm(quantum_operator, p)
objective_function= get_black_box_objective_sv(Graph,p,var_form,seed)
optimiser_function =  minimize(objective_function, initial_starting_points, method='COBYLA', options={'maxiter':500})
best_angles = optimiser_function.x
objective_value = optimiser_function.fun
return best_angles,objective_value



Thanks for providing all your code. I have looked through it, run both notebooks, and can confirm the difference in the run times. Since you have pretty large amount of the code I have not studied everything till the last line, but I came up with a suspicion when I looked at the Ruslan's code. The profiler output just confirmed what I suspected. Yes, you are right, the problem is in QAOAVarForm.construct_circuit() and the way how a QAOA circuit is constructed there. As I recall, the implementation relies on PauliTrotterEvolution to support operators and it is slow. Since you construct a new var form on each iteration, the overall optimization procedure is slow also.
If you could use QAOA directly, as it is designed, then you could speed up your code significantly. In this case var form is created one time per run.
• Hi ! Thanks for looking through my code - how does the QAOA method then optimise angles ? – jolene Apr 8 at 22:55
• Well, I'm not sure I understood the question. QAOA is based on VQE which, in turns, has compute_minimum_eigenvalue where a parameterized quantum circuit is built one time per problem and then finally an optimizer is called. – Anton Dekusar Apr 14 at 17:24