Browse Source

Add transmittance, calculating e, r

master
Lev 3 years ago
parent
commit
aa5e237db1
  1. 3
      model/channel.py
  2. 50
      model/main.py

3
model/channel.py

@ -40,6 +40,7 @@ class QChannelImpl(Channel):
mu: float = 1
# eta - the probability for a detector to react to a photon
detector_sensitivity: float = 0.8
transmittance: float = 0.8
def get_number_of_photos(self) -> int:
return round(np.random.poisson(self.mu))
@ -74,6 +75,8 @@ class QChannelImpl(Channel):
for photons, basis in zip(self.bob_photons, basises):
clicks = [random.uniform(0, 1) <= self.p_dc, random.uniform(0, 1) <= self.p_dc]
for ph_bit, ph_basis in photons:
if random.uniform(0, 1) > self.transmittance:
continue
if random.uniform(0, 1) <= self.detector_sensitivity: # We detected everything correctly
if ph_basis != basis:
if random.uniform(0, 1) > 0.5:

50
model/main.py

@ -2,29 +2,41 @@ from alice import Alice
from bob import Bob
from channel import QChannelImpl
from threading import Thread
import typing
if __name__ == '__main__':
n = 1000
channel = QChannelImpl()
alice, bob = Alice(channel), Bob(channel)
def run_qkd(alice, bob, n=1000):
alice_thread = Thread(target=lambda: alice.generate_key(n))
bob_thread = Thread(target=lambda: bob.generate_key(n))
alice_thread.start()
bob_thread.start()
while alice_thread.is_alive() or bob_thread.is_alive():
pass
print(list(map(int, alice.key)))
print(list(map(int, bob.key)))
print('Alice bits: ', *list(map(int, alice.sent)))
print('Bob bits: ',
*list(map(lambda t: {(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(t[0]), int(t[1]))], bob.my_results)))
print('Alice basises:', *list(map(int, alice.my_basises)))
print('Bob basises: ', *list(map(int, bob.my_basises)))
bob_correctness = [left + right == 1 for left, right in bob.my_results]
print(' ', *[
int(k) if c and b1 == b2 else ' '
for c, k, b1, b2 in zip(bob_correctness, alice.sent, bob.my_basises, alice.my_basises)])
print(' ', *[
{(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(k[0]), int(k[1]))] if c and b1 == b2 else ' '
for c, k, b1, b2 in zip(bob_correctness, bob.my_results, bob.my_basises, alice.my_basises)])
print(f'{100 * sum(k1 == k2 for k1, k2 in zip(alice.key, bob.key)) / len(alice.key):.2f}%, key length: {len(alice.key)}')
def get_e_and_r(alice, bob, n=1000) -> typing.Tuple[float, float]:
return 1 - sum(k1 == k2 for k1, k2 in zip(alice.key, bob.key)) / len(alice.key), len(alice.key) / n
if __name__ == '__main__':
N = 1000
channel = QChannelImpl()
alice, bob = Alice(channel), Bob(channel)
run_qkd(alice, bob)
# print(list(map(int, alice.key)))
# print(list(map(int, bob.key)))
# print('Alice bits: ', *list(map(int, alice.sent)))
# print('Bob bits: ',
# *list(map(lambda t: {(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(t[0]), int(t[1]))], bob.my_results)))
# print('Alice basises:', *list(map(int, alice.my_basises)))
# print('Bob basises: ', *list(map(int, bob.my_basises)))
# bob_correctness = [left + right == 1 for left, right in bob.my_results]
# print(' ', *[
# int(k) if c and b1 == b2 else ' '
# for c, k, b1, b2 in zip(bob_correctness, alice.sent, bob.my_basises, alice.my_basises)])
# print(' ', *[
# {(0, 1): 1, (1, 0): 0, (0, 0): 2, (1, 1): 3}[(int(k[0]), int(k[1]))] if c and b1 == b2 else ' '
# for c, k, b1, b2 in zip(bob_correctness, bob.my_results, bob.my_basises, alice.my_basises)])
# print(f'{100 * sum(k1 == k2 for k1, k2 in zip(alice.key, bob.key)) / len(alice.key):.2f}%, key length: {len(alice.key)}')
e, r = get_e_and_r(alice, bob, N)
print(f'E: {e * 100:.1f}%, R: {r}')

Loading…
Cancel
Save