# CS Group Project (Fixed)

Apr 22nd, 2021 (edited)
860
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import math
2. import random
3.
4.
5. # COMPLETE ONE FUNCTION AT A TIME
6. # SUBMIT THE CODE AND GET IT ACCEPTED
7. # THEN, START THE NEXT FUNCTION
8.
9.
11. def createQubit(alpha, beta):
12.     """
13.    Parameters:
14.    Probability amplitutde = alpha -> float
15.    Probability amplitutde = beta  -> float
16.
17.    Return:
18.    A qubit state -> list [x, y], which represents x|0> + y|1>
19.    """
20.     N = math.sqrt(alpha**2 + beta**2)
21.     return [alpha/N, beta/N]
22.
23.
24. # DO NOT modify this function
25. # You may need to use this function in TASK 2 and TASK 3
26. def stateFromProbabilities(p0, p1):
27.     """
28.    Parameters:
29.    Probability of Zero = p0 -> float
30.    Probability of One  = p1 -> float
31.
32.    Return:
33.    Measured state from the probabilities
34.    """
35.     r = random.random()
36.
37.     if r < p0:
38.         return 0
39.     else:
40.         return 1
41.
43. def ZBasisMeasure(state):
44.     """
45.    This function performs a simulated Z Basis measurement.
46.
47.    Parameters:
48.    A qubit state = state -> list [x, y]
49.
50.    Return:
51.    0 or 1 depending on the simulated measurement result
52.    """
53.     x, y = state
54.
55.     # p0 = probability of reading 0
56.     p0 = x**2
57.
58.     # p1 = probability of reading 1
59.     p1 = y**2
60.
61.     return stateFromProbabilities(p0, p1)
62.
64. def XBasisMeasure(state):
65.     """
66.    This function performs a simulated X Basis measurement
67.
68.    Parameters:
69.    A qubit state = state -> list [x, y]
70.
71.    Return:
72.    0 or 1 depending on the simulated measurement result
73.    """
74.     [x, y] = state
75.
76.     # p0 = probability of reading 0
77.     p0 = 0.5*(x**2 + y**2) + x*y
78.
79.     # p1 = probability of reading 1
80.     p1 = 0.5*(x**2 + y**2) - x*y
81.
82.     return stateFromProbabilities(p0, p1)
83.
85. def tomographyExperiment(alpha, beta, numTrials):
86.     """
87.    This funciton performs a simple quantum tomography experiment.
88.
89.    Parameters:
90.    Probability amplitutde = alpha     -> float
91.    Probability amplitutde = beta      -> float
92.    Number of trials       = numTrials -> int
93.
94.    Return:
95.    A tuple: (guess, E)
96.
97.    where:
98.    Estimated qubit state  = guess     -> list [guess_x, guess_y]
99.    Error                  = E         -> float
100.    """
101.     outcomes = [0, 0]
102.     [x, y] = qubit = createQubit(alpha, beta)        # creates a qubit representing |q> = x|0> + y|0>
103.     for i in range(numTrials):
104.         m = ZBasisMeasure(qubit)            # Measures outcome in Z basis
105.         outcomes[m] += 1
106.     guess_x = math.sqrt(outcomes[0] / numTrials)
107.     guess_y = math.sqrt(outcomes[1] / numTrials)
108.
109.     print('Estimated x =', guess_x)
110.     print('Estimated y =', guess_y)
111.
112.     E = (guess_x - x)**2 + (guess_y - y)**2
113.
114.     print('The error is', E)
115.     return ([guess_x, guess_y], E)
116.
117.
118. ######### Quantum Key Distribution #########
120.
121. # DO NOT modify this function
122. def AlicePrepare():
123.     """
124.    Alice prepares a qubit using this function.
125.    First, she chooses a random key and a random basis.
126.    Next, she prepares an actual qubit 'state'.
127.
128.    Return:
129.    A tuple: (key, basis, state)
130.    """
131.     key = random.randint(0, 1)
132.
133.     # For Z Basis Measure, basis = 0
134.     # For X Basis Measure, basis = 1
135.     basis = random.randint(0, 1)
136.
137.     if (key == 0) and (basis == 0):
138.         # A |0> bit is created when alpha = 1 and beta = 0.
139.         state = createQubit(1, 0)
140.     elif (key == 1) and (basis == 0):
141.         # A |1> bit is created when alpha = 0 and beta = 1.
142.         state = createQubit(0, 1)
143.     elif (key == 0) and (basis == 1):
144.         state = createQubit(1. / math.sqrt(2.), 1. / math.sqrt(2.))
145.     else:
146.         state = createQubit(1. / math.sqrt(2.), -1. / math.sqrt(2.))
147.
148.     return (key, basis, state)
149.
150.
151. def BobMeasure(state):
152.     """
153.    Bob measures the qubit using this function.
154.    First, he chooses a random basis (Z or X). Hint: Notice the function above. How basis was generated?
155.    Next, he measures in that basis
156.    This measurement value is his key
157.
158.    Parameters:
159.    A qubit state = state -> list [x, y]
160.
161.    Return:
162.    A tuple: (basis, key) Here, basis = 0 for ZBasisMeasure and basis = 1 for XBasisMeasure
163.    """
164.     basis = random.randint(0, 1)
165.     key = ZBasisMeasure(state) if basis == 0 else XBasisMeasure(state)
166.     return (basis, key)
167.
168.
169. def QKDSimulation(numTrials):
170.     """
171.    This function simulates the BB84 QKD protocol for "numTrials" times
172.    In each trial:
173.        First, run Alice's preparation
174.        Next, Bob's measurement
175.        Finally, perform reconciliation (step 5 in the writeup)
176.        Append to 'aliceKey' and 'bobKey'
177.
178.    Parameters:
179.    Number of trials = numTrials -> int
180.
181.    Return:
182.    Shared kay       = aliceKey  -> list
183.    """
184.     aliceKey = []
185.     bobKey = []
186.     for i in range(numTrials):
187.         aliceKeyBit, aliceBasis, state = AlicePrepare()
188.         bobBasis, bobKeyBit = BobMeasure(state)
189.         if aliceBasis == bobBasis:
190.             aliceKey.append(aliceKeyBit)
191.             bobKey.append(bobKeyBit)
192.     print('Size of key is', len(aliceKey))
193.     return aliceKey
194.
195. if __name__ == "__main__":
196.     alpha = float(input())
197.     beta = float(input())
198.     numTrials = int(input())
199.     print(tomographyExperiment(alpha, beta, numTrials))
200.     print(QKDSimulation(numTrials))
RAW Paste Data