-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHybrid_nn.py
359 lines (312 loc) · 11.3 KB
/
Hybrid_nn.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
import pennylane as qml
import torch
from torch import nn
import embedding
dev = qml.device('default.qubit', wires=8)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
"""
This part is a code for Hybrid Model 1.
Hybrid Model 1 transforms 8 dimensional features to 8 dimensional features using Fully connected classical NN.
Model1_Fidelity uses fideliy as a loss function.
Model1_HSinner uses Hilbert-Schmidt inner product as a loss function.
"""
@qml.qnode(dev, interface="torch")
def circuit1(inputs):
embedding.QuantumEmbedding1(inputs[0:8])
embedding.QuantumEmbedding1_inverse(inputs[8:16])
return qml.probs(wires=range(8))
class Model1_Fidelity(torch.nn.Module):
def __init__(self):
super().__init__()
self.qlayer1 = qml.qnn.TorchLayer(circuit1, weight_shapes={})
self.linear_relu_stack1 = nn.Sequential(
nn.Linear(8, 10),
nn.ReLU(),
nn.Linear(10,10),
nn.ReLU(),
nn.Linear(10,8)
)
def forward(self, x1, x2):
x1 = self.linear_relu_stack1(x1)
x2 = self.linear_relu_stack1(x2)
x = torch.concat([x1, x2], 1)
x = self.qlayer1(x)
return x[:,0]
class Model1_HSinner(torch.nn.Module):
def __init__(self):
super().__init__()
self.matrix_fn1 = qml.matrix(circuit1)
self.linear_relu_stack1 = nn.Sequential(
nn.Linear(8, 10),
nn.ReLU(),
nn.Linear(10,10),
nn.ReLU(),
nn.Linear(10,8)
)
def forward(self, x1, x2):
x1 = self.linear_relu_stack1(x1)
x2 = self.linear_relu_stack1(x2)
x = torch.concat([x1, x2], 1).to("cpu")
x = [torch.real(torch.trace(self.matrix_fn1(a))) for a in x]
x = torch.stack(x, dim = 0).to(device)
return x / 2**8
"""
This part is a code for Hybrid Model 2.
Hybrid Model 2 transforms 8 dimensional features to 16 dimensional features.
16 dimensional output is used as a rotation angle of ZZ feature embedding.
Model2_Fidelity uses fidelity loss as a loss function.
Model2_HSinner uses Hilbert-Schmidt inner product as a loss function.
"""
@qml.qnode(dev, interface="torch")
def circuit2(inputs):
embedding.QuantumEmbedding2(inputs[0:16])
embedding.QuantumEmbedding2_inverse(inputs[16:32])
return qml.probs(wires=range(8))
class Model2_Fidelity(torch.nn.Module):
def __init__(self):
super().__init__()
self.qlayer2 = qml.qnn.TorchLayer(circuit2, weight_shapes={})
self.matrix_fn2 = qml.matrix(circuit2)
self.linear_relu_stack2 = nn.Sequential(
nn.Linear(8, 20),
nn.ReLU(),
nn.Linear(20,20),
nn.ReLU(),
nn.Linear(20,16)
)
def forward(self, x1, x2):
x1 = self.linear_relu_stack2(x1)
x2 = self.linear_relu_stack2(x2)
x = torch.concat([x1, x2], 1)
x = self.qlayer2(x)
return x[:,0]
class Model2_HSinner(torch.nn.Module):
def __init__(self):
super().__init__()
self.qlayer2 = qml.qnn.TorchLayer(circuit2, weight_shapes={})
self.matrix_fn2 = qml.matrix(circuit2)
self.linear_relu_stack2 = nn.Sequential(
nn.Linear(8, 20),
nn.ReLU(),
nn.Linear(20,20),
nn.ReLU(),
nn.Linear(20,16)
)
def forward(self, x1, x2):
x1 = self.linear_relu_stack2(x1)
x2 = self.linear_relu_stack2(x2)
x = torch.concat([x1, x2], 1).to("cpu")
x = [torch.real(torch.trace(self.matrix_fn2(a))) for a in x]
x = torch.stack(x, dim=0).to(device)
return x / 2**8
"""
This part of code implements Hybrid Model 3.
Hybrid Model 3 transforms 28 * 28 dimensional features to 16 dimensional features using CNN.
16 dimensional features are used as a rotation angle of the ZZ feature embedding.
Model3_Fidelity uses fidelity loss as a loss function.
Model3_HSinner uses Hilbert Schmidt inner as a loss function.
"""
@qml.qnode(dev, interface="torch")
def circuit3(inputs):
embedding.QuantumEmbedding2(inputs[0:16])
embedding.QuantumEmbedding2_inverse(inputs[16:32])
return qml.probs(wires=range(8))
class Model3_Fidelity(torch.nn.Module):
def __init__(self):
super().__init__()
# Layer1: 28 * 28 -> 14 * 14
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2)
)
# Layer2: 14 * 14 -> 7 * 7
self.layer2 = torch.nn.Sequential(
torch.nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2)
)
# Fully connected Layers 7 * 7 -> 16
self.fc = torch.nn.Linear(7 * 7, 16, bias=True)
self.qlayer3 = qml.qnn.TorchLayer(circuit3, weight_shapes={})
def forward(self, x1, x2):
x1 = self.layer1(x1)
x1 = self.layer2(x1)
x1 = x1.view(-1, 7 * 7)
x1 = self.fc(x1)
x2 = self.layer1(x2)
x2 = self.layer2(x2)
x2 = x2.view(-1, 7 * 7)
x2 = self.fc(x2)
x = torch.concat([x1, x2], 1)
x = self.qlayer3(x)
return x[:,0]
class Model3_HSinner(torch.nn.Module):
def __init__(self):
super().__init__()
self.matrix_fn3 = qml.matrix(circuit3)
# Layer1: 28 * 28 -> 14 * 14
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2)
)
# Layer2: 14 * 14 -> 7 * 7
self.layer2 = torch.nn.Sequential(
torch.nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2)
)
# Fully connected Layers 7 * 7 -> 16
self.fc = torch.nn.Linear(7 * 7, 16, bias=True)
def forward(self, x1, x2):
x1 = self.layer1(x1)
x1 = self.layer2(x1)
x1 = x1.view(-1, 7 * 7)
x1 = self.fc(x1)
x2 = self.layer1(x2)
x2 = self.layer2(x2)
x2 = x2.view(-1, 7 * 7)
x2 = self.fc(x2)
x = torch.concat([x1, x2], 1).to("cpu")
x = [torch.real(torch.trace(self.matrix_fn3(a))) for a in x]
x = torch.stack(x, dim=0).to(device)
return x / 2**8
# Training Amplitude Model
class Model_Amplitude(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear_relu_stack = nn.Sequential(
nn.Linear(16,32),
nn.ReLU(),
nn.Linear(32,32),
nn.ReLU(),
nn.Linear(32,16)
)
def forward(self, x1, x0):
x1 = self.linear_relu_stack(x1)
x0 = self.linear_relu_stack(x0)
return torch.sum(x1 * x0, dim=-1)
"""
Below are hybrid models that uses distance as a loss function.
The codes are out of interest as the are not efficiently calculable with quantum computers.
Use for comparison purposes.
# Hybrid Distance Model1
@qml.qnode(dev, interface="torch")
def distance_circuit1(inputs):
embedding.QuantumEmbedding1(inputs[0:8])
return qml.density_matrix(wires=range(8))
class DistanceModel1_Trace(torch.nn.Module):
def __init__(self):
super().__init__()
self.qlayer1_distance = qml.qnn.TorchLayer(distance_circuit1, weight_shapes={})
self.linear_relu_stack1 = nn.Sequential(
nn.Linear(8, 10),
nn.ReLU(),
nn.Linear(10,10),
nn.ReLU(),
nn.Linear(10,8)
)
def forward(self, x1, x0):
x1 = self.linear_relu_stack1(x1)
x0 = self.linear_relu_stack1(x0)
rhos1 = self.qlayer1_distance(x1)
rhos0 = self.qlayer1_distance(x0)
rho1 = torch.sum(rhos1, dim=0) / len(x1)
rho0 = torch.sum(rhos0, dim=0) / len(x0)
rho_diff = rho1 - rho0
eigvals = torch.linalg.eigvals(rho_diff)
return -0.5 * torch.real(torch.sum(torch.abs(eigvals)))
class DistanceModel1_HS(torch.nn.Module):
def __init__(self):
super().__init__()
self.qlayer1_distance = qml.qnn.TorchLayer(distance_circuit1, weight_shapes={})
self.linear_relu_stack1 = nn.Sequential(
nn.Linear(8, 10),
nn.ReLU(),
nn.Linear(10,10),
nn.ReLU(),
nn.Linear(10,8)
)
def forward(self, x1, x0):
x1 = self.linear_relu_stack1(x1)
x0 = self.linear_relu_stack1(x0)
rhos1 = self.qlayer1_distance(x1)
rhos0 = self.qlayer1_distance(x0)
rho1 = torch.sum(rhos1, dim=0) / len(x1)
rho0 = torch.sum(rhos0, dim=0) / len(x0)
rho_diff = rho1 - rho0
return -0.5 * torch.trace(rho_diff @ rho_diff)
# Hybrid Distance Model 2
@qml.qnode(dev, interface="torch")
def distance_circuit2(inputs):
embedding.QuantumEmbedding2(inputs[0:16])
return qml.density_matrix(wires=range(8))
class DistanceModel2_Trace(torch.nn.Module):
def __init__(self):
super().__init__()
self.qlayer2_distance = qml.qnn.TorchLayer(distance_circuit2, weight_shapes={})
self.linear_relu_stack2 = nn.Sequential(
nn.Linear(8, 20),
nn.ReLU(),
nn.Linear(20,20),
nn.ReLU(),
nn.Linear(20,16)
)
def forward(self, x1, x0):
x1 = self.linear_relu_stack2(x1)
x0 = self.linear_relu_stack2(x0)
rhos1 = self.qlayer2_distance(x1)
rhos0 = self.qlayer2_distance(x0)
rho1 = torch.sum(rhos1, dim=0) / len(x1)
rho0 = torch.sum(rhos0, dim=0) / len(x0)
rho_diff = rho1 - rho0
eigvals = torch.linalg.eigvals(rho_diff)
return -0.5 * torch.real(torch.sum(torch.abs(eigvals)))
class DistanceModel2_HS(torch.nn.Module):
def __init__(self):
super().__init__()
self.qlayer2_distance = qml.qnn.TorchLayer(distance_circuit2, weight_shapes={})
self.linear_relu_stack2 = nn.Sequential(
nn.Linear(8, 20),
nn.ReLU(),
nn.Linear(20,20),
nn.ReLU(),
nn.Linear(20,16)
)
def forward(self, x1, x0):
x1 = self.linear_relu_stack2(x1)
x0 = self.linear_relu_stack2(x0)
rhos1 = self.qlayer2_distance(x1)
rhos0 = self.qlayer2_distance(x0)
rho1 = torch.sum(rhos1, dim=0) / len(x1)
rho0 = torch.sum(rhos0, dim=0) / len(x0)
rho_diff = rho1 - rho0
return -0.5 * torch.trace(rho_diff @ rho_diff)
"""
# Get model function
def get_model(model):
if model == 'Model1_Fidelity':
return Model1_Fidelity()
elif model == 'Model1_HSinner':
return Model1_HSinner()
elif model == 'Model2_Fidelity':
return Model2_Fidelity()
elif model == 'Model2_HSinner':
return Model2_HSinner()
elif model == 'Model3_Fidelity':
return Model3_Fidelity()
elif model == 'Model3_HSinner':
return Model3_HSinner()
elif model == 'Model_Amplitude':
return Model_Amplitude()
"""
elif model == 'DistanceModel1_Trace':
return DistanceModel1_Trace()
elif model == 'DistanceModel1_HS':
return DistanceModel1_HS()
elif model == 'DistanceModel2_Trace':
return DistanceModel2_Trace()
elif model == 'DistanceModel2_HS':
return DistanceModel2_HS()
"""