-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseparable_simulations
174 lines (125 loc) · 4.02 KB
/
separable_simulations
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
import pennylane as qml
from pennylane import numpy as np
from pennylane.optimize import NesterovMomentumOptimizer
from scipy.stats import unitary_group
import matplotlib.pyplot as plt
import itertools
from functools import reduce
N = 2
dev = qml.device('default.qubit', wires=N)
def U_phi(x):
# x_2 = (pi - x_0)(pi - x_1)
for i in range(N):
qml.RZ( x[i], wires=0)
for (j, pair) in enumerate(itertools.combinations(range(N), r=2)):
qml.CNOT(wires=[pair[0], pair[1]])
qml.RZ( x[N + j], pair[1])
qml.CNOT(wires=[pair[0], pair[1]])
def featuremap(x):
for i in range(layers):
for j in range(N):
qml.Hadamard(wires=j)
U_phi(x)
def layer(W): # 6 weights are specified at each layer
for i in range(N):
if i == (N-1):
qml.Rot(W[0, 0], W[0, 1], W[0, 2], wires=0)
qml.Rot(W[N-1, 0], W[N-1, 1], W[N-1, 2], wires=N-1)
qml.CNOT(wires=[0, N-1])
else:
# euler angles
qml.Rot(W[i, 0], W[i, 1], W[i, 2], wires=i)
qml.Rot(W[i+1, 0], W[i+1, 1], W[i+1, 2], wires=i + 1)
qml.CNOT(wires=[i, i+1])
@qml.qnode(dev)
def circuit(weights, x, n=0):
featuremap(x)
for W in weights:
layer(W)
return qml.expval.PauliZ(wires=n)
def variational_classifier(var, x): # x is a keyword argument -> fixed (not trained)
weights = var[0]
bias = var[1]
exp_Z = circuit(weights, x, n=0)
for i in range(1, N):
e = circuit(weights,x,n=i)
exp_Z *= e
return exp_Z + bias
def square_loss(labels, predictions):
loss = 0
for l, p in zip(labels, predictions):
loss = loss + (l - p) ** 2
loss = loss / len(labels)
return loss
def accuracy(labels, predictions):
#print(labels, predictions)
loss = 0
for l, p in zip(labels, predictions):
if abs(l - p) < 1e-5:
loss = loss + 1
loss = loss / len(labels)
return loss
def cost(var, X, Y):
predictions = [variational_classifier(var, x) for x in X]
#if (len(Y) == num_data):
# print("[(pred, label), ...]: ", list(zip(predictions, Y)))
return square_loss(Y, predictions)
def gen_random_U():
random_U = unitary_group.rvs(2 ** N)
random_U = random_U / (np.linalg.det(random_U) ** (1/(2**N))) # so that det = 1
return random_U
@qml.qnode(dev)
def data_label(x, i=0):
#print(u)
#print("label the following:", x)
featuremap(x)
qml.QubitUnitary(random_U, wires=list(range(N)))
return qml.expval.PauliZ(wires=i)
def gen_data(thresh):
#thresh = 0.3
X = np.array([])
Y = np.array([])
ctr = 0 # num valid data pts
maxval = 0.0
minval = 0.0
np.random.seed(0)
while ctr < 40:
x = np.random.rand(N) * 2 * np.pi
for pair in itertools.combinations(range(N), r=2):
x = np.append(x, (np.pi - x[pair[0]]) * (np.pi - x[pair[1]]))
y = []
for i in range(N):
y.append(data_label(x, i=i))
y_prod = reduce((lambda x, y: x * y), y)
#print(y, y_prod)
if (y_prod > maxval):
maxval = y_prod
print("new max separation: ", maxval)
elif (y_prod < minval):
minval = y_prod
print("new min separation: ", minval)
if y_prod > thresh:
Y = np.append(Y, +1)
X = np.append(X, x)
ctr += 1
#print("+1")
elif y_prod < -1 * thresh:
Y = np.append(Y, -1)
X = np.append(X, x)
ctr += 1
#print("-1")
X = X.reshape(-1, 3)
print("Data: ", list(zip(X, Y)))
return X, Y
def divide_train_test(X, Y):
global num_data
num_data = len(Y)
global num_train
num_train = int(0.5 * num_data)
print("size data, size train: ", num_data, num_train)
index = np.random.permutation(range(num_data))
X_train = X[index[:num_train]]
Y_train = Y[index[:num_train]]
X_test = X[index[num_train:]]
Y_test = Y[index[num_train:]]
return X_train, Y_train, X_test, Y_test