Mathematical Functions#

These functions add successor symbols to a model’s underlying directed acyclic graph that perform mathematical operations on the predecessor symbols and inputs.

absolute(x, /)[source]#

Return the absolute value element-wise on a symbol.

An alias for the Python abs() function.

Parameters:

x (ArraySymbol) – Array symbol.

Returns:

Successor symbol that returns the absolute value element-wise on the predecessor symbol.

Return type:

Absolute

Examples

This example adds the minimum absolute value of an integer decision variable to a model.

>>> from dwave.optimization.model import Model
>>> from dwave.optimization.mathematical import absolute
...
>>> model = Model()
>>> i = model.integer((3, 3), lower_bound=-10)
>>> j = absolute(i).min()
>>> model.states.resize(1)
>>> with model.lock():
...     i.set_state(0, [[4, -3, 1], [-2, 1, 2], [-4, 7, 22]])
...     print(j.state(0))
1.0

See also

Absolute: Generated symbol

abs() (Python function), absolute (NumPy function)

Added in version 0.6.2.

add(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike, *xi: ArraySymbol | ArrayLike) Add | NaryAdd[source]#

Sum element-wise two or more symbols and arrays.

Equivalently, you can use the + operator (e.g., i + j).

Parameters:
  • x1 – Operand array symbol or array-like to be added.

  • x2 – Operand array symbol or array-like to be added.

  • *xi – Additional array symbols or array-like to be added.

Returns:

Successor symbol that sums the predecessor symbols (and/or arrays) element-wise. For two symbols, generates an Add symbol; for three or more symbols, generates a NaryAdd symbol.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import add

Add two symbols.

>>> model = Model()
>>> i = model.integer(2)
>>> j = model.integer(2)
>>> i_plus_j = add(i, j)    # alternatively: i_plus_j = i + j
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [3, 5])
...     j.set_state(0, [7, 5])
...     print(i_plus_j.state(0))
[10. 10.]

Add a symbol and array.

>>> model = Model()
>>> i = model.integer(2)
>>> i_plus_array = i + [5, -3]     # equivalently: i_plus_array = add(i, [5, -3])

See also

Add, NaryAdd: Generated symbol

add: NumPy function

divide(), mod(), multiply(), safe_divide(), subtract()

arange(start: int | ArraySymbol | None = None, stop: int | ArraySymbol | None = None, step: int | ArraySymbol | None = None) ArraySymbol[source]#

Evenly space values within an interval.

Parameters:
  • start – Start of the interval; if only one argument is provided, interpreted as the stop argument. Default is 0.

  • stop – End of the interval.

  • step – Spacing between values. Default is 1

To add the generated ARange symbol to the model’s underlying directed acyclic graph, at least one argument must be a symbol in the model, thus providing one or more predecessors.

Returns:

Successor symbol with evenly spaced integer values.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import arange
...
>>> i = model.integer(1, lower_bound=-5)
>>> points = arange(i, 4, 2)
>>> with model.lock():
...     model.states.resize(2)
...     i.set_state(0, -4)
...     i.set_state(1, 0)
...     print(points.state(0))
...     print(points.state(1))
[-4. -2.  0.  2.]
[0. 2.]

See also

ARange: Generated symbol

arange(): NumPy function

Added in version 0.5.2.

argsort(array: ArraySymbol) ArgSort[source]#

Order a symbol’s indices to sort the flattened array’s values.

Creates a symbol that, for the given symbol, returns an ordering of indices that represents a stable sort of the values. Although the indices are for a flattened array, similar to NumPy’s argsort() function with axis=None, this symbol is shaped identically to the predecessor symbol.

Parameters:

array – Array symbol to sort.

Returns:

Successor symbol with an ordering of the predecessor symbol’s indices that sorts its flattened array’s values.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import argsort
...
>>> model = Model()
>>> a = model.constant([5, 2, 7, 4, 9, 1])
>>> indices = argsort(a)
>>> indices.shape()
(6,)
>>> with model.lock():
...    model.states.resize(1)
...    print(indices.state())
[5. 1. 3. 0. 2. 4.]
>>> a = model.constant([[5, 2, 7], [4, 9, 1]])
>>> indices = argsort(a)
>>> indices.shape()
(2, 3)
>>> with model.lock():
...    model.states.resize(1)
...    print(indices.state())
[[5. 1. 3.]
 [0. 2. 4.]]

See also

ArgSort: Generated symbol

argsort(): NumPy function

Added in version 0.6.4.

as_array_symbols(*arrays: ArraySymbol | ArrayLike, model: Model | None = None) tuple[ArraySymbol, ...][source]#

Convert arrays to array symbols.

Parameters:
  • *arrays – Arrays to convert.

  • model – Model to which to add the generated ArraySymbol symbols. If not specified, inferred from the given inputs.

Returns:

Tuple of symbols representing the converted arrays.

Examples

>>> import numpy as np
>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import as_array_symbols
...
>>> model = Model()
>>> c = as_array_symbols(np.random.randint(
...     low=-3,
...     high=5,
...     size=(15, 25)),
...     model=model)
>>> min_c = c[0].min()
>>> with model.lock():
...    model.states.resize(1)
...    print(min_c.state(0) >= -3)
True

See also

ArraySymbol: Generated symbol

atleast_1d(), atleast_2d(), constant(), input()

Added in version 0.6.11.

atleast_1d(array: ArraySymbol) ArraySymbol[source]#
atleast_1d(*arrays: ArraySymbol) tuple[ArraySymbol, ...]

Ensure array symbols are at least one dimensional.

Parameters:

arrays – One or more array symbols.

Returns:

Successor array symbol, or a tuple of successor array symbols, with at least one dimension.

Examples

>>> from dwave.optimization import atleast_1d, Model
...
>>> model = Model()
>>> a = model.constant(1)
>>> b = model.constant([2])
>>> c, d = atleast_1d(a, b)
>>> c.shape()  # a is converted into a 1d array
(1,)
>>> d.shape()  # b is not changed
(1,)
>>> d is b
True
>>> e = model.constant([[0, 1, 2], [3, 4, 5]])
>>> f = atleast_1d(e)  # e is not changed
>>> f.shape()
(2, 3)
>>> f is e
True

