forked from zlianghahaha/QPulse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvqe.py
150 lines (140 loc) · 6.54 KB
/
vqe.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import qiskit
import numpy as np
import pathos.multiprocessing as multiprocessing
from utils import *
from itertools import repeat
from qiskit.test.mock import *
from qiskit import pulse, IBMQ
from qiskit.compiler import assemble
def vqe_napa(tune_param, fixed_param, partial_list, pauli_dict, ansatz_seed, tune_freq, n_qubit, backend, max_jobs, n_shot):
merged_param = merge(partial_param=tune_param, fixed_param=fixed_param, partial_list=partial_list)
print("All parameters: ", merged_param)
p = multiprocessing.Pool(max_jobs)
prepulse = ansatz_init(merged_param=merged_param,backend=backend,ansatz_seed=ansatz_seed, option=0, n_qubit=n_qubit)
keys = [key for key in pauli_dict]
values = [pauli_dict[key] for key in pauli_dict]
expect_values = p.starmap(one_in_all,zip(repeat(prepulse),repeat(n_qubit),repeat(n_shot),repeat(backend),keys,values))
p.close()
p.join()
print("E for cur_iter: ",sum(expect_values))
return sum(expect_values)
#def generate_firstlayer(backend, num_qubits, amp, angle):
#inance,params,args=(pauli_dict,n_qubit,backend,max_jobs,n_shot,)
def finance(params,pauli_dict,n_qubit,backend,max_jobs,n_shot, pulse_id):
print("params in def finance in vqe.py: ", params)
# assert(len(params)%2==0)
width_len = int(len(params)-1*(n_qubit-1))
split_ind = int(width_len/2)
amp = np.array(params[:split_ind])
amp_fixed = np.array(params[:int(len(params)/2)-1*(n_qubit -1)])
angle_fixed = np.array(params[int(len(params)/2)-1*(n_qubit-1):width_len])*np.pi*2
angle = np.array(params[split_ind:width_len])*np.pi*2
width_1 = (np.array(params[width_len:]))
num_items = (1024 - 256) // 16 + 1
width_norm = (width_1 - 256) / (1024 - 256)
width_norm = np.clip(width_norm, 0, 1)
width = (np.round(width_norm * (num_items - 1)) * 16 + 256).astype(int)
amp_fixed = amp_fixed.tolist()
angle_fixed = angle_fixed.tolist()
amp = amp.tolist()
angle = angle.tolist()
width = width.tolist()
p = multiprocessing.Pool(max_jobs)
keys = [key for key in pauli_dict]
values = [pauli_dict[key] for key in pauli_dict]
if n_qubit == 4:
if pulse_id ==1:
prepulse = HE_pulse(backend,amp,angle,width)
elif pulse_id ==2:
prepulse = HE_pulsefixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id ==3:
prepulse = Decaylayer_pulse(backend,amp,angle,width)
elif pulse_id == 4:
prepulse = Decaylayer_pulsefixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id == 5:
prepulse = block_pulse(backend,amp,angle,width)
elif pulse_id == 6:
prepulse = block_pulsefixedamp(backend,amp_fixed,angle_fixed,width)
else:
print('Pulse ID is wrong. Please type 1 - 6.')
elif n_qubit == 2:
if pulse_id ==1:
prepulse = HE_pulse2q(backend,amp,angle,width)
elif pulse_id ==2:
prepulse = HE_pulse2qfixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id ==3:
prepulse = Decaylayer_pulse2q(backend,amp,angle,width)
elif pulse_id == 4:
prepulse = Decaylayer_pulse2qfixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id == 5:
prepulse = block_dressedpulse2q(backend,amp,angle,width)
elif pulse_id == 6:
prepulse = block_dressedpulse2qfixedamp(backend,amp_fixed,angle_fixed,width)
else:
print('Pulse ID is wrong. Please type 1 - 6.')
else:
print('Number of qubits is out of range, current we support 2 qubits and 4 qubits applications, more qubits will be adds on.')
expect_values = p.starmap(finance_one,zip(repeat(prepulse),repeat(n_qubit),repeat(n_shot),repeat(backend),keys,values))
p.close()
p.join()
print("E for cur_iter: ",sum(expect_values))
return sum(expect_values)
def vqe(params,pauli_dict,n_qubit,backend,max_jobs,n_shot,pulse_id):
print("params in def chemistry in vqe.py: ", params)
# assert(len(params)%2==0)
width_len = int(len(params)-1*(n_qubit-1))
split_ind = int(width_len/2)
amp = np.array(params[:split_ind])
amp_fixed = np.array(params[:int(len(params)/2)-1*(n_qubit-1)])
angle_fixed = np.array(params[int(len(params)/2)-1*(n_qubit-1):width_len])*np.pi*2
angle = np.array(params[split_ind:width_len])*np.pi*2
width_1 = (np.array(params[width_len:]))
num_items = (1024 - 256) // 16 + 1
width_norm = (width_1 - 256) / (1024 - 256)
width_norm = np.clip(width_norm, 0, 1)
width = (np.round(width_norm * (num_items - 1)) * 16 + 256).astype(int)
amp_fixed = amp_fixed.tolist()
angle_fixed = angle_fixed.tolist()
amp = amp.tolist()
angle = angle.tolist()
width = width.tolist()
p = multiprocessing.Pool(max_jobs)
keys = [key for key in pauli_dict]
values = [pauli_dict[key] for key in pauli_dict]
if n_qubit == 4:
if pulse_id ==1:
prepulse = HE_pulse(backend,amp,angle,width)
elif pulse_id ==2:
prepulse = HE_pulsefixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id ==3:
prepulse = Decaylayer_pulse(backend,amp,angle,width)
elif pulse_id == 4:
prepulse = Decaylayer_pulsefixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id == 5:
prepulse = block_pulse(backend,amp,angle,width)
elif pulse_id == 6:
prepulse = block_pulsefixedamp(backend,amp_fixed,angle_fixed,width)
else:
print('Pulse ID is wrong. Please type 1 - 6.')
elif n_qubit == 2:
if pulse_id ==1:
prepulse = HE_pulse2q(backend,amp,angle,width)
elif pulse_id ==2:
prepulse = HE_pulse2qfixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id ==3:
prepulse = Decaylayer_pulse2q(backend,amp,angle,width)
elif pulse_id == 4:
prepulse = Decaylayer_pulse2qfixedamp(backend,amp_fixed,angle_fixed,width)
elif pulse_id == 5:
prepulse = block_dressedpulse2q(backend,amp,angle,width)
elif pulse_id == 6:
prepulse = block_dressedpulse2qfixedamp(backend,amp_fixed,angle_fixed,width)
else:
print('Pulse ID is wrong. Please type 1 - 6.')
else:
print('Number of qubits is out of range, current we support 2 qubits and 4 qubits applications, more qubits will be adds on.')
expect_values = p.starmap(vqe_one,zip(repeat(prepulse),repeat(n_qubit),repeat(n_shot),repeat(backend),keys,values))
p.close()
p.join()
print("E for cur_iter: ",sum(expect_values))
return sum(expect_values)