BILP
- class qubovert.problems.BILP(*args, **kwargs)
BILP.
Class to manage converting Binary Integer Linear Programming problems to and from their QUBO and QUSO formluations. Based on the paper “Ising formulations of many NP problems”, hereforth designated as [Lucas].
The goal of the BILP problem is to find the minimum value of \(\mathbf{c} \cdot \mathbf{x}\) subject to \(S \mathbf{x} = \mathbf{b}\). Here \(\mathbf{c}\), \(\mathbf{b}\), and \(S\) define the problem, and \(\mathbf{x}\), is a vector of boolean variables.
BILP inherits some methods and attributes from the Problem class. See
help(qubovert.problems.Problem)
.Example
>>> from qubovert.problems import BILP >>> from any_module import qubo_solver >>> # or you can use my bruteforce solver... >>> # from qubovert.utils import solve_qubo_bruteforce as qubo_solver >>> >>> c = [1, 2, -1] >>> S = [[1, 0, 0], [0, 1, -1]] >>> b = [0, 1] >>> problem = BILP(c, S, b) >>> >>> Q = problem.to_qubo() >>> obj, sol = qubo_solver(Q) >>> solution = problem.convert_solution(sol) >>> >>> print(solution) [0 1 0] >>> print(problem.is_solution_valid(solution)) True >>> print(obj == np.dot(c, solution)) True
__init__.
The goal of the BILP problem is to find the minimum value of \(\mathbf{c} \cdot \mathbf{x}\) subject to \(S \mathbf{x} = \mathbf{b}\). Here \(\mathbf{c}\), \(\mathbf{b}\), and \(S\) define the problem, and \(\mathbf{x}\), is a vector of boolean variables. All naming conventions follow the names in the paper [Lucas].
- Parameters
c (list or numpy one-dim array of integers.) – Array representing the \(\mathbf{c}\) vector.
c
should have lengthN
.S (list of lists or two-dim numpy array of integers.) – Array representing the \(S\) matrix.
S
should bem
byN
.b (list or one-dim numpy array of integers.) – Array representing the \(\mathbf{b}\) vector.
b
should have lengthm
.
Examples
>>> c = >>> b = >>> S = >>> problem = BILP(c, S, b)
- property S
A copy of the
S
matrix. Updating the copy will not update the instance set.- Returns
S
- Return type
numpy array.
- property b
A copy of the
b
vector. Updating the copy will not update the instance set.- Returns
b
- Return type
numpy array.
- property c
A copy of the
c
vector. Updating the copy will not update the instance set.- Returns
c
- Return type
numpy array.
- convert_solution(solution, spin=False)
convert_solution.
Convert the solution to the QUBO or QUSO to the solution to the BILP problem.
- Parameters
solution (iterable or dict.) – The QUBO or QUSO solution output. The QUBO solution output is either a list or tuple where indices specify the label of the variable and the element specifies whether it’s 0 or 1 for QUBO (or 1 or -1 for QUSO), or it can be a dictionary that maps the label of the variable to is value.
spin (bool (optional, defaults to False)) – spin indicates whether
solution
is the solution to the boolean {0, 1} formulation of the problem or the spin {1, -1} formulation of the problem. This parameter usually does not matter, and it will be ignored if possible. The only time it is used is ifsolution
contains all 1’s. In this case, it is unclear whethersolution
came from a spin or boolean formulation of the problem, and we will figure it out based on thespin
parameter.
- Returns
res – An array representing the \(\mathbf{x}\) vector.
- Return type
np.array.
- is_solution_valid(solution, spin=False)
is_solution_valid.
Returns whether or not the proposed solution satisfies the constraint that \(S\mathbf{x} = \mathbf{b}\).
- Parameters
solution (iterable or dict.) – solution can be the output of BILP.convert_solution, or the QUBO or QUSO solver output. The QUBO solution output is either a list or tuple where indices specify the label of the variable and the element specifies whether it’s 0 or 1 for QUBO (or 1 or -1 for QUSO), or it can be a dictionary that maps the label of the variable to is value.
spin (bool (optional, defaults to False)) – spin indicates whether
solution
is the solution to the boolean {0, 1} formulation of the problem or the spin {1, -1} formulation of the problem. This parameter usually does not matter, and it will be ignored if possible. The only time it is used is ifsolution
contains all 1’s. In this case, it is unclear whethersolution
came from a spin or boolean formulation of the problem, and we will figure it out based on thespin
parameter.
- Returns
valid – True if the proposed solution is valid, else False.
- Return type
boolean.
- property num_binary_variables
num_binary_variables.
The number of binary variables that the QUBO and QUSO use.
- Returns
num – The number of variables in the QUBO/QUSO formulation.
- Return type
integer.
- solve_bruteforce(*args, **kwargs)
solve_bruteforce.
Solve the problem bruteforce. THIS SHOULD NOT BE USED FOR LARGE PROBLEMS! This is implemented in the parent
qubovert.utils.Problem
class. Some problems use a lot of slack binary variables for their QUBO/QUSO formulations. If this is the case, then the child class for this problem should override this method with a better bruteforce solver. But, for problems that do not use slack variables, this method will suffice. It converts the problem to QUBO, solves it withqubovert.utils.solve_qubo_bruteforce
, and then calls and returnsconvert_solution
.- Parameters
aruments (arguments and keyword arguments.) – Contains args and kwargs for the
to_qubo
method. Also contains aall_solutions
boolean flag, which indicates whether or not to return all the solutions, or just the best one found.all_solutions
defaults to False.- Returns
res – If
all_solutions
is False, thenres
is just the output of theconvert_solution
method. Ifall_solutions
is True, thenres
is a list of outputs of theconvert_solution
method, e.g. a converted solution for each solution that the bruteforce solver returns.- Return type
the output or outputs of the
convert_solution
method.
- to_pubo(*args, **kwargs)
to_pubo.
Since the model is already degree two,
self.to_pubo
will simply returnqubovert.utils.PUBOMatrix(self.to_qubo(*args, **kwargs))
.- Returns
P – The upper triangular PUBO matrix, a PUBOMatrix object. For most practical purposes, you can use PUBOMatrix in the same way as an ordinary dictionary. For more information, see
help(qubovert.utils.PUBOMatrix)
.- Return type
qubovert.utils.PUBOMatrix object.
- to_puso(*args, **kwargs)
to_puso.
Create and return PUSO model representing the problem. Should be implemented in child classes. If this method is not implemented in the child class, then it simply calls
to_pubo
orto_quso
and converts to a PUSO formulation.- Parameters
arguments (Defined in the child class.) – They should be parameters that define lagrange multipliers or factors in the QUSO model.
- Returns
H – For most practical purposes, you can use PUSOMatrix in the same way as an ordinary dictionary. For more information, see
help(qubovert.utils.PUSOMatrix)
.- Return type
qubovert.utils.PUSOMatrix object.
- Raises
RecursionError` if neither to_pubo nor to_puso are define –
in the subclass. –
- to_qubo(A=None, B=1)
to_qubo.
Create and return the BILP problem in QUBO form following section 3 of [Lucas]. The Q matrix for the QUBO will be returned as an uppertriangular dictionary. Thus, the problem becomes minimizing \(\sum_{i \leq j} x_i x_j Q_{ij}\).
A
andB
are parameters to enforce constraints.- Parameters
A (positive float (optional, defaults to None)) – A enforces the constraints. If
A
is None, then a default value will be chosen as given in section 3 of [Lucas].B (positive float that is less than A (optional, defaults to 1)) – See section 3 of [Lucas].
- Returns
Q – The upper triangular QUBO matrix, a QUBOMatrix object. For most practical purposes, you can use QUBOMatrix in the same way as an ordinary dictionary. For more information, see help(qubovert.utils.QUBOMatrix).
- Return type
qubovert.utils.QUBOMatrix object.
- to_quso(*args, **kwargs)
to_quso.
Create and return QUSO model representing the problem. Should be implemented in child classes. If this method is not implemented in the child class, then it simply calls
to_qubo
and converts the QUBO formulation to an QUSO formulation.- Parameters
arguments (Defined in the child class.) – They should be parameters that define lagrange multipliers or factors in the QUSO model.
- Returns
L – The upper triangular coupling matrix, where two element tuples represent couplings and one element tuples represent fields. For most practical purposes, you can use IsingCoupling in the same way as an ordinary dictionary. For more information, see
help(qubovert.utils.QUSOMatrix)
.- Return type
qubovert.utils.QUSOMatrix object.
- Raises
RecursionError` if neither to_qubo nor to_quso are define –
in the subclass. –