Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- # COMPLETE ONE FUNCTION AT A TIME
- # SUBMIT THE CODE AND GET IT ACCEPTED
- # THEN, START THE NEXT FUNCTION
- # TASK1
- def createQubit(alpha, beta):
- """
- Parameters:
- Probability amplitutde = alpha -> float
- Probability amplitutde = beta -> float
- Return:
- A qubit state -> list [x, y], which represents x|0> + y|1>
- """
- N = math.sqrt(alpha**2 + beta**2)
- return [alpha/N, beta/N]
- # DO NOT modify this function
- # You may need to use this function in TASK 2 and TASK 3
- def stateFromProbabilities(p0, p1):
- """
- Parameters:
- Probability of Zero = p0 -> float
- Probability of One = p1 -> float
- Return:
- Measured state from the probabilities
- """
- r = random.random()
- if r < p0:
- return 0
- else:
- return 1
- # TASK2
- def ZBasisMeasure(state):
- """
- This function performs a simulated Z Basis measurement.
- Parameters:
- A qubit state = state -> list [x, y]
- Return:
- 0 or 1 depending on the simulated measurement result
- """
- x, y = state
- # p0 = probability of reading 0
- p0 = x**2
- # p1 = probability of reading 1
- p1 = y**2
- return stateFromProbabilities(p0, p1)
- # TASK3
- def XBasisMeasure(state):
- """
- This function performs a simulated X Basis measurement
- Parameters:
- A qubit state = state -> list [x, y]
- Return:
- 0 or 1 depending on the simulated measurement result
- """
- [x, y] = state
- # p0 = probability of reading 0
- p0 = 0.5*(x**2 + y**2) + x*y
- # p1 = probability of reading 1
- p1 = 0.5*(x**2 + y**2) - x*y
- return stateFromProbabilities(p0, p1)
- # TASK4
- def tomographyExperiment(alpha, beta, numTrials):
- """
- This funciton performs a simple quantum tomography experiment.
- Parameters:
- Probability amplitutde = alpha -> float
- Probability amplitutde = beta -> float
- Number of trials = numTrials -> int
- Return:
- A tuple: (guess, E)
- where:
- Estimated qubit state = guess -> list [guess_x, guess_y]
- Error = E -> float
- """
- outcomes = [0, 0]
- [x, y] = qubit = createQubit(alpha, beta) # creates a qubit representing |q> = x|0> + y|0>
- for i in range(numTrials):
- m = ZBasisMeasure(qubit) # Measures outcome in Z basis
- outcomes[m] += 1
- guess_x = math.sqrt(outcomes[0] / numTrials)
- guess_y = math.sqrt(outcomes[1] / numTrials)
- print('Estimated x =', guess_x)
- print('Estimated y =', guess_y)
- E = (guess_x - x)**2 + (guess_y - y)**2
- print('The error is', E)
- return ([guess_x, guess_y], E)
- ######### Quantum Key Distribution #########
- # TASK5
- # DO NOT modify this function
- def AlicePrepare():
- """
- Alice prepares a qubit using this function.
- First, she chooses a random key and a random basis.
- Next, she prepares an actual qubit 'state'.
- Return:
- A tuple: (key, basis, state)
- """
- key = random.randint(0, 1)
- # For Z Basis Measure, basis = 0
- # For X Basis Measure, basis = 1
- basis = random.randint(0, 1)
- if (key == 0) and (basis == 0):
- # A |0> bit is created when alpha = 1 and beta = 0.
- state = createQubit(1, 0)
- elif (key == 1) and (basis == 0):
- # A |1> bit is created when alpha = 0 and beta = 1.
- state = createQubit(0, 1)
- elif (key == 0) and (basis == 1):
- state = createQubit(1. / math.sqrt(2.), 1. / math.sqrt(2.))
- else:
- state = createQubit(1. / math.sqrt(2.), -1. / math.sqrt(2.))
- return (key, basis, state)
- def BobMeasure(state):
- """
- Bob measures the qubit using this function.
- First, he chooses a random basis (Z or X). Hint: Notice the function above. How basis was generated?
- Next, he measures in that basis
- This measurement value is his key
- Parameters:
- A qubit state = state -> list [x, y]
- Return:
- A tuple: (basis, key) Here, basis = 0 for ZBasisMeasure and basis = 1 for XBasisMeasure
- """
- basis = random.randint(0, 1)
- key = ZBasisMeasure(state) if basis == 0 else XBasisMeasure(state)
- return (basis, key)
- def QKDSimulation(numTrials):
- """
- This function simulates the BB84 QKD protocol for "numTrials" times
- In each trial:
- First, run Alice's preparation
- Next, Bob's measurement
- Finally, perform reconciliation (step 5 in the writeup)
- Append to 'aliceKey' and 'bobKey'
- Parameters:
- Number of trials = numTrials -> int
- Return:
- Shared kay = aliceKey -> list
- """
- aliceKey = []
- bobKey = []
- for i in range(numTrials):
- aliceKeyBit, aliceBasis, state = AlicePrepare()
- bobBasis, bobKeyBit = BobMeasure(state)
- if aliceBasis == bobBasis:
- aliceKey.append(aliceKeyBit)
- bobKey.append(bobKeyBit)
- print('Size of key is', len(aliceKey))
- return aliceKey
- if __name__ == "__main__":
- alpha = float(input())
- beta = float(input())
- numTrials = int(input())
- print(tomographyExperiment(alpha, beta, numTrials))
- print(QKDSimulation(numTrials))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement