-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathheisenberg_kernel_comparison.py
253 lines (199 loc) · 9.54 KB
/
heisenberg_kernel_comparison.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import fnmatch
import itertools as it
import json
import numpy as np
import os
from sklearn.model_selection import cross_val_score
from sklearn import svm
from sklearn.kernel_ridge import KernelRidge
from tqdm import tqdm
from constants import DATA_DIR
from src.data.utils import ints_to_bits_and_recipes
from src.properties.correlation import compute_correlation_matrix_from_shadow
from src.properties.entropy import compute_entropies_from_shadow
DATA_ROOT = os.path.join(DATA_DIR, '2d_heisenberg/')
RESULTS_ROOT = './results/'
_PROP_CORRELATIONS = 'correlations'
_PROP_ENTROPIES = 'entropies'
_RBF_KERNEL = 'rbf'
def main(rows, cols, prop, kernel_name):
assert prop in [_PROP_CORRELATIONS, _PROP_ENTROPIES]
qubits = rows * cols
# load training data
train_hamiltonians, train_couplings, train_shadow_y_values, train_true_y_values = load_data(
rows, cols, 'train',
prop)
# load testing data
test_hamiltonians, test_couplings, test_shadow_y_values, test_true_y_values = load_data(
rows, cols, 'test', prop)
if kernel_name == _RBF_KERNEL:
train_kernel_mat = train_couplings
test_kernel_mat = test_couplings
kernel = 'rbf'
else:
raise ValueError(f'unknown kernel_name {kernel_name}')
# containers for predictions and true correlation matrices
test_predicted_properties = {i: np.zeros(shape=(qubits, qubits)) for i in
test_hamiltonians}
test_true_properties = {i: np.zeros(shape=(qubits, qubits)) for i in
test_hamiltonians}
train_predicted_properties = {i: np.zeros(shape=(qubits, qubits)) for i in
train_hamiltonians}
train_true_properties = {i: np.zeros(shape=(qubits, qubits)) for i in
train_hamiltonians}
# make predictions
pbar = tqdm(total=((qubits + 1) * qubits) / 2,
desc=f'running inference for {prop} with {kernel_name} kernel')
for i in range(qubits):
for j in range(i, qubits):
idx = i * qubits + j
if i == j and prop == _PROP_CORRELATIONS:
test_predictions_instance = np.ones(shape=len(test_hamiltonians))
train_predictions_instance = np.ones(shape=len(train_hamiltonians))
else:
# make predictions
train_predictions_instance, test_predictions_instance = train_and_predict(
train_kernel_mat, test_kernel_mat, train_shadow_y_values[:, idx],
kernel=kernel
)
for hid, pred in enumerate(test_predictions_instance):
test_predicted_properties[hid][i, j] = test_predicted_properties[hid][
j, i] = pred
test_true_properties[hid][i, j] = test_true_properties[hid][j, i] = \
test_true_y_values[hid, idx]
for hid, pred in enumerate(train_predictions_instance):
train_predicted_properties[hid][i, j] = train_predicted_properties[hid][
j, i] = pred
train_true_properties[hid][i, j] = train_true_properties[hid][j, i] = \
train_true_y_values[hid, idx]
pbar.update()
# dump predictions and ground truths
res_root = os.path.join(
RESULTS_ROOT,
f'conditional_heisenberg_{rows}x{cols}/{kernel_name}-kernel/ns1000/results/'
)
props_dir_test = os.path.join(res_root, f'properties/test/model/')
data_dir_test = os.path.join(res_root, f'data/{rows}x{cols}/test/')
props_dir_train = os.path.join(res_root, f'properties/train/model/')
data_dir_train = os.path.join(res_root, f'data/{rows}x{cols}/train/')
if not os.path.exists(os.path.join(props_dir_test, f'{prop}')):
os.makedirs(os.path.join(props_dir_test, f'{prop}'))
if not os.path.exists(os.path.join(props_dir_train, f'{prop}')):
os.makedirs(os.path.join(props_dir_train, f'{prop}'))
if not os.path.exists(data_dir_test):
os.makedirs(data_dir_test)
if not os.path.exists(data_dir_train):
os.makedirs(data_dir_train)
# compute mse for each hamiltonian and dump to props dir
test_mse = {
idx: np.mean((test_predicted_properties[idx] - test_true_properties[idx]) ** 2)
for idx in test_hamiltonians
}
train_mse = {
idx: np.mean(
(train_predicted_properties[idx] - train_true_properties[idx]) ** 2) for idx
in train_hamiltonians
}
print(
f'Test Prediction MSE for {prop} with {kernel_name} kernel: {np.mean(list(test_mse.values()))}')
print(
f'Train Prediction MSE for {prop} with {kernel_name} kernel: {np.mean(list(train_mse.values()))}')
with open(os.path.join(props_dir_test, f'model_{prop}_mse.json'), 'w') as f:
json.dump(test_mse, f)
with open(os.path.join(props_dir_train, f'model_{prop}_mse.json'), 'w') as f:
json.dump(train_mse, f)
# dump individual predictions and ground truths
for idx in test_hamiltonians:
# predictions
np.save(os.path.join(props_dir_test, f'{prop}/{prop}_model_id{idx}.npy'),
test_predicted_properties[idx])
# ground truths
fn_tag = 'correlation_matrix' if prop == _PROP_CORRELATIONS else 'entanglement_entropies'
np.save(os.path.join(data_dir_test, f'{fn_tag}_id{idx}.npy'),
test_true_properties[idx])
for idx in train_hamiltonians:
# predictions
np.save(os.path.join(props_dir_train, f'{prop}/{prop}_model_id{idx}.npy'),
train_predicted_properties[idx])
# ground truths
fn_tag = 'correlation_matrix' if prop == _PROP_CORRELATIONS else 'entanglement_entropies'
np.save(os.path.join(data_dir_train, f'{fn_tag}_id{idx}.npy'),
train_true_properties[idx])
def train_and_predict(train_kernel, test_kernel, train_labels_shadow, kernel='linear'):
# instance-wise normalization
for i in range(len(train_kernel)):
train_kernel[i] /= np.linalg.norm(train_kernel[i])
for i in range(len(test_kernel)):
test_kernel[i] /= np.linalg.norm(test_kernel[i])
# use cross validation to find the best method + hyper-param
best_cv_score, test_score = 999.0, 999.0
test_predictions = np.nan
train_predictions = np.nan
for ML_method in [
lambda cx: svm.SVR(kernel=kernel, C=cx),
lambda cx: KernelRidge(kernel=kernel, alpha=1 / (2 * cx))
]:
for C in [0.0125, 0.025, 0.05, 0.125, 0.25, 0.5, 1.0, 2.0]:
score = -np.mean(cross_val_score(
ML_method(C), train_kernel, train_labels_shadow, cv=5,
scoring="neg_root_mean_squared_error"
))
if best_cv_score > score:
clf = ML_method(C)
clf.fit(train_kernel, train_labels_shadow.ravel())
test_predictions = clf.predict(test_kernel)
train_predictions = clf.predict(train_kernel)
best_cv_score = score
return train_predictions, test_predictions
def load_data(rows, cols, split, prop):
data_dir = os.path.join(DATA_ROOT, f'{rows}x{cols}', split)
# get hamiltonian ids
hamiltonians_ids = sorted([
int(fn[(fn.find('id') + 2):fn.find('.npy')]) for fn in os.listdir(data_dir)
if fnmatch.fnmatch(fn, 'data_id*.npy')
])
true_properties = np.empty(shape=(len(hamiltonians_ids), (rows * cols) ** 2))
shadow_properties = np.empty(shape=(len(hamiltonians_ids), (rows * cols) ** 2))
couplings = []
if prop == _PROP_CORRELATIONS:
fn_tag = 'correlation_matrix'
def prop_func(b, r):
return compute_correlation_matrix_from_shadow(b, r, k=1)
elif prop == _PROP_ENTROPIES:
fn_tag = 'entanglement_entropies'
def prop_func(b, r):
return compute_entropies_from_shadow(b, r)
else:
raise ValueError(f'unknown property {prop}')
# loop through data dir
for i, hid in enumerate(sorted(hamiltonians_ids)):
# load true correlation
true_prop_hid = np.load(os.path.join(data_dir, f'{fn_tag}_id{hid}.npy'))
true_properties[i, :] = true_prop_hid.flatten()
# compute shadow correlations
samples = np.load(os.path.join(data_dir, f'data_id{hid}.npy'))
bits, recipes = ints_to_bits_and_recipes(samples)
shadow_prop = prop_func(bits, recipes)
shadow_properties[i, :] = shadow_prop.flatten()
# load couplings
coupling_factors = np.load(
os.path.join(data_dir, f'coupling_matrix_id{hid}.npy'))
coupling_factors = [coupling_factors[si, sj] for (si, sj) in
it.combinations(range(rows * cols), 2) if
((sj % cols > 0) and sj - si == 1) or sj - si == cols]
couplings.append(coupling_factors)
couplings = np.array(couplings)
return hamiltonians_ids, couplings, shadow_properties, true_properties
if __name__ == '__main__':
# correlations
main(2, 5, prop=_PROP_CORRELATIONS, kernel_name=_RBF_KERNEL)
main(2, 6, prop=_PROP_CORRELATIONS, kernel_name=_RBF_KERNEL)
main(2, 7, prop=_PROP_CORRELATIONS, kernel_name=_RBF_KERNEL)
main(2, 8, prop=_PROP_CORRELATIONS, kernel_name=_RBF_KERNEL)
main(2, 9, prop=_PROP_CORRELATIONS, kernel_name=_RBF_KERNEL)
# entropies
main(2, 5, prop=_PROP_ENTROPIES, kernel_name=_RBF_KERNEL)
main(2, 6, prop=_PROP_ENTROPIES, kernel_name=_RBF_KERNEL)
main(2, 7, prop=_PROP_ENTROPIES, kernel_name=_RBF_KERNEL)
main(2, 8, prop=_PROP_ENTROPIES, kernel_name=_RBF_KERNEL)
main(2, 9, prop=_PROP_ENTROPIES, kernel_name=_RBF_KERNEL)