ConstrainedQuadraticInstance#
- class iqm.applications.qubo.ConstrainedQuadraticInstance(cqm, penalty=1.0)[source]#
Bases:
ProblemInstance
A class for constrainted quadratic binary problems.
The class saves the problem as a
ConstrainedQuadraticModel
and uses this object for its various methods. When the problem needs to be transformed into a QUBO, a private method_recalculate_bqm()
is used to return aBinaryQuadraticModel
formulation of the problem, making the constraints soft and penalizing their breaking withpenalty
.- Parameters:
cqm (ConstrainedQuadraticModel) – The problem encoded as a
ConstrainedQuadraticModel
, passed over from a subclass.penalty (float) – The numerical penalty incurred by violating each constraint of the problem, to be used when the problem is transformed into a BQM.
Attributes
The BQM representation of the problem, penalizing constraint violation.
The
ConstrainedQuadraticModel
representation of the problem instance.The dimension of the problem (i.e., the number of binary variables).
The penalty for breaking the constraints.
The QUBO graph of the problem instance.
The QUBO matrix of the problem instance.
Methods
_recalculate_bqm
()constraints_checker
(bit_str)Checks whether the constrains of the problem are satisfied.
fix_constraint_violation
(counts)Post-processing which takes a dictionary of counts and changes the bitstrings in it in some minimal way so that they all satisfy the constraints.
fix_constraint_violation_bitstring
(bit_str)Post-processing which takes a solution and changes it in some minimal way so that it satisfies the constraints.
fix_variables
(variables)Fixes (assigns) some of the problem variables.
initialize_properties
([max_size])The initialization method for upper/lower bound, average/best quality.
loss
(bit_str)The loss function calculated for a given solution.
quality
(bit_str)The quality function overridden for constrainted problems.
satisfy_constraints
(counts)Post-processing method that takes a dictionary and removes the bitstrings which don't satisfy the constraints.
- property cqm: ConstrainedQuadraticModel#
The
ConstrainedQuadraticModel
representation of the problem instance.
- property bqm: BinaryQuadraticModel#
The BQM representation of the problem, penalizing constraint violation.
The problem represented as
BinaryQuadraticModel
. This is calculated using_recalculate_bqm()
by inserting the constraints as penalties to the cost function.
- fix_variables(variables)[source]#
Fixes (assigns) some of the problem variables.
This method is not implemented in
ConstrainedQuadraticInstance
because in the general case, fixing one variable might have implications for the other variables (implied by the constraints). Therefore, this method needs to be implemented in a subclass ofConstrainedQuadraticInstance
, if it’s needed.
- property qubo_matrix: ndarray#
The QUBO matrix of the problem instance.
The matrix is obtained from the internal attribute
bqm
.The i,i diagonal entry corresponds to the local field acting on the i-th variable.
The i,j entry above the diagonal corresponds to the interaction between the i-th and j-th variables.
The entries below the diagonal are empty.
- property qubo_graph: Graph#
The QUBO graph of the problem instance.
The nodes / edges of the graph have a
bias
parameter containing the local field / interaction strength of the corresponding variable(s). Variables without interaction aren’t connected by edges in the graph.
- loss(bit_str)[source]#
The loss function calculated for a given solution.
It is equivalent to the quality of the solution, but with extra penalties for breaking the constraints.
- quality(bit_str)[source]#
The quality function overridden for constrainted problems.
For solutions violating the constraints, the “quality” isn’t defined. If a user asks for the quality of the solution, this function first checks if the constraints are satisfied, prints out a warning if they aren’t, and then returns the loss function.
- initialize_properties(max_size=30)[source]#
The initialization method for upper/lower bound, average/best quality.
This is the method from the parent class
ProblemInstance
, overridden so that the bruteforce search only includes solutions which satisfy the constraints.- Parameters:
max_size (int | None) – The maximum size of problems for which the properties may be calculated.
- Raises:
ValueError – If
initialize_properties()
was called on aConstrainedQuadraticInstance
object with dimension larger thanmax_size
.- Return type:
None
- fix_constraint_violation(counts)[source]#
Post-processing which takes a dictionary of counts and changes the bitstrings in it in some minimal way so that they all satisfy the constraints.
Iterates through the dictionary
counts
and for each key callsfix_constraint_violation_bitstring()
. In case that multiple bitstrings get mapped to the same bitstring, their respective frequencies (values) are added.
- fix_constraint_violation_bitstring(bit_str)[source]#
Post-processing which takes a solution and changes it in some minimal way so that it satisfies the constraints.
This is not possible to do generally, so this method is not implemented for :class:ConstrainedQuadraticInstance` and it needs to be defined for the individual subclasses of
ConstrainedQuadraticInstance
(if it’s possible to do). If the input bitstringbit_str
already satisfies the problem constraints, it should be returned unchanged.