View on GitHub
Open this notebook in GitHub to run it yourself
Solving the Rectangles Packing problem with Classiq
Rectangle Packing
**The rectangle packing problem is a classic optimization problem where the goal is to pack a set of given rectangles into a larger container rectangle (or bin) in a way that optimizes certain criteria, such as minimizing the total area used, minimizing wasted space, or maximizing the number of rectangles packed. This problem arises in various practical applications, including logistics (loading containers), manufacturing (cutting stock problems), and electronics (VLSI design).**
In this demonstration, we explore the rectangle packing problem where the objective is to arrange N rectangles of different sizes within a fixed grid container.The challenge lies in efficiently positioning these rectangles to maximize space utilization without overlap.This problem is a common optimization task with applications in areas such as logistics and manufacturing.
The Quantum Approximate Optimization Algorithm (QAOA) for rectangles packing
The rectangle packing problem is NP-hard, meaning that as the number of rectangles increases, the computational effort required grows exponentially for classical algorithms. QAOA offers a potential exponential speedup. Solving the rectangles problem using QAOA holds promise due to the potential computational advantages offered by quantum computing, particularly in tackling the complexity and size of the problem more efficiently than classical methods.Solving QAOA with Classiq
Classiq allows expressing a given optimization challenge in 3 simple steps:- Define the optimization problem Classiq seamlessly integrate a well known open source classical optimization modeling language with a diverse set of optimization capabilities (Pyomo).
- Plug the optimization model into Classiq The Combinatorial Optimization engine fully translates the Pyomo model to Qmod which is then synthesized into a Quantum Program object that encapsulates the QAOA implementation.
- Execute and analyze results Classiq allows execution of the Quantum Program on any leading quantum backend - hardware or simulator.
- Define the optimization problem
- Sets and Parameters:
-
container_widthandcontainer_heightdefine the dimensions of the container. -
rectanglesis a list of tuples, where each tuple represents the width and height of a rectangle. -
num_rectanglesis the number of rectangles.
- Variables:
model.placeis a a 3-dimensional binary variable to represent whether a rectangle r bottom left corner is placed at position (i, j).
- Constraints:
-
one_place_ruleensues each rectangle is placed at most once. -
within_container_ruleensure each rectangle fits within the container. -
non_overlap_ruleensures that rectangles do not overlap.
- Objective Function:
- In our example the objective is to maximize the number of rectangles placed.
Parameters
We will define a toy model of 3 rectangles packed into an 8 pixel grid container:Optimization Model
Output:
- Plug into Classiq
Initialize combinatorial optimization engine
In order to solve the Pyomo model defined above, we use the Classiq combinatorial optimization engine. For the quantum part of the QAOA algorithm (QAOAConfig) - define the number of repetitions (num_layers):
max_iteration) and the -parameter (alpha_cvar) for running CVaR-QAOA, an improved variation of the QAOA algorithm [3]:
Pluging-in the Pyomo model into the combinatorial optimization engine constructs the Qmod:
Lastly, we load the model, based on the problem and algorithm parameters, which we can use to solve the problem:- Solve and Analyze
Synthesize and view our QAOA circuit:
Output:
Output:
Execute QAOA:
execute function on the quantum program we have generated:

| probability | cost | solution | count | |
|---|---|---|---|---|
| 75 | 0.0010 | -3.0 | [0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, … | 10 |
| 80 | 0.0009 | -3.0 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, … | 9 |
| 27 | 0.0014 | -3.0 | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, … | 14 |
| 285 | 0.0005 | -3.0 | [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, … | 5 |
| 223 | 0.0006 | -3.0 | [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, … | 6 |
Output:

Visualize the results
In order to visualize thebest_solution as a floor plan we construct few simple post processing utilities.
To save qubits, the combinatorinal optimization engine creates a quantum program with qubits only correlating to binary variables that are not constraint to known fixed values. First, lets create a function that extracts the qubit indexes of variables that have no difference between the upper and lower bound of the constraints:
best_solution appends zeros into the the executed quantum program solution for each variable with no constraint boundary, we will use the extract_no_boundries_qubit_indexes above to “reorganize” the solution vector so we can properly visualize it:

Solve classically, visualize results and compare:
Running the following requires to install the classical solver with ‘brew install glpk’Output: