Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

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).** floorplan_example_bigger.png
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:
  1. Define the optimization problem Classiq seamlessly integrate a well known open source classical optimization modeling language with a diverse set of optimization capabilities (Pyomo).
The Pyomo language supports a wide variety of problem types, such as integer linear programming, quadratic programming, graph theory problems, SAT problems, and many more.
  1. 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.
The Quantum Program can be visually analyzed for debugging and even educational purposes.
  1. Execute and analyze results Classiq allows execution of the Quantum Program on any leading quantum backend - hardware or simulator.

  1. Define the optimization problem
We will first define a classical optimization model: We encode the rectangles positions into a binary variable that represents the rectangle id and its position within a given 2-dimensional container grid. We require that each rectangle is placed at most once and that rectangles are placed within the container grid and do not overlap.
  1. Sets and Parameters:
  • container_width and container_height define the dimensions of the container.
  • rectangles is a list of tuples, where each tuple represents the width and height of a rectangle.
  • num_rectangles is the number of rectangles.
  1. Variables:
  • model.place is a a 3-dimensional binary variable to represent whether a rectangle r bottom left corner is placed at position (i, j).
  1. Constraints:
  • one_place_rule ensues each rectangle is placed at most once.
  • within_container_rule ensure each rectangle fits within the container.
  • non_overlap_rule ensures that rectangles do not overlap.
  1. Objective Function:
  • In our example the objective is to maximize the number of rectangles placed.
This is a basic model and can be extended to include more sophisticated features like rotation of rectangles, different objective functions, or more complex constraints.

Parameters

We will define a toy model of 3 rectangles packed into an 8 pixel grid container:

Optimization Model

Output:

  1. 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):
For the classical optimization part of the QAOA algorithm we define the maximum number of classical iterations (max_iteration) and the α\alpha-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:
We also set the quantum backend we want to execute on:

  1. Solve and Analyze
We can now synthesize and view the QAOA circuit (ansatz) used to solve the optimization problem:
Synthesize and view our QAOA circuit:
Output:
Output:
Execute QAOA:
We now solve the problem by calling the execute function on the quantum program we have generated:
We can check the convergence of the run:
output We can also examine the statistics of the algorithm:
And the histogram:
Output:
output

Visualize the results

In order to visualize the best_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:
Since the 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:
We can now run the floorplan visualization function:
output

Solve classically, visualize results and compare:

Running the following requires to install the classical solver with ‘brew install glpk’
Output: