-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_2_script.py
151 lines (104 loc) · 3.85 KB
/
d_2_script.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
#
# Copyright 2021-2024 Budapest Quantum Computing Group
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import json
import time
from datetime import datetime
import piquasso as pq
from piquasso.cvqnn import get_cvqnn_weight_indices
import numpy as np
from utils import AdamOptimizer, gaussian_mmd_grad, gaussian_mmd
now = datetime.now()
folder_name = "data"
d = 2
shots = 1000
diff_shots = shots
learning_rate = 0.005
seed_sequence = 12345
cutoff = 10
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
FILENAME = f"{folder_name}/2_mode_{now.day}_{now.hour}_{now.minute}_{shots}_{seed_sequence}.json"
def save_data(data):
with open(FILENAME, "w") as f:
json.dump(data, f)
def get_samples(simulator, weights, shots):
with pq.Program() as program:
pq.Q() | pq.Vacuum()
pq.Q(0) | pq.CubicPhase(6.0)
pq.Q(1) | pq.CubicPhase(3.0)
pq.Q(0, 1) | pq.CrossKerr(np.pi / 6)
pq.Q(0) | pq.Displacement(weights[0])
pq.Q(1) | pq.Squeezing(weights[1])
pq.Q(0, 1) | pq.Beamsplitter(weights[2])
pq.Q() | pq.HomodyneMeasurement()
samples = simulator.execute(program, shots).samples
return samples
if __name__ == "__main__":
ITER = 600
simulator = pq.PureFockSimulator(
d=d, config=pq.Config(cutoff=cutoff, seed_sequence=seed_sequence)
)
weights_to_learn = np.array([1.0, 0.1, np.pi / 6])
weights = np.zeros_like(weights_to_learn)
initial_weights = np.copy(weights)
weight_indices = get_cvqnn_weight_indices(d)
s_D = 1.0
s_S = 1.0
shift_amounts = [s_D, s_S, np.pi / 2]
multipliers = [1.0 / s_D, 1.0 / np.sinh(s_S), 1.0]
optimizer = AdamOptimizer(learning_rate, number_of_weights=len(weights))
grad_vector = np.empty_like(weights)
data = {
"losses": [],
"weights": [],
"shots": shots,
"cutoff": cutoff,
"learning_rate": learning_rate,
"ts": s_S,
"td": s_D,
"d": d,
"weights_to_learn": weights_to_learn.tolist(),
"seed_sequence": seed_sequence,
"weight_history": [],
"time": None,
}
start_time = time.time()
samples = get_samples(simulator, weights, shots)
for i in range(ITER):
samples_to_learn = get_samples(simulator, weights_to_learn, shots)
for j in range(len(weights)):
weights[j] += shift_amounts[j]
plus_samples = get_samples(simulator, weights, diff_shots)
weights[j] -= 2 * shift_amounts[j]
minus_samples = get_samples(simulator, weights, diff_shots)
weights[j] += shift_amounts[j]
mmd_grad = gaussian_mmd_grad(
plus_samples, minus_samples, samples, samples_to_learn
)
grad_vector[j] = multipliers[j] * mmd_grad
weights = optimizer.update_weights(weights, grad_vector)
samples = get_samples(simulator, weights, shots)
if True:
loss = gaussian_mmd(samples_to_learn, samples)
print("-----------------")
print(f"{i}. loss: {loss}")
print("weights:", weights)
print("grad:", grad_vector)
data["losses"].append(loss)
data["weights"] = [weights.tolist()]
data["weight_history"].append(weights.tolist())
data["time"] = time.time() - start_time
save_data(data)