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 returnTrue
ifa.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
DisjointLists
symbol.>>> 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
IntegerVariable
symbols 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_size
method.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
None
if 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:
Symbol
Base class for symbols that can be interpreted as an array.
- all()[source]#
Create an
All
symbol.The new symbol returns True when all elements evaluate to True.
- any()[source]#
Create an
Any
symbol.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
Copy
Equivalent class.Added in version 0.5.1.
- flatten()[source]#
Return an array symbol collapsed into one dimension.
Equivalent to
symbol.reshape(-1)
.
- 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
IntegerVariable
symbols 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.
- prod(axis=None)[source]#
Create a
Prod
symbol.The new symbol returns the product of its elements.
Added in version 0.5.1: The
axis
keyword argument was added in version 0.5.1.
- reshape(*shape)[source]#
Create a
Reshape
symbol.- 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)
- shape()[source]#
Return the shape of the symbol.
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,)
- 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
Size
symbol.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)
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:
ArraySymbol
Return evenly spaced integer values within a given interval.
See also
arange()
: equivalent function.Added in version 0.5.2.
- class Absolute#
Bases:
ArraySymbol
Absolute value element-wise on a symbol.
Examples
This example adds the absolute value of an integer decision variable to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> i = model.integer(1, lower_bound=-50, upper_bound=50) >>> i_abs = abs(i) >>> type(i_abs) <class 'dwave.optimization.symbols.Absolute'>
- class Add#
Bases:
ArraySymbol
Addition 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:
ArraySymbol
Advanced 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:
ArraySymbol
Tests 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:
ArraySymbol
Boolean AND element-wise between two symbols.
See also
logical_and()
: equivalent function.
- class Any#
Bases:
ArraySymbol
Tests 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 BasicIndexing#
Bases:
ArraySymbol
Basic 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:
ArraySymbol
Binary 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:
ArraySymbol
Concatenate symbol.
See also
concatenate()
equivalent function.Added in version 0.4.3.
- class Constant#
Bases:
ArraySymbol
Constant symbol.
Examples
This example adds a constant symbol to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> a = model.constant(20) >>> type(a) <class 'dwave.optimization.symbols.Constant'>
- 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
IntegerVariable
symbols 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
True
is supported.
- Returns:
A copy of the state.
- class Copy#
Bases:
ArraySymbol
An 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:
ArraySymbol
Disjoint-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:
Symbol
Disjoint-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_size
Boolean array.- Parameters:
index – Index of the state to set
state – Assignment of values for the state.
- class DisjointList#
Bases:
ArraySymbol
Disjoint-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:
Symbol
Disjoint-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:
ArraySymbol
Division 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:
ArraySymbol
Equality 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 Expit#
Bases:
ArraySymbol
Takes the values of a symbol and returns the corresponding logistic sigmoid (expit).
See also
expit()
: equivalent function.Added in version 0.5.2.
- class IntegerVariable#
Bases:
ArraySymbol
Integer 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:
ArraySymbol
Smaller-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:
Symbol
Find 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:
ArraySymbol
Return whether the parent LP symbol’s current solution is feasible.
See also
Added in version 0.6.0.
- class LinearProgramObjectiveValue#
Bases:
ArraySymbol
Return the objective value of the parent LP symbol’s current solution.
See also
Added in version 0.6.0.
- class LinearProgramSolution#
Bases:
ArraySymbol
Return the current solution of the parent LP symbol as an array.
See also
Added in version 0.6.0.
- class ListVariable#
Bases:
ArraySymbol
List 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, state)#
Set the state of the list node.
The given state must be a permuation of
range(len(state))
.
- class Log#
Bases:
ArraySymbol
Takes 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:
ArraySymbol
Logical truth value element-wise on a symbol.
See also
logical()
: equivalent function.
- class Max#
Bases:
ArraySymbol
Maximum value in the elements of a symbol.
Examples
This example adds the maximum 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'>
- class Maximum#
Bases:
ArraySymbol
Maximum 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 Min#
Bases:
ArraySymbol
Minimum value in the elements of a symbol.
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'>
- class Minimum#
Bases:
ArraySymbol
Minimum 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:
ArraySymbol
Modulus 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:
ArraySymbol
Multiplication 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:
ArraySymbol
Addition 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:
ArraySymbol
Maximum 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:
ArraySymbol
Minimum 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:
ArraySymbol
Multiplication 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:
ArraySymbol
Numerical 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:
ArraySymbol
Logical negation element-wise on a symbol.
See also
logical_not()
: equivalent function.
- class Or#
Bases:
ArraySymbol
Boolean OR element-wise between two symbols.
See also
logical_or()
: equivalent function.
- class PartialProd#
Bases:
ArraySymbol
Multiply of the elements of a symbol along an axis.
See also
ArraySymbol.prod()
Added in version 0.5.1.
- class PartialSum#
Bases:
ArraySymbol
Sum of the elements of a symbol along an axis.
Examples
This example adds the sum of a binary symbol along an axis to a model.
>>> from dwave.optimization.model import Model >>> model = Model() >>> x = model.binary((10, 5)) >>> x_sum_0 = x.sum(axis=0) >>> type(x_sum_0) <class 'dwave.optimization.symbols.PartialSum'>
- class Permutation#
Bases:
ArraySymbol
Permutation 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:
ArraySymbol
Product of the elements of a symbol.
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 = i.prod() >>> type(i_prod) <class 'dwave.optimization.symbols.Prod'>
- class Put#
Bases:
ArraySymbol
A 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:
ArraySymbol
Quadratic 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:
ArraySymbol
Reshaped 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 Rint#
Bases:
ArraySymbol
Takes 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 SetVariable#
Bases:
ArraySymbol
Set 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, state)#
Set the state of the set node.
The given state must be a permuation of
range(len(state))
.
- class Size#
Bases:
ArraySymbol
- class Square#
Bases:
ArraySymbol
Squares 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:
ArraySymbol
Square 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:
ArraySymbol
Subtraction 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:
ArraySymbol
Sum of the elements of a symbol.
Examples
This example adds the sum 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 = i.sum() >>> type(i_sum) <class 'dwave.optimization.symbols.Sum'>
- class Where#
Bases:
ArraySymbol
Return elements chosen from x or y depending on condition.
See also
where()
: equivalent function.
- class Xor#
Bases:
ArraySymbol
Boolean XOR element-wise between two symbols.