classmethod ConstrainedQuadraticModel.from_quadratic_model(qm: QuadraticModel | BinaryQuadraticModel) ConstrainedQuadraticModel[source]#

Construct a constrained quadratic model from a quadratic model or binary quadratic model.

The specified model is set as the objective to be minimzed in the constructed constrained quadratic model (CQM). You can then add constraints that feasible solutions should meet.


qm – Binary quadratic model (BQM) or quadratic model (QM).


This example creates a CQM to minimize a triangular problem with the added constraint that one of the variables must have value 1 in feasible solutions.

>>> from dimod import ConstrainedQuadraticModel, BinaryQuadraticModel
>>> bqm = BinaryQuadraticModel.from_ising({}, {'ab': 1, 'bc': 1, 'ac': 1})
>>> cqm = ConstrainedQuadraticModel().from_bqm(bqm)
>>> cqm.objective.linear
{'a': 0.0, 'b': 0.0, 'c': 0.0}
>>> cqm.objective.quadratic
{('b', 'a'): 1.0, ('c', 'a'): 1.0, ('c', 'b'): 1.0}
>>> label1 = cqm.add_constraint_from_model(BinaryQuadraticModel({'a': 0}, {}, 0, 'SPIN'), '>=', 0)