-
Notifications
You must be signed in to change notification settings - Fork 2
/
QAOA+_benchmark.py
103 lines (83 loc) · 3.69 KB
/
QAOA+_benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
Use the benchmark graphs to test the performance of QAOA+
"""
import os, sys, argparse, glob
import numpy as np
from ansatz import qaoa_plus
import pickle, random
from utils.graph_funcs import *
from utils.helper_funcs import *
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-p','--path', type=str, default=None,
help='path to dqva project')
parser.add_argument('--alg', type=str, default='QAOA+',
help='name of algorithm to use')
parser.add_argument('--graph', type=str, default=None,
help='glob path to the benchmark graph(s)')
parser.add_argument('-P', type=int, default=1,
help='P-value for algorithm')
parser.add_argument('--sim', type=str, default='qasm',
help='Choose the simulation backend')
parser.add_argument('--reps', type=int, default=4,
help='Number of repetitions to run')
#parser.add_argument('-m', type=int, default=3,
# help='Number of mixer rounds')
#parser.add_argument('--shots', type=int, default=8192,
# help='Number of shots')
parser.add_argument('-v', type=int, default=1,
help='verbose')
#parser.add_argument('--plim', type=int, default=None,
# help='Limit the number of parameters')
args = parser.parse_args()
return args
def main():
args = parse_args()
DQVAROOT = args.path
if DQVAROOT[-1] != '/':
DQVAROOT += '/'
sys.path.append(DQVAROOT)
all_graphs = glob.glob(DQVAROOT + args.graph)
graph_type = all_graphs[0].split('/')[-2]
savepath = DQVAROOT+'benchmark_results/{}_P{}_{}/'.format(args.alg, args.P, args.sim)
if not os.path.isdir(savepath):
os.mkdir(savepath)
savepath += '{}/'.format(graph_type)
if not os.path.isdir(savepath):
os.mkdir(savepath)
for graphfn in all_graphs:
graphname = graphfn.split('/')[-1].strip('.txt')
cur_savepath = savepath + '{}/'.format(graphname)
if not os.path.isdir(cur_savepath):
os.mkdir(cur_savepath)
G = graph_from_file(graphfn)
print(graphname, G.edges())
nq = len(G.nodes)
for i in range(args.reps):
print('Rep {}/{}'.format(i+1, args.reps))
data_list = []
for Lambda in np.arange(0.1, 10, 0.7):
data_dict = {'lambda':Lambda, 'graph':graphfn}
out = qaoa_plus.solve_mis(args.P, G, Lambda)
# Compute the approximation ratio by pruning the resulting measurements
ratio = qaoa_plus.get_approximation_ratio(out, args.P, G)
data_dict['ratio'] = ratio
ranked_probs = qaoa_plus.get_ranked_probs(args.P, G, out['x'])
for i, rp in enumerate(ranked_probs):
if rp[2]:
data_dict['rank'] = i+1
data_dict['prob'] = rp[1]*100
break
if 'rank' not in data_dict.keys():
data_dict['rank'] = -1
data_dict['prob'] = 0
print('lambda: {:.3f}, ratio = {:.3f}, rank = {}, prob = {:.3f}'.format(
Lambda, ratio, data_dict['rank'], data_dict['prob']))
data_list.append(data_dict)
# Save the results
savename = '{}_{}_P{}_{}_rep{}.pickle'.format(graphname, args.alg, args.P,
args.sim, i+1)
with open(cur_savepath+savename, 'ab') as pf:
pickle.dump(data_list, pf)
if __name__ == '__main__':
main()