Symbols#
Symbols are a model’s decision variables, intermediate variables, constants, and mathematical operations.
See the Symbols section for an introduction to working with symbols.
All symbols listed in the Model Symbols
subsection below inherit from the Symbol class and, for most
mathematical symbols, the ArraySymbol class.
Symbol#
All symbols inherit from the Symbol class and therefore inherit its
methods.
- class Symbol[source]#
Base class for symbols.
Each symbol corresponds to a node in the directed acyclic graph representing the problem.
- equals(other)[source]#
Compare whether two symbols are identical.
- Parameters:
other – A symbol for comparison.
Equal symbols represent the same quantity in the model.
Note that comparing symbols across models is expensive.
See also
Symbol.maybe_equals(): an alternative for equality testing that can return false positives but is faster.
- has_state(index=0)[source]#
Return the initialization status of the indexed state.
- Parameters:
index – Index of the queried state.
- Returns:
True if the state is initialized.
- id()[source]#
Return the “identity” of the underlying node.
This identity is unique to the underlying node, rather than the identity of the Python object representing it. Therefore,
symdol.id()is not the same asid(symbol)!Examples
>>> from dwave.optimization import Model ... >>> model = Model() >>> a = model.binary() >>> aa, = model.iter_symbols() >>> assert a.id() == aa.id() >>> assert id(a) != id(aa)
While symbols are not hashable, the
.id()is.>>> model = Model() >>> x = model.integer() >>> seen = {x.id()}
See also
shares_memory():a.shares_memory(b)is equivalent toa.id() == b.id().equals():a.equals(b)will returnTrueifa.id() == b.id(). Though the inverse is not necessarily true.
- iter_predecessors()[source]#
Iterate over a symbol’s predecessors in the model.
Examples
This example constructs a \(b = \sum a\) model, where \(a\) is a multiplication of two symbols, and iterates over the predecessor’s of \(b\) (which is just \(a\)).
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer((2, 2), upper_bound=20) >>> c = model.constant([[21, 11], [10, 4]]) >>> a = c * i >>> b = a.sum() >>> a.equals(next(b.iter_predecessors())) True
- iter_successors()[source]#
Iterate over a symbol’s successors in the model.
Examples
This example constructs iterates over the successor symbols of a
DisjointListssymbol.>>> from dwave.optimization.model import Model >>> model = Model() >>> lsymbol, lsymbol_lists = model.disjoint_lists( ... primary_set_size=5, ... num_disjoint_lists=2) >>> lsymbol_lists[0].equals(next(lsymbol.iter_successors())) True
- maybe_equals(other)[source]#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- reset_state(index)[source]#
Reset the state of a symbol and any successor symbols.
- Parameters:
index – Index of the state to reset.
Examples
This example sets two states on a symbol with two successor symbols and resets just one state.
>>> from dwave.optimization import Model >>> model = Model() >>> lsymbol, lsymbol_lists = model.disjoint_lists(primary_set_size=5, num_disjoint_lists=2) >>> with model.lock(): ... model.states.resize(2) ... lsymbol.set_state(0, [[0, 4], [1, 2, 3]]) ... lsymbol.set_state(1, [[3, 4], [0, 1, 2]]) ... print(f"state 0: {lsymbol_lists[0].state(0)} and {lsymbol_lists[1].state(0)}") ... print(f"state 1: {lsymbol_lists[0].state(1)} and {lsymbol_lists[1].state(1)}") ... lsymbol.reset_state(0) ... print("After reset:") ... print(f"state 0: {lsymbol_lists[0].state(0)} and {lsymbol_lists[1].state(0)}") ... print(f"state 1: {lsymbol_lists[0].state(1)} and {lsymbol_lists[1].state(1)}") state 0: [0. 4.] and [1. 2. 3.] state 1: [3. 4.] and [0. 1. 2.] After reset: state 0: [0. 1. 2. 3. 4.] and [] state 1: [3. 4.] and [0. 1. 2.]
Determine if two symbols share memory.
- Parameters:
other – Another symbol.
- Returns:
True if the two symbols share memory.
- state_size()[source]#
Return an estimated size, in bytes, of a symbol’s state.
The number of bytes returned by this method is only an estimate. Some symbols hold additional information that is not accounted for.
For most symbols, which are arrays, this method is subclassed by the
state_sizemethod.See also
ArraySymbol.state_size()An estimate of the size of an array symbol’s state.Model.state_size()An estimate of the size of a model’s state.
- topological_index()[source]#
Topological index of the symbol.
Return
Noneif the model is not topologically sorted.Examples
This example prints the indices of a two-symbol model.
>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(100, lower_bound=20) >>> sum_i = i.sum() >>> with model.lock(): ... for symbol in model.iter_symbols(): ... print(f"Symbol {type(symbol)} is node {symbol.topological_index()}") Symbol <class 'dwave.optimization.symbols.IntegerVariable'> is node 0 Symbol <class 'dwave.optimization.symbols.Sum'> is node 1
ArraySymbol#
Most mathematical symbols inherit from the ArraySymbol class and
therefore inherit its methods.
- class ArraySymbol[source]#
Bases:
SymbolBase class for symbols that can be interpreted as an array.
- all()[source]#
Create an
Allsymbol.The new symbol returns True when all elements evaluate to True.
- any()[source]#
Create an
Anysymbol.The new symbol returns True when any elements evaluate to True.
Added in version 0.4.1.
- copy()[source]#
Return an array symbol that is a copy of the array.
See also
CopyEquivalent class.Added in version 0.5.1.
- flatten()[source]#
Return an array symbol collapsed into one dimension.
Equivalent to
symbol.reshape(-1).
- max(*, initial=None)[source]#
Create a
Maxsymbol.The new symbol returns the maximum value in its elements.
- Parameters:
initial –
The starting value for the product operation.
Added in version 0.6.4.
Examples
This example adds the minimum value of an integer decision variable to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(100, lower_bound=-50, upper_bound=50) >>> i_max = i.max() >>> type(i_max) <class 'dwave.optimization.symbols.Max'>
See also
- maybe_equals(other)[source]#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- min(*, initial=None)[source]#
Create a
Minsymbol.The new symbol returns the minimum value in its elements.
- Parameters:
initial –
The starting value for the product operation.
Added in version 0.6.4.
Examples
This example adds the minimum value of an integer decision variable to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(100, lower_bound=-50, upper_bound=50) >>> i_min = i.min() >>> type(i_min) <class 'dwave.optimization.symbols.Min'>
See also
- prod(*, axis=None, initial=None)[source]#
Create a
Prodsymbol.The new symbol returns the product of its elements.
- Parameters:
axis –
Axis along which the a product operation is performed.
Added in version 0.5.1.
initial –
The starting value for the product operation.
Added in version 0.6.4.
Examples
This example adds the product of an integer symbol’s elements to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(100, lower_bound=-50, upper_bound=50) >>> i.prod() <dwave.optimization.symbols.Prod at ...>
See also
- reshape(*shape)[source]#
Create a
Reshapesymbol.- Parameters:
shape – Shape of the created symbol.
The new symbol reshapes without changing the antecedent symbol’s data.
Examples
This example reshapes a column vector into a row vector.
>>> from dwave.optimization import Model >>> model = Model() >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> j.shape() (3,) >>> k = j.reshape((1, 3)) >>> k.shape() (1, 3)
- resize(shape, fill_value=None)[source]#
Return a new
Resizesymbol with the given shape.- Parameters:
shape – Shape of the new array. All dimension sizes must be non-negative.
fill_value – The value to be used if the resulting array is larger than the given one. Defaults to 0.
- Returns:
A
Resizesymbol.
Examples
>>> from dwave.optimization import Model ... >>> model = Model() >>> s = model.set(10) # subsets of range(10) >>> s_2x2 = s.resize((2, 2), fill_value=-1) ... >>> model.states.resize(1) >>> with model.lock(): ... s.set_state(0, [0, 1, 2]) ... print(s_2x2.state(0)) [[ 0. 1.] [ 2. -1.]]
Added in version 0.6.4.
- shape()[source]#
Return the shape of the symbol.
A dynamic array symbol returns a
-1in the first dimension.Examples
This example returns the shape of a newly instantiated symbol.
>>> from dwave.optimization import Model >>> model = Model() >>> x = model.binary(20) >>> x.shape() (20,) >>> s = model.set(20) >>> s.shape() (-1,)
- size()[source]#
Return the number of elements in the symbol.
If the symbol has a fixed size, returns that size as an integer. Otherwise, returns a
Sizesymbol.Examples
This example checks the size of a \(2 \times 3\) binary symbol.
>>> from dwave.optimization import Model >>> model = Model() >>> x = model.binary((2, 3)) >>> x.size() 6
- state(index=0, *, copy=True)[source]#
Return the state of the symbol.
- Parameters:
index – Index of the state.
copy – Currently only True is supported.
- Returns:
State as a
numpy.ndarray.
Examples
This example prints a symbol’s two states: initialized and uninitialized.
>>> from dwave.optimization import Model >>> model = Model() >>> x = model.binary((2, 3)) >>> z = x.sum() >>> with model.lock(): ... model.states.resize(2) ... x.set_state(0, [[0, 0, 1], [1, 0, 1]]) ... print(z.state(0)) ... print(z.state(1)) 3.0 0.0
- state_size()[source]#
Return an estimate of the size, in bytes, of an array symbol’s state.
For an array symbol, the estimate of the state size is exactly the number of bytes needed to encode the array.
Examples
This example returns the size of an integer symbol. In this example, the symbol encodes a \(5\times4\) of integers, each represented by a \(8\) byte float. Therefore the estimated state size is \(5*4*8 = 160\) bytes.
>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer((5, 4)) # 5x4 array of integers >>> i.state_size() # 5*4*8 bytes 160
See also
Symbol.state_size()An estimate of the size of a symbol’s state.Model.state_size()An estimate of the size of a model’s state.
- strides()[source]#
Return the stride length, in bytes, for traversing a symbol.
- Returns:
Tuple of the number of bytes to step in each dimension when traversing a symbol.
Examples
This example returns the size of an integer symbol.
>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer((2, 3), upper_bound=20) >>> i.strides() (24, 8)
- sum(*, axis=None, initial=None)[source]#
Create a
Sumsymbol.The new symbol returns the sum of its elements.
- Parameters:
axis –
Axis along which the a plus operation is performed.
Added in version 0.4.1.
initial –
The starting value for the plus operation.
Added in version 0.6.4.
Examples
This example adds the product of an integer symbol’s elements to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(100, lower_bound=-50, upper_bound=50) >>> i.sum() <dwave.optimization.symbols.Sum at ...>
See also
Model Symbols#
Each operation, decision, constant, mathematical function, and flow control is modeled using a symbol. The following symbols are available for modelling.
In general, symbols should be created using the methods inherited from
Symbol and ArraySymbol, rather than by the constructors
of the following classes.
- class ARange#
Bases:
ArraySymbolReturn evenly spaced integer values within a given interval.
See also
arange(): equivalent function.Added in version 0.5.2.
- class Absolute#
Bases:
ArraySymbolAbsolute value element-wise on a symbol.
See also
absolute(): equivalent function.
- class AccumulateZip#
Bases:
ArraySymbolUsing a supplied
Expression, perform an element-wise accumulate operation along one or more array operands. The accumulate operation (represented by theExpression) takes as input one value from each of the operand arrays, as well as the result of the previously computed operation, and computes a new value at the next output index.This takes inspiration from numpy.ufunc.accumulate but is different in that the accumulate operation can take an arbitrary number of arguments, instead of always two. These arguments come from “zipping” the supplied predecessor arrays together.
Thus if we are given an expression expr, predecessor arrays A, B, C, and an initial value init, this node is equivalent to the pseudocode:
r = [0] * len(A) t = init for args in zip(A, B, C): t = expr(t, *args) r[i] = t return r
- Parameters:
expression – An
Expressionrepresenting the accumulate operation. The first input on the expression will be given the previous output of the operation at each iteration over the values of the operands.operands – A list of the 1d-array symbols that will be the operands to the accumulate. There should be one fewer operands than inputs on the expression.
initial (optional) – A float representing the value used to start the accumulate. This will be used to set the last input of the expression on the very first iteration.
Added in version 0.6.4.
- class Add#
Bases:
ArraySymbolAddition element-wise of two symbols.
Examples
This example adds two integer symbols.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(10, lower_bound=-50, upper_bound=50) >>> j = model.integer(10, lower_bound=0, upper_bound=10) >>> k = i + j >>> type(k) <class 'dwave.optimization.symbols.Add'>
- class AdvancedIndexing#
Bases:
ArraySymbolAdvanced indexing.
Examples
This example uses advanced indexing to set a symbol’s values.
>>> from dwave.optimization.model import Model >>> model = Model() >>> prices = model.constant([i for i in range(20)]) >>> items = model.set(20) >>> values = prices[items] >>> type(values) <class 'dwave.optimization.symbols.AdvancedIndexing'>
- class All#
Bases:
ArraySymbolTests whether all elements evaluate to True.
Examples
This example checks all elements of a binary array.
>>> from dwave.optimization.model import Model >>> model = Model() >>> x = model.binary((20, 30)) >>> all_x = x.all() >>> type(all_x) <class 'dwave.optimization.symbols.All'>
- class And#
Bases:
ArraySymbolBoolean AND element-wise between two symbols.
See also
logical_and(): equivalent function.
- class Any#
Bases:
ArraySymbolTests whether any elements evaluate to True.
Examples
This example checks the elements of a binary array.
>>> from dwave.optimization.model import Model >>> model = Model() >>> model.states.resize(1) >>> x = model.constant([True, False, False]) >>> a = x.any() >>> with model.lock(): ... assert a.state()
>>> y = model.constant([False, False, False]) >>> b = y.any() >>> with model.lock(): ... assert not b.state()
Added in version 0.4.1.
- class ArgSort#
Bases:
ArraySymbolReturn an ordering of the indices that would sort (flattened) values of the given symbol. Note that while it will return an array with identical shape to the given symbol, the returned indices will always be indices on flattened array, similar to
numpy.argsort(a, axis=None).Always performs a index-wise stable sort such that the relative order of values is maintained in the returned order.
See also
argsort(): equivalent method.Added in version 0.6.4.
- class BSpline#
Bases:
ArraySymbolBspline node that takes in an array pointer, an integer degree and two vectors for knots and coefficients.
Examples
This example creates a BSpline symbol.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import bspline >>> model = Model() >>> x = model.integer(lower_bound=3, upper_bound=4) >>> k = 2 >>> t = [0, 1, 2, 3, 4, 5, 6] >>> c = [-1, 2, 0, -1] >>> bspline_node = bspline(x, k, t, c) >>> type(bspline_node) <class 'dwave.optimization.symbols.BSpline'>
- class BasicIndexing#
Bases:
ArraySymbolBasic indexing.
Examples
This example uses basic indexing to set a symbol’s values.
>>> from dwave.optimization.model import Model >>> model = Model() >>> prices = model.constant([i for i in range(20)]) >>> low_prices = prices[:10] >>> type(low_prices) <class 'dwave.optimization.symbols.BasicIndexing'>
- class BinaryVariable#
Bases:
ArraySymbolBinary decision-variable symbol.
Examples
This example adds a binary variable to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> x = model.binary((20, 30)) >>> type(x) <class 'dwave.optimization.symbols.BinaryVariable'>
- set_state(index, state)#
Set the state of the binary symbol.
The given state must be binary array with the same shape as the symbol.
- Parameters:
index – Index of the state to set
state – Assignment of values for the state.
Examples
This example sets two states for a \(2 \times 3\)-sized binary symbol.
>>> from dwave.optimization.model import Model >>> model = Model() >>> x = model.binary((2, 3)) >>> model.states.resize(2) >>> x.set_state(0, [[True, True, False], [False, True, False]]) >>> x.set_state(1, [[False, True, False], [False, True, False]])
- class Concatenate#
Bases:
ArraySymbolConcatenate symbol.
See also
concatenate()equivalent function.Added in version 0.4.3.
- class Constant#
Bases:
ArraySymbolConstant symbol.
See also
constant(): equivalent method.- maybe_equals(other)#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- state(index=0, *, copy=True)#
Return the state of the constant symbol.
- Parameters:
index – Index of the state.
copy – Copy the state. Currently only
Trueis supported.
- Returns:
A copy of the state.
- class Copy#
Bases:
ArraySymbolAn array symbol that is a copy of another array symbol.
See also
ArraySymbol.copy()Equivalent method.Added in version 0.5.1.
- class DisjointBitSet#
Bases:
ArraySymbolDisjoint-sets successor symbol.
Examples
This example adds a disjoint-sets symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> s = model.disjoint_bit_sets(primary_set_size=100, num_disjoint_sets=5) >>> type(s[1][0]) <class 'dwave.optimization.symbols.DisjointBitSet'>
- set_index()#
Return the index for the set.
- class DisjointBitSets#
Bases:
SymbolDisjoint-sets decision-variable symbol.
Examples
This example adds a disjoint-sets symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> s = model.disjoint_bit_sets(primary_set_size=100, num_disjoint_sets=5) >>> type(s[0]) <class 'dwave.optimization.symbols.DisjointBitSets'>
- num_disjoint_sets()#
Return the number of disjoint sets in the symbol.
- set_state(index, state)#
Set the state of the disjoint-sets symbol.
The given state must be a partition of
range(primary_set_size)intonum_disjoint_sets()partitions, encoded as a 2Dnum_disjoint_sets\(\times\)primary_set_sizeBoolean array.- Parameters:
index – Index of the state to set
state – Assignment of values for the state.
- class DisjointList#
Bases:
ArraySymbolDisjoint-lists successor symbol.
Examples
This example adds a disjoint-lists symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> l = model.disjoint_lists(primary_set_size=10, num_disjoint_lists=2) >>> type(l[1][0]) <class 'dwave.optimization.symbols.DisjointList'>
- list_index()#
Return the index for the list.
- class DisjointLists#
Bases:
SymbolDisjoint-lists decision-variable symbol.
Examples
This example adds a disjoint-lists symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> l = model.disjoint_lists(primary_set_size=10, num_disjoint_lists=2) >>> type(l[0]) <class 'dwave.optimization.symbols.DisjointLists'>
- num_disjoint_lists()#
Return the number of disjoint lists in the symbol.
- set_state(index, state)#
Set the state of the disjoint-lists symbol.
The given state must be a partition of
range(primary_set_size)intonum_disjoint_lists()partitions as a list of lists.- Parameters:
index – Index of the state to set
state – Assignment of values for the state.
- class Divide#
Bases:
ArraySymbolDivision element-wise between two symbols.
Examples
This example divides two integer symbols.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(10, lower_bound=-50, upper_bound=-1) >>> j = model.integer(10, lower_bound=1, upper_bound=10) >>> k = i/j >>> type(k) <class 'dwave.optimization.symbols.Divide'>
- class Equal#
Bases:
ArraySymbolEquality comparison element-wise between two symbols.
Examples
This example creates an equality operation between integer symbols.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(25, upper_bound=100) >>> j = model.integer(25, lower_bound=-100) >>> k = i == j >>> type(k) <class 'dwave.optimization.symbols.Equal'>
- class Exp#
Bases:
ArraySymbolTakes the values of a symbol and returns the corresponding base-e exponential.
See also
exp(): equivalent function.Added in version 0.6.2.
- class Expit#
Bases:
ArraySymbolTakes the values of a symbol and returns the corresponding logistic sigmoid (expit).
See also
expit(): equivalent function.Added in version 0.5.2.
- class Extract#
Bases:
ArraySymbolReturn elements chosen from x or y depending on condition.
See also
where(): equivalent function.
- class Input#
Bases:
ArraySymbolAn input symbol. Functions as a “placeholder” in a model.
- integral()#
Whether the input symbol will always output integers.
- lower_bound()#
Lowest value allowed to the input.
- set_state(index, state)#
Set the state of the input symbol.
The given state must be the same shape as the input symbol’s shape.
- upper_bound()#
Largest value allowed to the input.
- class IntegerVariable#
Bases:
ArraySymbolInteger decision-variable symbol.
Examples
This example adds an integer symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(25, upper_bound=100) >>> type(i) <class 'dwave.optimization.symbols.IntegerVariable'>
- lower_bound()#
The lowest value allowed for the integer symbol.
- set_state(index, state)#
Set the state of the integer node.
The given state must be integer array of the integer node shape.
- upper_bound()#
The highest value allowed for the integer symbol.
- class LessEqual#
Bases:
ArraySymbolSmaller-or-equal comparison element-wise between two symbols.
Examples
This example creates an inequality operation between integer symbols.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(25, upper_bound=100) >>> j = model.integer(25, lower_bound=-100) >>> k = i <= j >>> type(k) <class 'dwave.optimization.symbols.LessEqual'>
- class LinearProgram#
Bases:
SymbolFind a solution to the linear program (LP) defined by the predecessors.
See also
Added in version 0.6.0.
- state(index=0)#
Return the current solution to the LP.
If the LP is not feasible, the solution is not meaningful.
- class LinearProgramFeasible#
Bases:
ArraySymbolReturn whether the parent LP symbol’s current solution is feasible.
See also
Added in version 0.6.0.
- class LinearProgramObjectiveValue#
Bases:
ArraySymbolReturn the objective value of the parent LP symbol’s current solution.
See also
Added in version 0.6.0.
- class LinearProgramSolution#
Bases:
ArraySymbolReturn the current solution of the parent LP symbol as an array.
See also
Added in version 0.6.0.
- class ListVariable#
Bases:
ArraySymbolList decision-variable symbol.
Examples
This example adds a list symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> l = model.list(10) >>> type(l) <class 'dwave.optimization.symbols.ListVariable'>
- set_state(index, values)#
Set the state of the list node.
The given values must be a sub-permuation of
range(n)wherenis the size of the list.
- class Log#
Bases:
ArraySymbolTakes the values of a symbol and returns the corresponding natural logarithm (log).
See also
log(): equivalent function.Added in version 0.5.2.
- class Logical#
Bases:
ArraySymbolLogical truth value element-wise on a symbol.
See also
logical(): equivalent function.
- class Max#
Bases:
ArraySymbolMaximum value in the elements of a symbol.
See also
max()equivalent method.- initial#
The initial value to the operation. Returns
Noneif not provided.Added in version 0.6.4.
- maybe_equals(other)#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- class Maximum#
Bases:
ArraySymbolMaximum values in an element-wise comparison of two symbols.
Examples
This example sets a symbol’s values to the maximum values of two integer decision variables.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import maximum ... >>> model = Model() >>> i = model.integer(100, lower_bound=-50, upper_bound=50) >>> j = model.integer(100, lower_bound=-20, upper_bound=150) >>> k = maximum(i, j) >>> type(k) <class 'dwave.optimization.symbols.Maximum'>
- class Mean#
Bases:
ArraySymbol- Mean value of the elements of a symbol. If symbol is empty,
mean defaults to 0.0.
Examples
This example takes the mean of one symbol.
>>> from dwave.optimization import Model >>> from dwave.optimization.mathematical import mean ... >>> model = Model() >>> i = model.integer(4) >>> m = mean(i) >>> type(m) <class 'dwave.optimization.symbols.Mean'>
See also
mean(): equivalent method.Added in version 0.6.4.
- class Min#
Bases:
ArraySymbolMinimum value in the elements of a symbol.
See also
min()equivalent method.- initial#
The initial value to the operation. Returns
Noneif not provided.Added in version 0.6.4.
- maybe_equals(other)#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- class Minimum#
Bases:
ArraySymbolMinimum values in an element-wise comparison of two symbols.
Examples
This example sets a symbol’s values to the minimum values of two integer decision variables.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import minimum ... >>> model = Model() >>> i = model.integer(100, lower_bound=-50, upper_bound=50) >>> j = model.integer(100, lower_bound=-20, upper_bound=150) >>> k = minimum(i, j) >>> type(k) <class 'dwave.optimization.symbols.Minimum'>
- class Modulus#
Bases:
ArraySymbolModulus element-wise between two symbols.
Examples
This example calculates the modulus of two integer symbols.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(10, lower_bound=-50, upper_bound=50) >>> j = model.integer(10, lower_bound=-20, upper_bound=150) >>> k = i % j >>> type(k) <class 'dwave.optimization.symbols.Modulus'>
- class Multiply#
Bases:
ArraySymbolMultiplication element-wise between two symbols.
Examples
This example multiplies two integer symbols.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(10, lower_bound=-50, upper_bound=50) >>> j = model.integer(10, lower_bound=0, upper_bound=10) >>> k = i*j >>> type(k) <class 'dwave.optimization.symbols.Multiply'>
- class NaryAdd#
Bases:
ArraySymbolAddition element-wise of N symbols.
Examples
This example add three integer symbols.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import add ... >>> model = Model() >>> i = model.integer((10, 10), lower_bound=-50, upper_bound=50) >>> j = model.integer((10, 10), lower_bound=-20, upper_bound=150) >>> k = model.integer((10, 10), lower_bound=0, upper_bound=100) >>> l = add(i, j, k) >>> type(l) <class 'dwave.optimization.symbols.NaryAdd'>
- class NaryMaximum#
Bases:
ArraySymbolMaximum values in an element-wise comparison of N symbols.
Examples
This example sets a symbol’s values to the maximum values of three integer decision variables.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import maximum ... >>> model = Model() >>> i = model.integer((10, 10), lower_bound=-50, upper_bound=50) >>> j = model.integer((10, 10), lower_bound=-20, upper_bound=150) >>> k = model.integer((10, 10), lower_bound=0, upper_bound=100) >>> l = maximum(i, j, k) >>> type(l) <class 'dwave.optimization.symbols.NaryMaximum'>
- class NaryMinimum#
Bases:
ArraySymbolMinimum values in an element-wise comparison of N symbols.
Examples
This example sets a symbol’s values to the minimum values of three integer decision variables.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import minimum ... >>> model = Model() >>> i = model.integer((10, 10), lower_bound=-50, upper_bound=50) >>> j = model.integer((10, 10), lower_bound=-20, upper_bound=150) >>> k = model.integer((10, 10), lower_bound=0, upper_bound=100) >>> l = minimum(i, j, k) >>> type(l) <class 'dwave.optimization.symbols.NaryMinimum'>
- class NaryMultiply#
Bases:
ArraySymbolMultiplication element-wise between N symbols.
Examples
This example multiplies three integer decision variables.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import multiply ... >>> model = Model() >>> i = model.integer((10, 10), lower_bound=-50, upper_bound=50) >>> j = model.integer((10, 10), lower_bound=-20, upper_bound=150) >>> k = model.integer((10, 10), lower_bound=0, upper_bound=100) >>> l = multiply(i, j, k) >>> type(l) <class 'dwave.optimization.symbols.NaryMultiply'>
- class Negative#
Bases:
ArraySymbolNumerical negative element-wise on a symbol.
Examples
This example add the negative of an integer array.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(100, upper_bound=50) >>> i_minus = -i >>> type(i_minus) <class 'dwave.optimization.symbols.Negative'>
- class Not#
Bases:
ArraySymbolLogical negation element-wise on a symbol.
See also
logical_not(): equivalent function.
- class Or#
Bases:
ArraySymbolBoolean OR element-wise between two symbols.
See also
logical_or(): equivalent function.
- class PartialProd#
Bases:
ArraySymbolMultiply of the elements of a symbol along an axis.
See also
prod()equivalent method.Added in version 0.5.1.
- initial#
The initial value to the operation. Returns
Noneif not provided.Added in version 0.6.4.
- maybe_equals(other)#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- class PartialSum#
Bases:
ArraySymbolSum of the elements of a symbol along an axis.
See also
sum()equivalent method.Added in version 0.4.1.
- initial#
The initial value to the operation. Returns
Noneif not provided.Added in version 0.6.4.
- maybe_equals(other)#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- class Permutation#
Bases:
ArraySymbolPermutation of the elements of a symbol.
Examples
This example creates a permutation of a constant symbol.
>>> from dwave.optimization.model import Model >>> model = Model() >>> C = model.constant([[1, 2, 3], [2, 3, 1], [0, 1, 0]]) >>> l = model.list(3) >>> p = C[l, :][:, l] >>> type(p) <class 'dwave.optimization.symbols.Permutation'>
- class Prod#
Bases:
ArraySymbolProduct of the elements of a symbol.
See also
prod()equivalent method.- initial#
The initial value to the operation. Returns
Noneif not provided.Added in version 0.6.4.
- maybe_equals(other)#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- class Put#
Bases:
ArraySymbolA symbol that replaces the specified elements in an array with given values.
See also
put(): equivalent function.Added in version 0.4.4.
- class QuadraticModel#
Bases:
ArraySymbolQuadratic model.
Examples
This example adds a quadratic model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> x = model.binary(3) >>> Q = {(0, 0): 0, (0, 1): 1, (0, 2): 2, (1, 1): 1, (1, 2): 3, (2, 2): 2} >>> qm = model.quadratic_model(x, Q) >>> type(qm) <class 'dwave.optimization.symbols.QuadraticModel'>
- get_linear(v)#
Get the linear bias of v
- get_quadratic(u, v)#
Get the quadratic bias of u and v. Returns 0 if not present.
- num_interactions()#
The number of quadratic interactions in the quadratic model
- num_variables()#
The number of variables in the quadratic model.
- class Reshape#
Bases:
ArraySymbolReshaped symbol.
Examples
This example adds a reshaped binary symbol.
>>> from dwave.optimization.model import Model >>> model = Model() >>> x = model.binary((2, 3)) >>> x_t = x.reshape((3, 2)) >>> type(x_t) <class 'dwave.optimization.symbols.Reshape'>
- class Resize#
Bases:
ArraySymbolResize symbol.
Added in version 0.6.4.
- class Rint#
Bases:
ArraySymbolTakes the values of a symbol and rounds them to the nearest integer.
Examples
This example adds the round-int of a decision variable to a model.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import rint >>> model = Model() >>> i = model.constant(10.4) >>> ii = rint(i) >>> type(ii) <class 'dwave.optimization.symbols.Rint'>
- class SafeDivide#
Bases:
ArraySymbolSafe division element-wise between two symbols.
See also
safe_divide(): equivalent function.Added in version 0.6.2.
- class SetVariable#
Bases:
ArraySymbolSet decision-variable symbol.
A set variable’s possible states are the subsets of
range(n).- Parameters:
model – The model.
n – The possible states of the set variable are the subsets of
range(n).min_size – The minimum set size.
max_size – The maximum set size.
Examples
This example adds a set symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> s = model.set(10) >>> type(s) <class 'dwave.optimization.symbols.SetVariable'>
- set_state(index, values)#
Set the state of the set node.
The given state must be a subset of
range(n)wherenis the size of the set.
- class Size#
Bases:
ArraySymbol
- class Square#
Bases:
ArraySymbolSquares element-wise of a symbol.
Examples
This example adds the squares of an integer decision variable to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(10, lower_bound=-5, upper_bound=5) >>> ii = i**2 >>> type(ii) <class 'dwave.optimization.symbols.Square'>
- class SquareRoot#
Bases:
ArraySymbolSquare root of a symbol.
Examples
This example adds the square root of an integer decision variable to a model.
>>> from dwave.optimization.model import Model >>> from dwave.optimization.mathematical import sqrt >>> model = Model() >>> i = model.constant(10) >>> ii = sqrt(i) >>> type(ii) <class 'dwave.optimization.symbols.SquareRoot'>
- class Subtract#
Bases:
ArraySymbolSubtraction element-wise of two symbols.
Examples
This example subtracts two integer symbols.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(10, lower_bound=-50, upper_bound=50) >>> j = model.integer(10, lower_bound=0, upper_bound=10) >>> k = i - j >>> type(k) <class 'dwave.optimization.symbols.Subtract'>
- class Sum#
Bases:
ArraySymbolSum of the elements of a symbol.
See also
sum()equivalent method.- initial#
The initial value to the operation. Returns
Noneif not provided.Added in version 0.6.4.
- maybe_equals(other)#
Compare to another symbol.
This method exists because a complete equality test can be expensive.
- Parameters:
other – Another symbol in the model’s directed acyclic graph.
- Returns: integer
Supported return values are:
0—Not equal (with certainty)1—Might be equal (no guarantees); a complete equality test is necessary2—Are equal (with certainty)
Examples
This example compares
IntegerVariablesymbols of different sizes.>>> from dwave.optimization import Model >>> model = Model() >>> i = model.integer(3, lower_bound=0, upper_bound=20) >>> j = model.integer(3, lower_bound=-10, upper_bound=10) >>> k = model.integer(5, upper_bound=55) >>> i.maybe_equals(j) 1 >>> i.maybe_equals(k) 0
See also
equals(): a more expensive form of equality testing.
- class Where#
Bases:
ArraySymbolReturn elements chosen from x or y depending on condition.
See also
where(): equivalent function.
- class Xor#
Bases:
ArraySymbolBoolean XOR element-wise between two symbols.