View on GitHub
Open this notebook in GitHub to run it yourself
- Add a different demand for each customer.
- Add the cost for opening a facility at each location.
- Consider different categories of facilities and that customers can have multiple allocations for different facilities.
Mathematical Modeling
The input of the model is a set of customers , a set of potential locations for facilities , an matrix where is the cost of customer buying from facility , and the total number of facilities we want to open is . Define a binary variable for the optimization problem: an matrix such that if the customer is allocated to facility . The objective function to minimize is the total cost function: Constraints: (1) Each customer is supplied: . (2) Total number of open facilities is : (the inner product is zero if the th facility is not open).Alternative Modeling
There is an alternative modeling for adding another variable to the model: a binary vector of size , which indicates which facilities are open. In this formulation the second constraint can be written as together with an inequality constraint . A model that combines equality and inequality constraints may become available in the future. Note that this alternative modeling has a quadratic unconstrained binary optimization (QUBO) problem (compared to the formulation above where constraint (2) is a polynomial of degree ). However, the alternative modeling has more variables to minimize on, and thus refers to more qubits.Example
If you can open facilities in Japan, USA, and France, and you have four customers whose costs for buying from these three locations are given. To open in total facilities, the optimization problem is to find where to open the facilities and which customer is allocated to which facility. Draw this specific example on a graph. There are locations and customers, where the weights of the edges between them signify the costs: Suggestions:- Give a general problem description and then givens, or provide givens after each mention.
- Standardize writing numbers appearing in sentences when less than 10: “4” vs. “four”.

Building the Pyomo Model from a Matrix of Distances
Solving with Classiq
Take the specific example outlined above:CombinatorialProblem Python class.
Under the hood it translates the Pyomo model to a quantum model of the QAOA algorithm [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 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:
Output:

Optimization Results
Examine the statistics of the algorithm. To get samples with the optimized parameters, call thesample method:
| solution | probability | cost | |
|---|---|---|---|
| 1035 | {‘x’: [[1, 1, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]} | 0.000488 | 0.87 |
| 756 | {‘x’: [[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 0, 0]]} | 0.000488 | 0.95 |
| 230 | {‘x’: [[1, 0, 0, 0], [0, 1, 1, 1], [0, 0, 0, 0]]} | 0.000977 | 1.24 |
| 766 | {‘x’: [[0, 1, 1, 1], [0, 0, 0, 0], [1, 0, 0, 0]]} | 0.000488 | 1.27 |
| 329 | {‘x’: [[1, 1, 1, 0], [0, 0, 0, 0], [0, 0, 0, 1]]} | 0.000977 | 1.39 |

Output:
Best Solution
Plot the quantum result only if you get the right solution (to avoid problems with printing the table and graph):Output:
Output:
Output:
| A | B | C | D | |
|---|---|---|---|---|
| Japan | 1 | 1 | 1 | 0 |
| USA | 0 | 0 | 0 | 1 |
| France | 0 | 0 | 0 | 0 |

Compare to a Classical Solver
Output:
Output:
Output:
| A | B | C | D | |
|---|---|---|---|---|
| Japan | 1.000000 | 1.000000 | 1.000000 | 0.000000 |
| USA | 0.000000 | 0.000000 | 0.000000 | 1.000000 |
| France | 0.000000 | -0.000000 | 0.000000 | 0.000000 |
