def add_interaction(self, u, v, bias, vartype=None):
        """Add an interaction and/or quadratic bias to a binary quadratic model.

        Args:
            v (variable):
                One of the pair of variables to add to the model. Can be any python object
                that is a valid dict key.

            u (variable):
                One of the pair of variables to add to the model. Can be any python object
                that is a valid dict key.

            bias (bias):
                Quadratic bias associated with u, v. If u, v is already in the model, this value
                is added to the current quadratic bias. Many methods and functions expect `bias` to
                be a number but this is not explicitly checked.

            vartype (:class:`.Vartype`, optional, default=None):
                Vartype of the given bias. If None, the vartype of the binary
                quadratic model is used. Valid values are :class:`.Vartype.SPIN` or
                :class:`.Vartype.BINARY`.

        Examples:
            This example creates an Ising model with two variables, adds a third,
            adds to the bias of the initial interaction, and creates
            a new interaction.

            >>> import dimod
            ...
            >>> bqm = dimod.BinaryQuadraticModel({0: 0.0, 1: 1.0}, {(0, 1): 0.5}, -0.5, dimod.SPIN)
            >>> len(bqm.quadratic)
            1
            >>> bqm.add_interaction(0, 2, 2)        # Add new variable 2
            >>> bqm.add_interaction(0, 1, .25)
            >>> bqm.add_interaction(1, 2, .25, vartype=dimod.BINARY)     # Binary value is converted to spin value
            >>> len(bqm.quadratic)
            3
            >>> bqm.quadratic[(0, 1)]
            0.75

        """
        if u == v:
            raise ValueError("no self-loops allowed, therefore ({}, {}) is not an allowed interaction".format(u, v))

        _adj = self._adj

        if vartype is not None and vartype is not self.vartype:
            if self.vartype is Vartype.SPIN and vartype is Vartype.BINARY:
                # convert from binary to spin
                bias /= 4

                self.add_offset(bias)
                self.add_variable(u, bias)
                self.add_variable(v, bias)

            elif self.vartype is Vartype.BINARY and vartype is Vartype.SPIN:
                # convert from spin to binary

                self.add_offset(bias)
                self.add_variable(u, -2 * bias)
                self.add_variable(v, -2 * bias)

                bias *= 4
            else:
                raise ValueError("unknown vartype")
        else:
            # so that they exist.
            if u not in self:
                _adj[u] = {}
            if v not in self:
                _adj[v] = {}

        if u in _adj[v]:
            _adj[u][v] = _adj[v][u] = _adj[u][v] + bias
        else:
            _adj[u][v] = _adj[v][u] = bias

        try:
            self._counterpart.add_interaction(u, v, bias, vartype=self.vartype)
        except AttributeError:
            pass
def add_interaction ( self , u , v , bias , vartype = None ) : if u == v : raise ValueError ( "no self-loops allowed, therefore ({}, {}) is not an allowed interaction" . format ( u , v ) ) _adj = self . _adj if vartype is not None and vartype is not self . vartype : if self . vartype is Vartype . SPIN and vartype is Vartype . BINARY : bias /= 4 self . add_offset ( bias ) self . add_variable ( u , bias ) self . add_variable ( v , bias ) elif self . vartype is Vartype . BINARY and vartype is Vartype . SPIN : self . add_offset ( bias ) self . add_variable ( u , - 2 * bias ) self . add_variable ( v , - 2 * bias ) bias *= 4 else : raise ValueError ( "unknown vartype" ) else : if u not in self : _adj [ u ] = { } if v not in self : _adj [ v ] = { } if u in _adj [ v ] : _adj [ u ] [ v ] = _adj [ v ] [ u ] = _adj [ u ] [ v ] + bias else : _adj [ u ] [ v ] = _adj [ v ] [ u ] = bias try : self . _counterpart . add_interaction ( u , v , bias , vartype = self . vartype ) except AttributeError : pass
Add an interaction and/or quadratic bias to a binary quadratic model.

        Args:
            v (variable):
                One of the pair of variables to add to the model. Can be any python object
                that is a valid dict key.

            u (variable):
                One of the pair of variables to add to the model. Can be any python object
                that is a valid dict key.

            bias (bias):
                Quadratic bias associated with u, v. If u, v is already in the model, this value
                is added to the current quadratic bias. Many methods and functions expect `bias` to
                be a number but this is not explicitly checked.

            vartype (:class:`.Vartype`, optional, default=None):
                Vartype of the given bias. If None, the vartype of the binary
                quadratic model is used. Valid values are :class:`.Vartype.SPIN` or
                :class:`.Vartype.BINARY`.

        Examples:
            This example creates an Ising model with two variables, adds a third,
            adds to the bias of the initial interaction, and creates
            a new interaction.

            >>> import dimod
            ...
            >>> bqm = dimod.BinaryQuadraticModel({0: 0.0, 1: 1.0}, {(0, 1): 0.5}, -0.5, dimod.SPIN)
            >>> len(bqm.quadratic)
            1
            >>> bqm.add_interaction(0, 2, 2)        # Add new variable 2
            >>> bqm.add_interaction(0, 1, .25)
            >>> bqm.add_interaction(1, 2, .25, vartype=dimod.BINARY)     # Binary value is converted to spin value
            >>> len(bqm.quadratic)
            3
            >>> bqm.quadratic[(0, 1)]
            0.75