diff --git a/definition.py b/definition.py new file mode 100644 index 0000000000000000000000000000000000000000..e133b08fae6a7f90728af3be0cf6acf646fa0479 --- /dev/null +++ b/definition.py @@ -0,0 +1,144 @@ +from __future__ import (absolute_import, division, print_function, unicode_literals) +from cmath import cos, sin, exp, polar, acos +from math import pi +import matplotlib as mpl +from mpl_toolkits.mplot3d import Axes3D +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg + +mpl.rcParams['legend.fontsize'] = 10 + +class Plate: + """type = ''LR, 'LP', 'CP' + Fast axis unchanged convention (pour un LR) + orientation = Right, Left (pour un CP)""" + def __init__(self, element, theta = 0, delta = 0, orientation = ""): + self.element = element + self.delta = delta + self.theta = theta + self.orientation = orientation + +def coef(real, imag): + a = 0 + if real != 0: + a = real + if imag != 0: + a+= imag*1j + return(a) + +class Photon: + def __init__(self, state0, state1): + self.state0 = [state0] + self.state1 = [state1] + + def state(self): + print(self.state0[-1], "|0> + ", self.state1[-1], "|1>") + + def round_state(self,decimal,i): + s_0r = round((self.state0[i]).real,decimal) + s_0i = round((self.state0[i]).imag,decimal) + s_1r = round((self.state1[i]).real,decimal) + s_1i = round((self.state1[i]).imag,decimal) + return(s_0r, s_0i, s_1r, s_1i) + + def is_2D(self,i): + s_0r = round((self.state0[i]).real,3) + s_0i = round((self.state0[i]).imag,3) + s_1r = round((self.state1[i]).real,3) + s_1i = round((self.state1[i]).imag,3) + return (s_0r*s_0i == 0 and s_1r*s_1i == 0 and s_0r*s_1i == 0 and s_1r*s_0i == 0) + + def pur(self,i): + rho0, phi0 = polar(self.state0[i]) + rho1, phi1 = polar(self.state1[i]) + return (2*acos(rho0), phi1-phi0) + + def gate(self,plate): + alpha = self.state0[-1] + beta = self.state1[-1] + if plate.element == 'LP': + self.state0.append(alpha*cos(plate.theta)*cos(plate.theta) + beta*cos(plate.theta)*sin(plate.theta)) + self.state1.append(alpha*cos(plate.theta)*sin(plate.theta) + beta*sin(plate.theta)*sin(plate.theta)) + if plate.element == 'LR': + self.state0.append(alpha*(cos(plate.theta)*cos(plate.theta) + exp(1j*plate.delta)*sin(plate.theta)*sin(plate.theta)) + beta*(1-exp(1j*plate.delta))*cos(plate.theta)*sin(plate.theta)) + self.state1.append(alpha*(1-exp(1j*plate.delta))*cos(plate.theta)*sin(plate.theta) + beta*(sin(plate.theta)*sin(plate.theta) + exp(1j*plate.delta)*cos(plate.theta)*cos(plate.theta))) + else: + if plate.orientation == "Right": + self.state0.append(1/2*(alpha + 1j*beta)) + self.state1.append(1/2*(beta - 1j*alpha)) + else: + self.state0.append(1/2*(alpha - 1j*beta)) + self.state1.append(1/2*(beta + 1j*alpha)) + + def representation(self,i): + + #Bloch Sphere + + fig = plt.figure() + ax = fig.gca(projection='3d') + """thismanager = plt.get_current_fig_manager() + thismanager.window.SetPosition((500, 0)) + thismanager.window.wm_geometry("+500+0")""" + ax.set_title('Step '+str(i)) + theta = np.linspace(0, 2 * np.pi, 100) + z = np.zeros(100) + x = np.sin(theta) + y = np.cos(theta) + ax.plot(x, y, z, color = 'black', linestyle='dashed', linewidth=0.5, label='sphere') + ax.plot(y, z, x, color = 'black', linestyle='dashed', linewidth=0.5) + ax.plot(z, x, y, color = 'black', linestyle='dashed', linewidth=0.5) + ax.quiver(0, 0, 0, 0, 0, 1, color = 'black', arrow_length_ratio = 0.1) + ax.text(0, 0, 1.1, '|0>', color = 'black') + ax.quiver(0, 0, 0, 0, 0, -1, color = 'black', arrow_length_ratio = 0.1) + ax.text(0, 0, -1.1, '|1>', color = 'black') + + if i>0: + theta, phi = self.pur(i-1) + ax.quiver(0, 0, 0, sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta), color = 'red', arrow_length_ratio = 0.1, label ='before') + ax.text(sin(theta)*cos(phi)+0.1, sin(theta)*sin(phi)+0.1, cos(theta)+0.1, 'before', color = 'red') + + theta, phi = self.pur(i) + ax.quiver(0, 0, 0, sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta), color = 'green', arrow_length_ratio = 0.1, label ='after') + ax.text(sin(theta)*cos(phi)+0.1, sin(theta)*sin(phi)+0.1, cos(theta)+0.1, 'after', color = 'green') + + ax.grid(False) + ax.axis(False) + ax.legend() + + #2D representation + + if self.is_2D(i): + fig2 = plt.figure() + ax_2D = fig2.add_subplot(111) + theta = np.linspace(0, 2 * np.pi, 100) + x = np.sin(theta) + y = np.cos(theta) + ax_2D.plot(x,y, color = 'black', linestyle='dashed', linewidth=0.5, label='circle') + ax_2D.quiver(*[0, 0], [0, 1], [1,0], scale = 1, scale_units = 'xy', color = 'black') + ax_2D.text(0, 1.1, '|0>', color = 'black') + ax_2D.text(1.1, 0, '|1>', color = 'black') + ax_2D.grid(False) + ax_2D.axis(False) + ax_2D.legend() + (s_0r, s_0i, s_1r, s_1i) = self.round_state(3,i) + if (s_0r != 0 or s_1r != 0): + ax_2D.quiver(*[0, 0], [s_1r], [s_0r], scale = 1, scale_units = 'xy', color = 'red', label = 'Phase nul') + ax_2D.text(s_1r +0.1, s_0r + 0.1, 'after', color = 'red') + else: + ax_2D.quiver(*[0, 0], [s_1i], [s_0i], scale = 1, scale_units = 'xy', color = 'red', label = 'Phase pi/2') + ax_2D.text(s_1i +0.1, s_0i + 0.1, 'after', color = 'red') + + if i>0: + if self.is_2D(i-1): + (s_0r, s_0i, s_1r, s_1i) = self.round_state(3,i-1) + if (s_0r != 0 or s_1r != 0): + ax_2D.quiver(*[0, 0], [s_1r], [s_0r], scale = 1, scale_units = 'xy', color = 'green', label = 'Phase nul') + ax_2D.text(s_1r +0.1, s_0r + 0.1, 'before', color = 'green') + else: + ax_2D.quiver(*[0, 0], [s_1i], [s_0i], scale = 1, scale_units = 'xy', color = 'green', label = 'Phase pi/2') + ax_2D.text(s_1i +0.1, s_0i + 0.1, 'before', color = 'green') + + + plt.show() +