forked from mit-han-lab/torchquantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_2qubit_4class.py
213 lines (165 loc) · 6.36 KB
/
mnist_2qubit_4class.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
"""
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.
"""
"""
use 2 qubit to perform 4 class classification,
We can choose four different observables to measure the qubit state:
1. XX
2. YY
3. ZZ
4. XY
"""
import torch
import torch.nn.functional as F
import torch.optim as optim
import argparse
import torchquantum as tq
import torchquantum.functional as tqf
from torchquantum.measurement import expval_joint_analytical
from torchquantum.dataset import MNIST
from torch.optim.lr_scheduler import CosineAnnealingLR
import random
import numpy as np
class QFCModel(tq.QuantumModule):
class QLayer(tq.QuantumModule):
def __init__(self):
super().__init__()
self.n_wires = 2
self.random_layer = tq.RandomLayer(
n_ops=50, wires=list(range(self.n_wires))
)
# gates with trainable parameters
self.rx0 = tq.RX(has_params=True, trainable=True)
self.ry0 = tq.RY(has_params=True, trainable=True)
self.rz0 = tq.RZ(has_params=True, trainable=True)
self.crx0 = tq.CRX(has_params=True, trainable=True)
def forward(self, qdev: tq.QuantumDevice):
self.random_layer(qdev)
# some trainable gates (instantiated ahead of time)
self.rx0(qdev, wires=0)
self.ry0(qdev, wires=1)
self.rz0(qdev, wires=0)
self.crx0(qdev, wires=[0, 1])
def __init__(self):
super().__init__()
self.n_wires = 2
# the encoder here is just for illustration purpose, may not be the best choice
self.encoder = tq.GeneralEncoder(
tq.encoder_op_list_name_dict["2x8_rxryrzrxryrzrxry"]
)
self.q_layer = self.QLayer()
def forward(self, x, use_qiskit=False):
qdev = tq.QuantumDevice(
n_wires=self.n_wires, bsz=x.shape[0], device=x.device, record_op=True
)
bsz = x.shape[0]
x = F.avg_pool2d(x, 6).view(bsz, 16)
self.encoder(qdev, x)
self.q_layer(qdev)
obs_xx = expval_joint_analytical(qdev, "XX")
obs_yy = expval_joint_analytical(qdev, "YY")
obs_zz = expval_joint_analytical(qdev, "ZZ")
obs_xy = expval_joint_analytical(qdev, "XY")
x = torch.stack([obs_xx, obs_yy, obs_zz, obs_xy], dim=1)
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(
"--static", action="store_true", help="compute with " "static mode"
)
parser.add_argument("--pdb", action="store_true", help="debug with pdb")
parser.add_argument(
"--wires-per-block", type=int, default=2, help="wires per block int static mode"
)
parser.add_argument(
"--epochs", type=int, default=5, help="number of training epochs"
)
args = parser.parse_args()
if args.pdb:
import pdb
pdb.set_trace()
seed = 0
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
dataset = MNIST(
root="./mnist_data",
train_valid_split_ratio=[0.9, 0.1],
digits_of_interest=[0, 1, 2, 3],
n_test_samples=100,
)
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()
# test
valid_test(dataflow, "test", model, device, qiskit=False)
if __name__ == "__main__":
main()