def qaoa(gb, *a): G, C, M, k, p = a[0], a[1], a[2], a[3], a[4] state = common.dicke(len(G.nodes), k) for i in range(p): state = common.phase_separator(state, C, gb[i]) state = common.mixer(state, M, gb[p + i]) return -common.expectation(G, state)
def qaoa(p, g_list, b_list): global G, C, M, k state = np.zeros(2**len(G.nodes)) state = common.dicke(state, G, k) for i in range(p): state = common.phase_separator(state, C, g_list[i]) state = common.ring_mixer(state, M, b_list[i]) return common.expectation(G, state)
def qaoa(gamma, beta, G, C, M, k, gi, flag): state = common.dicke(len(G.nodes), k) state = common.phase_separator(state, C, gamma) state = common.mixer(state, M, beta) if flag == 0: return common.prob(G, state, gi) else: return common.expectation(G, state)
def qaoa_initial(gb, G, C, M, k, p, m): size = int(comb(len(G.nodes), k)) state = np.zeros(size) state[m] = 1 for i in range(p): state = common.phase_separator(state, C, gb[i]) state = common.mixer(state, M, gb[p + i]) return -common.expectation(G, k, state)
def temp_map(G, k, p, gi, exp_opt, prev, angles): num_steps = 30 gamma = 0 beta = 0 g_list, grid, graph_angles = [], [], [] grid_max = 0 fig, ax = plt.subplots() C = common.create_C(G) M = common.create_complete_M(len(G.nodes)) print('0/' + str(num_steps) + '\t' + str(datetime.datetime.now().time())) for i in range(0, num_steps): for j in range(0, num_steps): if p > 1: graph_angles.extend(prev[0]) graph_angles.append(gamma) if p > 1: graph_angles.extend(prev[1]) graph_angles.append(beta) state = dicke_ps_complete.qaoa([G, C, M, k, p], graph_angles) exp = common.expectation(G, state) g_list.append(exp) if grid_max < exp: grid_max = exp gamma += pi/(2*(num_steps-1)) graph_angles = [] beta += pi/(num_steps-1) gamma = 0 grid.append(g_list) g_list = [] print(str(i+1) + '/' + str(num_steps) + '\t' + str(datetime.datetime.now().time())) grid = list(reversed(grid)) print('-------------- max grid <C>: ' + str(grid_max)) print('-------------- max optm <C>: ' + str(exp_opt)) im = ax.imshow(grid, aspect='auto', extent=(0, pi/2, 0, pi), interpolation='gaussian', cmap=cm.inferno_r) cbar = ax.figure.colorbar(im, ax=ax) cbar.ax.set_ylabel('$\\langle C \\rangle$', rotation=-90, va="bottom") axes = plt.gca() axes.set_ylim([0, pi]) axes.set_xlim([0, pi/2]) plt.xlabel('$\\gamma$') plt.ylabel('$\\beta$') plt.title('$\\beta \\ vs \\ \\gamma$, opt_exp=' + str(exp_opt) + '\nn=' + str(len(G.nodes)) + ', k=' + str(k) + \ ', p=' + str(p) + ', grid_size=' + str(num_steps) + 'x' + str(num_steps) + ', gi=' + str(gi)) plt.scatter(angles[0], angles[1], s=50, c='yellow', marker='o') #plt.show() path = '3-reg/' + str(gi) + '/' if not os.path.exists(path): os.mkdir(path) plt.savefig(path + str(p) + '.png') plt.cla()
def write_grid(gi, p): G = nx.read_gpickle('../benchmarks/atlas/' + str(gi) + '.gpickle') C = common.create_C(G) M = common.create_complete_M(len(G.nodes)) k = int(len(G.nodes) / 2) num_steps = 50 gamma, beta = 0, 0 grid, g_list = [], [] grid_max = 0 fig, ax = plt.subplots() print('0/' + str(num_steps) + '\t' + str(datetime.datetime.now().time())) for i in range(0, num_steps): for j in range(0, num_steps): state = dicke_ps_complete.qaoa([G, C, M, k, p], [gamma, beta]) exp = common.expectation(G, state) g_list.append(exp) if grid_max < exp: grid_max = exp #print('g: ' + str(gamma) + ', b: ' + str(beta) + ', exp: ' + str(exp)) gamma += pi / (2 * (num_steps - 1)) beta += pi / (num_steps - 1) gamma = 0 grid.append(g_list) g_list = [] print( str(i + 1) + '/' + str(num_steps) + '\t' + str(datetime.datetime.now().time())) grid = list(reversed(grid)) print('-------------- max grid <C>: ' + str(grid_max)) im = ax.imshow(grid, aspect='auto', extent=(0, pi / 2, 0, pi), interpolation='gaussian', cmap=cm.inferno_r) cbar = ax.figure.colorbar(im, ax=ax) cbar.ax.set_ylabel('$\\langle C \\rangle$', rotation=-90, va="bottom") plt.xlabel('$\\gamma$') plt.ylabel('$\\beta$') plt.title('$\\beta \\ vs \\ \\gamma$\nn=' + str(len(G.nodes)) + ', k=' + str(k) + \ ', p=' + str(p) + ', grid_size=' + str(num_steps) + 'x' + str(num_steps) + ', gi=' + str(gi)) #plt.scatter(opt[0], opt[1], s=50, c='yellow', marker='o') folder = 'grid/' if not os.path.exists(folder): os.mkdir(folder) pickle.dump([[len(G.nodes), k, p, num_steps, gi], grid], open(folder + str(gi) + '.grid', 'wb')) plt.show()
def work(gi, p, s): G = nx.read_gpickle('../benchmarks/atlas/' + str(gi) + '.gpickle') C = common.create_C(G) M = common.create_complete_M(len(G.nodes)) k = int(len(G.nodes) / 2) best_exp = -1 for j in range(s): angles = [ random.uniform(0, pi / 2) if i < p else random.uniform(0, pi) for i in range(2 * p) ] exp = common.expectation( G, dicke_ps_complete.qaoa([G, C, M, k, p], angles)) if exp > best_exp: best_exp = exp print('rank: ' + str(rank) + ', best_exp: ' + str(best_exp)) return best_exp
def monte_carlo(gi, p, s, num_samples): G = nx.read_gpickle('../benchmarks/atlas/' + str(gi) + '.gpickle') C = common.create_C(G) M = common.create_complete_M(len(G.nodes)) k = int(len(G.nodes) / 2) data = [] for i in range(num_samples): max_exp = -1 for j in range(s): angles = [ random.uniform(0, pi / 2) if i < p else random.uniform(0, pi) for i in range(2 * p) ] exp = common.expectation( G, dicke_ps_complete.qaoa([G, C, M, k, p], angles)) if exp > max_exp: max_exp = exp data.append(max_exp) return data
def qaoa(gb, G, C, M, k, p): state = common.dicke(len(G.nodes), k) for i in range(p): state = common.phase_separator(state, C, gb[i]) state = common.mixer(state, M, gb[p + i]) return -common.expectation(G, k, state)
def qaoa(gamma, beta, G, C, M, k): state = common.dicke(len(G.nodes), k) state = common.phase_separator(state, C, gamma) state = common.mixer(state, M, beta) return common.expectation(G, k, state)
def opt(args, *a): state = qaoa(a[0], a[1], a[2], a[3], a[4], args[0], args[1]) return -common.expectation(a[0], state)
def opt(args, *extras): state = qaoa(extras[0], extras[1], extras[2], extras[3], extras[4], extras[5], args[0], args[1], args[2]) return -common.expectation(extras[0], state)
def opt_restricted(args, *a): state = qaoa_restricted(a, args) return -common.expectation(a[0], state)
def opt(args, *a): state = qaoa(a, args) return -common.expectation(a[0], state)