import numpy as np import matplotlib.pyplot as plt import time # initialization wxyz = 4754 np.random.seed(wxyz) # settings N = int(100000) 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 # calculate outcome of observation station x1 = (np.cos(2 * (phi1 - thetas[:, 0])) >= (2*np.random.rand(N) -1)).reshape(N, 1) t1 = T_0 * np.random.rand(N) * np.sin(2 * (phi1 - thetas[:, 0])) ** 4 x2 = (np.cos(2 * (phi2 - thetas[:, 1])) >= (2*np.random.rand(N) -1)).reshape(N, 1) t2 = T_0 * np.random.rand(N) * np.sin(2 * (phi2 - thetas[:, 1])) ** 4 # 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) # creates array to store counts in # 1st index ~ without time constraints (0) or with coincidence constraint (1) # 2nd index ~ the respective outcome [0, 0] (0), [0, 1] (1), [1, 0] (2), or [1, 1] (3) counts = np.empty((2, 4), dtype=np.int32) # calculates counts without time constraints 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) # calculates counts with coincidence constraint counts[1, 0] = np.sum(np.logical_and(np.all(x1x2 == np.array([0, 0]), axis=1), coincidences), axis=0) counts[1, 1] = np.sum(np.logical_and(np.all(x1x2 == np.array([0, 1]), axis=1), coincidences), axis=0) counts[1, 2] = np.sum(np.logical_and(np.all(x1x2 == np.array([1, 0]), axis=1), coincidences), axis=0) counts[1, 3] = np.sum(np.logical_and(np.all(x1x2 == np.array([1, 1]), axis=1), coincidences), axis=0) # computes expectancy values for x1*x2, x1 and x2 E_all = np.sum(counts, axis=1).astype(float) print(E_all) 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 return E_12, E_1, E_2 def try_all_configs(n_configs): # allocates storage for each required expectancy value 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) # simulates all configurations 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_ # correlation phi = np.linspace(0, 360/n_configs*(n_configs-1), n_configs) phi_theory = np.linspace(0, 360/n_configs*(n_configs-1), n_configs*100) theory = -np.cos(2 * phi_theory * (2 * np.pi / 360)) plt.figure() plt.xlabel(r'$\Delta \varphi$ in degrees') plt.ylabel(r'$\left$') plt.xticks([0, 45, 90, 135, 180, 225, 270, 305, 360]) 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$ in degrees') plt.ylabel(r'$\left\left$') 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() # correlation phi = np.linspace(0, 360/n_configs*(n_configs-1), n_configs) phi_theory = np.linspace(0, 360/n_configs*(n_configs-1), n_configs*100) theory = -np.cos(2 * phi_theory * (2 * np.pi / 360)) plt.figure() plt.xlabel(r'$\Delta \varphi$ in degrees') plt.ylabel(r'$\left$') plt.xticks([0, 45, 90, 135, 180, 225, 270, 305, 360]) plt.plot(phi_theory, theory, '.', markersize=1, color='orange') plt.plot(phi, E_12[:, 1], 'o', color='blue') plt.savefig('correlation_coinc.pdf') if showPlots: plt.show() plt.close() # product of expectation values line = np.zeros(phi_theory.shape) plt.figure() plt.xlabel(r'$\Delta \varphi$ in degrees') plt.ylabel(r'$\left\left$') 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[:, 1]*E_2[:, 1], 'o', color='blue') plt.savefig('expectation_value_coinc.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')