Variables¶
D-Wave’s samplers mostly1 solve quadratic models of various sorts. Quadratic models are characterized by having one or two variables per term. A simple example of a quadratic model is,
\[D = Ax + By + Cxy\]
where \(A\), \(B\), and \(C\) are constants. Single variable terms—\(Ax\) and \(By\) here—are linear with the constant biasing the term’s variable. Two-variable terms—\(Cxy\) here—are quadratic with a relationship between the variables.
The variables in these models may be of the following types:
Binary: \(v_i \in\{-1,+1\} \text{ or } \{0,1\}\), represented by the dimod
Vartype
classesBINARY
andSPIN
.Typically used for applications that optimize over decisions that could either be true (or yes) or false (no); for example,
Should the antenna transmit or no?
Did a network node experience failure?
Discrete: for example a variable that can be assigned one of the values of the set
{red, green, blue, yellow}
, represented by the dimodVartype
classINTEGER
.Typically used for applications that optimize over several distinct options; for example,
Which shift should employee X work?
Should the state be colored red, blue, green or yellow?
Integer: represented by the dimod
Vartype
classINTEGER
.Typically used for applications that optimize the number of something; for example,
How many widgets should be loaded onto the truck?
Real: represented by the dimod
Vartype
classREAL
.Typically used for applications that optimize over an uncountable set; for example,
Where should the sensor be built?
- 1
Ocean also provides some higher-order tools for developing and testing your code; for example, the
ExactPolySolver
class.
Supported Models and Hybrid Samplers¶
D-Wave’s quantum computers solve binary quadratic models; Leap hybrid solvers can solver models with more varied variable types.
Variables |
Models |
Hybrid Samplers |
Examples |
---|---|---|---|
Binary |
|||
Binary, discrete |
|||
Binary, integer |
|||
Binary, integer, real |
Variable Representations and Labels¶
Ocean enables you to represent a variable with a quadratic model, as described in dimod’s symbolic math documentation. 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'])