# 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:

degree[source]

Degree of the polynomial.

Type

int

variables[source]

Variables of the polynomial.

Type

set

vartype[source]

One of `SPIN` or `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¶

 Create a shallow copy. `BinaryPolynomial.energies`(samples_like[, dtype]) Energies of the given samples. `BinaryPolynomial.energy`(sample_like[, dtype]) 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, ...]) Normalize biases of the binary polynomial to fit the provided range(s). Relabel variables of a binary polynomial as specified by mapping. `BinaryPolynomial.scale`(scalar[, ignored_terms]) Multiply the polynomial by the given scalar. Return a binary polynomial over {0, 1} variables. Construct a higher-order Ising problem from a binary polynomial. Construct a higher-order unconstrained binary optimization (HUBO) problem from a binary polynomial. Return a binary polynomial over {-1, +1} variables.

## Functions¶

 `poly_energies`(samples_like, poly) Calculates energy of samples from a higher order polynomial. `poly_energy`(sample_like, poly) Calculate energy of a sample from a higher order polynomial.

## Reducing to a Binary Quadratic Model¶

 `make_quadratic`(poly, strength[, vartype, bqm]) Create a binary quadratic model from a higher order polynomial. `make_quadratic_cqm`(poly[, vartype, cqm]) Create a constrained quadratic model from a higher order polynomial. Reduce a binary polynomial to linear and quadratic terms, plus constraints.