Skip to main content
IBM Quantum Platform
This page is from an old version of Qiskit SDK and does not exist in the latest version. We recommend you migrate to the latest version. See the release notes for more information.

qiskit.optimization.applications.ising.knapsack

Convert knapsack parameters instances into Pauli list The parameters are a list of values a list of weights and a maximum weight of the knapsack.

In the Knapsack Problem we are given a list of objects that each has a weight and a value. We are also given a maximum weight we can carry. We need to pick a subset of the objects so as to maximize the total value without going over the maximum weight.

If we have the weights w[i], the values v[i] and the maximum weight W_max. We express the solution as a binary array x[i] where we have a 1 for the items we take in the solution set. We need to maximize sum(x[i]*v[i]) while respecting W_max >= sum(x[i]*w[i])

Functions

get_operator(values, weights, max_weight)Generate Hamiltonian for the knapsack problem.
get_solution(x, values)Get the solution to the knapsack problem from the bitstring that represents to the ground state of the Hamiltonian
knapsack_value_weight(solution, values, weights)Get the total wight and value of the items taken in the knapsack.
Was this page helpful?
Report a bug or request content on GitHub.