-
Notifications
You must be signed in to change notification settings - Fork 2
/
mps rand qmlf.py
200 lines (172 loc) · 6.04 KB
/
mps rand qmlf.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
# %%
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)
# %%
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[3])
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
N_QUBITS = (r + 1)
print(N_QUBITS)
n_layers = 2
N_PARAMS_B = 4
dev = qml.device('default.qubit', wires= N_QUBITS)
fpqc_weights = weights = 2 * np.pi * np.random.random(size=(n_layers, 3, r), requires_grad=True)
fpqc_x = 2 * np.pi *np.random.random(size = (r))
mps_weights = 2 * np.pi * np.random.random(size=(N_QUBITS-1,N_PARAMS_B), requires_grad=True)
mps_x = 2 * np.pi *np.random.random(size = (N_QUBITS))
def FPQC_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])
def MPS_Block(weights,wires):
qml.RY(weights[0], wires=wires[0])
qml.RX(weights[1], wires=wires[0])
qml.RZ(weights[2], wires=wires[1])
qml.RY(weights[3], wires=wires[1])
qml.CNOT(wires=wires)
@qml.qnode(dev, interface="autograd")
def FPQC(weights,x):
qml.AngleEmbedding(x,wires=range(N_QUBITS)[1:]) # Features x are embedded in rotation angles
for j in range(n_layers):
FPQC_Block(weights[j])
return qml.expval(qml.PauliZ(wires=0))
def MPS_PQC(w,x):
qml.AngleEmbedding(x,wires=range(N_QUBITS)) # Features x are embedded in rotation angles
qml.MPS(wires=range(N_QUBITS), n_block_wires=2,block=MPS_Block, n_params_block=N_PARAMS_B, template_weights=w) # Variational layer
return qml.expval(qml.PauliZ(N_QUBITS-1)) # Expectation value of the \sigma_z operator on the last qubit
FPQC(fpqc_weights,fpqc_x)
print(qml.draw(FPQC,expansion_strategy ="device")(fpqc_weights,fpqc_x))
# %%
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(PQC, weights, x, y):
predictions = [PQC(weights, x_) 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
optimizer = [qml.AdamOptimizer(.1), qml.AdagradOptimizer(.1)]
opt = optimizer[1]
batch_size = train_size//max_steps
cst = [cost(FPQC, fpqc_weights, x, target_y)] # initial cost
cst_t = [cost(FPQC, fpqc_weights, x_t, target_y_t)]
epochs = 10
# %%
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
weights,_,_,_= opt.step(cost, FPQC, fpqc_weights, x_batch, y_batch) # Calculating weights using the batches.
c = cost(FPQC, weights, x, target_y) # Calculating the cost using the whole train data
c_t = cost(FPQC, weights, x_t, target_y_t)
cst.append(c)
cst_t.append(c_t)
# %%