Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
Consider the optimization problem where you have a set of MM customers and a set of NN potential locations for opening a facility. Given transportation costs between facilities and customers and the number of facilities you would like to open (PP), determine which facilities to open such that the total transportation cost between facilities and customers is minimal, under the constraint that each customer is allocated to only one facility. Possible extensions:
  • 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 MM customers {1,…,M}\{1,\dots,M\}, a set of NN potential locations for facilities {1,…,N}\{1,\dots,N\}, an N×MN\times M matrix dd where dnmd_{nm} is the cost of customer mm buying from facility nn, and the total number of facilities we want to open is PP. Define a binary variable for the optimization problem: an N×MN\times M matrix xx such that xnm=1x_{nm}=1 if the customer mm is allocated to facility nn. The objective function to minimize is the total cost function: min⁡x∑n,mdnmxnm\min_{x} \sum_{n,m} d_{nm}x_{nm} Constraints: (1) Each customer is supplied: ∀m∈[0,M]   ∑nxnm=1\forall m\in[0,M] \,\,\, \sum_n x_{nm}=1. (2) Total number of open facilities is PP: ∑nΠm(1−xnm)=N−P\sum_n\Pi_m (1-x_{nm})=N-P (the inner product is zero if the n−n-th facility is not open).

Alternative Modeling

There is an alternative modeling for adding another variable to the model: a binary vector yy of size NN, which indicates which facilities are open. In this formulation the second constraint can be written as ∑nyn=P\sum_n y_n=P together with an inequality constraint ∀n,m: xnm≤yn\forall n,m:\, x_{nm} \leq y_n. 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 mm). 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 P=2P=2 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 N=3N=3 locations and M=4M=4 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”.
output

Building the Pyomo Model from a Matrix of Distances

Solving with Classiq

Take the specific example outlined above:
To solve the Pyomo model, use the 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:
Now, solve the problem by calling the 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 α\alpha-parameter (quantile) for running CVaR-QAOA, an improved variation of the QAOA algorithm [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 to uniformly sampled results:
And compare the histograms:
output Plot the solution:
Output:
Define a function that plots solutions:

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:
ABCD
Japan1110
USA0001
France0000
output

Compare to a Classical Solver

Output:
Output:
Output:
ABCD
Japan1.0000001.0000001.0000000.000000
USA0.0000000.0000000.0000001.000000
France0.000000-0.0000000.0000000.000000
output