-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
101 lines (66 loc) · 2.03 KB
/
benchmark.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
import piquasso as pq
import numba as nb
import numpy as np
import matplotlib.pyplot as plt
from piquasso.cvqnn import (
generate_random_cvqnn_weights,
create_layers,
)
from datetime import datetime
import os
import json
import time
max_d = 10
layer_count = 1
seed_sequence = 1234
shots = 100
now = datetime.now()
folder_name = "data"
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
FILENAME = f"{folder_name}/benchmark_{now.day}_{now.hour}_{now.minute}_{max_d}_{layer_count}_{shots}_{seed_sequence}.json"
def get_samples(simulator, weights, shots):
layers = create_layers(weights)
with pq.Program() as program:
pq.Q() | pq.Vacuum()
pq.Q() | layers
pq.Q(*tuple(range(simulator.d))) | pq.HomodyneMeasurement()
return simulator.execute(program, shots).samples
def generate_cvqnn_samples(simulator, d):
weights_to_learn = generate_random_cvqnn_weights(
layer_count, d, active_var=1.0, passive_var=np.pi, rng=simulator.config.rng
)
samples = get_samples(simulator, weights_to_learn, shots)
return samples
def save_data(data):
with open(FILENAME, "w") as f:
json.dump(data, f)
def benchmark():
ITER = 100
warmup = 10
times = {"means": [], "range": []}
for d in range(1, 11):
print("d:", d)
runtimes = []
cutoff = 7
if d == 7:
ITER = 1
warmup = 1
for i in range(ITER + warmup):
simulator = pq.PureFockSimulator(
d=d,
config=pq.Config(
cutoff=cutoff, seed_sequence=seed_sequence + cutoff + i
),
)
start_time = time.time()
generate_cvqnn_samples(simulator, d)
if i >= warmup:
runtimes.append(time.time() - start_time)
mean_runtime = np.mean(runtimes)
print(mean_runtime)
times["means"].append(mean_runtime)
times["range"].append(d)
save_data(times)
if __name__ == "__main__":
benchmark()