liveBook · Manning

Knapsack problem

As explained in Building Quantum Software with Python

The knapsack problem is an optimization problem that selects items with given weights and values to maximize total value without exceeding a weight capacity.

The knapsack problem is a binary optimization problem: choose a subset of items with given weights and values so that total value is as large as possible without exceeding a fixed capacity. Each item has a binary decision variable: 1 includes it, and 0 excludes it. With n items, there are 2^n possible selections, so even simple item-by-item choices grow quickly into a large search space. section 2.1.1 section 2.1.2

Figure 2.1 The knapsack problem is a binary optimization problem where the optimal solution is the collection of items with the highest value that does not exceed the weight limit.
figure

The diagram captures the constraint and objective: keep total weight within the limit while selecting the highest-value feasible collection. section 2.1.1

Model a selection as a binary string with one position per item. The total value and total weight are linear functions of those binary variables: each included item contributes its value to one total and its weight to the other. This representation makes every candidate selection directly comparable against both the capacity constraint and the value objective. section 2.1.1 section 2.3.1

Three-item capacity example

For capacity 4, use items with (value, weight) pairs (2, 3), (3, 2), and (1, 1). The eight binary selections include 101, with value 3 and weight 4, and 110, with value 4 and weight 3. Selection 110 is optimal: it includes items 1 and 2, stays within capacity, and beats every other feasible selection. section 2.1.2 section 2.3.2

A quantum representation uses a selection register with one qubit per item, plus registers for the selection’s total value and total weight. A measured selection bit of 1 means its corresponding item is included. The value and weight registers encode the two linear functions for every selection, forming a state that associates each binary key with its computed totals. section 2.3.1 section 10.1.2

Figure 10.5 Three registers encoding an item selection, its total value, and its total weight for solving the knapsack problem
figure

The three-register layout makes the representation explicit: one register identifies a selection, while two others hold its associated value and weight. section 10.1.2

A quantum search-oriented solution separates the problem into reusable parts:

  • Encode one qubit per item so binary outcomes represent candidate selections. section 2.3.1
  • Compute value and weight from the selection bits as linear functions, using controlled phase rotations and an inverse quantum Fourier transform for the function encoding. section 10.1.2
  • Recognize selections meeting a maximum weight and minimum value with an oracle, then use amplitude amplification to raise the probability of measuring desired outcomes. section 2.3.2

A practical interface accepts parallel value and weight lists plus a maximum weight. For the three-item example, the search begins with capacity 4 and a minimum value equal to the best single item’s value, 3. Raising the minimum value to 4 isolates the optimal feasible selection, while quantum measurement returns outcomes according to their resulting probabilities. section 2.3.2 section 2.3.2

The same encoding can be implemented as a quantum circuit by placing selection and output registers into equal superposition, applying controlled phase rotations for each coefficient-term pair, and applying the inverse quantum Fourier transform to the output register. Grid-state visualizations then show that each selection aligns with its computed value or weight. section 10.1.2 section 10.1.2

How Building Quantum Software with Python covers it

How deep it goes
Develops a worked three-item model through register encoding, circuit operations, and probability-based search, while deferring encoding details and later patterns.
Examples
Uses three items of weights 3, 2, 1 and values 2, 3, 1 under capacity 4, enumerating eight selections.
What it assumes
Assumes basic binary strings, complex numbers, trigonometry, and probability, while requiring no prior quantum mechanics or hardware knowledge.
What you can do after
Apply it via solve_knapsack and encode item selections, values, and weights as quantum-register functions.
How it connects
Connects binary optimization to binary variables, linear functions, quantum registers, oracles, amplitude amplification, measurement, and inverse quantum Fourier transform.

Also explained in

Compare how 4 books explain knapsack problem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Quantum Software with Python. The section links point to the parts of the book it draws on.