Problem parent class
Note that the problems or np modules will not be imported with from qubovert import *. You must import qubovert.problems explicitly. Thus this parent class is accessed with qubovert.problems.Problem.
- class qubovert.problems.Problem(*args, **kwargs)
Problem.
This acts a parent class to all the QUBO and QUSO conversion problem classes. The
__new__method keeps track of the problem args. Thereprmethod uses those input args, such thateval(repr(cls)) == cls. Finally, we define a__eq__method to determine if two problems are the same. The rest of the methods are to be implemented in child classes.- Subclasses must define at least one of the following methods:
to_qubo,to_quso.
If at least one of them is defined, then they will both work. The same is true for
to_puboandto_puso.Probleminherits fromConversions, for more details seehelp(qubovert.utils.Conversions)__new__.
Creates the object and keeps track of the input arguments and keyword arguments. Child classes should not change this. This method will be called before every __init__ is called. We use __new__ to keep track of input arguments instead of using __init__ so that child class implementations don’t have to worry about it. Ie child classes don’t have to call super().__init__(*args, **kwargs) in their __init__ method.
- Parameters
arguments (Defined in child classes.) –
- Returns
obj
- Return type
instance of the child class.
- convert_solution(solution, *args, **kwargs)
convert_solution.
Convert the solution to the QUBO to the solution to the problem. Should be implemented in child classes. If it is not implemented in the child class, then this function will by default return the same solution as what inputted.
- 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.) – spin indicates whether
solutionis 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 ifsolutioncontains all 1’s. In this case, it is unclear whethersolutioncame from a spin or boolean formulation of the problem, and we will figure it out based on thespinparameter.
- Returns
- Return type
Implemented in the child class.
- is_solution_valid(solution, *args, **kwargs)
is_solution_valid.
Returns whether or not the proposed solution is valid. Should be implemented in child classes. If it is not implemented in the child class, then this function will by default return True.
- Parameters
solution (iterable or dict.) – solution can be the output of
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.) – spin indicates whether
solutionis 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 ifsolutioncontains all 1’s. In this case, it is unclear whethersolutioncame from a spin or boolean formulation of the problem, and we will figure it out based on thespinparameter.
- 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/QUSO uses. Should be implemented in the child class.
- Returns
num – The number of variables in the QUBO/QUSO formulation.
- Return type
int.
- 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.Problemclass. 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_qubomethod. Also contains aall_solutionsboolean flag, which indicates whether or not to return all the solutions, or just the best one found.all_solutionsdefaults to False.- Returns
res – If
all_solutionsis False, thenresis just the output of theconvert_solutionmethod. Ifall_solutionsis True, thenresis a list of outputs of theconvert_solutionmethod, e.g. a converted solution for each solution that the bruteforce solver returns.- Return type
the output or outputs of the
convert_solutionmethod.
- to_pubo(*args, **kwargs)
to_pubo.
Since the model is already degree two,
self.to_pubowill 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_puboorto_qusoand 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(*args, **kwargs)
to_qubo.
Create and return upper triangular QUBO representing the problem. Should be implemented in child classes. If this method is not implemented in the child class, then it simply calls
to_qusoand converts the quso formulation to a QUBO formulation.- Parameters
arguments (Defined in the child class.) – They should be parameters that define lagrange multipliers or factors in the QUBO.
- 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.
- Raises
RecursionError` if neither to_qubo nor to_quso are define –
in the subclass. –
- 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_quboand 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. –