-
Notifications
You must be signed in to change notification settings - Fork 8
/
gnn.py
378 lines (317 loc) · 10.6 KB
/
gnn.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import json
import glob
import numpy as np
import pandas as pd
from qiskit import transpile
from qiskit import execute
from qiskit.providers.fake_provider import FakeLima
from qiskit.primitives import Estimator
from qiskit.circuit.random import random_circuit
import torch
from torch.optim import Adam
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.nn.functional import dropout
from torch_geometric.nn import GCNConv, global_mean_pool, Linear, ChebConv, SAGEConv
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from tqdm import tqdm
import matplotlib.pyplot as plt
import seaborn as sns
from blackwater.data.loaders.exp_val import CircuitGraphExpValMitigationDataset
from blackwater.data.generators.exp_val import exp_value_generator
from blackwater.data.utils import generate_random_pauli_sum_op
from blackwater.library.ngem.estimator import ngem
from qiskit.quantum_info import random_clifford
import random
from qiskit.circuit.library import HGate, SdgGate
from qiskit.circuit import ClassicalRegister
from blackwater.data.utils import (
generate_random_pauli_sum_op,
create_estimator_meas_data,
circuit_to_graph_data_json,
get_backend_properties_v1,
encode_pauli_sum_op,
create_meas_data_from_estimators
)
from blackwater.data.generators.exp_val import ExpValueEntry
from blackwater.metrics.improvement_factor import improvement_factor, Trial, Problem
from qiskit_aer import AerSimulator, QasmSimulator
from qiskit.providers.fake_provider import FakeMontreal, FakeLima
from torch_geometric.nn import (
GCNConv,
TransformerConv,
GATv2Conv,
global_mean_pool,
Linear,
ChebConv,
SAGEConv,
ASAPooling,
dense_diff_pool,
avg_pool_neighbor_x
)
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from torch_geometric.utils import to_dense_adj, to_dense_batch
from mlp import MLP3
class ExpValCircuitGraphModel(torch.nn.Module):
def __init__(
self,
num_node_features: int,
hidden_channels: int,
exp_value_size: int = 4,
dropout: float = 0.2
):
super().__init__()
self.transformer1 = TransformerConv(
num_node_features, hidden_channels,
heads=3,
dropout=0.1
)
self.pooling1 = ASAPooling(hidden_channels * 3, 0.5)
self.transformer2 = TransformerConv(
hidden_channels * 3, hidden_channels,
heads=2,
dropout=0.1
)
self.pooling2 = ASAPooling(hidden_channels * 2, 0.5)
self.body_seq = torch.nn.Sequential(
Linear(hidden_channels * 2 + 1 + exp_value_size, hidden_channels),
torch.nn.Dropout(dropout),
Linear(hidden_channels, exp_value_size)
)
def forward(self,
exp_value, observable,
circuit_depth, nodes,
edge_index, batch):
graph = self.transformer1(nodes, edge_index)
graph, edge_index, _, batch, _ = self.pooling1(
graph, edge_index, batch=batch
)
graph = self.transformer2(graph, edge_index)
graph, edge_index, _, batch, _ = self.pooling2(
graph, edge_index, batch=batch
)
graph = global_mean_pool(graph, batch)
merge = torch.cat((
graph,
torch.squeeze(exp_value, 1),
circuit_depth
), dim=1)
return self.body_seq(merge)
class ExpValCircuitGraphModel_2(torch.nn.Module):
def __init__(
self,
num_node_features: int,
hidden_channels: int,
exp_value_size: int = 4,
dropout: float = 0.5
):
super().__init__()
self.transformer1 = TransformerConv(
num_node_features, hidden_channels,
heads=3,
dropout=0.1
)
self.pooling1 = ASAPooling(hidden_channels * 3, 0.5)
self.transformer2 = TransformerConv(
hidden_channels * 3, hidden_channels,
heads=2,
dropout=0.1
)
self.pooling2 = ASAPooling(hidden_channels * 2, 0.5)
self.body_seq = MLP2(
input_size=hidden_channels * 2 + 1 + exp_value_size,
hidden_size=hidden_channels,
output_size=exp_value_size,
dropout_rate=dropout
)
def forward(self,
exp_value, observable,
circuit_depth, nodes,
edge_index, batch):
graph = self.transformer1(nodes, edge_index)
graph, edge_index, _, batch, _ = self.pooling1(
graph, edge_index, batch=batch
)
graph = self.transformer2(graph, edge_index)
graph, edge_index, _, batch, _ = self.pooling2(
graph, edge_index, batch=batch
)
graph = global_mean_pool(graph, batch)
merge = torch.cat((
graph,
torch.squeeze(exp_value, 1),
circuit_depth
), dim=1)
return self.body_seq(merge)
class ExpValCircuitGraphModel_3(torch.nn.Module):
def __init__(
self,
num_node_features: int,
hidden_channels: int,
exp_value_size: int = 4,
dropout: float = 0.3
):
super().__init__()
self.transformer1 = TransformerConv(
num_node_features, hidden_channels,
heads=5,
dropout=0.1
)
self.pooling1 = ASAPooling(hidden_channels * 5, 0.5)
self.transformer2 = TransformerConv(
hidden_channels * 5, hidden_channels,
heads=3,
dropout=0.1
)
self.pooling2 = ASAPooling(hidden_channels * 3, 0.5)
self.body_seq = MLP3(
input_size=hidden_channels * 3 + 1 + exp_value_size,
hidden_size=hidden_channels * 5,
output_size=exp_value_size,
dropout_rate=dropout
)
def forward(self,
exp_value, observable,
circuit_depth, nodes,
edge_index, batch):
graph = self.transformer1(nodes, edge_index)
graph, edge_index, _, batch, _ = self.pooling1(
graph, edge_index, batch=batch
)
graph = self.transformer2(graph, edge_index)
graph, edge_index, _, batch, _ = self.pooling2(
graph, edge_index, batch=batch
)
graph = global_mean_pool(graph, batch)
merge = torch.cat((
graph,
torch.squeeze(exp_value, 1),
circuit_depth
), dim=1)
return self.body_seq(merge)
class ExpValCircuitGraphModel_4(torch.nn.Module):
# Inferior to 3
def __init__(
self,
num_node_features: int,
hidden_channels: int,
exp_value_size: int = 4,
dropout: float = 0.3
):
super().__init__()
self.transformer1 = TransformerConv(
num_node_features, hidden_channels,
heads=5,
dropout=0.1
)
self.pooling1 = ASAPooling(hidden_channels * 5, 0.5)
self.transformer2 = TransformerConv(
hidden_channels * 5, hidden_channels,
heads=3,
dropout=0.1
)
self.pooling2 = ASAPooling(hidden_channels * 3, 0.5)
self.body_seq = MLP3(
input_size=hidden_channels * 3 + 1 + exp_value_size,
hidden_size=hidden_channels,
output_size=exp_value_size,
dropout_rate=dropout
)
def forward(self,
exp_value, observable,
circuit_depth, nodes,
edge_index, batch):
graph = self.transformer1(nodes, edge_index)
graph, edge_index, _, batch, _ = self.pooling1(
graph, edge_index, batch=batch
)
graph = self.transformer2(graph, edge_index)
graph, edge_index, _, batch, _ = self.pooling2(
graph, edge_index, batch=batch
)
graph = global_mean_pool(graph, batch)
merge = torch.cat((
graph,
torch.squeeze(exp_value, 1),
circuit_depth
), dim=1)
return self.body_seq(merge)
if __name__ == "__main__":
train_paths = [
'./data/mbd_datasets2/theta_0.05pi/train/step_1.json',
]
val_paths = [
'./data/mbd_datasets2/theta_0.05pi/val/step_1.json',
]
BATCH_SIZE = 32
train_loader = DataLoader(
CircuitGraphExpValMitigationDataset(
train_paths,
),
batch_size=BATCH_SIZE,
shuffle=True
)
val_loader = DataLoader(
CircuitGraphExpValMitigationDataset(
val_paths,
),
batch_size=BATCH_SIZE,
shuffle=False
)
for data in train_loader:
print(data.noisy_0.shape)
break
model = ExpValCircuitGraphModel_3(
num_node_features=22,
hidden_channels=15,
exp_value_size=4,
)
criterion = torch.nn.MSELoss()
optimizer = Adam(model.parameters(), lr=0.001)
scheduler = ReduceLROnPlateau(optimizer,
'min',
factor=0.1,
patience=15,
verbose=True,
min_lr=0.00001)
min_valid_loss = np.inf
train_losses = []
val_losses = []
N_EPOCHS = 100
progress = tqdm(range(N_EPOCHS), desc='Model training', leave=True)
for epoch in progress:
train_loss = 0.0
model.train()
for i, data in enumerate(train_loader):
optimizer.zero_grad()
out = model(
data.noisy_0,
data.observable,
data.circuit_depth,
data.x,
data.edge_index,
data.batch
)
loss = criterion(out, torch.squeeze(data.y, 1))
train_loss += loss.item()
loss.backward()
optimizer.step()
valid_loss = 0.0
model.eval()
for i, data in enumerate(val_loader):
out = model(
data.noisy_0,
data.observable,
data.circuit_depth,
data.x,
data.edge_index,
data.batch)
loss = criterion(out, torch.squeeze(data.y, 1))
valid_loss += loss.item()
scheduler.step(valid_loss)
if epoch >= 1:
train_losses.append(train_loss / len(train_loader))
val_losses.append(valid_loss / len(val_loader))
progress.set_description(f"{round(train_losses[-1], 5)}, {round(val_losses[-1], 5)}")
progress.refresh()