Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"source": [
"# Travelling Salesman Problem\n",
"\n",
"## Introduction\n",
"\n",
"The \"Travelling Salesperson Problem\" [[1](#TSPWiki)] refers to finding the shortest route between cities, given their relative distances. In a more general sense, given a weighted directed graph, one shall 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 following graph\n"
"The \"Travelling Salesman Problem\" [[1](#TSPWiki)] 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\\rightarrow 1\\rightarrow 2\\rightarrow 3$ yields a total distance 3, which is the shortest:\n"
]
},
{
Expand Down Expand Up @@ -57,10 +55,7 @@
"tags": []
},
"source": [
"one can see that the route which goes along $0\\rightarrow 1\\rightarrow 2\\rightarrow 3$ yields a total distance 3, which is the shortest.\n",
"\n",
"As many other real world problems, this task may be cast into a combinatorial optimization problem. In this demo, we will show how the Quantum Approximate Optimization Algorithm [[2](#QAOA)] can be employed on the Classiq platform to solve the Travelling Salesperson Problem.\n",
"\n"
"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](#QAOA)] on the Classiq platform to solve the Travelling Salesperson Problem.\n"
]
},
{
Expand All @@ -70,9 +65,9 @@
"tags": []
},
"source": [
"## Mathematical formulation\n",
"## Mathematical Formulation\n",
"\n",
"As a first step, we have to model the problem mathematically. The input is the set of distances between the cities: this is given by a matrix $w$, whose $(i,j)$ entry refers to the distance between city $i$ to city $j$. The output of the model is an optimized route. Any route can be captured by a binary matrix $x$ which states at each step (row) which city was visited (column):\n",
"First, model the problem mathematically. The input is the set of distances between the cities: this is given by a matrix $w$, whose $(i,j)$ entry refers to the distance between city $i$ to city $j$. The output of the model is an optimized route. Any route can be captured by a binary matrix $x$ that states at each step (row) which city was visited (column):\n",
"$\\begin{aligned}\n",
"x_{ij} =\n",
"\\begin{cases}\n",
Expand All @@ -89,18 +84,18 @@
"0 & 0 & 1 & 0 \\\\\n",
"1 & 0 & 0 & 0\n",
"\\end{pmatrix}\n",
"\\end{aligned}$ means that we start from city 1 go to city 3 then to city 2 and end at city 0.\n",
"\\end{aligned}$ means starting from city 1, going to city 3 and then to city 2, and ending at city 0.\n",
"\n",
"#### The constrained optimization problem is thus defined as follows:\n",
"**The constrained optimization problem is defined as follows:**\n",
"\n",
"- find x, that minimizes the path distance -\n",
"Find x, which minimizes the path distance -\n",
" $\\begin{aligned}\n",
"\\min_{x_{i, p} \\in \\{0, 1\\}} \\Sigma_{i, j} w_{i, j} \\Sigma_p x_{i, p} x_{j, p + 1}\\\\\n",
"\\end{aligned}$\n",
"\n",
"(note that the inner sum over $p$ is simply an indicator for whether we go from city $i$ to city $j$)\n",
"(Note that the inner sum over $p$ is simply an indicator for whether to go from city $i$ to city $j$.)\n",
"\n",
"such that:\n",
"such that\n",
"\n",
"- each point is visited once -\n",
" $\\begin{aligned}\n",
Expand All @@ -112,9 +107,9 @@
"\\forall p, \\hspace{0.2cm} \\Sigma_i x_{i, p} = 1\\\\\n",
"\\end{aligned}$\n",
"\n",
"#### Directed graph\n",
"**Directed graph:**\n",
"\n",
"In some cases, as in the graph above, not all cities are connected, and it is more proper to describe the problem with a weighted, directed graph. In that case, since we would like to find the shortest path, unconnected cities are assumed to have an infinite distance between them. For example, the graph above corresponds to the matrix\n",
"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:\n",
"\n",
"$\\begin{aligned}\n",
"w=\\begin{pmatrix}\n",
Expand All @@ -125,17 +120,17 @@
"\\end{pmatrix}\n",
"\\end{aligned}$\n",
"\n",
"In practice we will choose a large enough weight rather than infinity.\n"
"In practice, choose a large enough weight rather than infinity.\n"
]
},
{
"cell_type": "markdown",
"id": "2c8d2493-525d-4aa9-8b93-dd10263a4749",
"metadata": {},
"source": [
"# Solving with the Classiq platform\n",
"## Solving with the Classiq Platform\n",
"\n",
"We go through the steps of solving the problem with the Classiq platform, using QAOA algorithm. The solution is based on defining a pyomo model for the optimization problem we would like to solve.\n"
"Solve the problem with the Classiq platform using QAOA by defining a Pyomo model.\n"
]
},
{
Expand All @@ -145,7 +140,7 @@
"tags": []
},
"source": [
"## Building the Pyomo model from a matrix of distances input\n"
"### Building the Pyomo Model from a Matrix of Distances Input\n"
]
},
{
Expand All @@ -171,7 +166,7 @@
},
"outputs": [],
"source": [
"## We define a function which gets the matrix of distances and returns a Pyomo model\n",
"## Define a function that gets the matrix of distances and returns a Pyomo model\n",
"\n",
"\n",
"def PyomoTSP(dis_mat: np.ndarray) -> pyo.ConcreteModel:\n",
Expand All @@ -182,7 +177,7 @@
" NofCities = dis_mat.shape[0] # total number of cities\n",
" cities = range(NofCities) # list of cities\n",
"\n",
" # we define our variable, which is the binary matrix x: x[i, j] = 1 indicates that point i is visited at step j\n",
" # Define the variable, which is the binary matrix x: x[i, j] = 1 indicates that point i is visited at step j\n",
" model.x = pyo.Var(cities, cities, domain=pyo.Binary)\n",
"\n",
" # we add constraints\n",
Expand All @@ -194,7 +189,7 @@
" def each_point_visited_once_rule(model, jj):\n",
" return sum(model.x[ii, jj] for ii in range(NofCities)) == 1\n",
"\n",
" # we define our Objective function\n",
" # Define the Objective function\n",
" def is_connected(i1: int, i2: int):\n",
" return sum(model.x[i1, kk] * model.x[i2, kk + 1] for kk in cities[:-1])\n",
"\n",
Expand All @@ -214,9 +209,9 @@
"tags": []
},
"source": [
"## Generating a specific problem\n",
"### Generating a Specific Problem\n",
"\n",
"Let us pick a specific problem: the graph introduced above.\n"
"Pick a specific problem: the graph introduced above:\n"
]
},
{
Expand All @@ -239,7 +234,7 @@
}
],
"source": [
"# We generate a graph which defines our problem\n",
"# Generate a graph that defines the problem\n",
"import networkx as nx\n",
"\n",
"graph = nx.DiGraph()\n",
Expand All @@ -258,7 +253,7 @@
"id": "236ed658-b452-4bf9-bea9-a75f7e80c478",
"metadata": {},
"source": [
"We convert the graph object into a matrix of distances and then generate a pyomo model for this example"
"Convert the graph object into a matrix of distances and then generate a Pyomo model for this example:"
]
},
{
Expand All @@ -283,9 +278,9 @@
"tags": []
},
"source": [
"## Setting Up the Classiq Problem Instance\n",
"### Setting Up the Classiq Problem Instance\n",
"\n",
"In order to solve the Pyomo model defined above, we use the `CombinatorialProblem` python class. Under the hood it translates the Pyomo model to a quantum model of the QAOA algorithm [[1](#QAOA)], with cost hamiltonian translated from the Pyomo model. We can choose the number of layers for the QAOA ansatz using the argument `num_layers`."
"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](#QAOA)], with the cost Hamiltonian translated from the Pyomo model. Choose the number of layers for the QAOA ansatz using the `num_layers` argument:"
]
},
{
Expand Down Expand Up @@ -320,9 +315,9 @@
"id": "02db795d-3851-46c2-8236-5a926081cb73",
"metadata": {},
"source": [
"## Synthesizing the QAOA Circuit and Solving the Problem\n",
"### Synthesizing the QAOA Circuit and Solving the Problem\n",
"\n",
"We can now synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:"
"Synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:"
]
},
{
Expand Down Expand Up @@ -354,7 +349,7 @@
"id": "b6dc9014-cd93-4ed2-b691-329cbe2b8dc7",
"metadata": {},
"source": [
"We now solve the problem by calling the `optimize` method of the `CombinatorialProblem` object. For the classical optimization part of the QAOA algorithm we define the maximum number of classical iterations (`maxiter`) and the $\\alpha$-parameter (`quantile`) for running CVaR-QAOA, an improved variation of the QAOA algorithm [[2](#cvar)]:"
"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](#cvar)]:"
]
},
{
Expand Down Expand Up @@ -382,7 +377,7 @@
"id": "69bb4ea0-23e9-4bb4-8bec-a81d8c136bd1",
"metadata": {},
"source": [
"We can check the convergence of the run:"
"Check the convergence of the run:"
]
},
{
Expand Down Expand Up @@ -431,15 +426,15 @@
"tags": []
},
"source": [
"# Optimization Results"
"## Optimization Results"
]
},
{
"cell_type": "markdown",
"id": "d42c2eaa-5e53-426d-ab28-84f1270b2f2e",
"metadata": {},
"source": [
"We can also examine the statistics of the algorithm. In order to get samples with the optimized parameters, we call the `sample` method:"
"Examine the statistics of the algorithm. To get samples with the optimized parameters, call the `sample` method:"
]
},
{
Expand Down Expand Up @@ -533,7 +528,7 @@
"id": "53fbe569-e622-4c2c-a57c-d3cdf04c896b",
"metadata": {},
"source": [
"We will also want to compare the optimized results to uniformly sampled results:"
"Compare the optimized results with uniformly sampled results:"
]
},
{
Expand Down Expand Up @@ -601,7 +596,7 @@
"id": "191a4566-c10b-4107-9a47-0b326e092fb1",
"metadata": {},
"source": [
"Let us plot the solution:"
"Plot the solution:"
]
},
{
Expand Down Expand Up @@ -633,7 +628,7 @@
"id": "1a103578-5993-4d08-94f1-8cc93973b0df",
"metadata": {},
"source": [
"Lastly, we can compare to the classical solution of the problem:"
"Lastly, compare with the classical solution of the problem:"
]
},
{
Expand Down Expand Up @@ -708,7 +703,7 @@
"id": "d8f3d6b0-5de9-42c9-b52a-4c31ef2d30a7",
"metadata": {},
"source": [
"If we get the right solution we plot it:"
"If you get the right solution, plot it:"
]
},
{
Expand Down Expand Up @@ -802,11 +797,11 @@
"\n",
"## References\n",
"\n",
"<a id='TSPWiki'>[1]</a>: [Travelling Salesperson Problem (Wikipedia)](https://en.wikipedia.org/wiki/Travelling_salesman_problem)\n",
"<a id='TSPWiki'>[1]</a> [Travelling Salesman Problem (Wikipedia).](https://en.wikipedia.org/wiki/Travelling_salesman_problem)\n",
"\n",
"<a id='QAOA'>[2]</a>: [Farhi, Edward, Jeffrey Goldstone, and Sam Gutmann. \"A quantum approximate optimization algorithm.\" arXiv preprint arXiv:1411.4028 (2014).](https://arxiv.org/abs/1411.4028)\n",
"<a id='QAOA'>[2]</a> [Farhi, Edward, Jeffrey Goldstone, and Sam Gutmann. (2014). A quantum approximate optimization algorithm. arXiv preprint arXiv:1411.4028.](https://arxiv.org/abs/1411.4028)\n",
"\n",
"<a id='cvar'>[3]</a>: [Barkoutsos, Panagiotis Kl, et al. \"Improving variational quantum optimization using CVaR.\" Quantum 4 (2020): 256.](https://arxiv.org/abs/1907.04769)\n"
"<a id='cvar'>[3]</a> [Barkoutsos, Panagiotis Kl, et al. (2020). Improving variational quantum optimization using CVaR. Quantum 4: 256.](https://arxiv.org/abs/1907.04769)\n"
]
}
],
Expand Down
Loading