qiskit_addon_opt_mapper.applications.Knapsack

class Knapsack(values, weights, max_weight)[source]

Bases: OptimizationApplication

Optimization application for the “knapsack problem” [1].

References

[1]: “Knapsack problem”, https://en.wikipedia.org/wiki/Knapsack_problem

Init method.

Parameters:
  • values (list[int]) – A list of the values of items

  • weights (list[int]) – A list of the weights of items

  • max_weight (int) – The maximum weight capacity

__init__(values, weights, max_weight)[source]

Init method.

Parameters:
  • values (list[int]) – A list of the values of items

  • weights (list[int]) – A list of the weights of items

  • max_weight (int) – The maximum weight capacity

Return type:

None

Methods

__init__(values, weights, max_weight)

Init method.

interpret(result)

Interpret a result as item indices.

sample_most_likely(state_vector)

Compute the most likely binary string from state vector.

to_optimization_problem()

Represent as an optimization problem.

Attributes

max_weight

Getter of max_weight.

interpret(result)[source]

Interpret a result as item indices.

Parameters:

result (ndarray) – The calculated result of the problem

Returns:

A list of items whose corresponding variable is 1

Return type:

list[int]

property max_weight: int

Getter of max_weight.

Returns:

The maximal weight for the knapsack problem

static sample_most_likely(state_vector)[source]

Compute the most likely binary string from state vector.

Parameters:

state_vector (QuasiDistribution | Statevector | ndarray | dict) – state vector or counts or quasi-probabilities.

Returns:

binary string as numpy.ndarray of ints.

Raises:

ValueError – if state_vector is not QuasiDistribution, Statevector, np.ndarray, or dict.

Return type:

ndarray

to_optimization_problem()[source]

Represent as an optimization problem.

Convert a knapsack problem instance into a OptimizationProblem

Returns:

The OptimizationProblem created from the knapsack problem instance.

Return type:

OptimizationProblem