Variables#
This section mainly relates to the quadratic models submitted to Leap service solvers such as the hybrid CQM solver.[1] For the variables in nonlinear models, see an introduction in the Model Construction (Nonlinear Models) section and the reference documentation under the dwave-optimization section.
The variables in supported models may be of the following types.
Variable |
Usage |
Quadratic Model |
Nonlinear Model |
---|---|---|---|
Binary. \(v_i \in\{-1,+1\} \text{ or } \{0,1\}\). |
Typically used for applications that optimize over decisions that could either be true (or yes) or false (no); for example,
|
||
Discrete. For example, a variable that can be assigned one of the values of
the set |
Typically used for applications that optimize over several distinct options; for example,
|
||
Integer. |
Typically used for applications that optimize the number of something; for example,
|
||
Real. |
Typically used for applications that optimize over an uncountable set; for example,
|
Variable Representations and Labels#
Ocean software enables you to represent a variable with a quadratic model, as described in the Symbolic Math section. This makes it important to distinguish between such a variable’s representation and its label.
For example, in the code below, variables a, i, j
are represented by
QuadraticModel
objects and the ten variables in array x
by BinaryQuadraticModel
objects:
>>> a = dimod.Real("a")
>>> i, j = dimod.Integers(["i", "j"])
>>> x = dimod.BinaryArray([f"x{i}" for i in range(10)])
Each such variable is represented by a quadratic model that has a single linear bias of 1,
>>> x[0]
BinaryQuadraticModel({'x0': 1.0}, {}, 0.0, 'BINARY')
with its single variable having a specified label; e.g., x0
for the
first model in x
.
The code below adds two variables to a
ConstrainedQuadraticModel
. The first, using the
add_variable()
method, adds a variable by
specifying a label, "b"
, and the type of required variable, "REAL"
. The
second, using the
add_constraint_from_model()
method,
specifies the variable i
instantiated above as a
QuadraticModel
object.
>>> cqm = dimod.ConstrainedQuadraticModel()
>>> cqm.add_variable("b", "REAL")
'b'
>>> cqm.add_constraint_from_model(i, ">=", 2, "Min i")
'Min i'
>>> cqm.variables
Variables(['b', 'i'])