dimod.ConstrainedQuadraticModel.substitute_self_loops

ConstrainedQuadraticModel.substitute_self_loops() Dict[Hashable, Hashable][source]

Replace any self-loops in the objective or constraints.

Self-loop \(i^2\) is removed by introducing a new variable \(j\) with interaction \(i*j\) and adding constraint \(j == i\).

Acts on the objective and constraints in-place.

Returns

Mapping from the integer variable labels to their introduced counterparts. The constraint enforcing \(j == i\) uses the same label.

Examples

Note that the ellipses in the outputs below are stand-ins for the generated variable labels, which differ between runs.

>>> from dimod import Integer, ConstrainedQuadraticModel
>>> i = Integer('i')
>>> cqm = ConstrainedQuadraticModel()
>>> cqm.add_constraint(i*i <=3, label='i squared')
'i squared'
>>> cqm.substitute_self_loops() 
{...}
>>> cqm.constraints 
{'i squared': Le(QuadraticModel({'i': 0.0, '...': 0.0}, {('...', 'i'): 1.0}, 0.0, {'i': 'INTEGER', '...': 'INTEGER'}, dtype='float64'), 3),
'...': Eq(QuadraticModel({'i': 1.0, '...': -1.0}, {}, 0.0, {'i': 'INTEGER', '...': 'INTEGER'}, dtype='float64'), 0)}