Guest User

Untitled

a guest
Dec 19th, 2021
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. import numpy as np
  2. from math import pi, sin, cos
  3. import matplotlib.pyplot as plt
  4.  
  5. def rotate(xy, theta):
  6.     R = np.array([[cos(theta), -sin(theta)],
  7.                   [sin(theta),  cos(theta)]])
  8.     return R.dot(xy)
  9.  
  10. def project(u, v):
  11.     return np.dot(u, v)
  12.  
  13. def generate_samples(n=10000):
  14.  
  15.     # uniformly sample a 2D vector from the unit circle
  16.     angles = np.random.uniform(low=0, high=2 * pi, size=(n,1))
  17.     xs = np.cos(angles)
  18.     ys = np.sin(angles)
  19.     return np.hstack([xs, ys])
  20.  
  21. # number of samples
  22. n = 5000
  23.  
  24. # generate entangled particles. I've called these xy and uv
  25. xy = generate_samples(n)
  26. uv = -xy
  27.  
  28. # P, Q, R are measurement settings. In this case, we will take 3 axes, and measure
  29. # if the generated vector is positive or negative along the axis by projecting it,
  30. # and seeing if the projecting is positive or negative
  31. P = np.array([1., 0.])
  32. R = rotate(P, 240 * pi / 180)
  33.  
  34. # we will vary Q, and use the inequality Pr(P+,Q+) \leq Pr(P+,R+) + Pr(Q+,R+) to ensure that
  35. # Bell's inequality holds over the test range
  36. angle_settings = np.linspace(0, 2 * pi)
  37. PQ_data, PR_data, QR_data = [], [], []
  38. PQ_data_qm, PR_data_qm, QR_data_qm = [], [], []
  39. correlation = []
  40. for phi in angle_settings:
  41.  
  42.     # rotate the sensor axis Q
  43.     Q = rotate(P, phi)
  44.  
  45.     # project the data point onto P, Q, R
  46.     projected_p1 = np.array([project(x, P) for x in xy])
  47.     projected_q1 = np.array([project(x, Q) for x in xy])
  48.     projected_r1 = np.array([project(x, R) for x in xy])
  49.  
  50.     # project the entangled data point onto P, Q, R
  51.     projected_p2 = np.array([project(x, P) for x in uv])
  52.     projected_q2 = np.array([project(x, Q) for x in uv])
  53.     projected_r2 = np.array([project(x, R) for x in uv])
  54.  
  55.     # get Pr(P+,Q+), Pr(P+,R+), Pr(Q+,R+)
  56.     PQ_positive = np.logical_and(projected_p1 > 0, projected_q2 > 0)
  57.     PR_positive = np.logical_and(projected_p1 > 0, projected_r2 > 0)
  58.     QR_positive = np.logical_and(projected_r1 > 0, projected_q2 > 0)
  59.     PQ_data.append(np.sum(PQ_positive) / n)
  60.     PR_data.append(np.sum(PR_positive) / n)
  61.     QR_data.append(np.sum(QR_positive) / n)
  62.  
  63.     # get quantum correlation. This is just (# of agreements - # of disagreements) / n between detectors
  64.     # P and Q. Why? Because if the detectors always agree, the correlation is 1. If they always disagree,
  65.     # the correlation is -1. If they disagree as much as they agree, the correlation is zero.
  66.     agreement = np.sum(PQ_positive + np.logical_and(projected_p1 < 0, projected_q2 < 0))
  67.     disagreement = np.sum(np.logical_and(projected_p1 > 0, projected_q2 < 0) + np.logical_and(projected_p1 < 0, projected_q2 > 0))
  68.     corr = (agreement - disagreement) / n
  69.     correlation.append(corr)
  70.  
  71. # Bell's inequality holds, and the correlation plot is linear. Shit.
  72. plt.plot(angle_settings * 180 / pi, PQ_data)
  73. plt.plot(angle_settings * 180 / pi, [p + q for p, q in zip(PR_data, QR_data)])
  74. plt.plot(angle_settings * 180 / pi, correlation)
  75. plt.plot(angle_settings * 180 / pi, -np.cos(angle_settings), "--k")
  76. plt.title("Bell's inequality with locally hidden variables")
  77. plt.xlim([0,360])
  78. plt.ylim([-1, 1])
  79. plt.ylabel("Pr(.)")
  80. plt.xlabel("angle between P, Q (degrees)")
  81. plt.legend(["Pr(P > 0, Q > 0)","Pr(P > 0, R > 0) + Pr(R > 0, Q > 0)", "correlation", "QM prediction"])
  82. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment