dimod.BinaryQuadraticModel.contract_variables

BinaryQuadraticModel.contract_variables(u, v)[source]

Enforce u, v being the same variable in a binary quadratic model.

The resulting variable is labeled ‘u’. Values of interactions between v and variables that u interacts with are added to the corresponding interactions of u.

Parameters:
  • u (variable) – Variable in the binary quadratic model.
  • v (variable) – Variable in the binary quadratic model.

Examples

This example creates a binary quadratic model representing the K4 complete graph and contracts node (variable) 3 into node 2. The interactions between 3 and its neighbors 1 and 4 are added to the corresponding interactions between 2 and those same neighbors.

>>> import dimod
...
>>> linear = {1: 1, 2: 2, 3: 3, 4: 4}
>>> quadratic = {(1, 2): 12, (1, 3): 13, (1, 4): 14,
...              (2, 3): 23, (2, 4): 24,
...              (3, 4): 34}
>>> bqm = dimod.BinaryQuadraticModel(linear, quadratic, 0.5, dimod.SPIN)
>>> bqm.contract_variables(2, 3)
>>> 3 in bqm.linear
False
>>> bqm.quadratic[(1, 2)]
25