-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi layers.py
237 lines (205 loc) · 7.02 KB
/
multi layers.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
# %%
import pennylane as qml
from pennylane import numpy as np
import numpy
from tqdm import tqdm
from sklearn import metrics
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
from matplotlib import pyplot as plt
# %%
data_frame = pd.read_csv("AAPL max.csv", usecols=["Close"])
#data_frame = pd.read_csv("UK inflation all time.csv",usecols =[1],skiprows = 183)
data = pd.DataFrame.to_numpy(data_frame)
np.concatenate(data)
# #sample_size = 10080 # Limiting dataset to what was used in the paper.
sample_size = 200
sub_data = data[:sample_size + 1] # A small sample
print(sub_data[:10])
plt.plot(sub_data)
# %%
N = sample_size
pc = np.zeros((N,1))
for i in range(N-1):
pc[i] = (sub_data[i+1]-sub_data[i])/sub_data[i]
# %%
plt.scatter(np.linspace(0,N,N),pc,marker ='.')
#plt.ylim(-0.6,0.6)
plt.show
# %%
P = np.zeros((N//2,1))
nu = np.zeros((N//2,1))
for k in tqdm(range(N//2)):
sum = 0
for i in range(N):
sum += (pc[i] * np.exp(2 * np.pi * i * k * 1j * 1/N))
P[k] = np.abs(sum)**2
nu[k] = k/N
plt.loglog(nu, P)
# %%
num_components = 3
P_1d = np.concatenate(P)
ind = np.argpartition(P_1d, -num_components)[-num_components:]
amp2 = np.zeros(num_components)
DC = np.zeros(num_components)
for i in range(num_components):
amp2[i] = P[ind[i]]
DC[i] = nu[[ind[i]]]
amp = np.sqrt(amp2)
print(amp)
print(DC)
# threshold = 1
# amp2 = []
# DC = []
# for i in range(N//2):
# if P[i] > threshold:
# amp2.append(P[i])
# DC.append(nu[i])
# amp = np.sqrt(amp2)
# print(amp)
# print(DC)
# %%
#num_components = len(DC)
DC_sample = DC[0:num_components]
amp_sample = amp[0:num_components]
interval = (np.linspace(0,N,N)).reshape((N,1))
components = np.zeros((N,num_components))
for i in range(N):
for j in range(num_components):
components[i][j] = amp_sample[j] + np.sin(DC_sample[j] * interval[i])
DC_signal = np.sum(components, axis = -1)
DC_signal = DC_signal.reshape(N,1)
# %%
c_n = [0, 0.2, 0.5, 0.8, 1]
noise_scale = np.abs(np.max(DC_signal)-np.min(DC_signal))
np.random.seed(0)
noise = np.random.uniform(0,1,N) * noise_scale *(c_n[0])
noise = noise.reshape((N,1))
trend = np.array([np.zeros(N), 5e-2 * np.linspace(0,N,N), 2* 5e-5 * np.square(np.linspace(0,N,N))])
trend = trend.reshape(3,N,1)
full_signal = DC_signal + noise + trend[0]
plt.plot(range(N),full_signal)
# %%
r = num_components
#r = 16 # Following the paper
N_QUBITS = (r + 1)
print(N_QUBITS)
def weights(n_layers):
return(2 * np.pi * np.random.random(size=(n_layers, 3, r), requires_grad=True))
x = 2 * np.pi *np.random.random(size = (r))
dev = qml.device('default.qubit', wires= N_QUBITS)
def block(weights):
for i in range(1,N_QUBITS):
qml.IsingXX(weights[0][i-1], wires=[0, i]) # Are the qubits in the right place?
for i in range(1,N_QUBITS):
qml.IsingZZ(weights[1][i-1], wires=[0, i])
for i in range(1,N_QUBITS):
qml.IsingYY(weights[2][i-1], wires=[0, i])
@qml.qnode(dev, interface="autograd")
def PQC(weights, x, n_layers):
qml.AngleEmbedding(x,wires=range(N_QUBITS)[1:]) # Features x are embedded in rotation angles
for j in range(n_layers):
block(weights[j])
return qml.expval(qml.PauliZ(wires=0))
PQC(weights(2), x, n_layers = 2)
print(qml.draw(PQC,expansion_strategy ="device")(weights,x, n_layers = 2))
# %%
split_denom = 3
cut_factor = split_denom * N_QUBITS
trunc_size = int(N/cut_factor)*cut_factor
trunc_signal = full_signal[:trunc_size]
grouped_signal = trunc_signal.reshape(trunc_size//N_QUBITS, N_QUBITS)
shuffle_indices = np.arange(0,len(grouped_signal))
np.random.shuffle(shuffle_indices)
gsh_signal = grouped_signal[shuffle_indices]
train = gsh_signal[:int(2/3*(len(gsh_signal)))]
test = gsh_signal[int(2/3*(len(gsh_signal))):]
train_size = len(train)
test_size = len(test)
# %%
scaler = MinMaxScaler((0.2,0.8))
train_1d = train.reshape(train_size*N_QUBITS,1)
test_1d = test.reshape(test_size*N_QUBITS,1)
scaler.fit(train_1d)
scaled_train_1d = scaler.transform(train_1d)
scaled_test_1d = scaler.transform(test_1d)
scaled_train = scaled_train_1d.reshape(train_size,N_QUBITS)
scaled_test = scaled_test_1d.reshape(test_size, N_QUBITS)
final_train = scaled_train
final_test = scaled_test
# %%
def square_loss(targets, predictions):
loss = 0
for t, p in zip(targets, predictions):
loss += (t - p) ** 2
loss = loss / len(targets)
return 0.5*loss
def cost(weights, x, y,n_layers):
predictions = [PQC(weights, x_,n_layers) for x_ in x]
return square_loss(y, predictions)
x = np.zeros((train_size, r))
target_y = np.zeros((train_size,1))
for i in range(train_size):
x[i] = final_train[i][:-1]
target_y[i] = final_train[i][-1]
x_t = np.zeros((test_size, r)) # Already grouped and scaled
target_y_t = np.zeros((test_size,1))
for i in range(test_size):
x_t[i] = final_test[i][:-1]
target_y_t[i] = final_test[i][-1]
max_steps = 10
opt = qml.AdamOptimizer(.1)
batch_size = train_size//max_steps
cst = [cost(weights(2), x, target_y,n_layers = 2)] # initial cost
cst_t = [cost(weights(2), x_t, target_y_t,n_layers = 2)]
epochs = 10
# %%
layer_list = [2]
colors = ['b','g','r','c','m','y']
for j in range(len(layer_list)):
for i in tqdm(range(epochs)):
for step in range(max_steps):
# Select batch of data
batch_index = numpy.random.randint(0, max_steps, batch_size)
x_batch = x[batch_index]
y_batch = target_y[batch_index]
# Update the weights by one optimizer step
print(weights.type)
weights,_,_,_ = opt.step(cost, weights(layer_list[j]), x_batch, y_batch, layer_list[j]) # Calculating weights using the batches.
c = cost(weights(layer_list[j]), x, target_y,layer_list[j]) # Calculating the cost using the whole train data
c_t = cost(weights(layer_list[j]), x_t, target_y_t, layer_list[j])
cst.append(c)
cst_t.append(c_t)
print("initial; final cost with "+str(layer_list[j])+" layers:" + str(cst[0]) + ','+ str(cst[-1]))
# plt.semilogy(range(len(cst)), cst, colors[j],linestyle='-')
# plt.semilogy(cst_t, colors[j], linestyle = '--')
plt.plot(range(len(cst)), cst, colors[j],linestyle='-')
plt.plot(cst_t, colors[j], linestyle = '--')
plt.ylabel("Cost")
plt.xlabel("Step")
plt.show()
# %%
# t_predictions = np.zeros((test_size,1))
# for i in range(test_size):
# t_predictions[i] = (PQC(weights, x_t[i]))
# t_predictions = t_predictions.reshape((test_size, 1))
# real_predictions = scaler.inverse_transform(t_predictions)
# real_target = scaler.inverse_transform(target_y_t)
# plt.axline((1.5, 1.5), slope=1,color = '0',linestyle = '--')
# plt.plot(real_target[:-1], real_predictions[1:])
# plt.plot(real_target[:-1], real_target[1:])
# # %%
# unshuffled_pred = np.zeros(N)
# for i in range(len(real_predictions)):
# unshuffled_pred[(shuffle_indices[train_size:][i]) * N_QUBITS] = real_predictions[i]
# pred_index = []
# pred =[]
# for i in range(N):
# if unshuffled_pred[i] ==0:
# continue
# pred_index.append([i])
# pred.append([unshuffled_pred[i]])
# # %%
# plt.plot(full_signal)
# plt.scatter(pred_index,pred)
# print('MSE: ' + str(metrics.mean_squared_error(real_predictions,real_target)))