View on GitHub
Open this notebook in GitHub to run it yourself

Mathematical Formulation
First, model the problem mathematically. The input is the set of distances between the cities: this is given by a matrix , whose entry refers to the distance between city to city . The output of the model is an optimized route. Any route can be captured by a binary matrix that states at each step (row) which city was visited (column): For example: means starting from city 1, going to city 3 and then to city 2, and ending at city- The constrained optimization problem is defined as follows:
- in each step only a single point is visited -
Solving with the Classiq Platform
Solve the problem with the Classiq platform using QAOA by defining a Pyomo model.Building the Pyomo Model from a Matrix of Distances Input
Generating a Specific Problem
Pick a specific problem: the graph introduced above:
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 QAOA [1], with the 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:
optimize method of the CombinatorialProblem object.
For the classical optimization part of QAOA, define the maximum number of classical iterations (maxiter) and the -parameter (quantile) for running CVaR-QAOA, an improved variation of QAOA [2]:
Output:
Output:

Optimization Results
Examine the statistics of the algorithm. To get samples with the optimized parameters, call thesample method:
| solution | probability | cost | |
|---|---|---|---|
| 108 | {‘x’: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0… | 0.000488 | 3 |
| 41 | {‘x’: [[0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0… | 0.000977 | 5 |
| 896 | {‘x’: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0… | 0.000488 | 7 |
| 147 | {‘x’: [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0… | 0.000488 | 8 |
| 1240 | {‘x’: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1… | 0.000488 | 8 |

Output:
Output:
Output:
