From aa5e237db14324b95191f6069666ecfec985c96d Mon Sep 17 00:00:00 2001 From: ennucore Date: Wed, 3 Nov 2021 13:15:46 +0300 Subject: [PATCH] Add transmittance, calculating e, r --- model/channel.py | 3 +++ model/main.py | 50 ++++++++++++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/model/channel.py b/model/channel.py index 7fea19e..ba24565 100644 --- a/model/channel.py +++ b/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: diff --git a/model/main.py b/model/main.py index c7b1037..2223ddc 100644 --- a/model/main.py +++ b/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}')