Added in version 0.6.0.

atleast_2d(array: ArraySymbol) ArraySymbol[source]#
atleast_2d(*arrays: ArraySymbol) tuple[ArraySymbol, ...]

Ensure array symbols are at least two dimensional.

Parameters:

arrays – One or more array symbols.

Returns:

Successor array symbol, or a tuple of successor array symbols, with at least two dimensions.

Examples

>>> from dwave.optimization import atleast_2d, Model
...
>>> model = Model()
>>> a = model.constant(1)
>>> b = model.constant([[2]])
>>> c, d = atleast_2d(a, b)
>>> c.shape()  # a is converted into a 2d array
(1, 1)
>>> d.shape()  # c is not changed
(1, 1)
>>> d is b
True
>>> e = model.constant([[0, 1, 2], [3, 4, 5]])
>>> f = atleast_2d(e)  # e is not changed
>>> f.shape()
(2, 3)
>>> f is e
True

Added in version 0.6.0.

broadcast_shapes(*shapes: int | tuple[int, ...]) tuple[int, ...][source]#

Calculate the broadcast shape.

You can set a value of \(-1\) for a dimension to calculate the broadcast shape for dynamically sized array symbols.

See NumPy’s broadcasting documentation for an introduction to broadcasting.

Parameters:

*shapes – Shapes to be broadcast.

Returns:

Calculated broadcasted shape.

Examples

>>> from dwave.optimization import broadcast_shapes
>>> broadcast_shapes((1, 3), (2, 1))
(2, 3)
>>> broadcast_shapes((-1, 5), (1,))
(-1, 5)

Added in version 0.6.11.

broadcast_symbols(*args: ArraySymbol) tuple[ArraySymbol, ...][source]#

Broadcast multiple array symbols to a single shape.

For dynamically sized array symbols, you can make use of the \(-1\) value in the dynamic dimension, as shown in the examples.

See NumPy’s broadcasting documentation for an introduction to broadcasting.

Parameters:

*args – Array symbols to broadcast.

Returns:

Tuple of successor array symbols, all with the same shape. For each predecessor symbol, a new BroadcastTo symbol is returned unless the predecessor already has the broadcast shape, in which case the predecessor itself returned.

Examples

>>> from dwave.optimization import broadcast_symbols, broadcast_to, Model
...
>>> model = Model()
>>> x = model.integer(3)  # shape = (3,)
>>> y = model.constant([[0], [1]])  # shape = (2, 1)
>>> xb, yb = broadcast_symbols(x, y)
>>> xb.shape()
(2, 3)
>>> yb.shape()
(2, 3)
>>> # Dynamically sized array:
>>> s1 = model.set(4).reshape(-1, 1)
>>> s1.shape()
(-1, 1)
>>> s2 = broadcast_to(s1, (-1, 2))
>>> s2.shape()
(-1, 2)
>>> s3, s4 = broadcast_symbols(s1, s2)
>>> s3.shape()
(-1, 2)
>>> s4 is s2
True

Added in version 0.6.5.

broadcast_to(x: ArraySymbol, shape: tuple[int, ...] | int) ArraySymbol[source]#

Broadcast an array symbol to a new shape.

For dynamically sized array symbols, you can make use of the \(-1\) value in the dynamic dimension, as shown in the examples.

See NumPy’s broadcasting documentation for an introduction to broadcasting.

Parameters:
  • x – Array symbol to broadcast to a new shape.

  • shape – Shape for the broadcasted symbol. A single integer i is interpreted as (i,).

Returns:

Successor BroadcastTo symbol unless its predecessor symbol has the requested shape, in which case the predecessor itself is returned.

Examples

>>> from dwave.optimization import broadcast_to, Model
...
>>> model = Model()
>>> x = model.constant([0, 1, 2, 3, 4])
>>> x.shape()
(5,)
>>> y = broadcast_to(x, (2, 5))
>>> y.shape()
(2, 5)
>>> model.states.resize(1)
>>> with model.lock():
...     y.state()
array([[0., 1., 2., 3., 4.],
       [0., 1., 2., 3., 4.]])
>>> # Dynamically sized array:
>>> s = model.set(4)
>>> s.shape()
(-1,)
>>> s4 = broadcast_to(s.reshape(-1, 1), (-1, 4))
>>> with model.lock():
...     s.set_state(0, {2, 3})
...     print(s4.state(0))
[[2. 2. 2. 2.]
 [3. 3. 3. 3.]]

Added in version 0.6.5.

bspline(x: ArraySymbol, k: int, t: list, c: list) ArraySymbol[source]#

Return B-spline values for a symbol.

Parameters:
  • x – Array symbol for which to create B-spline values.

  • k – B-spline degree.

  • t – Knots.

  • c – Spline coefficients.

Returns:

Successor symbol with B-spline values for the predecessor symbol.

Examples

>>> from dwave.optimization import bspline, Model
...
>>> model = Model()
>>> x = model.integer(3, lower_bound=2, upper_bound=4)
>>> bs = bspline(x, k=2, t=[0, 1, 2, 3, 4, 5, 6], c=[-1, 2, 0, -1])
>>> with model.lock():
...     model.states.resize(1)
...     x.set_state(0, [2, 3, 4])
...     print(bs.state(0))
[ 0.5  1.  -0.5]

See also

BSpline: Generated symbol

BSpline: SciPy class

Added in version 0.5.4.

concatenate(arrays: Sequence[ArraySymbol], axis: int = 0) ArraySymbol[source]#

Concatenate one or more symbols across an axis.

Parameters:
  • arrays – Array symbols to concatenate.

  • axis – The concatenation axis. Default is axis 0 (vertical stacking in the case of 2D arrays).

Returns:

Successor symbol that concatenates the predecessor symbols along the specified axis.

Examples

This example concatenates two constant symbols along the first axis.

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import concatenate
...
>>> model = Model()
>>> a = model.constant([[0,1], [2,3]])
>>> b = model.constant([[4,5]])
>>> a_b = concatenate((a,b), axis=0)
>>> a_b.shape()
(3, 2)
>>> with model.lock():
...     model.states.resize(1)
...     print(a_b.state(0))
[[0. 1.]
 [2. 3.]
 [4. 5.]]

See also

Concatenate: Generated symbol

concatenate(): NumPy function

hstack(), stack(), vstack()

Added in version 0.4.3.

cos(x: ArraySymbol) Cos[source]#

Calculate element-wise the trigonometric cosine of a symbol.

Parameters:

x – Array giving the angles, in radians.

Returns:

Successor symbol that is the trigonometric cosine of the values in its predecessor symbol.

Examples

>>> import numpy as np
>>> from dwave.optimization import Model, cos
...
>>> model = Model()
>>> x = model.constant([0, np.pi / 2])
>>> y = cos(x)
...
>>> model.states.resize(1)
>>> with model.lock():
...     print(y.state())
[1.000000e+00 6.123234e-17]

See also

Cos: Generated symbol

cos: NumPy function

sin(), tanh()

Added in version 0.6.5.

divide(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) Divide[source]#

Divide element-wise two symbols (or arrays).

Equivalently, you can use the / operator (e.g., i / j).

Parameters:
Returns:

Successor symbol that divides the predecessor symbols (or arrays) element-wise.

Raises:

ValueError – If a denominator element is zero. See also the safe_divide() function.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import divide

Divide two symbols.

>>> model = Model()
>>> i = model.integer(2, lower_bound=1)
>>> j = model.integer(2, lower_bound=1)
>>> k = divide(i, j)   # alternatively: k = i / j
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [21, 10])
...     j.set_state(0, [7, 2])
...     print(k.state(0))
[3. 5.]

Divide a symbol and array.

>>> model = Model()
>>> i = model.integer(2, lower_bound=1)
>>> l = i / [2, 0.5] # equivalently: l = divide(i, [2, 0.5])

See also

Divide: Generated symbol

divide: NumPy function

add(), mod(), multiply(), safe_divide(), subtract()

equal(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) Equal[source]#

Return element-wise equality between two symbols (or arrays).

Equivalently, you can use the == operator (e.g., i == j).

Parameters:
Returns:

Successor symbol that is the element-wise equality of its predecessor symbols (or arrays).

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import equal

Equality between symbols.

>>> model = Model()
>>> i = model.integer(3, lower_bound=1)
>>> c = model.constant([[1, 3, 5], [2, 4, 6]])
>>> i_c = equal(i, c[0, :])     # Alternatively, i_c = i == c[0, :]
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [1, 3, 7])
...     print(i_c.state(0))
[1. 1. 0.]

Equality between a symbol and array.

>>> model = Model()
>>> i = model.integer(3, lower_bound=1)
>>> i_array = i == [2, 6, 8] # equivalently: i_array = equal(i, [2, 6, 8])

See also

Equal: Generated symbol

equal: NumPy function

less_equal(), isin()

exp(x: ArraySymbol) Exp[source]#

Calculate element-wise the natural (base-e) exponential of a symbol.

Parameters:

x – Array symbol.

Returns:

Successor symbol that is the base-e exponential values of its predecessor symbol’s array elements.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import exp
...
>>> model = Model()
>>> x = model.constant(1.0)
>>> exp_x = exp(x)
>>> model.states.resize(1)
>>> with model.lock():
...     print(exp_x.state())
2.718281828459045

See also

Exp: Generated symbol

exp: NumPy function

expit(), log(), softmax()

Added in version 0.6.2.

expit(x: ArraySymbol) Expit[source]#

Calculate element-wise the logistic sigmoid of a symbol.

Parameters:

x – Array symbol.

Returns:

Successor symbol that is the logistic sigmoid values of its predecessor symbol’s array elements.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import expit
...
>>> model = Model()
>>> x = model.constant(1.0)
>>> expit_x = expit(x)
>>> model.states.resize(1)
>>> with model.lock():
...     print(expit_x.state())
0.7310585786300049

See also

Expit: Generated symbol

expit: SciPy function

exp(), log(), softmax()

Added in version 0.5.2.

extract(condition: ArraySymbol, arr: ArraySymbol) Extract[source]#

Extract the elements of an array symbol where a condition is true.

Parameters:
  • condition – Condition under which to return elements of arr when it evaluates as True.

  • arr – Array symbol.

Returns:

Successor symbol that contains the elements of its predecessor symbol that meet the specified condition.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import extract
...
>>> model = Model()
>>> condition = model.binary(3)
>>> arr = model.constant([4, 5, 6])
>>> extracted = extract(condition, arr)
>>> model.states.resize(1)
>>> with model.lock():
...     condition.set_state(0, [True, False, True])
...     print(extracted.state())
[4. 6.]

See also

Extract: Generated symbol

extract(): NumPy function

isin(), put(), where()

Added in version 0.6.3.

hstack(arrays: Sequence[ArraySymbol]) ArraySymbol[source]#

Horizontally stack a sequence of array symbols.

Equivalent to concatenation along the second axis, except for one-dimensional array symbols, which it concatenates along the first axis.

Parameters:

arrays – Array symbols to be concatenated. Arrays must have the same shape along all but the second axis unless they are 1D arrays.

Returns:

Successor symbol that concatenates its predecessor array symbols.

Examples

>>> from dwave.optimization import hstack, Model
>>> model = Model()
>>> model.states.resize(1)
...
>>> a = model.constant([0])
>>> b = model.constant([1, 2, 3])
>>> h = hstack((a, b))
>>> with model.lock():
...     h.state()
array([0., 1., 2., 3.])
>>> model = Model()
>>> model.states.resize(1)
...
>>> a = model.constant([[0], [3]])
>>> b = model.constant([[1, 2], [4, 5]])
>>> h = hstack((a, b))
>>> with model.lock():
...     h.state()
array([[0., 1., 2.],
       [3., 4., 5.]])

See also

