Commit 306f34e3 authored by Orkun Şensebat's avatar Orkun Şensebat
Browse files

Merge branch 'tobi'

parents ca05f27b 324727cd
Pipeline #466094 passed with stage
in 1 minute and 2 seconds
import numpy as np
import math
class ObservationStation:
def __init__(self, T):
self.T = T
def set_a(self, a):
self.a = a
def execute(self, phi, r):
c = math.cos(2 * (self.a - phi))
s = math.sin(2 * (self.a - phi))
x = np.sign(c)
t_star = r * self.T * s ** 2
return x, t_star
def generate_phis():
phi1 = np.random.rand() * 2 * np.pi
phi2 = phi1 + np.pi / 2
return phi1, phi2
def simulate():
M = 100
N = int(1e6)
a = np.array([0, 1/4, 1/8, 3/8]) * np.pi
W = 1
T = 1000
station = (ObservationStation(T), ObservationStation(T))
# conducts EPRB experiment M times
for _ in range(M):
results = np.zeros((N, 4, 2), dtype=np.int8)
import matplotlib.pyplot as plt
import time
# settings
N = int(1e6)
T_0 = 1000
W = 1
showPlots = True
def simulate(phi1, phi2):
# generate random phis
thetas = np.empty((N, 2), dtype=np.float32)
thetas[:, 0] = np.random.rand(N) * 2 * np.pi
thetas[:, 1] = thetas[:, 0] + np.pi / 2
# generate random number for timestamp
rs = np.random.rand(N)
# calculate outcome of observation station
x1 = (np.cos(2 * (phi1 - thetas[:, 0])) >= (2*np.random.rand(N) -1)).reshape(N, 1)
t1 = T_0 * rs * np.sin(2 * (phi1 - thetas[:, 0])) ** 2
x2 = (np.cos(2 * (phi2 - thetas[:, 1])) >= (2*np.random.rand(N) -1)).reshape(N, 1)
t2 = T_0 * rs * np.sin(2 * (phi2 - thetas[:, 1])) ** 2
# determine which events are conincidences (1) and which not (0)
coincidences = np.abs(t1 - t2) < W
# zip measurement outcome together
x1x2 = np.concatenate((x1, x2), axis=1)
counts = np.empty((2, 4), dtype=np.int32)
counts[0, 0] = np.sum(np.all(x1x2 == np.array([0, 0]), axis=1), axis=0)
counts[0, 1] = np.sum(np.all(x1x2 == np.array([0, 1]), axis=1), axis=0)
counts[0, 2] = np.sum(np.all(x1x2 == np.array([1, 0]), axis=1), axis=0)
counts[0, 3] = np.sum(np.all(x1x2 == np.array([1, 1]), axis=1), axis=0)
counts[1, 0] = np.sum(np.all(x1x2 == np.array([0, 0]), axis=1) == coincidences, axis=0)
counts[1, 1] = np.sum(np.all(x1x2 == np.array([0, 1]), axis=1) == coincidences, axis=0)
counts[1, 2] = np.sum(np.all(x1x2 == np.array([1, 0]), axis=1) == coincidences, axis=0)
counts[1, 3] = np.sum(np.all(x1x2 == np.array([1, 1]), axis=1) == coincidences, axis=0)
E_all = np.sum(counts, axis=1).astype(float)
E_12 = (counts[:, 0] - counts[:, 1] - counts[:, 2] + counts[:, 3]).astype(float)
E_1 = (counts[:, 0] + counts[:, 1] - counts[:, 2] - counts[:, 3]).astype(float)
E_2 = (counts[:, 0] - counts[:, 1] + counts[:, 2] - counts[:, 3]).astype(float)
if (np.all(E_all > 0)):
E_12 /= E_all
E_1 /= E_all
E_2 /= E_all
# iterates through all pairs of settings
for p in range(4):
# selects a unique pair of settings from all possible settings
station[0].set_a(a[int(p/2)])
station[1].set_a(a[2+p%2])
# simulates 10^6 events
for n in range(N):
#if n%1000 == 0:
# print(p, n)
phi = generate_phis()
for s in range(2):
r = np.random.rand()
x, t_star = station[s].execute(phi[s], r)
results[n, p, s] = x
# alternative approach to simulating 10^6 events
return E_12, E_1, E_2
# calculates absolute X
products = np.prod(results, axis=2)
products[:, 2] *= -1
sums = np.sum(products, axis=1)
X_abs = np.abs(sums)
if np.any(X_abs >= 2):
print(f'|X| <= 2 is violated with |x| = {X_abs.max()}')
else:
print(f'|X| <= 2 is satisfied with |x| = {X_abs.max()}')
simulate()
def try_all_configs(n_configs):
E_12 = np.empty((n_configs, 2), dtype=np.float32)
E_1 = np.empty((n_configs, 2), dtype=np.float32)
E_2 = np.empty((n_configs, 2), dtype=np.float32)
for i in range(n_configs):
phi1 = 0
phi2 = i * 2 * np.pi / n_configs
E_12_, E_1_, E_2_ = simulate(phi1, phi2)
E_12[i, :] = E_12_
E_1[i, :] = E_1_
E_2[i, :] = E_2_
x = np.linspace(0, 360, n_configs)
plt.figure()
plt.plot(x, E_12[:, 0])
plt.show()
# correlation
phi = np.linspace(0, 360, n_configs)
phi_theory = np.linspace(0, 360, n_configs*100)
theory = -np.cos(2 * phi_theory * (2 * np.pi / 360))
plt.figure()
plt.xlabel(r'$\Delta \varphi$')
plt.ylabel(r'$<x_1 x_2>$')
plt.plot(phi_theory, theory, '.', markersize=1, color='orange')
plt.plot(phi, E_12[:, 0], 'o', color='blue')
#plt.savefig('correlation.pdf')
if showPlots:
plt.show()
plt.close()
# product of expectation values
line = np.zeros(phi_theory.shape)
plt.figure()
plt.xlabel(r'$\Delta \varphi$')
plt.ylabel(r'$<x_1><x_2>$')
plt.ylim([-1, 1])
plt.xticks([0, 45, 90, 135, 180, 225, 270, 305, 360])
plt.plot(phi_theory, line, '.', markersize=1, color='orange')
plt.plot(phi, E_1[:, 0]*E_2[:, 0], 'o', color='blue')
#plt.savefig('expectation_value.pdf')
if showPlots:
plt.show()
plt.close()
start = time.time()
try_all_configs(32)
duration = time.time() - start
print(f'simulation took {round(duration, 2)} seconds')
\ No newline at end of file
import example
import eprb
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment