View on GitHub
Open this notebook in GitHub to run it yourself
Mathematical Formulation
Given:-
A set of locations:
where
-
The first locations are depots:
- The remaining are cities:
-
Each vehicle :
- Starts and ends its route at its own depot
- Visits a sequence of cities
- Returns to its depot
- Each city must be visited exactly once by one vehicle only.
- Each vehicle route has positions (start + inner positions + end).
positions because driving between cities does not take the same amount of time.
Decision Variables
Let be a binary variable defined as where:(location: city or depot)
(vehicle/depot index)
Constraints
- Each city is visited exactly once:
2. Each car visits exactly one city in every position: Each vehicle has exactly one location assigned to each inner route position.
3. Depot start and end positions are fixed: Each vehicle starts and ends at its own depot only.
- No city is at the start or end:
Objective Function
The aim is to minimize total travel cost, defined as where:- is the Euclidean distance between locations and .
- The sum computes all transitions for each vehicle between consecutive positions.
Constraint Functions
All cities are visited only once: All positions are occupied only once: **These are added as penalty terms and multiplied by a scaling factor later on in thecost function.
Ensure they are actually equal to zero by adding them as penalty terms.**
Generating Problem Data
Defining Positions
Assuming the cities are more or less evenly divided among the vehicles, usepossible_number_of_cities_per_vehicle:
num_positions (or time slots) in a car route.
Add for the start (depot) and end (return to depot) positions:
- Starting depot
- Visiting cities
num_positions-1 - Ending depot
num_positions
Output:
Output:
Setting City Locations
Set random locations in a square of size(0, max_x_y)(0, max_x_y).
To have the same result, set the random seed. To have different results, change or delete the seed:
Output:

Output:
num_locations max_distance.
The minimal cost of any candidate is num_locations min_distance.
Hence, this is the maximal difference of eigenvalues of the total cost Hamiltonian:
DISTANCE_NORMALISATION:
Output:
Normalizing the Cost and Mixer Hamiltonians
This normalization improves the probability of finding good solutions. If this is too much information right now, skip it and focus on the VRP problem itself under “Defining the VRP Objective Function”. The mixer Hamiltonian is effectively and has eigenvalues and . So the difference between minimum and maximum eigenvalues is exactly- You can rescale it using a global scaling parameter:
- The distance normalization will take care of it later.
Output:
- Normalize the constraint Hamiltonian relative to the total cost Hamiltonian such that the constraint violation is 1~2 x larger than the maximal difference between total cost values:
Output:
Defining the VRP Objective Function
First, define number of functions that comprise the objective function. A major difficulty is to map 3D (city, position, vehicle) to 1D index in the array of decision variables.Finding the Right Index in the Array
Access the value of the binary decision variable x that encodes whether city (or depot)u is visited at position v by vehicle k.
u- cityv- position (time slot)x- the array of decision variablesk- the vehicle number
- Each city is assigned to exactly one vehicle and one route position.
- Each inner position of a route (per vehicle) is filled by exactly one city.
Determining Total Travel Cost for All Vehicle Routes
Defining Constraints
Building the QAOA Model
Define the number of layers:Synthesizing the Model
Output:
Executing
When you have the QAOA circuit, execute it in the hybrid scheme. First, defineExecutionSession for the optimization process and the initial parameters:
objective function of the VRP problem:
estimate_cost method of the ExecutionSession class:
Running the Optimization Process
Usescipy.optimize.minimize to minimize the cost function.
You may increase the max_iterations to have more iterations.
Analyzing the Results
If you have more iterations you can make a convergence graph:Optimizing Parameters
Watch for four or more circuit layers:Output:

Output:
Best Solution
Output:
Output:
Output:
