Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
241 lines (173 sloc) 7.48 KB
'''
written by Marcus Edwards on November 23, 2017
Code for the RPi Q.E.D. Device
'''
import numpy
from firebase import firebase
import serial
import time
from collections import Counter
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)
DEVICE_ID = 1 # replace with a unique ID
NUM_POSITIONS = 125
positionsDict = {}
finalPosition = 0
maxValue = 0
def talkg(steps):
gpio.output(20, True)
stepCount = 0
while StepCount < steps:
gpio.output(16, True)
time.sleep(0.000001)
gpio.output(16, False)
time.sleep(0.000001)
StepCount += 1
gpio.output(20, False)
finalPosition = (finalPosition + stepCount) % NUM_POSITIONS
def talkm(duration):
print('driving motor for {1} s'.format(duration))
GPIO.output(12, true)
time.sleep(duration)
GPIO.output(12, False)
def readp():
measurement = 0
print('resetting photo sensor capacitor...')
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, False)
time.sleep(0.1)
GPIO.setup(17, GPIO.IN)
while(GPIO.input(17) == False):
measurement += 1
print('photo sensor returned value of {0}'.format(measurement))
return measurement
def encode(a,b,g):
radius = a + (a/(2*g))
# center = numpy.array([-1/(2*g), -1/(2*g), b**2+b+a**2+a])
# reference = numpy.array([a,b,((2*(a**2+a)-1) + numpy.sqrt((2*(a**2+a)-1)**2 - 4*((a**2+a)**2+1/4-(a+a/(2*g))**2)))/2])
# data = numpy.array([-1+numpy.sqrt(1-4*g*(((2*(a**2+a)-1) - numpy.sqrt((2*(a**2+a)-1)**2 - 4*((a**2+a)**2+1/4-(a+a/(2*g))**2)))/2))/(2*g),b,((2*(a**2+a)-1) - numpy.sqrt((2*(a**2+a)-1)**2 - 4*((a**2+a)**2+1/4-(a+a/(2*g))**2)))/2])
# reference_norm = numpy.linalg.norm(reference-center)
# data_norm = numpy.linalg.norm(data-center)
dz = numpy.absolute(((2*(a**2+a)-1) + numpy.sqrt(numpy.absolute((2*(a**2+a)-1)**2 - 4*((a**2+a)**2+1/4-(a+a/(2*g))**2))))/2 - ((2*(a**2+a)-1) - numpy.sqrt(numpy.absolute((2*(a**2+a)-1)**2 - 4*((a**2+a)**2+1/4-(a+a/(2*g))**2))))/2)
dx = numpy.absolute(a - (-1+numpy.sqrt(numpy.absolute(1-4*g*(((2*(a**2+a)-1)) - numpy.sqrt(numpy.absolute((2*(a**2+a)-1)**2 - 4*((a**2+a)**2+1/4-(a+a/(2*g))**2))))/2))/(2*g)))
c = numpy.sqrt(dx**2+dz**2)
phi = numpy.arcsin(c*numpy.sin(numpy.pi - numpy.arcsin(dx/c))/radius)
theta = numpy.arcsin(b)
return numpy.array([phi,theta])
def decode(phi,theta,g):
c = numpy.sqrt(2-2*numpy.cos(phi))
alpha = - numpy.arcsin(numpy.sin(phi)/c)+numpy.pi/2
beta = numpy.pi - numpy.pi/2 - alpha
dx = c*numpy.sin(alpha)
dz = c*numpy.sin(beta)
b = numpy.sin(theta)
x = (dz/dx-1-g*b**2-b)/(2*g)
a = numpy.ceil(x)
return numpy.array([a,b])
def scanAllPositions():
talkm(0,1)
positions = 0
while(positions < 200):
print('position {0}'.format(positions))
talkg(1)
measurement = readp()
positionsDict[measurement] = positions
positions += 1
finalPosition = positions % NUM_POSITIONS
maxValue = max(positionsDict.values())
def firebaseAuth():
fire = firebase.FirebaseApplication('https://qed-database.firebaseio.com',None)
print('Auth success!')
return fire
def pauliZ(alpha,beta,dbHandle):
intitialState = numpy.array([[alpha],[beta]])
nextState = numpy.multiply(initialState,numpy.array([[1,0],[0,-1]]))
resultValue = rotate(nextState)
result = dbHandle.post('/devices/' + str(DEVICE_ID),{"result":resultValue})
print(result)
dbHandle.post('/devices/' + str(DEVICE_ID),{"done":"true"})
return measurement
def pauliY(alpha,beta,dbHandle):
intitialState = numpy.array([[alpha],[beta]])
nextState = numpy.multiply(initialState,numpy.array([[0,-numpy.complex(0,1)],[numpy.complex(0,1),0]]))
resultValue = rotate(nextState)
result = dbHandle.post('/devices/' + str(DEVICE_ID),{"result":resultValue})
print(result)
dbHandle.post('/devices/' + str(DEVICE_ID),{"done":"true"})
return measurement
def pauliX(alpha,beta,dbHandle):
intitialState = numpy.array([[alpha],[beta]])
nextState = numpy.multiply(initialState,numpy.array([[0,1],[1,0]]))
resultValue = rotate(nextState)
result = dbHandle.post('/devices/' + str(DEVICE_ID),{"result":resultValue})
print(result)
dbHandle.post('/devices/' + str(DEVICE_ID),{"done":"true"})
return measurement
def hadamard(alpha,beta,dbHandle):
intitialState = numpy.array([[alpha],[beta]])
nextState = numpy.multiply(initialState,numpy.array([[1/numpy.sqrt(2),1/numpy.sqrt(2)],[1/numpy.sqrt(2),-1/numpy.sqrt(2)]]))
resultValue = rotate(nextState)
result = dbHandle.post('/devices/' + str(DEVICE_ID),{"result":resultValue})
print(result)
dbHandle.post('/devices/' + str(DEVICE_ID),{"done":"true"})
return measurement
def rotate(nextState):
nextProbability = nextState.item(0,0)*nextState.item(0,0)
found = False
n=2
while (found == False):
base = nextProbability/n
if((1-base*numpy.floor_divide(1,base)) == 0):
found= True
n += 1
measurements = int(1/base)
probabilities = {}
for measurement in range(measurements):
if (measurement == (measurements - 1)):
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 0.2*measurement)), g: 256}
elif(measurement/measurements > 0.8):
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 0.4*measurement)), g: 128}
elif(measurement/measurements > 0.6):
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 0.6*measurement)), g: 64}
elif(measurement/measurements > 0.4):
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 0.8*measurement)), g: 32}
elif(measurement/measurements > 0.2):
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 1*measurement)), g: 16}
elif(measurement/measurements > 0.1):
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 1.2*measurement)), g: 8}
elif(measurement/measurements > 0.05):
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 1.4*measurement)), g: 4}
else:
probabilities[measurement*base] = {pos: int(NUM_POSITIONS/(2 + 1.6*measurement)), g: 2}
if(finalPosition < positionsDict[max(positionsDict)]):
steps = positionsDict[max(positionsDict)] - finalPosition
else:
steps = NUM_POSITIONS - abs(finalPosition - positionsDict[max(positionsDict)])
talkg(steps)
nextDict = probabilities.get(nextProbability, probabilities[min(probabilities.keys(), key=lambda k: abs(k-nextProbability))])
talkg(nextDict[pos])
measurementsArray = []
m = 0
while m < measurements:
measurementsArray.append(decode(2-readp()/maxValue,0,nextDict[g]))
m += 1
resultDistribution = dict(Counter(measurementsArray))
resultValue = numpy.sqrt(sum(resultDistribution) / float(len(resultDistribution)))
return resultValue
def checkDB(dbHandle):
result = dbHandle.get('/devices',DEVICE_ID)
print(result)
if(result['Z'] is not None):
pauliZ(result['Z'][0],result['Z'][1],dbHandle)
elif(result['Y'] is not null):
pauliY(result['Y'][0],result['Y'][1],dbHandle)
elif(result['X'] is not null):
pauliY(result['X'][0],result['X'][1],dbHandle)
elif(result['H'] is not null):
hadamard(result['H'][0],result['H'][1],dbHandle)
scanAllPositions()
dbHandle = firebaseAuth()
checkDB(dbHandle)
You can’t perform that action at this time.