Ejemplo n.º 1
0
def dense_graph_calculate_hamiltonian(W, dtype = None) :
    """ Calculate hamiltonian from given QUBO.

    Args:
      numpy.ndarray W : QUBO, W should be a upper/lower triangular or symmetric matrix.
      numpy.dtype dtype : Numerical precision, numpy.float32 or numpy.float64.

    Returns:
      tuple: tuple containing Hamiltonian.
        h(vector as numpy.array), J(2-D symmetric matrix as numpy.array), c(scalar value)
    """
 
    checkers.dense_graph.qubo(W)
    W = symmetrize(W)

    N = W.shape[0]
    h = np.ndarray((N), dtype=np.float64)
    W4 = - 0.25 * W

    for i in range(N) :
        h[i] = - 0.5 * np.sum(W[i])
    c = np.sum(W4) + np.sum(W4.diagonal())

    J = W4
    for i in range(0, N) :
        J[i][i] = 0

    return h, J, c
Ejemplo n.º 2
0
def dense_graph_calculate_E_from_spin(cext, cobj, h, J, c, q, dtype):
    h, J = common.fix_type([h, J], dtype)
    J = common.symmetrize(J)
    q = common.fix_type(q, np.int8)
    checkers.dense_graph.hJc(h, J, c)
    checkers.dense_graph.q(J, q)

    E = np.ndarray((1), dtype)
    cext.dense_graph_calculate_E_from_spin(cobj, E, h, J, c, q, dtype)
    return E[0]
Ejemplo n.º 3
0
def dense_graph_batch_calculate_E_from_spin(cext, cobj, h, J, c, q, dtype):
    h, J = common.fix_type([h, J], dtype)
    J = common.symmetrize(J)
    if len(q.shape) == 1:
        q = q.reshape(1, -1)
    checkers.dense_graph.hJc(h, J, c)
    checkers.dense_graph.qbatch(J, q)

    E = np.empty([q.shape[0]], dtype)
    cext.dense_graph_batch_calculate_E_from_spin(cobj, E, h, J, c, q, dtype)
    return E
Ejemplo n.º 4
0
def dense_graph_calculate_hamiltonian(cext, cobj, W, dtype):
    W = common.fix_type(W, dtype)
    W = common.symmetrize(W)
    checkers.dense_graph.qubo(W)

    N = W.shape[0]
    h = np.empty((N), dtype)
    J = np.empty((N, N), dtype)
    c = np.empty((1), dtype)
    cext.dense_graph_calculate_hamiltonian(cobj, h, J, c, W, dtype)
    return h, J, c[0]
Ejemplo n.º 5
0
def dense_graph_calculate_E(cext, cobj, W, x, dtype):
    W = common.fix_type(W, dtype)
    W = common.symmetrize(W)
    x = common.fix_type(x, np.int8)

    checkers.dense_graph.qubo(W)
    checkers.dense_graph.x(W, x)

    E = np.ndarray((1), dtype)
    cext.dense_graph_calculate_E(cobj, E, W, x, dtype)
    return E[0]
Ejemplo n.º 6
0
def dense_graph_batch_calculate_E(cext, cobj, W, x, dtype):
    W = common.fix_type(W, dtype)
    W = common.symmetrize(W)
    x = common.fix_type(x, np.int8)
    if len(x.shape) == 1:
        x = x.reshape(1, -1)
    checkers.dense_graph.qubo(W)
    checkers.dense_graph.xbatch(W, x)

    E = np.empty((x.shape[0]), dtype)
    cext.dense_graph_batch_calculate_E(cobj, E, W, x, dtype)
    return E
