forked from nhanvtran/qubit-readout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLP_Qubit_Readout_reduced.py
executable file
·191 lines (156 loc) · 6.86 KB
/
MLP_Qubit_Readout_reduced.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
from numpy import*
from pylab import*
import matplotlib.pyplot as plt
from h5py import File
import torch
import time
import numpy as np
from tqdm import tqdm
import torchvision.transforms as transforms
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
import torch.nn as nn
import torch.nn.functional as F
import json
if __name__ == "__main__":
device = torch.device('cpu')
plt.close('all')
start = time.time()
Visuals = False
file_dir = './data/'
expt_name = 'histogram'
filelist = [8]
tags = ['']
rancut = [6,6]
for jj,i in enumerate(filelist):
filename = str(i).zfill(5) + "_"+expt_name.lower()+".h5"
with File(file_dir + filename,'r') as a:
hardware_cfg = (json.loads(a.attrs['hardware_cfg']))
experiment_cfg = (json.loads(a.attrs['experiment_cfg']))
quantum_device_cfg = (json.loads(a.attrs['quantum_device_cfg']))
ran = hardware_cfg['awg_info']['keysight_pxi']['m3102_vpp_range']
expt_cfg = (json.loads(a.attrs['experiment_cfg']))[expt_name.lower()]
numbins = expt_cfg['numbins']
print (numbins)
numbins = 200
a_num = expt_cfg['acquisition_num']
ns = expt_cfg['num_seq_sets']
readout_length = quantum_device_cfg['readout']['length']
window = quantum_device_cfg['readout']['window']
atten = quantum_device_cfg['readout']['dig_atten']
freq = quantum_device_cfg['readout']['freq']
# print ('Readout length = ',readout_length)
# print ('Readout window = ',window)
# print ("Digital atten = ",atten)
# print ("Readout Freq = ",freq)
I = array(a['I'])
Q = array(a['Q'])
sample = a_num
I,Q = I/2**15*ran,Q/2**15*ran
colors = ['r','b','g']
labels= ['g','e','f']
titles=['I','Q']
IQs = median(I[::3],1),median(Q[::3],1),median(I[1::3],1),median(Q[1::3],1),median(I[2::3],1),median(Q[2::3],1)
IQsss = I.T.flatten()[0::3],Q.T.flatten()[0::3],I.T.flatten()[1::3],Q.T.flatten()[1::3],I.T.flatten()[2::3],Q.T.flatten()[2::3]
IQsss_t=torch.tensor(IQsss)
from torch.utils.data import Dataset, DataLoader
class Qubit_Readout_Dataset(Dataset):
def __init__(self):
self.labels = torch.zeros(3*sample)
self.data = torch.zeros(3*sample,2)
self.DataSet = torch.zeros((3*sample,3))
for ii in range(3):
self.DataSet[ii*sample:(ii+1)*sample,0] = IQsss_t[2*ii][::int(a_num/sample)]
self.DataSet[ii*sample:(ii+1)*sample,1] = IQsss_t[2*ii+1][::int(a_num/sample)]
self.DataSet[ii*sample:(ii+1)*sample,2] = ii
self.data[:,0] = self.DataSet[:,0]/torch.max(self.DataSet[:,0])
self.data[:,1] = self.DataSet[:,1]/torch.max(self.DataSet[:,1])
self.labels[:] = self.DataSet[:,2]
# self.labels = normalize(self.labels, p=2)
# self.data = normalize(self.data, p=2)
def __len__(self):
return self.labels.shape[0]
def __getitem__(self, idx):
return self.data[idx], self.labels[idx]
dataset = Qubit_Readout_Dataset()
train_data, test_data = torch.utils.data.random_split(dataset, [8000, 1000])
transforms = torch.nn.Sequential(
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)))
num_workers = 0
batch_size = 64
train_loader = torch.utils.data.DataLoader(train_data, batch_size = batch_size,
num_workers = num_workers, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size = batch_size,
num_workers = num_workers, shuffle=True)
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.fc1 = nn.Linear(2, 256)
self.fc2 = nn.Linear(256,128)
self.fc3 = nn.Linear(128,3)
# self.dropout = nn.Dropout(p=0.2)
def forward(self,x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
# x = self.dropout(x)
return x
model = Net()
model = model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
train_loss_track=np.array([])
n_epochs = 100
for epoch in tqdm(range(n_epochs), desc='Epoch'):
cc = 0
train_loss = 0
model.train() # prep model for training
for data, label in train_loader:
optimizer.zero_grad()
data = data.to(device)
output = model(data)
label = label.to(device)
loss = criterion(output, label.long())
loss.backward()
optimizer.step()
train_loss += loss.item()
train_loss_track=np.append(train_loss_track,np.asarray(train_loss))
plt.plot(train_loss_track)
plt.xlabel('Epochs')
plt.title("Training Loss")
plt.figure(figsize = (12,7))
plt.show()
torch.save(model.state_dict(), 'model.pt')
model.load_state_dict(torch.load('model.pt'))
model = model.to(device)
# initialize lists to monitor test loss and accuracy
test_loss = 0.0
model.eval()
cc = 0
y_true = torch.tensor([])
y_true = y_true.to(device)
y_pred = torch.tensor([])
y_pred = y_pred.to(device)
with torch.no_grad():
for data, target in test_loader:
data=data.to(device)
output = model(data)
target = target.to(device)
loss = criterion(output, target.long())
test_loss += loss.item()*data.size(0)
test_loss += loss.item()
val, ind = torch.max(output,1)
y_pred = torch.cat((y_pred, ind), 0)
y_true = torch.cat((y_true, target), 0)
acc = y_true-y_pred
accuracy = (len(y_true)-torch.count_nonzero(acc))/len(y_true)
accuracy = accuracy.item()
print('Confusion Matrix:')
print(confusion_matrix(y_pred.cpu(),y_true.cpu()))
print('Test Accuracy: %', accuracy*100)
# print('Test Loss: ', test_loss)
end = time.time()
print('Total Time Elapsed:', end - start, 'seconds')
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
param_num = count_parameters(model)
print("Number of Parameters: ", param_num)