Concatenate: Generated symbol

hstack(): NumPy function

concatenate(), stack(), vstack()

Added in version 0.6.0.

isin(element: ArraySymbol, test_elements: ArraySymbol) IsIn[source]#

Return which values of one array symbol are in another.

Determines element-wise containment between two symbols.

Parameters:
  • element – Array symbol.

  • test_elements – Array symbol containing the values against which to test each value of the element symbol.

Returns:

Successor array symbol, with the same shape as element, with values being the element-wise containment between its two predecessor symbols; i.e., true if the corresponding element in the first symbol is contained in the second and false otherwise.

Examples

>>> from dwave.optimization.model import Model
>>> from dwave.optimization.mathematical import isin
...
>>> model = Model()
>>> model.states.resize(1)
>>> element = model.constant([1.3, -1.0, 3.0])
>>> test_elements = model.constant([3.0, -1.1, -2.0, 4.1, -1.0])
>>> contains = isin(element, test_elements)
>>> with model.lock():
...     print(contains.state(0))
[0. 1. 1.]

See also

IsIn: Generated symbol

isin(): NumPy function

extract(), put(), where()

Added in version 0.6.8.

less_equal(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) LessEqual[source]#

Return element-wise less than or equal between two symbols (or arrays).

Parameters:
Returns:

Successor symbol that is the element-wise less than or equal (\(x_1 \le x_2\)) for its predecessor symbols (or arrays).

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import less_equal
...
>>> model = Model()
>>> i = model.integer(3, lower_bound=1)
>>> c = model.constant([[1, 3, 5], [2, 4, 6]])
>>> i_c = less_equal(i, c[0, :])     # Alternatively, i_c = i <= c[0, :]
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [1, 2, 7])
...     print(i_c.state(0))
[1. 1. 0.]
>>> # Testing equality between a symbol and an array
>>> i_array = i <= [2, 1, 8] # equivalently: i_array = less_equal(i, [2, 1, 8])

See also

LessEqual: Generated symbol

less_equal: NumPy function

equal(), isin()

linprog(c: ArraySymbol, A_ub: None | ArraySymbol = None, b_ub: None | ArraySymbol = None, A_eq: None | ArraySymbol = None, b_eq: None | ArraySymbol = None, *, b_lb: None | ArraySymbol = None, A: None | ArraySymbol = None, lb: None | ArraySymbol = None, ub: None | ArraySymbol = None) LPResult[source]#

Solve a linear program.

Linear programs solve problems of the form:

\[\begin{split}\text{minimize:} \\ & c^T x \\ \text{subject to:} \\ &b_{lb} \leq A x \leq b_{ub} \\ &A_{eq} x = b_{eq} \\ &lb \leq x \leq ub\end{split}\]

Or, equivalently:

\[\begin{split}\text{minimize:} \\ & c^T x \\ \text{subject to:} \\ &A_{ub} x \leq b_{ub} \\ &A_{eq} x = b_{eq} \\ &lb \leq x \leq ub\end{split}\]
Parameters:
  • c – Coefficients of the linear objective as a 1D array symbol.

  • A_ub – Alias of A. At most one of A_ub and A may be specified.

  • b_ub – Linear inequality upper bounds as a 1D array symbol.

  • A_eq – Linear equality matrix as a 2D array symbol.

  • b_eq – Linear equality bounds as a 1D array symbol.

  • b_lb – Linear inequality lower bounds as a 1D array symbol.

  • A – Linear inequality matrix as a 2D array symbol. At most one of A_ub and A may be specified.

  • lb – Lower bounds on x as a 1D array symbol.

  • ub – Upper bounds on x as a 1D array symbol.

Returns:

LPResult class containing the results of the linear program.

Examples

This example adds the following linear program to a model.

\[\begin{split}\text{minimize: } & -x_0 - 2x_1 \\ \text{subject to: } & x_0 + x_1 <= 1\end{split}\]
>>> from dwave.optimization import linprog, Model
...
>>> model = Model()
>>> c = model.constant([-1, -2])
>>> A_ub = model.constant([[1, 1]])
>>> b_ub = model.constant([1])
>>> res = linprog(c, A_ub=A_ub, b_ub=b_ub)
>>> _ = model.add_constraint(res.success)  # tell the model you want feasible solutions
...
>>> model.states.resize(1)
>>> x = res.x
>>> with model.lock():
...     x.state()
array([0., 1.])

Added in version 0.6.0.

log(x: ArraySymbol) Log[source]#

Calculate element-wise the natural logarithm of a symbol.

Parameters:

x – Array symbol.

Returns:

Successor symbol that is the natural logarithm values of of its predecessor symbol’s array elements.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import log
...
>>> model = Model()
>>> x = model.constant(1.0)
>>> log_x = log(x)
>>> model.states.resize(1)
>>> with model.lock():
...     print(log_x.state())
0.0

See also

Log: Generated symbol

log: NumPy function

exp(), expit(), softmax()

Added in version 0.5.2.

logical(x: ArraySymbol) Logical[source]#

Return the element-wise truth value of a symbol.

Parameters:

x – Array symbol.

Returns:

Successor symbol that is the element-wise truth value of its predecessor symbol’s array elements.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import logical
...
>>> model = Model()
>>> x = model.constant([0, 1, -2, .5])
>>> logical_x = logical(x)
>>> model.states.resize(1)
>>> with model.lock():
...     print(logical_x.state())
[0. 1. 1. 1.]
logical_and(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) And[source]#

Calculate element-wise the logical AND of two symbols (or arrays).

Parameters:
Returns:

Successor symbol that is the element-wise AND of the array elements of its predecessor symbols (or arrays).

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import logical_and
...
>>> model = Model()
>>> x = model.binary(3)
>>> y = model.binary(3)
>>> z = logical_and(x, y)
>>> with model.lock():
...     model.states.resize(1)
...     x.set_state(0, [True, True, False])
...     y.set_state(0, [False, True, False])
...     print(z.state(0))
[0. 1. 0.]
>>> # AND between a symbol and an array
>>> z_array = logical_and(x, [True, False, True])

