forked from zlianghahaha/QPulse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (123 loc) · 5.8 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import torch
import argparse
import itertools
import numpy as np
from vqe import *
from utils import *
from random import random
from qiskit import pulse, QuantumCircuit, IBMQ
from qiskit.providers.fake_provider import *
from scipy.optimize import minimize, LinearConstraint
import pdb
def IBMQ_ini(backend_str):
IBMQ.save_account('c7a71bc4b2858304571d35eb5e371e449e5c346110b36c744f51b65388d4ac1b7468740d58d6fcd44967b2080d02af986cc22f2406a84813bbabd9ff5bf06315', overwrite=True)
IBMQ.load_account() # Load account from disk
IBMQ.providers() # List all available providers
provider = IBMQ.get_provider(
hub="ibm-q-ornl", group="ornl", project="csc509"
)
#backend = provider.get_backend('ibmq_guadalupe')
#IBMQ.load_account()
# provider = IBMQ.get_provider(hub="ibm-q-research", group="MIT-1", project="main")
# provider = IBMQ.get_provider(hub='ibm-q-ornl', group='anl', project='csc428')
#provider = IBMQ.get_provider(hub="ibm-q-research", group="mass-inst-tech-1", project="main")
backend = provider.get_backend(backend_str)
return backend
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--backend', type=str, default='ibmq_quito',help='name of the backend(not a simulator)')
parser.add_argument('--optimizer',type=str, default='COBYLA', help='name of the non-gradient optimizer')
parser.add_argument('--policy', type=str, default='cxrx', help='name of the pulse growth policy')
parser.add_argument('--application', type=str, default='chemistry', help='name of the benchmark application')
parser.add_argument('--pulse_id', type=int, default=1, help='indicate the design space at pulse level.')
parser.add_argument('--molecule', type=str, default='H2', help='name of the molecules')
parser.add_argument('--n_assets', type=int, default=2, help='number of assets')
parser.add_argument('--tune_freq',type=bool, default=False, help='specify if frequencies are tuned')
parser.add_argument('--n_iter', type=int, default=100, help='number of training iterations')
parser.add_argument('--n_shot', type=int, default=1024, help='number of shots for measurement')
parser.add_argument('--n_step', type=int, default=1, help='number of pulse_layers')
parser.add_argument('--max_jobs', type=int, default=8, help='number of max_jobs for multiprocessing')
parser.add_argument('--rhobeg', type=float, default=0.1 , help='rhobeg for non-gradient optimizer')
#parser.add_argument('--n_parameters', type=int, default=7, help='number of parameters in pulse ansatz')
args = parser.parse_args()
backend_str = args.backend
optimizer = args.optimizer
policy = args.policy
application = args.application
pulse_id = args.pulse_id
molecule = args.molecule
assets = args.n_assets
tune_freq = args.tune_freq
n_iter = args.n_iter
n_shot = args.n_shot
n_step = args.n_step
rhobeg = args.rhobeg
max_jobs = args.max_jobs
#parameters = args.n_parameters
seed = 40
np.random.seed(seed)
if('Fake' in backend_str):
backend_sim = ""
exec('backend_sim='+backend_str+'()')
backend = aer.PulseSimulator.from_backend(backend_sim)
else:
backend = IBMQ_ini(backend_str)
torch.manual_seed(seed)
if application == 'chemistry':
if(molecule=='H2'):
dist_list = [0.735] #works on H2
elif(molecule=='HeH'):
dist_list = [1] #works: on HeH
elif(molecule=='LiH'):
dist_list = [1.5] #works on: LiH
elif(molecule=='H2O'):
dist_list = [1.5] #works on: LiH
else:
print('Molecule not Found')
pauli_dict, n_qubit = pauli_dict_dist(dist=dist_list[0],molecule=molecule)
if pulse_id == 1:
parameters = 5*n_qubit - 3
elif pulse_id == 2:
parameters = 2*(2*n_qubit - 1)
elif pulse_id == 3:
parameters = 6*n_qubit - 3
elif pulse_id == 4:
parameters = 5*n_qubit -2
elif pulse_id == 5:
parameters = 9 * n_qubit -7
elif pulse_id == 6:
parameters = 8*n_qubit - 6
else:
print('Please select correct Pulse_ID.')
params = np.zeros(parameters)
LC = gen_LC_finance(n_qubit, parameters)
#pauli_dict = finance_dict(2,seed,0.5)
finance_res = minimize(vqe,params,args=(pauli_dict,n_qubit,backend,max_jobs,n_shot,pulse_id),method=optimizer,
constraints=LC,options={'rhobeg':rhobeg,'maxiter':n_iter,'disp':True})
print(pauli_dict)
print('The optimized loss func value: {}'.format(finance_res.fun))
elif application == 'finance':
pauli_dict = finance_dict(assets,seed,0.5)
n_qubit = assets
if pulse_id == 1:
parameters = 5*n_qubit - 3
elif pulse_id == 2:
parameters = 2*(2*n_qubit - 1)
elif pulse_id == 3:
parameters = 6*n_qubit - 3
elif pulse_id == 4:
parameters = 5*n_qubit -2
elif pulse_id == 5:
parameters = 9 * n_qubit -7
elif pulse_id == 6:
parameters = 8*n_qubit - 6
else:
print('Please select correct Pulse_ID.')
params = np.zeros(parameters)
LC = gen_LC_finance(assets, parameters)
finance_res = minimize(finance,params,args=(pauli_dict,assets,backend,max_jobs,n_shot,pulse_id),method=optimizer,
constraints=LC,options={'rhobeg':rhobeg,'maxiter':n_iter,'disp':True})
print(pauli_dict)
print('The optimized loss func value: {}'.format(finance_res.fun))
else:
print('Application not found.')