Number Partitioning¶
In the Number Partitioning problem, the goal is to find, given a set of
For example, given the set of numbers
A Kind of Knapsack Problem
The Number Partitioning problem can be seen as a special case of the Knapsack problem, where there are
The Cost Function¶
The function to minimize can be defined as
where
Since this formulation is already in terms of Ising variables, it can be used directly in QAOA.
Number Partitioning in OpenQAOA¶
The Number Partitioning problem is defined by a set of integers, you can thus define the problem as follows:
from openqaoa.problems import NumberPartition
int_set = [1,2,3,4,10]
np_prob = NumberPartition(numbers=int_set)
np_qubo = np_prob.qubo
We can then access the underlying cost hamiltonian
You may also check all details of the problem instance in the form of a dictionary:
> np_qubo.asdict()
{'constant': 130,
'metadata': {},
'n': 5,
'problem_instance': {'n_numbers': 5,
'numbers': [1, 2, 3, 4, 10],
'problem_type': 'number_partition'},
'terms': [[0, 1],
[0, 2],
[0, 3],
[0, 4],
[1, 2],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
[3, 4]],
'weights': [4.0, 6.0, 8.0, 20.0, 12.0, 16.0, 40.0, 24.0, 60.0, 80.0]}