fix uniform distribution for points on a sphere

This commit is contained in:
Daniel Tsvetkov 2020-01-30 16:29:27 +01:00
parent 5266040972
commit 29b298e6a7

View File

@ -24,8 +24,10 @@ def math_sim(q2func=from_angles_1, iterations=1000, sample_count=1):
all_samples = defaultdict(int) all_samples = defaultdict(int)
for i in range(iterations): for i in range(iterations):
# print("Running iteration {}".format(i)) # print("Running iteration {}".format(i))
theta = round(np.random.uniform(0, np.pi), 3) # Generating uniform random points on a sphere is not trivial
phi = round(np.random.uniform(0, 2 * np.pi), 3) # https://www.bogotobogo.com/Algorithms/uniform_distribution_sphere.php
theta = np.arccos(2 * np.random.uniform(0, 1) - 1.0)
phi = 2 * np.pi * np.random.uniform(0, 1.0)
# print("theta: {:.2f} ({:.2f} deg) | phi: {:.2f} ({:.2f} deg)".format( # print("theta: {:.2f} ({:.2f} deg) | phi: {:.2f} ({:.2f} deg)".format(
# theta, np.rad2deg(theta), # theta, np.rad2deg(theta),
# phi, np.rad2deg(phi))) # phi, np.rad2deg(phi)))
@ -63,6 +65,9 @@ class MemoizedExp(object):
def gen_exp_for_cirq_0(): def gen_exp_for_cirq_0():
# TODO: FIX THIS AS AN EXPONENT UNIFORM DISTRIBUTION
# Generating uniform random points on a sphere is not trivial
# https://www.bogotobogo.com/Algorithms/uniform_distribution_sphere.php
theta = round(np.random.uniform(0, 1), 10) theta = round(np.random.uniform(0, 1), 10)
phi = round(np.random.uniform(0, 1), 10) phi = round(np.random.uniform(0, 1), 10)
return theta, phi return theta, phi
@ -132,5 +137,7 @@ def cirq_sim_1(*args, **kwargs):
if __name__ == "__main__": if __name__ == "__main__":
# rv = cirq.bloch_vector_from_state_vector(np.array([[0.988], [0.156j]]), 0)
# print(rv)
math_sim_0() math_sim_0()
cirq_sim_0() # cirq_sim_0()