See also

And: Generated symbol

logical_and: NumPy function

logical(), logical_not(), logical_or(), logical_xor()

logical_not(x: ArraySymbol) Not[source]#

Calculate element-wise the logical NOT of a symbol.

Parameters:

x – Array symbol.

Returns:

Successor symbol that is the element-wise NOT of the array elements of its predecessor symbol.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import logical_not
...
>>> model = Model()
>>> x = model.constant([0, 1, -2, .5])
>>> not_x = logical_not(x)
>>> model.states.resize(1)
>>> with model.lock():
...     print(not_x.state())
[1. 0. 0. 0.]

See also

Not: Generated symbol

logical_not: NumPy function

logical(), logical_and(), logical_or(), logical_xor()

logical_or(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) Or[source]#

Calculate element-wise the logical OR of two symbols (or arrays).

Parameters:
Returns:

Successor symbol that is the element-wise OR of the array elements of its predecessor symbols (or arrays).

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import logical_or
...
>>> model = Model()
>>> x = model.binary(3)
>>> y = model.binary(3)
>>> z = logical_or(x, y)
>>> with model.lock():
...     model.states.resize(1)
...     x.set_state(0, [True, True, False])
...     y.set_state(0, [False, True, False])
...     print(z.state(0))
[1. 1. 0.]
>>> # OR between a symbol and an array
>>> z_array = logical_or(x, [True, False, True])

See also

Or: equivalent symbol.

logical_or: NumPy function

logical(), logical_and(), logical_not(), logical_xor()

logical_xor(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) Xor[source]#

Calculate element-wise logical XOR of two symbols (or arrays).

Parameters:
Returns:

Successor symbol that is the element-wise XOR of the array elements of its predecessor symbols (or arrays).

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import logical_xor
...
>>> model = Model()
>>> x = model.binary(3)
>>> y = model.binary(3)
>>> z = logical_xor(x, y)
>>> with model.lock():
...     model.states.resize(1)
...     x.set_state(0, [True, True, False])
...     y.set_state(0, [False, True, False])
...     print(z.state(0))
[1. 0. 0.]
>>> # XOR between a symbol and an array
>>> z_array = logical_xor(x, [True, False, True])

See also

Xor: equivalent symbol.

logical_xor: NumPy function

logical(), logical_and(), logical_not(), logical_or()

Added in version 0.4.1.

matmul(x: ArraySymbol | ArrayLike, y: ArraySymbol | ArrayLike) MatrixMultiply[source]#

Compute the matrix product of two array symbols (or arrays).

Parameters:

The size of the last axis of x must be equal to the size of the second-to-last axis of y. If either x or y is one-dimensional, they are treated as a row or column vector respectively. If both are 1D, produces a scalar (and the operation is equivalent to the dot product of two vectors).

Otherwise, if the shapes can be broadcast together, either by broadcasting missing leading axes (e.g. \((2, 5, 3, 4, 2), (2, 6) \rightarrow (2, 5, 3, 4, 6)\)) or by broadcasting axes for which one of the operands has size 1 (e.g. \((3, 1, 4, 2), (1, 7, 2, 3) \rightarrow (3, 7, 4, 3)\)), returns the result after broadcasting.

Returns:

Successor symbol representing the matrix product. For x and y with shapes \((..., n, k)\) and \((..., k, m)\), the generated MatrixMultiply has shape \((..., n, m)\).

Examples

This example computes the dot product of two integer arrays and also multiplies the first symbol by an array with a different shape.

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import matmul
...
>>> model = Model()
>>> i = model.integer(3)
>>> j = model.integer(3)
>>> m = matmul(i, j)
>>> m_array = matmul(i, [[1, 2], [3, 4], [5, 6]])
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [1, 2, 3])
...     j.set_state(0, [4, 5, 6])
...     print(m.state(0))
...     print(m_array.state(0))
32.0
[22. 28.]

See also

MatrixMultiply: Generated symbol

matmul: NumPy function

multiply() function and prod() method

Added in version 0.6.10.

maximum(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike, *xi: ArraySymbol | ArrayLike) Maximum | NaryMaximum[source]#

Return an element-wise maximum of two or more symbols and arrays.

Parameters:
Returns:

Successor symbol that is the element-wise maximum of the predecessor symbols (or arrays). For two symbols, generates a Maximum symbol; for three or more symbols, generates a NaryMaximum symbol.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import maximum

Maximum between two symbols.

>>> model = Model()
>>> i = model.integer(2)
>>> j = model.integer(2)
>>> m = maximum(i, j)
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [3, 5])
...     j.set_state(0, [7, 2])
...     print(m.state(0))
[7. 5.]

Maximum between a symbol and array.

>>> model = Model()
>>> i = model.integer(2)
>>> m_array = maximum(i, [2, 6])

See also

Maximum, NaryMaximum: Generated symbol

maximum: NumPy function

max() method

mean(array: ArraySymbol) Mean[source]#

Calculate the mean value of an array symbol.

Parameters:

array – Array symbol.

Returns:

Successor symbol that is the mean of its predecessor symbol’s array elements. If the predecessor symbol is empty, mean defaults to 0.0.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import mean
...
>>> model = Model()
>>> i = model.integer(3)
>>> m = mean(i)
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [8, 4, 3])
...     print(m.state(0))
5.0

See also

Mean: Generated symbol

mean(): NumPy function

Added in version 0.6.4.

minimum(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike, *xi: ArraySymbol | ArrayLike) Minimum | NaryMinimum[source]#

Return an element-wise minimum of two or more symbols and arrays.

Parameters:
Returns:

Successor symbol that is the element-wise minimum of the given symbols (or arrays). For two symbols, generates a Minimum symbol; for three or more symbols, generates a NaryMinimum symbol.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import minimum

Minimum between two symbols.

>>> model = Model()
>>> i = model.integer(2)
>>> j = model.integer(2)
>>> m = minimum(i, j)
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [3, 5])
...     j.set_state(0, [7, 2])
...     print(m.state(0))
[3. 2.]

