-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQKD.py
More file actions
81 lines (61 loc) · 1.57 KB
/
QKD.py
File metadata and controls
81 lines (61 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import random
# Settings
numTries = 300
eavesdropper = True
showLists = False
showStats = True
# Stats
sameScheme = 0
sameBit = 0
sameBitSameScheme = 0
schemes = ['H/V', 'A/D']
alice = []
bob = []
aSchemesStr = ''
bSchemesStr = ''
aBitsStr = ''
bBitsStr = ''
for i in range(numTries):
# Generate random scheme and bit for Alice
aScheme = random.choice(schemes)
aBit = random.getrandbits(1)
# Generate random scheme for Bob
bScheme = random.choice(schemes)
# If Bob chooses the same scheme as Alice, Bob's bit is the same as Alice's.
if aScheme == bScheme:
# Keep track of how many times we get the same scheme
if showStats:
sameScheme += 1
if (eavesdropper & (random.random() < 0.5)):
bBit = random.getrandbits(1)
else:
bBit = aBit
if showStats & (aBit == bBit):
sameBitSameScheme += 1
else:
# Otherwise, Bob gets a random bit.
bBit = random.getrandbits(1)
if showStats & (aBit == bBit):
sameBit += 1
alice.append((aScheme, aBit))
bob.append((bScheme, bBit))
if showLists:
aSchemesStr += '|' + aScheme
bSchemesStr += '|' + bScheme
aBitsStr += "| " + str(aBit) + " "
bBitsStr += "| " + str(bBit) + " "
# print(alice)
# print(bob)
if showLists:
print("\nAlice: ")
print(aSchemesStr)
print(aBitsStr)
print("\nBob: ")
print(bSchemesStr)
print(bBitsStr)
print ("\n")
if showStats:
print("Same Scheme: " + str(round(sameScheme / numTries * 100, 2)) + "%")
print("Same Bit: " + str(round(sameBit / numTries * 100, 2)) + "%")
print("Same Bit for Same Scheme: " + str(round(sameBitSameScheme / sameScheme * 100, 2)) + "%")
print("\n")