-
Notifications
You must be signed in to change notification settings - Fork 42
/
photon_test.py
65 lines (48 loc) · 1.67 KB
/
photon_test.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
import time
from numpy import sin, cos
from numpy.random import random_sample, default_rng
from sequence.components.circuit import Circuit
from sequence.kernel.quantum_manager import QuantumManagerKet
from sequence.utils.quantum_state import QuantumState
SAMPLE_SIZE = 10000
basis = ((complex(1), complex(0)), (complex(0), complex(1)))
rng = default_rng()
qm = QuantumManagerKet()
meas_circ = Circuit(1)
meas_circ.measure(0)
runtimes = []
def timeit_wrapper(func):
def wrapper(*args, **kwargs):
start = time.perf_counter()
return_val = func(*args, **kwargs)
end = time.perf_counter()
runtimes.append(end - start)
return return_val
return wrapper
class ParticleOld:
def __init__(self, state):
self.qs = QuantumState()
self.qs.state = state
def measure(self):
return self.qs.measure(basis, rng)
class ParticleNew:
def __init__(self, state):
self.qs_key = qm.new(state)
def measure(self):
return qm.run_circuit(meas_circ, [self.qs_key], rng.random())
if __name__ == "__main__":
random_angles = random_sample(SAMPLE_SIZE)
states = []
for theta in random_angles:
states.append((cos(theta), sin(theta)))
@timeit_wrapper
def create_and_measure(states, particle_type):
for s in states:
p = particle_type(s)
p.measure()
create_and_measure(states, ParticleOld)
create_and_measure(states, ParticleNew)
print("Old Runtime: {}".format(runtimes[0]))
print("New Runtime: {}".format(runtimes[1]))
print("Old Per Photon: {}".format(runtimes[0]/SAMPLE_SIZE))
print("New Per Photon: {}".format(runtimes[1]/SAMPLE_SIZE))