Minimum between a symbol and array.

>>> model = Model()
>>> i = model.integer(2)
>>> m_array = minimum(i, [2, 6])

See also

Minimum, NaryMinimum: Generated symbol

minimum: NumPy function

min() method

mod(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) Modulus[source]#

Calculate element-wise the modulus of two symbols (or arrays).

Equivalently, you can use the % operator (e.g., i % j).

Parameters:
Returns:

Successor symbol that is the remainder of the predecessor symbols (or arrays) element-wise.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import mod

Calculate the modulus of two integer symbols, \(i \mod{j}\), with different combinations of positive and negative values.

>>> model = Model()
>>> i = model.integer(4, lower_bound=-5)
>>> j = model.integer(4, lower_bound=-3)
>>> k = mod(i, j) # alternatively: k = i % j
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [5, -5, 5, -5])
...     j.set_state(0, [3, 3, -3, -3])
...     print(k.state(0))
[ 2.  1. -1. -2.]

Calculate the modulus of a scalar float value and a binary symbol.

>>> model = Model()
>>> j = model.binary(2)
>>> k = 0.33 % j # equivalently: k = mod(0.33, j)
>>> with model.lock():
...     model.states.resize(1)
...     j.set_state(0, [0, 1])
...     print(k.state(0))
[0.   0.33]

See also

See Also: Modulus: Generated symbol

mod: NumPy function

add(), divide(), multiply(), safe_divide(), subtract()

multiply(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike, *xi: ArraySymbol | ArrayLike) Multiply | NaryMultiply[source]#

Multiply element-wise two or more symbols and arrays.

Equivalently, you can use the * operator (e.g., i * j).

Parameters:
  • x1 – Operand array symbol or array-like to be multiplied.

  • x2 – Operand array symbol or array-like to be multiplied.

  • *xi – Additional array symbol or array-like to be multiplied.

Returns:

Successor symbol that multiplies the predecessor symbols (and/or arrays) element-wise. For two symbols, generates a Multiply symbol; for three or more symbols, generates a NaryMultiply symbol.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import multiply

Multiply two symbols.

>>> model = Model()
>>> i = model.integer(2)
>>> j = model.integer(2)
>>> k = multiply(i, j)   # alternatively: k = i * j
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [3, 5])
...     j.set_state(0, [7, 2])
...     print(k.state(0))
[21. 10.]

Multiply a symbol and array.

>>> model = Model()
>>> i = model.integer(2)
>>> k_array = i * [8, 4] # equivalently: k_array = multiply(i, [8, 4])

See also

See Also: Multiply, NaryMultiply: Generated symbol

multiply: NumPy function

add(), divide(), mod(), safe_divide(), subtract()

put(array: ArraySymbol, indices: ArraySymbol, values: ArraySymbol) Put[source]#

Replace the specified elements of an array symbol with specified values.

This function is mostly equivalent to the following function defined for NumPy arrays.

def put(array, indices, values):
    array = array.copy()
    array.flat[indices] = values
    return array
Parameters:
  • array – Array symbol to modify. Must be not be a dynamically sized array symbol or a scalar.

  • indices

    Indices in array, flattened, of values to be replaced.

    Warning

    Ensure that indices does not contain duplicate values. For duplicate values, which of the possible corresponding values of values is to be propagated is undefined, and performance of the model is likely to be degraded.

  • values – Values to place in array for the elements specified by indices of indices.

Examples

