Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
The “Travelling Salesman Problem” [1] refers to finding the shortest route between cities, given their relative distances. In a more general sense, given a weighted directed graph, find the shortest route along the graph that goes through all the cities, where the weights correspond to the distance between cities. For example, in the graph below, the route along 0→1→2→30\rightarrow 1\rightarrow 2\rightarrow 3 yields a total distance 3, which is the shortest:
output As with many real world problems, this task can be cast as a combinatorial optimization problem. This demo shows how to employ the Quantum Approximate Optimization Algorithm [2] on the Classiq platform to solve the Travelling Salesperson Problem.

Mathematical Formulation

First, model the problem mathematically. The input is the set of distances between the cities: this is given by a matrix ww, whose (i,j)(i,j) entry refers to the distance between city ii to city jj. The output of the model is an optimized route. Any route can be captured by a binary matrix xx that states at each step (row) which city was visited (column): xij={1at time step i the route is in city j0else\begin{aligned} x_{ij} = \begin{cases} 1 & \text{at time step } i \text{ the route is in city } j \\ 0 & \text{else} \end{cases}\\ \end{aligned} For example: x=(0100000100101000)\begin{aligned} x=\begin{pmatrix} 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 0 \end{pmatrix} \end{aligned} means starting from city 1, going to city 3 and then to city 2, and ending at city
  1. The constrained optimization problem is defined as follows:
Find x, which minimizes the path distance - $ min⁡xi,p∈{0,1}Σi,jwi,jΣpxi,pxj,p+1\begin{aligned} \min_{x_{i, p} \in \{0, 1\}} \Sigma_{i, j} w_{i, j} \Sigma_p x_{i, p} x_{j, p + 1}\\ \end{aligned} (Note that the inner sum over $p$ is simply an indicator for whether to go from city $i$ to city $j$.) such that - each point is visited once - $ \begin{aligned} \forall i, \hspace{0.2cm} \Sigma_p x_{i, p} = 1\\ \end{aligned}
  • in each step only a single point is visited -
$ ∀p,Σixi,p=1\begin{aligned} \forall p, \hspace{0.2cm} \Sigma_i x_{i, p} = 1\\ \end{aligned} ∗∗Directedgraph:∗∗Insomecases,suchasthegraphabove,notallcitiesareconnected,anditismoresuitabletodescribetheproblemwithaweighted,directedgraph.Inthiscase,tofindtheshortestpath,assumethatunconnectedcitieshaveaninfinitedistancebetweenthem.Forexample,thegraphabovecorrespondstothismatrix: **Directed graph:** In some cases, such as the graph above, not all cities are connected, and it is more suitable to describe the problem with a weighted, directed graph. In this case, to find the shortest path, assume that unconnected cities have an infinite distance between them. For example, the graph above corresponds to this matrix: w=(∞12∞∞∞12∞1∞1∞∞∞∞)\begin{aligned} w=\begin{pmatrix} \infty & 1 & 2 & \infty \\ \infty & \infty & 1 & 2 \\ \infty & 1 & \infty & 1 \\ \infty & \infty & \infty & \infty \end{pmatrix} \end{aligned} Inpractice,choosealargeenoughweightratherthaninfinity. In practice, choose a large enough weight rather than infinity.

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:
output Convert the graph object into a matrix of distances and then generate a Pyomo model for this example:

Setting Up the Classiq Problem Instance

To solve the Pyomo model defined above, use the CombinatorialProblem 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:
Solve the problem by calling the optimize method of the CombinatorialProblem object. For the classical optimization part of QAOA, define the maximum number of classical iterations (maxiter) and the α\alpha-parameter (quantile) for running CVaR-QAOA, an improved variation of QAOA [2]:
Output:
Check the convergence of the run:
Output:
output

Optimization Results

Examine the statistics of the algorithm. To get samples with the optimized parameters, call the sample method:
Compare the optimized results with uniformly sampled results:
And compare the histograms:
output Plot the solution:
Output:
Lastly, compare with the classical solution of the problem:
Output:
If you get the right solution, plot it:
Output:
output

References

[1] Travelling Salesman Problem (Wikipedia). [2] Farhi, Edward, Jeffrey Goldstone, and Sam Gutmann. (2014). A quantum approximate optimization algorithm. arXiv preprint arXiv:1411.4028. [3] Barkoutsos, Panagiotis Kl, et al. (2020). Improving variational quantum optimization using CVaR. Quantum 4: 256.