How to get angles optimized by classical optimizer from QisKit's QAOA Module?

I have the following simple optimization in QAOA:

from qiskit_optimization.algorithms import MinimumEigenOptimizer

# from qiskit_aer import Aer
from qiskit.algorithms.minimum_eigensolvers import QAOA
from qiskit.algorithms.optimizers import COBYLA
from qiskit.primitives import Sampler

n_qubits = len(G.nodes())
_ = [problem.binary_var("x{}".format(i)) for i in range(n_qubits)]
problem.maximize(
)

meo = MinimumEigenOptimizer(QAOA(sampler=Sampler(), optimizer=COBYLA(maxiter=100)))
result = meo.solve(problem)
print(result.prettyprint())
print("\ndisplay the best 5 solution samples")
for sample in result.samples[:5]:
print(sample)


I want to get the actual angles found by QAOA, the $$\beta, \gamma$$. How do I get these from the results of this algorithm?

To get optimized parameters from QAOA you can also do this. The MinimumEigenOptimizer returns a MinimumEigenOptimizationResult which has a field min_eigen_solver_result which is as the API ref linked states  the result obtained from the underlying algorithm. Now QAOA extends SamplingVQE and provides an identical result object, a SamplingVQEResult. The final β,γ can be in that field optimal_point which is just the list of floats the optimizer was working with, or in optimal_parameter which is a dictionary of the β,γ parameters to the values.

Your code sample did not run, I edited it to add a G taken from the Optimization MaxCut tutorial and other imports.

from qiskit_optimization import QuadraticProgram
from qiskit_optimization.algorithms import MinimumEigenOptimizer

from qiskit.algorithms.minimum_eigensolvers import QAOA
from qiskit.algorithms.optimizers import COBYLA
from qiskit.primitives import Sampler
import numpy as np

import networkx as nx
n = 4
G = nx.Graph()
elist = [(0, 1, 1.0), (0, 2, 1.0), (0, 3, 1.0), (1, 2, 1.0), (2, 3, 1.0)]

n_qubits = len(G.nodes())
_ = [problem.binary_var("x{}".format(i)) for i in range(n_qubits)]
problem.maximize(
)

meo = MinimumEigenOptimizer(QAOA(sampler=Sampler(), optimizer=COBYLA(maxiter=100)))
result = meo.solve(problem)
print(result.prettyprint())
print("\ndisplay the best 5 solution samples")
for sample in result.samples[:5]:
print(sample)

# Print the final QAOA parameters

print(result.min_eigen_solver_result.optimal_point)
print(result.min_eigen_solver_result.optimal_parameters)


the extra prints I added printed this for me

[5.60276426 4.22978775]
{ParameterVectorElement(β[0]): 5.602764261565667, ParameterVectorElement(γ[0]): 4.229787753395598}

• Thanks for editting it. I forgot to include G. Thanks so much I'll try this out! Apr 29 at 14:51

You can pass a callback function to QAOA. This callback can access the intermediate data at each optimization step. This data includes the optimizer parameters for the ansatz:

def callback(evaluation_count, optimizer_parameters, estimated_value, metadata):
print(optimizer_parameters)

meo = MinimumEigenOptimizer(QAOA(sampler=Sampler(), optimizer=COBYLA(maxiter=100), callback=callback))

• Seems doable (although haven't tried it yet). However, seems like it might slow things down given the number of iterations could be on the order of thousands for QAOA - right? Apr 29 at 17:44
• Each eval it just makes call to the callback code. That in of itself is quick, and as long as the code you add is simple it's as much overhead for each iter as that code. You can see an example done for VQE here: qiskit.org/documentation/tutorials/algorithms/… and its the same for QAOA. Some optimizers even have a callback of their own, which can have more optimizer specific info as well. Check the Qiskit Terra API ref for more details. Apr 29 at 19:16