# Higher-Order Models¶

Sometimes it is nice to work with problems that are not restricted to quadratic interactions.

## Binary Polynomials¶

class BinaryPolynomial(poly, vartype)[source]

A polynomial with binary variables and real-valued coefficients.

Parameters: poly (mapping/iterable) – Polynomial as a mapping of form {term: bias, …}, where term is a collection of variables and bias the associated bias. It can also be an iterable of 2-tuples (term, bias). vartype (Vartype/str/set) – Variable type for the binary quadratic model. Accepted input values: Vartype.SPIN, 'SPIN', {-1, 1} Vartype.BINARY, 'BINARY', {0, 1}
degree

The degree of the polynomial.

Type: int
variables

The variables.

Type: set
vartype

One of Vartype.SPIN or Vartype.BINARY.

Type: Vartype

Examples

Binary polynomials can be constructed in many different ways. The following are all equivalent

>>> poly = dimod.BinaryPolynomial({'a': -1, 'ab': 1}, dimod.SPIN)
>>> poly = dimod.BinaryPolynomial({('a',): -1, ('a', 'b'): 1}, dimod.SPIN)
>>> poly = dimod.BinaryPolynomial([('a', -1), (('a', 'b'), 1)], dimod.SPIN)
>>> poly = dimod.BinaryPolynomial({'a': -1, 'ab': .5, 'ba': .5}, dimod.SPIN)


Binary polynomials act a mutable mappings but the terms can be accessed with any sequence.

>>> poly = dimod.BinaryPolynomial({'a': -1, 'ab': 1}, dimod.BINARY)
>>> poly['ab']
1
>>> poly['ba']
1
>>> poly[{'a', 'b'}]
1
>>> poly[('a', 'b')]
1
>>> poly['cd'] = 4
>>> poly['dc']
4


### Methods¶

 BinaryPolynomial.copy() Create a shallow copy. BinaryPolynomial.energies(samples_like[, dtype]) The energies of the given samples. BinaryPolynomial.energy(sample_like[, dtype]) The energy of the given sample. BinaryPolynomial.from_hising(h, J[, offset]) Construct a binary polynomial from a higher-order Ising problem. BinaryPolynomial.from_hubo(H[, offset]) Construct a binary polynomial from a higher-order unconstrained binary optimization (HUBO) problem. BinaryPolynomial.normalize([bias_range, …]) Normalizes the biases of the binary polynomial such that they fall in the provided range(s). BinaryPolynomial.relabel_variables(mapping) Relabel variables of a binary polynomial as specified by mapping. BinaryPolynomial.scale(scalar[, ignored_terms]) Multiply the polynomial by the given scalar. BinaryPolynomial.to_binary([copy]) Return a binary polynomial over {0, 1} variables. BinaryPolynomial.to_hising() Construct a higher-order Ising problem from a binary polynomial. BinaryPolynomial.to_hubo() Construct a higher-order unconstrained binary optimization (HUBO) problem from a binary polynomial. BinaryPolynomial.to_spin([copy]) Return a binary polynomial over {-1, +1} variables.

## Reducing to a Binary Quadratic Model¶

 make_quadratic(poly, strength[, vartype, bqm]) Create a binary quadratic model from a higher order polynomial.