Ejemplo n.º 7
0
    def set_qubo(self, W, optimize = sqaod.minimize) :
        """ set QUBO.

        Args:
          numpy.ndarray W :
            QUBO matrix.  W should be a sqaure matrix.
            Upper/Lower triangular matrices or symmetric matrices are accepted.
          optimize : optimize direction, `sqaod.maximize, sqaod.minimize <preference.html#sqaod-maximize-sqaod-minimize>`        """
        checkers.dense_graph.qubo(W)
        W = symmetrize(W)
        N = W.shape[0]
        self._W = optimize.sign(W)
        self._N = N
        self._x = []
        self._optimize = optimize
Ejemplo n.º 8
0
def dense_graph_calculate_E_from_spin(h, J, c, q, dtype = None) :
    """ Calculate desne graph QUBO energy from spins.

    Args:
      (numpy.ndarray) h, J, c : 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}.
      numpy.dtype dtype : Numerical precision, numpy.float32 or numpy.float64

    Returns:
      floating point number : QUBO energy
    """
    checkers.dense_graph.hJc(h, J, c)
    checkers.dense_graph.q(J, q)
    J = symmetrize(J)
    
    return - c - np.dot(h, q) - np.dot(q, np.matmul(J, q.T))
Ejemplo n.º 9
0
    def set_hamiltonian(self, h, J, c):
        """ set Hamiltonian.

        Args:
          numpy.ndarray : h(vector), J(matrix)
          floating point number : c

          Shapes for h, J must be (N), (N, N) where N is problem size.

          J must be a upper/lower triangular or symmetric matrix.
        """

        checkers.dense_graph.hJc(h, J, c)
        J = symmetrize(J)
        self._optimize = sqaod.minimize
        self._h, self._J, self._c = h, J, c
        self._N = J.shape[0]
        self._m = self._N // 2
Ejemplo n.º 10
0
    def set_qubo(self, W, optimize=sqaod.minimize):
        """ set QUBO.

        Args:
          numpy.ndarray W :
            QUBO matrix.  W should be a sqaure matrix.
            Upper/Lower triangular matrices or symmetric matrices are accepted.
          optimize : optimize direction, `sqaod.maximize, sqaod.minimize <preference.html#sqaod-maximize-sqaod-minimize>`        """

        checkers.dense_graph.qubo(W)
        W = symmetrize(W)

        h, J, c = formulas.dense_graph_calculate_hamiltonian(W)
        self._optimize = optimize
        self._h, self._J, self._c = optimize.sign(h), optimize.sign(
            J), optimize.sign(c)
        self._N = W.shape[0]
        self._m = self._N // 2
Ejemplo n.º 11
0
def dense_graph_batch_calculate_E_from_spin(h, J, c, q, dtype = None) :
    """ Batched version of the function to calculate dense graph QUBO energy from spins.

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

    Returns:
      QUBO energy
    """
    checkers.dense_graph.hJc(h, J, c)
    checkers.dense_graph.qbatch(J, q)
    J = symmetrize(J)
    
    if len(q.shape) == 1:
        q = q.reshape(1, -1)
    return - c - np.matmul(h, q.T) - np.sum(q.T * np.matmul(J, q.T), 0)
Ejemplo n.º 12
0
def dense_graph_batch_calculate_E(W, x, dtype = None) :
    """ Batched version of the function to calculate dense graph QUBO energy from bits.

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

    Returns:
      QUBO energy
    """
    
    checkers.dense_graph.qubo(W)
    checkers.dense_graph.xbatch(W, x)
    W = symmetrize(W)
    
    if len(x.shape) == 1:
        x = x.reshape(1, -1)
    return np.sum(x * np.matmul(W, x.T).T, 1)
Ejemplo n.º 13
0
def dense_graph_calculate_E(W, x, dtype = None) :
    """ Calculate desne graph QUBO energy from bits.

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

    Returns:
      QUBO energy
    """
    
    checkers.dense_graph.qubo(W)
    checkers.dense_graph.x(W, x)
    W = symmetrize(W)

    if len(x.shape) != 1 :
        if x.shape[0] != 1 :
            raise Exception('Wrong dimention of x')
        x = x.reshape(-1)
    return np.dot(x, np.matmul(W, x.T))