-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathmnist_clifford_qnn.py
224 lines (176 loc) · 7.09 KB
/
mnist_clifford_qnn.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
"""
MIT License
Copyright (c) 2020-present TorchQuantum Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import torch
import torch.nn.functional as F
import torch.optim as optim
import argparse
import torchquantum as tq
from torchquantum.dataset import MNIST
from torch.optim.lr_scheduler import CosineAnnealingLR
import random
import numpy as np
# need to make sure all the gates are RX RY RZ and parameters are 0, pi/2,
# pi, 3pi/2 four types
from torchquantum.layer import RXYZCXLayer0
class QFCModel(tq.QuantumModule):
def __init__(self):
super().__init__()
self.n_wires = 4
self.q_device = tq.QuantumDevice(n_wires=self.n_wires)
self.encoder = tq.GeneralEncoder(tq.encoder_op_list_name_dict["4x4_ryzxy"])
self.q_layer = RXYZCXLayer0({"n_wires": 4, "n_blocks": 4})
self.measure = tq.MeasureAll(tq.PauliZ)
def forward(self, x, use_qiskit=False):
bsz = x.shape[0]
x = F.avg_pool2d(x, 6).view(bsz, 16)
if use_qiskit:
x = self.qiskit_processor.process_parameterized(
self.q_device, self.encoder, self.q_layer, self.measure, x
)
else:
self.encoder(self.q_device, x)
self.q_layer(self.q_device)
x = self.measure(self.q_device)
x = x.reshape(bsz, 2, 2).sum(-1).squeeze()
x = F.log_softmax(x, dim=1)
return x
def train(dataflow, model, device, optimizer):
for feed_dict in dataflow["train"]:
inputs = feed_dict["image"].to(device)
targets = feed_dict["digit"].to(device)
outputs = model(inputs)
loss = F.nll_loss(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"loss: {loss.item()}", end="\r")
def valid_test(dataflow, split, model, device, qiskit=False):
target_all = []
output_all = []
with torch.no_grad():
for feed_dict in dataflow[split]:
inputs = feed_dict["image"].to(device)
targets = feed_dict["digit"].to(device)
outputs = model(inputs, use_qiskit=qiskit)
target_all.append(targets)
output_all.append(outputs)
target_all = torch.cat(target_all, dim=0)
output_all = torch.cat(output_all, dim=0)
_, indices = output_all.topk(1, dim=1)
masks = indices.eq(target_all.view(-1, 1).expand_as(indices))
size = target_all.shape[0]
corrects = masks.sum().item()
accuracy = corrects / size
loss = F.nll_loss(output_all, target_all).item()
print(f"{split} set accuracy: {accuracy}")
print(f"{split} set loss: {loss}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--epochs", type=int, default=20, help="number of training epochs"
)
parser.add_argument("--pdb", action="store_true", help="pdb")
parser.add_argument(
"--finetune", action="store_true", help="quantization aware finetuning"
)
seed = 42
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
args = parser.parse_args()
if args.pdb:
import pdb
pdb.set_trace()
dataset = MNIST(
root="./mnist_data",
train_valid_split_ratio=[0.9, 0.1],
digits_of_interest=[3, 6],
)
dataflow = dict()
for split in dataset:
sampler = torch.utils.data.RandomSampler(dataset[split])
dataflow[split] = torch.utils.data.DataLoader(
dataset[split],
batch_size=256,
sampler=sampler,
num_workers=8,
pin_memory=True,
)
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model = QFCModel().to(device)
n_epochs = args.epochs
optimizer = optim.Adam(model.parameters(), lr=5e-3, weight_decay=1e-4)
scheduler = CosineAnnealingLR(optimizer, T_max=n_epochs)
for epoch in range(1, n_epochs + 1):
# train
print(f"Epoch {epoch}:")
train(dataflow, model, device, optimizer)
print(optimizer.param_groups[0]["lr"])
# valid
valid_test(dataflow, "valid", model, device)
scheduler.step()
model.eval()
# test
print("Test with floating point model:")
valid_test(dataflow, "test", model, device, qiskit=False)
model.train()
for module in model.modules():
module.clifford_quantization = True
# perform quantization-aware finetuning
if args.finetune:
optimizer = optim.Adam(model.parameters(), lr=5e-3)
scheduler = CosineAnnealingLR(optimizer, T_max=n_epochs)
for epoch in range(1, n_epochs + 1):
# train
print(f"Finetuning Epoch {epoch}:")
train(dataflow, model, device, optimizer)
print(optimizer.param_groups[0]["lr"])
# valid
valid_test(dataflow, "valid", model, device)
scheduler.step()
model.eval()
print("Test with clifford quantized model:")
valid_test(dataflow, "test", model, device, qiskit=False)
# # run on Qiskit simulator and real Quantum Computers
# try:
# from qiskit import IBMQ
# from torchquantum.plugin import QiskitProcessor
#
# # firstly perform simulate
# print(f"\nTest with Qiskit Simulator")
# processor_simulation = QiskitProcessor(use_real_qc=False)
# model.set_qiskit_processor(processor_simulation)
# valid_test(dataflow, 'test', model, device, qiskit=True)
#
# # then try to run on REAL QC
# backend_name = 'ibmq_quito'
# print(f"\nTest on Real Quantum Computer {backend_name}")
# processor_real_qc = QiskitProcessor(use_real_qc=True,
# backend_name=backend_name)
# model.set_qiskit_processor(processor_real_qc)
# valid_test(dataflow, 'test', model, device, qiskit=True)
# except ImportError:
# print("Please install qiskit, create an IBM Q Experience Account and "
# "save the account token according to the instruction at "
# "'https://github.com/Qiskit/qiskit-ibmq-provider', "
# "then try again.")
if __name__ == "__main__":
main()