Array

class Array(bit_list)[source]

Multi-dimensional array.

Parameters:bit_list (list/numpy.ndarray) –

The object from which a new array is created. Accepted input:

  • (Nested) list of Express, Array, int or float.
  • numpy.ndarray
shape

Shape of this array.

Type:tuple[int]

Example

Create a new array with Binary.

>>> from pyqubo import Array, Binary
>>> Array.create('x', shape=(2, 2), vartype='BINARY')
Array([[Binary(x[0][0]), Binary(x[0][1])],
       [Binary(x[1][0]), Binary(x[1][1])]])

Create a new array from a nested list of Express.

>>> array = Array([[Binary('x0'), Binary('x1')], [Binary('x2'), Binary('x3')]])
>>> array
Array([[Binary(x0), Binary(x1)],
       [Binary(x2), Binary(x3)]])

Get the shape of the array.

>>> array.shape
(2, 2)

Access an element with index.

>>> array[0, 0]  # = array[(0, 0)]
Binary(x0)

Use slice “:” to select a subset of the array.

>>> array[:, 1]  # = array[(slice(None), 1)]
Array([Binary(x1), Binary(x3)])
>>> sum(array[:, 1])
(Binary(x1)+Binary(x3))

Use list or tuple to select a subset of the array.

>>> array[[0, 1], 1]
Array([Binary(x1), Binary(x3)])
>>> array[(0, 1), 1]
Array([Binary(x1), Binary(x3)])

Create an array from numpy array.

>>> import numpy as np
>>> Array(np.array([[1, 2], [3, 4]]))
Array([[1, 2],
       [3, 4]])

Create an array from list of Array.

>>> Array([Array([1, 2]), Array([3, 4])])
Array([[1, 2],
       [3, 4]])
static Array.create(name, shape, vartype)[source]

Create a new array with Spins or Binary.

Parameters:
  • name (str) – Name of the matrix. It is used as a part of the label of variables. For example, if the name is ‘x’, the label of (i, j) th variable will be x[i][j].
  • shape (int/tuple[int]) – Dimensions of the array.
  • vartype (dimod.Vartype/str/set, optional) –

    Variable type of the solution. Accepted input values:

    • Vartype.SPIN, 'SPIN', {-1, 1}
    • Vartype.BINARY, 'BINARY', {0, 1}

Example

>>> from pyqubo import Array
>>> array = Array.create('x', shape=(2, 2), vartype='BINARY')
>>> array # doctest: +SKIP
Array([[Binary(x[0][0]), Binary(x[0][1])],
       [Binary(x[1][0]), Binary(x[1][1])]])
>>> array[0] # doctest: +SKIP
Array([Binary(x[0][0]), Binary(x[0][1])])

Matrix Operation

Array.T Returns a transposed array.
Array.dot(other) Returns a dot product of two arrays.
Array.matmul(other) Returns a matrix product of two arrays.
Array.reshape(new_shape) Returns a reshaped array.

Arithmetic Operation

Array.add(other) Returns a sum of self and other.
Array.subtract(other) Returns a difference between other and self.
Array.mul(other) Returns a multiplicity of self by other.
Array.div(other) Returns division of self by other.

Construction

Array.fill(obj, shape) Create a new array with the given shape, all filled with the given object.