Browse Source

Theory calculations

master
Lev 3 years ago
parent
commit
af058396fe
  1. BIN
      images/re_vs_pdc_eve.png
  2. 29
      model/eve.py
  3. 36
      model/main.py

BIN
images/re_vs_pdc_eve.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 49 KiB

29
model/eve.py

@ -28,3 +28,32 @@ class EveBS:
else: else:
self.obtained_data.append(None) self.obtained_data.append(None)
return passed return passed
@dataclass
class EvePNS:
k: int = 1
my_basises: typing.List[bool] = field(default_factory=list)
obtained_data: typing.List[typing.Optional[bool]] = field(default_factory=list)
def process_photons(self, photons: typing.List[typing.Tuple[bool, bool]]) -> typing.List[typing.Tuple[bool, bool]]:
basis = choice([False, True])
self.my_basises.append(basis)
clicks = [0, 0]
passed = []
captured = 0
for photon in photons:
if captured < self.k:
captured += 1
photon_bit, photon_basis = photon
if photon_basis == basis:
clicks[photon_bit] += 1
else:
clicks[choice([0, 1])] += 1
else:
passed.append(photon)
if any(clicks) and not all(clicks):
self.obtained_data.append(bool(clicks[1]))
else:
self.obtained_data.append(None)
return passed

36
model/main.py

@ -1,8 +1,12 @@
import json
import os.path
from alice import Alice from alice import Alice
from bob import Bob from bob import Bob
from channel import ChannelSym from channel import ChannelSym
from threading import Thread from threading import Thread
import typing import typing
import sympy as sp
from eve import EveBS from eve import EveBS
@ -21,11 +25,17 @@ def get_e_and_r(alice, bob, n=1000) -> typing.Tuple[float, float]:
channel_parameters = {'p_opt': 0.05, 'p_dc': 0.05, 'mu': 1, 'detector_sensitivity': 0.8, 'transmittance': 0.8} channel_parameters = {'p_opt': 0.05, 'p_dc': 0.05, 'mu': 1, 'detector_sensitivity': 0.8, 'transmittance': 0.8}
PAR_NAMES = {'p_opt': 'p_{opt}', 'p_dc': 'p_{dc}', 'mu': r'\mu', 'detector_sensitivity': r'\eta', 'transmittance': 't'}
mu, t, p_dc, p_opt, eta = sp.symbols(r'\mu t p_{dc} p_{opt} \eta')
e_theory = 0.5 * (p_dc + p_opt * mu * t * eta) / (2 * p_dc + mu * t * eta)
q_theory = (mu * t * eta + 2 * p_dc) / 2 * (1 - e_theory * 2)
def plot(parameter, values, add_eve: bool = True): def plot(parameter, values, add_eve: bool = False):
data_r, data_e = [], [] data_r, data_e = [], []
n = 1000 data_r_theoretical, data_e_theoretical = [], []
parameters_sp = {PAR_NAMES[pname]: channel_parameters[pname] for pname in PAR_NAMES.keys()}
n = 10000
for val in values: for val in values:
print(val) print(val)
channel = ChannelSym(**{parameter: val}) channel = ChannelSym(**{parameter: val})
@ -37,12 +47,22 @@ def plot(parameter, values, add_eve: bool = True):
e, r = get_e_and_r(alice, bob, n) e, r = get_e_and_r(alice, bob, n)
data_r.append(r) data_r.append(r)
data_e.append(e) data_e.append(e)
parameters_sp[PAR_NAMES.get(parameter, parameter)] = val
data_r_theoretical.append(float(q_theory.evalf(subs=parameters_sp)))
data_e_theoretical.append(float(e_theory.evalf(subs=parameters_sp)))
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
plt.plot(values, data_r) plt.plot(values, data_r, label='$R$')
plt.plot(values, data_e) plt.plot(values, data_e, label='$E$')
plt.legend(['R', 'E']) plt.plot(values, data_r_theoretical, label='$R_{theory}$')
plt.xlabel({'p_dc': '$p_{dc}$'}.get(parameter, parameter)) plt.plot(values, data_e_theoretical, label='$E_{theory}$')
plt.title('$R$ and $E$ vs. ' + {'p_dc': '$p_{dc}$'}.get(parameter, parameter) + ' with Eve' * add_eve) plt.legend()
if not os.path.exists('output'):
os.mkdir('output')
with open(f'output/dep_{parameter}.json', 'w') as f:
f.write(json.dumps([list(values), data_r, data_e, data_r_theoretical, data_e_theoretical]))
plt.xlabel('$' + PAR_NAMES.get(parameter, parameter) + '$')
plt.title('$R$ and $E$ vs. $' + PAR_NAMES.get(parameter, parameter) + '$' + ' with Eve' * add_eve)
plt.savefig(f'output/dep_{parameter}.png')
plt.show() plt.show()
@ -75,3 +95,5 @@ def run_one():
if __name__ == '__main__': if __name__ == '__main__':
import numpy as np import numpy as np
plot('p_dc', np.arange(0, 0.1, 0.01)) plot('p_dc', np.arange(0, 0.1, 0.01))
plot('detector_sensitivity', np.arange(0.5, 1, 0.05))
plot('mu', np.arange(0.5, 1.5, 0.1))

Loading…
Cancel
Save