|
| 1 | +from term_grouping import * |
| 2 | +from collections import Counter |
| 3 | +import time |
| 4 | +import numpy as np |
| 5 | +import argparse |
| 6 | + |
| 7 | + |
| 8 | +def parse_args(): |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + parser.add_argument('-s','--savepath',type=str,default=None,help='Path to save data') |
| 11 | + args = parser.parse_args() |
| 12 | + return args |
| 13 | + |
| 14 | + |
| 15 | +def many_trials(filename, commutativity_type, numtrials): |
| 16 | + H = parseHamiltonian(filename) |
| 17 | + ops = [term[1] for term in H] |
| 18 | + Nq = max([int(op[-1][1:]) for op in ops]) + 1 |
| 19 | + print('--------------') |
| 20 | + print(filename) |
| 21 | + results = [] |
| 22 | + runtimes = [] |
| 23 | + for i in range(numtrials): |
| 24 | + start_time = time.time() |
| 25 | + cliques = genMeasureCircuit(H, Nq, commutativity_type) |
| 26 | + end_time = time.time() |
| 27 | + results += [len(cliques)] |
| 28 | + runtimes += [end_time - start_time] |
| 29 | + return Counter(results), runtimes |
| 30 | + |
| 31 | + |
| 32 | +def main(): |
| 33 | + |
| 34 | + args = parse_args() |
| 35 | + |
| 36 | + Hfiles = ['hamiltonians/H2_6-31g_{}_0.7_AS4.txt'.format(e) for e in ['JW','BK','BKSF','BKT','PC']] |
| 37 | + |
| 38 | + results = [] |
| 39 | + for f in Hfiles: |
| 40 | + for comm_type, comm_str in zip([QWCCommutativity, FullCommutativity],['QWC','FULL']): |
| 41 | + ret, times = many_trials(f, comm_type, 100) |
| 42 | + results += [(f,comm_str,ret,times)] |
| 43 | + |
| 44 | + print('\n\n--------------') |
| 45 | + print('All trials finished') |
| 46 | + with open(args.savepath, 'w') as savefile: |
| 47 | + for r in results: |
| 48 | + print(r[0],r[1],r[2],'{:.3f}'.format(np.mean(r[3]))) |
| 49 | + savefile.write('{0} {1} {2} {3:.3f}\n'.format(r[0],r[1],r[2],np.mean(r[3]))) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + main() |
0 commit comments