View on GitHub
Open this notebook in GitHub to run it yourself
Defining the Optimization Problem
Encode each node as a binary variable:
Setting Up the Classiq Problem Instance
To solve the Pyomo model defined above, use theCombinatorialProblem Python class.
Under the hood, it translates the Pyomo model to a quantum model of the Quantum Approximate Optimization Algorithm (QAOA) [1], with a cost Hamiltonian translated from the Pyomo model.
Choose the number of layers for the QAOA ansatz using the num_layers argument:
Synthesizing the QAOA Circuit and Solving the Problem
Synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:Output:
Output:
optimize method of the CombinatorialProblem object.
For the classical optimization part of the QAOA algorithm, define the maximum number of classical iterations (maxiter) and the -parameter (quantile) for running CVaR-QAOA, an improved variation of the QAOA algorithm [2]:
Output:

Viewing the Optimization Results
Examine the statistics of the algorithm. The optimization is always defined as a minimization problem, so the Pyomo-to-Qmod translator changes the positive maximization objective to negative minimization. To get samples with the optimized parameters, call thesample method:
| solution | probability | cost | |
|---|---|---|---|
| 37 | {‘x’: [1, 1, 1, 1, 0, 0, 0]} | 0.006836 | -4 |
| 52 | {‘x’: [0, 1, 1, 1, 0, 1, 0]} | 0.004395 | -4 |
| 44 | {‘x’: [1, 0, 0, 1, 1, 0, 0]} | 0.005371 | -3 |
| 34 | {‘x’: [0, 0, 0, 1, 1, 1, 0]} | 0.007812 | -3 |
| 50 | {‘x’: [1, 1, 0, 0, 0, 0, 1]} | 0.004883 | -3 |

Output:

Comparing to a Classical Solver
Lastly, compare to the classical solution of the problem:Output:
