qiskit-documentation/docs/api/qiskit/0.29/qiskit.optimization.applica...

27 lines
1.8 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: knapsack
description: API reference for qiskit.optimization.applications.ising.knapsack
in_page_toc_min_heading_level: 2
python_api_type: module
python_api_name: qiskit.optimization.applications.ising.knapsack
---
<span id="qiskit-optimization-applications-ising-knapsack" />
# 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. |