# Quadratic Models: Unconstrained¶

Samplers such as D-Wave quantum computers accept unconstrained models (binary quadratic models, for D-Wave systems: binary because variables are represented by qubits that return two states and quadratic because you can configure coupling strengths between pairs of qubits). Hybrid quantum-classical samplers can accept non-binary models; for example, quadratic models with discrete variables.

When using such samplers to handle problems with constraints, you typically
formulate the constraints as penalties: see
Getting Started with D-Wave Solvers.
(Constrained models, such as the
`dimod.ConstrainedQuadraticModel`

, can support constraints natively.)

## Supported Models¶

**Quadratic Models**The quadratic model (QM) class,

`dimod.QuadraticModel`

, encodes polynomials of binary, integer, and discrete variables, with all terms of degree two or less.For the QM class, its attributes and methods, see Quadratic Models.

**Binary Quadratic Models**The binary quadratic model (BQM) class,

`dimod.BinaryQuadraticModel`

, encodes Ising and quadratic unconstrained binary optimization (QUBO) models used by samplers such as the D-Wave system.For an introduction to BQMs, see Binary Quadratic Models.

For the BQM class, its attributes and methods, see Binary Quadratic Models.

**Discrete Quadratic Models**The discrete quadratic model (BQM) class,

`dimod.DiscreteQuadraticModel`

, encodes polynomials of discrete variables, with all terms of degree two or less.For an introduction to DQMs, see Discrete Quadratic Models.

See examples of using QPU solvers and Leap hybrid solvers on these models in the dwave-examples GitHub repository.

## Model Construction¶

dimod provides a variety of model generators. These are especially useful for testing code and learning.

### Example: dimod BQM Generator¶

This example generates a BQM from a fully-connected graph (a clique) where all linear biases are zero and quadratic values are uniformly selected -1 or +1 values.

```
>>> bqm = dimod.generators.random.ran_r(1, 7, cls=dimod.AdjVectorBQM)
```

Typically you construct a model when reformulating your problem, using such techniques as those presented in D-Wave’s system documentation’s oceandocs:doc_handbook.

### Example: Formulating a Max-Cut Problem as a BQM¶

The small four-node maximum cut problem shown in this figure,

Can be represented, as shown in the dwave-examples Maximum Cut example, by a QUBO:

For learning and testing with small BQMs, constructing BQMs in Python is convenient:

```
>>> qubo = {(0, 0): -3, (1, 1): -1, (0, 1): 2, (2, 2): -1,
... (0, 2): 2, (3, 3): -1, (0, 3): 2}
>>> bqm = dimod.BQM.from_qubo(qubo)
```

For performance, especially with very large BQMs, you might read the data from a
file using methods,
such as `from_file()`

or from NumPy arrays.

### Example: Interaction Between Integer Variables¶

This example constructs a QM with an interaction between two integer variables.

```
>>> qm = dimod.QuadraticModel()
>>> qm.add_variables_from('INTEGER', ['i', 'j'])
>>> qm.add_quadratic('i', 'j', 1.5)
```