amplify.BinaryMatrix

class BinaryMatrix

Upper triangular matrix representation of QUBO model with real coefficients.

The QUBO model will be the form of \(y = \min_q q^{T} Q q\), where \(q\) is a vector of variables and \(Q\) is a square matrix.

This class denotes the upper triangular matrix representation of \(Q\).

Note

In the descriptions of class methods, \(Q\) and \(q\) are the matrix and the vector this class represents, respectively.

Note

The following operators are defined for the class.
  • Indexing: a[slices] (__getitem__(), __setitem__())

  • Equality: a == b (__eq__())

  • Inequality: a != b (__ne__())

  • Addition: a + b (__add__(), __radd__(), __iadd__())

  • Subtraction: a - b (__sub__(), __rsub__(), __isub__())

  • Multiplication: a * b (__mul__(), __rmul__(), __imul__())

  • Division: a / b (__truediv__(), __rtruediv__(), __itruediv__())

  • Floor Division: a // b (__floordiv__(), __rfloordiv__(), __ifloordiv__())

__init__(size)

Returns a zero-matirx with specified size.

Parameters

size (int) – Size of the matrix.

Example

>>> from amplify import BinaryMatrix
>>> m = BinaryMatrix(3)
>>> m[0, 1] = 1
>>> m[0, 2] = 2
>>> m[1, 2] = 3
>>> m
[[0, 1, 2],
 [0, 0, 3],
 [0, 0, 0]]

Methods

__init__(size)

Returns a zero-matirx with specified size.

evaluate(self, object)

Evaluates the matrix with array q.

resize(self, size)

Resizes the matrix.

size(self)

Returns the matrix size.

to_BinaryMatrix(self)

Converts the matrix to BinaryMatrix.

to_IsingMatrix(self)

Converts the matrix to IsingMatrix.

to_Poly(self)

Converts the matrix to BinaryPoly.

to_numpy(self)

no docstring

evaluate(self, object)

Evaluates the matrix with array q.

Parameters

object – array-like. The size of arary object should be equal to the matrix size.

Returns

\(q^{T} Q q\)

Example

>>> from amplify import BinaryMatrix
>>> m = BinaryMatrix(3)
>>> m[0, 1] = 1
>>> m[0, 2] = 2
>>> m[1, 2] = 3
>>> m
[[0, 1, 2],
 [0, 0, 3],
 [0, 0, 0]]
>>> m.evaluate([0, 1, 1])
3.0
resize(self: amplify.BinaryMatrix, size: int)None

Resizes the matrix.

Example

>>> from amplify import BinaryMatrix
>>> m = BinaryMatrix(2)
>>> m[0, 1] = 1
>>> m
[[0, 1],
 [0, 0]]
>>> m.resize(3)
>>> m
[[0, 1, 0],
 [0, 0, 0],
 [0, 0, 0]]
size(self: amplify.BinaryMatrix)int

Returns the matrix size.

Returns

size of the matrix

Return type

int

Example

>>> from amplify import BinaryMatrix
>>> m = BinaryMatrix(3)
>>> m.size()
3
to_BinaryMatrix(self: amplify.BinaryMatrix)Tuple[amplify.BinaryMatrix, float]

Converts the matrix to BinaryMatrix.

This function returns the pair of the converted BinaryMatrix \(Q\) and the constant term \(c\) in the converted QUBO formulation.

Returns

\((Q, c)\)

Example

>>> from amplify import BinaryMatrix
>>> m = BinaryMatrix(3)
>>> m[0, 1] = 1
>>> m[0, 2] = 2
>>> m[1, 2] = 3
>>> m
[[0, 1, 2],
 [0, 0, 3],
 [0, 0, 0]]
>>> m.to_BinaryMatrix()
([[0, 1, 2],
 [0, 0, 3],
 [0, 0, 0]], 0.0)
to_IsingMatrix(self: amplify.BinaryMatrix)Tuple[amplify.IsingMatrix, float]

Converts the matrix to IsingMatrix.

By this function, the QUBO formulation \(q^T Q q\) would be transformed to the Ising model formulation :math:s^{mathrm{T}} J s - mathrm{Tr}J + s^{mathrm{T}} cdot operatorname{diag} J`, where \(s\) is a vector of variables and \(J\) is an upper triangular square matrix. The conversion is along \(s = 2q - \left\{1 \right\}\).

This function returns the pair of the converted IsingMatrix \(J\) and the constant term \(c\) in the converted Ising model formulation.

Returns

\((J, c)\)

Example

>>> from amplify import BinaryMatrix
>>> m = BinaryMatrix(3)
>>> m[0, 1] = 1
>>> m[0, 2] = 2
>>> m[1, 2] = 3
>>> m
[[0, 1, 2],
 [0, 0, 3],
 [0, 0, 0]]
>>> m.to_IsingMatrix()
([[0.75, 0.25, 0.5],
 [0, 1, 0.75],
 [0, 0, 1.25]], 1.5)
to_Poly(self: amplify.BinaryMatrix)amplify.BinaryPoly

Converts the matrix to BinaryPoly.

Returns

polynomial expression of the matrix

Return type

BinaryPoly

Example

>>> from amplify import BinaryMatrix
>>> m = BinaryMatrix(3)
>>> m[0, 1] = 1
>>> m[0, 2] = 2
>>> m[1, 2] = 3
>>> m
[[0, 1, 2],
 [0, 0, 3],
 [0, 0, 0]]
>>> m.to_Poly()
q_0 q_1 + 2.000000 q_0 q_2 + 3.000000 q_1 q_2
to_numpy(self: amplify.BinaryMatrix)numpy.ndarray[numpy.float64]

no docstring