0
$\begingroup$

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 = QuadraticProgram()
_ = [problem.binary_var("x{}".format(i)) for i in range(n_qubits)]
problem.maximize(
    linear=nx.adjacency_matrix(G).dot(np.ones(n_qubits)),
    quadratic=-nx.adjacency_matrix(G),
)

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?

I'm not seeing it in the docs: https://qiskit.org/documentation/stubs/qiskit.algorithms.minimum_eigensolvers.QAOA.html#qiskit.algorithms.minimum_eigensolvers.QAOA

$\endgroup$

2 Answers 2

3
$\begingroup$

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()
G.add_nodes_from(np.arange(0, n, 1))
elist = [(0, 1, 1.0), (0, 2, 1.0), (0, 3, 1.0), (1, 2, 1.0), (2, 3, 1.0)]
G.add_weighted_edges_from(elist)

n_qubits = len(G.nodes())
problem = QuadraticProgram()
_ = [problem.binary_var("x{}".format(i)) for i in range(n_qubits)]
problem.maximize(
    linear=nx.adjacency_matrix(G).dot(np.ones(n_qubits)),
    quadratic=-nx.adjacency_matrix(G),
)

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}
$\endgroup$
1
  • $\begingroup$ Thanks for editting it. I forgot to include G. Thanks so much I'll try this out! $\endgroup$
    – somewhere
    Apr 29, 2023 at 14:51
1
$\begingroup$

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))
$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – somewhere
    Apr 29, 2023 at 17:44
  • 2
    $\begingroup$ 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. $\endgroup$
    – Steve Wood
    Apr 29, 2023 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.