>>> import numpy as np
>>> from dwave.optimization import Model
>>> from dwave.optimization import put
...
>>> model = Model()
...
>>> array = model.constant(np.zeros((3, 3)))
>>> indices = model.constant([0, 1, 2])
>>> values = model.integer(3)
>>> array = put(array, indices, values)  # replace values in array
...
>>> model.states.resize(1)
>>> with model.lock():
...     values.set_state(0, [10, 20, 30])
...     print(array.state(0))
[[10. 20. 30.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

See also

Put: Generated symbol

put(): NumPy function

extract(), isin(), where()

Added in version 0.4.4.

resize(array: ArraySymbol, shape: int | Sequence[int], fill_value: None | float = None) Resize[source]#

Resize a symbol to a specified shape.

Parameters:
  • array – Array symbol to be resized.

  • shape – Shape of the new array. All dimension sizes must be non-negative.

  • fill_value – Value to be used if the successor array is larger than its predecessor. Defaults to 0.

Returns:

Successor symbol with the specified shape.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import resize
...
>>> model = Model()
>>> s = model.set(10)  # subsets of range(10)
>>> s_2x2 = resize(s, (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.]]

See also

Resize: Generated symbol

resize(): NumPy function

resize() method

broadcast_to(), copy(), reshape(), roll(), transpose()

Added in version 0.6.4.

rint(x: ArraySymbol) Rint[source]#

Round element-wise the values of an array symbol.

Rounds the value of every element to its nearest integer.

Parameters:

x – Array symbol.

Returns:

Successor symbol with values of its predecessor symbol rounded to the nearest integer.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import rint
...
>>> model = Model()
>>> x = model.constant(1.3)
>>> rint_x = rint(x)
>>> model.states.resize(1)
>>> with model.lock():
...     print(rint_x.state())
1.0

See also

Rint: Generated symbol

roll(array: ArraySymbol, shift: ArraySymbol | tuple[int, ...] | int, axis: None | tuple[int, ...] | int = None) Roll[source]#

Roll an array symbol’s elements along an axis.

Parameters:
  • array – Array symbol.

  • shift – Number of places to shift the array elements. If axis is specified, shift must be a single number that applies to all axes or the same length as axis.

  • axis – Axis or axes to be shifted. If not specified, the array is treated as flat while shifting.

Returns:

Successor symbol with the values of its predecessor shifted.

Examples

>>> from dwave.optimization import Model, roll
...
>>> model = Model()
>>> x = model.constant(range(10))
>>> r0 = roll(x, 2)
>>> r1 = roll(x, -2)
>>> r2 = roll(x.reshape(2, 5), shift=1, axis=0)
>>> r3 = roll(x.reshape(2, 5), shift=[1, 2], axis=[1, 0])
...
>>> model.states.resize(1)
>>> with model.lock():
...     print(r0.state())
...     print(r1.state())
...     print(r2.state())
...     print(r3.state())
[8. 9. 0. 1. 2. 3. 4. 5. 6. 7.]
[2. 3. 4. 5. 6. 7. 8. 9. 0. 1.]
[[5. 6. 7. 8. 9.]
 [0. 1. 2. 3. 4.]]
[[4. 0. 1. 2. 3.]
 [9. 5. 6. 7. 8.]]

See also

Roll: Generated symbol

roll(): NumPy function

broadcast_to(), copy(), reshape(), resize(), transpose()

Added in version 0.6.9.

safe_divide(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) SafeDivide[source]#

Divide element-wise two symbols but return 0 where the denominator is 0.

This function is not strictly mathematical division. Rather it encodes the following function:

\[\begin{split}f(a, b) = \begin{cases} a / b & \text{for } b \neq 0 \\ 0 & \text{for } b = 0 \end{cases}\end{split}\]

Such a definition is useful [1] in cases where x2 is non-zero by construction, or otherwise enforced to be non-zero, because you can create a model with division using a symbol that might otherwise have zero values.

Parameters:
Returns:

Successor symbol that divides the predecessor symbols (or arrays) element-wise.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import safe_divide
...
>>> model = Model()
>>> a = model.constant([-1, 0, 1, 2])
>>> b = model.constant([2, 1, 0, -1])
>>> x = safe_divide(a, b)
>>> model.states.resize(1)
>>> with model.lock():
...     print(x.state())
[-0.5  0.   0.  -2. ]

See also

SafeDivide: Generated symbol

divide: NumPy function

add(), divide(), mod(), multiply(), subtract()

Added in version 0.6.2.

sin(x) Sin[source]#

Calculate element-wise the trigonometric sine of a symbol.

Parameters:

x – Array giving the angles, in radians.

Returns:

Successor symbol that is the trigonometric sine of the values in its predecessor symbol.

Examples

>>> import numpy as np
>>> from dwave.optimization import Model, sin
...
>>> model = Model()
>>> x = model.constant([0, np.pi / 2])
>>> y = sin(x)
...
>>> model.states.resize(1)
>>> with model.lock():
...     print(y.state())
[0. 1.]

See also

Sin: Generated symbol

sin: NumPy function

cos(), tanh()

Added in version 0.6.5.

softmax(array: ArraySymbol) SoftMax[source]#

Return softmax of a symbol.

Given a flattened array \(x: [x_1, x_2, ..., x_n]\), \(\text{softmax}(x)\) returns an array \([y_1, y_2, ..., y_n]\) such that \(y_i = \frac{\exp(x_i)}{(\exp(x_1) + \exp(x_2) + ... + \exp(x_n))}\).

Parameters:

array – Array symbol.

Returns:

Successor symbol where elements are the softmax of the predecessor symbol’s corresponding elements.

Example

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import softmax
>>> import numpy as np
...
>>> model = Model()
>>> i = model.integer(3)
>>> sm = softmax(i)
>>> expected = np.array([0.0900305731703, 0.6652409557748, 0.244728471054])
>>> with model.lock():
...     model.states.resize(1)
...     i.set_state(0, [1, 3, 2])
...     print(np.isclose(sm.state(), expected).all())
True

See also

SoftMax: equivalent symbol.

softmax(): SciPy function

exp(), expit(), log()

Added in version 0.6.5.

sqrt(x: ArraySymbol) SquareRoot[source]#

Calculate element-wise the square root of a symbol.

Parameters:

x – Array symbol.

Returns:

Successor symbol that is the square root of the values of its predecessor symbol.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import sqrt
...
>>> model = Model()
>>> x = model.constant(16)
>>> sqrt_x = sqrt(x)
>>> model.states.resize(1)
>>> with model.lock():
...     print(sqrt_x.state())
4.0

See also

SquareRoot: Generated symbol

Square

stack(arrays: Sequence[ArraySymbol], axis: int = 0) ArraySymbol[source]#

Stack a sequence of array symbols.

Parameters:

arrays – Sequence of array symbols to join.

Returns:

Successor symbol that joins its predecessor array symbols on an axis.

Examples

This example stacks three scalars on axis 0.

>>> from dwave.optimization import Model, stack
...
>>> model = Model()
>>> x = [model.constant(1), model.constant(2), model.constant(3)]
>>> s = stack(x)
>>> s.shape()
(3,)
>>> with model.lock():
...     model.states.resize(1)
...     print(s.state(0))
[1. 2. 3.]

This example stacks three 1D arrays on axis 0 and 1.

>>> from dwave.optimization import Model, stack
...
>>> model = Model()
>>> a = model.constant([1,2])
>>> b = model.constant([3,4])
>>> c = model.constant([5,6])
>>> s0 = stack((a,b,c), axis=0)
>>> s0.shape()
(3, 2)
>>> s1 = stack((a,b,c), axis=1)
>>> s1.shape()
(2, 3)
>>> with model.lock():
...     model.states.resize(1)
...     print(s0.state(0))
...     print(s1.state(0))
[[1. 2.]
 [3. 4.]
 [5. 6.]]
[[1. 3. 5.]
 [2. 4. 6.]]

See also

Concatenate: Generated symbol

stack(): NumPy function

concatenate(), hstack(), vstack()

Added in version 0.5.0.

subtract(x1: ArraySymbol | ArrayLike, x2: ArraySymbol | ArrayLike) Subtract[source]#

Subtract element-wise two symbols (or arrays).

Equivalently, you can use the - operator (e.g., i - j).

Parameters:
  • x1 – Operand array symbol or array-like to subtract.

  • x2 – Operand array symbol or array-like to subtract.

Returns:

Successor symbol that subtracts the predecessor symbols (or arrays) element-wise.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import subtract

Subtract two symbols.

>>> model = Model()
>>> i = model.integer(2, lower_bound=3)
>>> j = model.integer(2, upper_bound=20)
>>> k = subtract(i, j)   # alternatively: k = i - j
>>> with model.lock():
...    model.states.resize(1)
...    i.set_state(0, [21, 10])
...    j.set_state(0, [7, 2])
...    print(k.state(0))
[14.  8.]

Subtract an array from a symbol.

>>> model = Model()
>>> i = model.integer(2, lower_bound=3)
>>> l = i - [2, 2] # equivalently: l = subtract(i, [2, 2])

See also

Subtract: Generated symbol

subtract: NumPy function

add(), divide(), mod(), multiply(), safe_divide()

tanh(x) Tanh[source]#

Calculate element-wise the trigonometric hyperbolic tangent of a symbol.

Parameters:

x – Array giving the angles, in radians.

Returns:

Successor symbol that is the trigonometric hyperbolic tangent of the values in its predecessor symbol.

Examples

>>> import numpy as np
>>> from dwave.optimization import Model, tanh
...
>>> model = Model()
>>> x = model.constant([0, np.pi / 2])
>>> y = tanh(x)
...
>>> model.states.resize(1)
>>> with model.lock():
...     print(y.state())
[0.         0.91715234]

See also

Tanh: equivalent symbol.

tanh: NumPy function

cos(), sin()

Added in version 0.6.11.

transpose(array: ArraySymbol) Transpose[source]#

Transpose an array symbol.

Parameters:

array – Array symbol to transpose. For a dynamically sized array, must have dimension at most one.

Returns:

Successor symbol that is the transpose of its predecessor symbol. For a one-dimensional array, returns an unchanged view of the predecessor array. For a 2D array, returns the standard matrix transpose. For an \(n\)-dimensional array, the transpose simply reverses the order of the axes.

Examples

This example transposes a 5-element vector.

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import transpose
...
>>> model = Model()
>>> array = model.constant([0, 1, 2, 3, 4])
>>> transpose = transpose(array)
>>> model.states.resize(1)
>>> with model.lock():
...     print(transpose.state())
[0. 1. 2. 3. 4.]

This example transposes a \(2 \times 3\) matrix.

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import transpose
...
>>> model = Model()
>>> array = model.constant([[0, 1, 2], [3, 4, 5]])
>>> transpose = transpose(array)
>>> model.states.resize(1)
>>> with model.lock():
...     print(transpose.state())
[[0. 3.]
 [1. 4.]
 [2. 5.]]

This example transposes a \(2 \times 3 \times 2\) matrix.

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import transpose
...
>>> model = Model()
>>> array = model.constant([[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]])
>>> transpose = transpose(array)
>>> model.states.resize(1)
>>> with model.lock():
...     print(transpose.state())
[[[ 0.  6.]
  [ 2.  8.]
  [ 4. 10.]]

 [[ 1.  7.]
  [ 3.  9.]
  [ 5. 11.]]]

See also

Transpose: equivalent symbol.

transpose(): NumPy function

broadcast_to(), copy(), reshape() resize() roll()

Added in version 0.6.8.

vstack(arrays: Sequence[ArraySymbol]) ArraySymbol[source]#

Vertically stack a sequence of array symbols.

Equivalent to concatenation along the first axis.

Parameters:

arrays – Array symbols to be concatenated. Arrays must have the same shape along all but the first axis unless they are 0D or 1D arrays.

Returns:

Successor array symbol that concatenates its predecessor array symbols.

Examples

>>> from dwave.optimization import vstack, Model
>>> model = Model()
>>> model.states.resize(1)
...
>>> a = model.constant([0])
>>> b = model.constant([1])
>>> h = stack((a, b))
>>> with model.lock():
...     h.state()
array([[0.],
       [1.]])
>>> model = Model()
>>> model.states.resize(1)
...
>>> a = model.constant([0, 1, 2])
>>> b = model.constant([[3, 4, 5], [6, 7, 8]])
>>> h = vstack((a, b))
>>> with model.lock():
...     h.state()
array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]])

