dimod.ConstrainedQuadraticModel.substitute_self_loops

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

Replace any integer 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

>>> 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': QuadraticModel({'i': 0.0, 'cf651f3d-bdf8-4735-9139-eee0a32e217f': 0.0}, {('cf651f3d-bdf8-4735-9139-eee0a32e217f', 'i'): 1.0}, 0.0, {'i': 'INTEGER', 'cf651f3d-bdf8-4735-9139-eee0a32e217f': 'INTEGER'}, dtype='float64') <= 3,
'cf651f3d-bdf8-4735-9139-eee0a32e217f': QuadraticModel({'i': 1.0, 'cf651f3d-bdf8-4735-9139-eee0a32e217f': -1.0}, {}, 0.0, {'i': 'INTEGER', 'cf651f3d-bdf8-4735-9139-eee0a32e217f': 'INTEGER'}, dtype='float64') == 0}