コード例 #1
0
 def __sqa(self, v_0, params):
     h0_sampled, h0_prob = self.__forward(v_0)
     sol = sq.cpu
     if sq.is_cuda_available():
         import sqaod.cuda
         sol = sqaod.cuda
     ann = sol.bipartite_graph_annealer()
     ann.seed(13255)
     ann.set_qubo(self.b, self.c, self.W.T, sq.maximize)
     ann.set_preferences(n_trotters=params.get("trotter", 10))
     ann.prepare()
     ann.randomize_spin()
     Ginit = params.get("Ginit", 5.)
     Gfin = params.get("Gfin", 0.001)
     beta = params.get("beta", 50)
     tau = params.get("tau", 0.99)
     v_sampled = np.zeros(self.n_visible)
     h_sampled = np.zeros(self.n_hidden)
     n_sample = params.get("n_sample", 1)
     for _ in range(n_sample):
         G = Ginit
         while Gfin <= G:
             ann.anneal_one_step(G, beta)
             G *= tau
         xlist = ann.get_x()
         best_index = np.argmax(ann.get_E())
         v_sampled += xlist[best_index][0]
         h_sampled += xlist[best_index][1]
     v_sampled = v_sampled / n_sample
     h_sampled = h_sampled / n_sample
     return v_0.mean(axis=0), h0_prob.mean(axis=0), v_sampled, h_sampled
コード例 #2
0
def solver(arr):
	W = arr
	output = []
	sol = sq.cpu
	if sq.is_cuda_available() :
	    import sqaod.cuda
	    sol = sqaod.cuda

	ann = sol.dense_graph_annealer()
	ann.seed(1)
	ann.set_qubo(W, sq.minimize)
	ann.set_preferences(n_trotters = W.shape[0])

	h, J, c = ann.get_hamiltonian()
	
	output.append(h); output.append(J); output.append(c)
	
	ann.prepare()
	ann.randomize_spin()

	Ginit = 5.
	Gfin = 0.01
	beta = 1. / 0.02
	tau = 0.99

	G = Ginit
	while Gfin <= G :
	    ann.anneal_one_step(G, beta)
	    G *= tau
	    
	E = ann.get_E()
	q = ann.get_q()
	x = ann.get_x()

	summary = sq.make_summary(ann)

	out_summary = []
	nToShow = min(len(summary.xlist), 4)
	for idx in range(nToShow) :
		out_summary.append(summary.xlist[idx])

	
	output.append(out_summary)
	return output, out_summary
コード例 #3
0
from __future__ import print_function
import sqaod
import numpy as np

if sqaod.is_cuda_available():
    import sqaod.cuda
else:
    print('cuda not available')


def output(searcher):
    summary = sqaod.make_summary(searcher)
    print('E {}'.format(summary.E))
    print('Number of solutions : {}'.format(len(summary.xlist)))
    for x in summary.xlist:
        print(x)


def anneal(ann):
    print(ann.__class__)

    Ginit = 5.
    Gfin = 0.01

    nRepeat = 1
    beta = 1. / 0.02
    tau = 0.99

    for loop in range(0, nRepeat):
        sqaod.anneal(ann, Ginit, Gfin, beta)
        output(ann)
コード例 #4
0
import numpy as np
import sqaod as sq
    

N = 8

# 1. set problem.  As an example, a matrix filled with 1. is used.
W = np.ones((N, N))

# 2. choosing solver .
sol = sq.cpu # use CPU searchealer
# If you want to use CUDA, check CUDA availability with sq.is_cuda_available().
if sq.is_cuda_available() :
    import sqaod.cuda
    sol = sqaod.cuda
    
# 3. instanciate solver
search = sol.dense_graph_bf_searcher()

# 4. Setting problem
# Setting W and optimize direction (minimize or maxminize)
# n_trotters is implicitly set to N/4 by dfault.
search.set_qubo(W, sq.maximize)

# 5. (optional) set preferences,
# Set tile_size.  Typical not required.
search.set_preferences(tile_size = W.shape[0])

# altternative for 4. and 5.
# W, optimize direction and tile_size are able to be set on instantiation.