See also

Concatenate: Generated symbol

vstack(): NumPy function

concatenate(), hstack(), stack()

Added in version 0.6.0.

where(condition: ArraySymbol, x: ArraySymbol, y: ArraySymbol) Where[source]#

Select elements from either of two array symbols.

Parameters:
  • condition – Condition that where true selects elements from x and where false from y. If x and y are dynamically sized, condition must be a single value.

  • x – Array symbol from which to choose values.

  • y – Array symbol from which to choose values.

Returns:

Successor symbol with element values from predecessor symbol x where condition is true and from predecessor symbol y where false.

Examples

>>> from dwave.optimization import Model
>>> from dwave.optimization.mathematical import where

This example uses a binary array to to select between two arrays.

>>> model = Model()
>>> condition = model.binary(3)
>>> x = model.constant([1., 2., 3.])
>>> y = model.constant([4., 5., 6.])
>>> a = where(condition, x, y)
>>> with model.lock():
...     model.states.resize(1)
...     condition.set_state(0, [True, True, False])
...     print(a.state())
[1. 2. 6.]

This example uses a single binary variable to choose between two sets.

>>> model = Model()
>>> condition = model.binary()
>>> x = model.set(10)  # any subset of range(10)
>>> y = model.set(10)  # any subset of range(10)
>>> a = where(condition, x, y)
>>> with model.lock():
...     model.states.resize(1)
...     condition.set_state(0, True)
...     x.set_state(0, [0., 2., 3.])
...     y.set_state(0, [1])
...     print(a.state())
[0. 2. 3.]

See also

Where: Generated symbol

where(): NumPy function

extract(), isin(), put()

Classes#

class LPResult(lp: LinearProgram)[source]#

Outputs of a linear program.

See the linprog() function for information and usage.

property fun: LinearProgramObjectiveValue[source]#

Value of the objective.

lp: LinearProgram[source]#

Linear-program symbol.

See also

LinearProgram

property success: LinearProgramFeasible[source]#

True if the linear program’s indexed state is a feasible solution.

property x: LinearProgramSolution[source]#

Assignments of the decision variables.