コード例 #1
0
def work(gi, p, s):
    low_g, up_g = 0, 2*pi
    low_b, up_b = 0, pi/2
    G, C, M, k = common.get_complete(gi)
    sample_g, sample_b = common.MLHS(p, s, low_g, up_g, low_b, up_b)
    bounds = [[low_g,up_g] if j < p else [low_b,up_b] for j in range(2*p)]
    eps = 1e-3
    best_exp, best_angles = -1, []
    total_eval = 0
    for i in range(s):
        angles = sample_g[i]
        angles.extend(sample_b[i])
        options = {'disp': None, 'maxfun': 100, 'gtol': eps, 'ftol': eps}
        optimal = minimize(qaoa, angles, method='L-BFGS-B', bounds=bounds, args=(G, C, M, k, p), options=options)
        total_eval += optimal.nfev
        print('total_eval: ' + str(total_eval))
        if -optimal.fun > best_exp + eps:
            best_exp = -optimal.fun
            best_angles = [list(optimal.x)]
            print('s: ' + str(i) + ', ' + str(-optimal.fun) + ', angles: ' + str(optimal.x))
        elif -optimal.fun > best_exp - eps:
            best_angles.append(list(optimal.x))
    #print(str(rank) + ', ' + str(best) + ', ' + str(angles))
    ''' 
    G, C, M, k = common.get_complete(gi)
    options = {'disp': 1, 'maxfun': 100, 'gtol': 1e-2}
    optimal = minimize(qaoa, np.array([0.5*pi, 0.1*pi]), method='L-BFGS-B', bounds=[[0,2*pi],[0,pi/2]], args=(G, C, M, k, p), options=options)
    print('optimal: ' + str(-optimal.fun))
    print('angles: ' + str(optimal.x))
    return -optimal.fun, optimal.x
    '''
    print('')
    print(best_exp)
    print(best_angles)
    return best_exp, best_angles
コード例 #2
0
def work(gi, p, s):
    G, C, M, k = common.get_stuff(gi)
    sample_g, sample_b = common.MLHS(p, s, 0, pi / 2, 0, pi)
    bounds = [[0, pi / 2] if j < p else [0, pi] for j in range(2 * p)]
    best = -1
    for i in range(s):
        kwargs = {
            'method': 'L-BFGS-B',
            'args': (G, C, M, k, p),
            'bounds': bounds
        }
        optimal = basinhopping(qaoa, [sample_g[i], sample_b[i]],
                               minimizer_kwargs=kwargs,
                               niter=2,
                               disp=False)
        if -optimal.fun > best: best = -optimal.fun
    return best
コード例 #3
0
def work(gi, p, s):
    random.seed(random.randint(1,10000) + rank)
    G, C, M, k = common.get_stuff(gi)
    sample_g, sample_b = common.MLHS(p, s, 0, 0.6, 2.9, pi)
    bounds = [[0,0.6] if j < p else [2.9,pi] for j in range(2*p)]
    eps = 0.001
    best, angles = -1, []
    for i in range(s):
        kwargs = {'method': 'L-BFGS-B', 'args': (G, C, M, k, p), 'bounds': bounds}
        optimal = basinhopping(qaoa, [sample_g[i], sample_b[i]], minimizer_kwargs=kwargs, niter=0, disp=False)
        if -optimal.fun > best + eps:
            best = -optimal.fun
            angles = [list(optimal.x)]
        elif -optimal.fun > best - eps:
            angles.append(list(optimal.x))
    #print(str(rank) + ', ' + str(best) + ', ' + str(angles))
    return best, angles
コード例 #4
0
def work(gi, p, s):
    G, C, M, k = common.get_stuff(gi)
    sample_g, sample_b = common.MLHS(p, s, 0, pi / 2, 0, pi)
    bounds = [[0, pi / 2] if j < p else [0, pi] for j in range(2 * p)]
    eps = 0.001
    best, angles = -1, []
    for i in range(s):
        kwargs = {
            'method': 'L-BFGS-B',
            'args': (G, C, M, k, p),
            'bounds': bounds
        }
        optimal = basinhopping(qaoa, [sample_g[i], sample_b[i]],
                               minimizer_kwargs=kwargs,
                               niter=2,
                               disp=False)
        if -optimal.fun > best + eps:
            best = -optimal.fun
            angles = [list(optimal.x)]
        elif -optimal.fun > best - eps:
            angles.append(list(optimal.x))
    return best, angles
コード例 #5
0
def classical(gi, p, s):
    G, C, M, k = common.get_stuff(gi)
    data = []
    num_trials = 20
    n_iter = 2
    for i in range(s):
        print('\ts = ' + str(i) + '\t' + str(datetime.datetime.now().time()))
        sample_g, sample_b = common.MLHS(p, num_trials, 0, pi / 2, 0, pi)
        init = [[0, pi / 2] if i < p else [0, pi] for i in range(2 * p)]
        exp = -1
        for i in range(num_trials):
            kwargs = {
                'method': 'L-BFGS-B',
                'args': (G, C, M, k, p),
                'bounds': init
            }
            optimal = basinhopping(qaoa, [sample_g[i], sample_b[i]],
                                   minimizer_kwargs=kwargs,
                                   niter=n_iter,
                                   disp=False)
            if -optimal.fun > exp:
                exp = -optimal.fun
        data.append(exp)
    return data