Skip to content

Instantly share code, notes, and snippets.

@vgmoose
Created January 18, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgmoose/0510cf8dd20291b7bdba to your computer and use it in GitHub Desktop.
Save vgmoose/0510cf8dd20291b7bdba to your computer and use it in GitHub Desktop.
seeds
import random, sys
dirs = ["up", "down"]
class Particle:
def __init__(self, seed, spin):
self.seed = seed
self.spin = spin
def measure(self, angle):
# return up or down
# where angle is P(up)
random.seed(self.seed)
rando = random.random()
zeroOrOne = (rando < angle)
return dirs[self.spin - zeroOrOne]
def create_particles(seed):
# return two entangled particles
a1 = Particle(seed, True)
a2 = Particle(seed, False)
return (a1, a2)
def sanity_check(seed):
# generate particles
p1, p2 = create_particles(seed)
# when measured with the same angle, should be opposite
print p1.measure(.5), p2.measure(.5)
def bell_test():
# measures at 3 different angles randomly
# and checks % of times they were opposite
# using random seeds for each set of particles
# expected output of this is 5/9 opposite
# init thangs
same_attempts = same_opp_outputs = same_same_outputs = dif_opp_outputs = dif_same_outputs = dif_attempts = 0
for x in range(1, 50000):
# generate particles
p1, p2 = create_particles(x)
# which angles we can choose from
angles = [0.333, 0.666, 0.999]
# decide on which two angles
# this is okay on the current seed because
# of http://stackoverflow.com/a/2221769
ang1 = random.choice(angles)
ang2 = random.choice(angles)
# measure the particles
m1 = p1.measure(ang1)
m2 = p2.measure(ang2)
# increment counters
same_attempts += (ang1 == ang2)
same_opp_outputs += (ang1==ang2)*(m1!=m2)
same_same_outputs += (ang1==ang2)*(m1==m2)
dif_attempts += (ang1 != ang2)
dif_opp_outputs += (ang1!=ang2)*(m1!=m2)
dif_same_outputs += (ang1!=ang2)*(m1==m2)
# print output
print "Bell Test", "\n", "-----------"
print "Number of same angle measurements:", same_attempts
print "Number of times opposite:", same_opp_outputs
print "Number of times same:", same_same_outputs
print "Percent opposite:", (same_opp_outputs/(same_attempts+0.0))
print ""
print "Number of different angle measurements:", dif_attempts
print "Number of times opposite:", dif_opp_outputs
print "Number of times same:", dif_same_outputs
print "Percent opposite:", (dif_opp_outputs/(dif_attempts+0.0)), "\n"
def bell_same_particle_test(seed):
# measures at 3 different angles randomly
# and checks % of times they were opposite
# using the same particles each time
# expected output of this is opposite
# init thangs
same_attempts = same_opp_outputs = same_same_outputs = dif_opp_outputs = dif_same_outputs = dif_attempts = 0
# generate single particle set
p1, p2 = create_particles(seed)
for x in range(1, 50000):
# which angles we can choose from
angles = [0.333, 0.666, 0.999]
# this on the other hand might not be ok
random.seed(x)
ang1 = random.choice(angles)
ang2 = random.choice(angles)
# measure the particles
m1 = p1.measure(ang1)
m2 = p2.measure(ang2)
# increment counters
same_attempts += (ang1 == ang2)
same_opp_outputs += (ang1==ang2)*(m1!=m2)
same_same_outputs += (ang1==ang2)*(m1==m2)
dif_attempts += (ang1 != ang2)
dif_opp_outputs += (ang1!=ang2)*(m1!=m2)
dif_same_outputs += (ang1!=ang2)*(m1==m2)
# print output
print "Bell Test w/ same particles", "\n", "-----------"
print "Number of same angle measurements:", same_attempts
print "Number of times opposite:", same_opp_outputs
print "Number of times same:", same_same_outputs
print "Percent opposite:", (same_opp_outputs/(same_attempts+0.0))
print ""
print "Number of different angle measurements:", dif_attempts
print "Number of times opposite:", dif_opp_outputs
print "Number of times same:", dif_same_outputs
print "Percent opposite:", (dif_opp_outputs/(dif_attempts+0.0)), "\n"
bell_test()
#def test_2(seed):
# measures at 3 different angles randomly
# and checks % of times they were opposite
# using the same particles each time
#print sanity_check(sys.argv[1])
bell_same_particle_test(sys.argv[1])
#def test_2(seed):
# measures at 3 different angles randomly
# and checks % of times they were opposite
#print sanity_check(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment