pyqubo.Array.dot

Array.dot(other)[source]

Returns a dot product of two arrays.

Parameters:other (Array) – Array.
Returns:Express/Array

Example

Dot calculation falls into four patterns.

  1. If both self and other are 1-D arrays, it is inner product of vectors.
>>> from pyqubo import Array, Binary
>>> array_a = Array([Binary('a'), Binary('b')])
>>> array_b = Array([Binary('c'), Binary('d')])
>>> array_a.dot(array_b)
((Binary(a)*Binary(c))+(Binary(b)*Binary(d)))
  1. If self is an N-D array and other is a 1-D array, it is a sum product over the last axis of self and other.
>>> array_a = Array([[Binary('a'), Binary('b')], [Binary('c'), Binary('d')]])
>>> array_b = Array([Binary('e'), Binary('f')])
>>> array_a.dot(array_b)
Array([((Binary(a)*Binary(e))+(Binary(b)*Binary(f))),                 ((Binary(c)*Binary(e))+(Binary(d)*Binary(f)))])
  1. If both self and other are 2-D arrays, it is matrix multiplication.
>>> array_a = Array([[Binary('a'), Binary('b')], [Binary('c'), Binary('d')]])
>>> array_b = Array([[Binary('e'), Binary('f')], [Binary('g'), Binary('h')]])
>>> array_a.dot(array_b)
Array([[((Binary(a)*Binary(e))+(Binary(b)*Binary(g))),                 ((Binary(a)*Binary(f))+(Binary(b)*Binary(h)))],
       [((Binary(c)*Binary(e))+(Binary(d)*Binary(g))),                 ((Binary(c)*Binary(f))+(Binary(d)*Binary(h)))]])
  1. If self is an N-D array and other is an M-D array (where N, M>=2), it is a sum product over the last axis of self and the second-to-last axis of other. If N = M = 3, (i, j, k, m) element of a dot product of self and other is:
dot(self, other)[i,j,k,m] = sum(self[i,j,:] * other[k,:,m])
>>> array_a = Array.create('a', shape=(3, 2, 4), vartype='BINARY')
>>> array_a.shape
(3, 2, 4)
>>> array_b = Array.create('b', shape=(5, 4, 3), vartype='BINARY')
>>> array_b.shape
(5, 4, 3)
>>> i, j, k, m = (1, 1, 3, 2)
>>> array_a.dot(array_b)[i, j, k, m] == sum(array_a[i, j, :] * array_b[k, :, m])
True

Dot product with list.

>>> array_a = Array([Binary('a'), Binary('b')])
>>> array_b = [3, 4]
>>> array_a.dot(array_b)
((Binary(a)*Num(3))+(Binary(b)*Num(4)))