Formulas

Formulas modules implement common and shared functions for solvers such as
  • QUBO -> Hamiltonian conversion
  • Energy calculation frmm bits or spins including batched versions.

There are 3 versions of formulas modules in sqaod.py, sqaod.cpu and sqaod.cuda.

The sqoad.py.formulas module has reference implementations. Other formuas modules in sqaod.cpu and sqaod.cuda are accleretaed by CPU or CUDA respectively.

sqaod.py.formulas

note:A parameter, dtype, has None as the default value, which is simply ignored in sqaod.py.formulas. Dtype paramter here is to keep function sigunatures being the same as those in other modules.
sqaod.py.formulas.dense_graph_calculate_hamiltonian(W, dtype=None)[source]

Calculate hamiltonian from given QUBO.

Parameters:
  • W (numpy.ndarray) – QUBO, W should be a upper/lower triangular or symmetric matrix.
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

tuple containing Hamiltonian.

h(vector as numpy.array), J(2-D symmetric matrix as numpy.array), c(scalar value)

Return type:

tuple

sqaod.py.formulas.dense_graph_calculate_E(W, x, dtype=None)[source]

Calculate desne graph QUBO energy from bits.

Parameters:
  • W (numpy.array) – QUBO, W should be a upper/lower triangular or symmetric matrix.
  • x (numpy.ndarray) – array of bit {0, 1}.
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

QUBO energy

sqaod.py.formulas.dense_graph_batch_calculate_E(W, x, dtype=None)[source]

Batched version of the function to calculate dense graph QUBO energy from bits.

Parameters:
  • W (numpy.ndarray) – QUBO, W should be a upper/lower triangular or symmetric matrix.
  • x (numpy.ndarray) – bit arrays repsesented as 2-D matrix(n_trotters x n_bits).
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64
Returns:

QUBO energy

sqaod.py.formulas.dense_graph_calculate_E_from_spin(h, J, c, q, dtype=None)[source]

Calculate desne graph QUBO energy from spins.

Parameters:
  • h, J, c ((numpy.ndarray)) – Hamiltonian h(1-D vector), W(sqauare matrix), c(scalar). W must be a upper/lower triangular or symmetric matrix.
  • q (numpy.ndarray) – array of spin {-1, 1}.
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64
Returns:

QUBO energy

Return type:

floating point number

sqaod.py.formulas.dense_graph_batch_calculate_E_from_spin(h, J, c, q, dtype=None)[source]

Batched version of the function to calculate dense graph QUBO energy from spins.

Parameters:
  • h, J, c (numpy.ndarray) – Hamiltonian h(1-D vector), W(sqauare matrix), c(scalar). W must be a upper/lower triangular or symmetric matrix.
  • q (numpy.ndarray) – bit arrays repsesented as 2-D matrix(n_trotters x n_bits).
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

QUBO energy

sqaod.py.formulas.bipartite_graph_calculate_hamiltonian(b0, b1, W, dtype=None)[source]

Calculate hamiltonian from given QUBO.

Parameters:
  • b0, b1, W (numpy.ndarray) – Bipartite graph QUBO.
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64
Returns:

tuple containing Hamiltonian.

h0(1-D numpy.array), h1(1-D numpy.array), J(matrix as numpy.array), c(scalr value).

Return type:

tuple

sqaod.py.formulas.bipartite_graph_calculate_E(b0, b1, W, x0, x1, dtype=None)[source]

Calculate bipartite graph QUBO energy from bits.

Parameters:
  • b0, b1, W (numpy.array) – QUBO. b0(vector as numpy.array), b1(vector as numpy.array), W(matrix as numpy.array). W should be a upper/lower triangular or symmetric matrix.
  • x0, x1 (numpy.ndarray) – array of bit {0, 1}.
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

QUBO energy

sqaod.py.formulas.bipartite_graph_batch_calculate_E(b0, b1, W, x0, x1, dtype=None)[source]

Batched version of the function to calculate dense graph QUBO energy from bits.

Parameters:
  • b0, b1, W (numpy.array) – QUBO. b0(vector as numpy.array), b1(vector as numpy.array), W(matrix as numpy.array). W should be a upper/lower triangular or symmetric matrix.
  • x0, x1 (numpy.ndarray) – bit arrays repsesented as 2-D matrix. x0 shape is n_trotters x n_bits_0, and x1 shape is n_trotters x n_bits_1
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

QUBO energy

sqaod.py.formulas.bipartite_graph_batch_calculate_E_2d(b0, b1, W, x0, x1, dtype=None)[source]

2D batched version of the function to calculate dense graph QUBO energy from bits.

Parameters:
  • b0, b1, W (numpy.array) – QUBO. b0(vector as numpy.array), b1(vector as numpy.array), W(matrix as numpy.array). W should be a upper/lower triangular or symmetric matrix.
  • x0, x1 (numpy.ndarray) – bit arrays repsesented as 2-D matrix. x0 shape is n_trotters x n_bits_0, and x1 shape is n_trotters x n_bits_1
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

QUBO energy as a matrix, whose shape is (len(x1), (len(x0)).

This function calculates QUBO energy for all combinations of elements x0 and x1, used in bipartite graph brute-force searchers.

sqaod.py.formulas.bipartite_graph_calculate_E_from_spin(h0, h1, J, c, q0, q1, dtype=None)[source]

Calculate bipartite graph QUBO energy from spins.

Parameters:
  • b0, b1, W (numpy.array) – QUBO. b0(vector as numpy.array), b1(vector as numpy.array), W(matrix as numpy.array). W should be a upper/lower triangular or symmetric matrix.
  • x0, x1 (numpy.ndarray) – array of bit {0, 1}.
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

QUBO energy

sqaod.py.formulas.bipartite_graph_batch_calculate_E_from_spin(h0, h1, J, c, q0, q1, dtype=None)[source]

Batched version of the function to calculate dense graph QUBO energy from bits.

Parameters:
  • b0, b1, W (numpy.array) – QUBO. b0(vector as numpy.array), b1(vector as numpy.array), W(matrix as numpy.array). W should be a upper/lower triangular or symmetric matrix.
  • x0, x1 (numpy.ndarray) – bit arrays repsesented as 2-D matrix. x0 shape is n_trotters x n_bits_0, and x1 shape is n_trotters x n_bits_1
  • dtype (numpy.dtype) – Numerical precision, numpy.float32 or numpy.float64.
Returns:

QUBO energy

sqaod.cpu.formulas, sqaod.cuda.formulas

Functions in sqaod.cpu.formulas sqaod.cuda.formulas have the same sinatures and functionlities, thus, documentations are ommited here.

One exception is the dtype parameter. The dtype paramter does not have its default value, and should be explicitly specified. Allowed values are numpy.float32 and numpy.float64. Floating-point parameters (such as vector, matrix, scalars) given to these functions will be converted to the type specified by dtype before calculation.