From d53fffa382d3a53adad158e6ec892e227ac04ee2 Mon Sep 17 00:00:00 2001 From: Lou Bernabeu <bernabeu.lou@gmail.com> Date: Fri, 24 Sep 2021 11:17:53 +0200 Subject: [PATCH] updated to add time dependence of steps --- __main__.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++---- elements.py | 58 ++++++++++++++++++++++++++++++-- graphics.py | 1 + matrices.py | 5 +++ 4 files changed, 151 insertions(+), 9 deletions(-) diff --git a/__main__.py b/__main__.py index f21af91..b3c2324 100644 --- a/__main__.py +++ b/__main__.py @@ -4,15 +4,20 @@ import matplotlib.pyplot as plt import elements import graphics import matrices +from math import sqrt print("Hi World!") ## Tests -print("###------ Portes anticommutantes ------") -source = elements.Source(initial_vector = np.array([1. + 0j, 0.])) +'''print("###------ Portes anticommutantes ------") +source = elements.Source(initial_vector = np.array([(3/4)*(1. + 0j), (4/5)*(1. + 0j)])) + +elements.Screen(previous = (source, "left")).show() BS1 = elements.BeamSplitter(previous = {"left": None, "right": (source, "left")}, matrix=matrices.HALF_MIRROR_LEFT_ACTIVE) +elements.Screen(previous = (BS1, "left")).show("left") +elements.Screen(previous = (BS1, "right")).show("right") X_left_1 = elements.Plate(previous = (BS1, "left"), jones_matrix=matrices.X) Z_left_2 = elements.Plate(previous = (X_left_1, "left"), jones_matrix = matrices.Z) @@ -21,20 +26,23 @@ Z_left_2 = elements.Plate(previous = (X_left_1, "left"), jones_matrix = matrices Z_right_1 = elements.Plate(previous = (BS1, "right"), jones_matrix = matrices.Z) X_right_2 = elements.Plate(previous = (Z_right_1, "left"), jones_matrix = matrices.X) -BS2 = elements.BeamSplitter(previous = {"left": (Z_left_2, "left"), "right": (X_right_2, "left")}, matrix = matrices.HALF_MIRROR_RIGHT_ACTIVE) +BS2 = elements.BeamSplitter(previous = {"left": (X_right_2, "left"), "right": (Z_left_2, "left")}, matrix = matrices.HALF_MIRROR_RIGHT_ACTIVE) screen_right = elements.Screen(previous = (BS2, "right")) screen_left = elements.Screen(previous = (BS2, "left")) screen_left.show("left") -screen_right.show("right") +screen_right.show("right")''' -print("### ------ Portes commutantes ------") +'''print("### ------ Portes commutantes ------") source2 = elements.Source(initial_vector = np.array([1.+0j, 0.])) BS12 = elements.BeamSplitter(previous = {"left": (source2, "left"), "right": None}, matrix=matrices.SYM_BS_INACTIVE) +elements.Screen(previous = (BS12, "left")).show() +elements.Screen(previous = (BS12, "right")).show() + Phase_left_12 = elements.Plate(previous = (BS12, "left"), jones_matrix=matrices.PHASE(np.pi/5)) Z_left_22 = elements.Plate(previous = (Phase_left_12, "left"), jones_matrix = matrices.Z) @@ -48,4 +56,80 @@ screen_right2 = elements.Screen(previous = (BS22, "right")) screen_left2 = elements.Screen(previous = (BS22, "left")) screen_left2.show("left") -screen_right2.show("right") +screen_right2.show("right")''' + + +'''print("###------ Tests BeamSplitters ------") +source = elements.Source(initial_vector = np.array([(3/5)*(1. + 0j), (4/5)*(1. + 0j)])) + +elements.Screen(previous = (source, "left")).show() + +BS1 = elements.BeamSplitter(previous = {"left": None, "right": (source, "left")}, matrix=matrices.HALF_MIRROR_LEFT_ACTIVE) + +elements.Screen(previous = (BS1, "left")).show("left") +elements.Screen(previous = (BS1, "right")).show("right")''' + +'''print("###------ Portes classiques ------") +source = elements.Source(initial_vector = np.array([1. + 0j, 0.])) + +elements.Screen(previous = (source, "left")).show() + +Nom = {str(matrices.X) : "X", + str(matrices.Y) : "Y", + str(matrices.Z) : "Z", + str(matrices.H) : "H", + str(matrices.S) : "S", + str(matrices.T) : "T",} + +Portes = [matrices.X, + matrices.Y, + matrices.Z, + matrices.H, + matrices.S, + matrices.T] + +for porte1 in Portes: + P1 = elements.Plate(previous = (source, "left"), jones_matrix=porte1) + elements.Screen(previous = (P1, "left")).show( Nom[str(porte1)] + "|ψ⟩") + for porte2 in Portes: + P2 = elements.Plate(previous = (P1, "left"), jones_matrix=porte2) + elements.Screen(previous = (P2, "left")).show(Nom[str(porte2)] + Nom[str(porte1)] + "|ψ⟩")''' + +print("###------ Tentative marche aléatoire ------") + + +def marche(n): + Photons = [elements.Source(initial_vector = np.array([0., 1.+0j]))] + for i in range(n): + New_Photons = [] + new_photons = dict() + for source in Photons: + PH = elements.Plate(previous = (source, "left"), jones_matrix=matrices.H) + BS = elements.BeamSplitter(previous = {"left": (PH, "left"), "right": None}, matrix=matrices.POLARISER_BS) + LEFT = elements.Path(previous = (BS, "left"), time = -1) + RIGHT = elements.Path(previous = (BS, "right"), time = 1) + Screen_left = elements.Screen(previous = (LEFT, "left")) + Screen_right = elements.Screen(previous = (RIGHT, "right")) + if Screen_left.temporality() in new_photons.keys(): + new_photons[Screen_left.temporality()][0] += Screen_left.out()[0] + new_photons[Screen_left.temporality()][1] += Screen_left.out()[1] + else: + new_photons[Screen_left.temporality()] = np.array([Screen_left.out()[0], Screen_left.out()[1]]) + if Screen_right.temporality() in new_photons.keys(): + new_photons[Screen_right.temporality()][0] += Screen_right.out()[0] + new_photons[Screen_right.temporality()][1] += Screen_right.out()[1] + else: + new_photons[Screen_right.temporality()] = np.array([Screen_right.out()[0], Screen_right.out()[1]]) + Photons = [elements.Source(initial_vector = list(new_photons.values())[k] ,time = list(new_photons.keys())[k] ) for k in range(len(new_photons))] + repartition = dict() + for photon in Photons: + if photon.temporality('left') in repartition.keys(): + repartition[photon.temporality('left')] += photon.out('left')[0]**2 + photon.out('left')[1]**2 + else: + repartition[photon.temporality('left')] = photon.out('left')[0]**2 + photon.out('left')[1]**2 + plt.plot(list(repartition.keys()),list(repartition.values())) + plt.title("Distribution de densité de probabilité") + plt.xlabel("Position") + plt.ylabel("Probabilité de présence") + plt.show() +marche(100) \ No newline at end of file diff --git a/elements.py b/elements.py index d9498ca..414560a 100644 --- a/elements.py +++ b/elements.py @@ -2,12 +2,16 @@ import numpy as np ### ------ Special elements ------ class Source: - def __init__(self, initial_vector=np.array([1., 0.+0j])): ## if no input state given, starts a |0> + def __init__(self, initial_vector=np.array([1., 0.+0j]), time = 0): ## if no input state given, starts a |0> self.photon = initial_vector self.next = None + self.time = time def out(self, side): return self.photon + + def temporality(self, side): + return self.time class Screen: @@ -17,14 +21,18 @@ class Screen: def show(self, name = 'ψ'): previous_component, previous_component_output_side = self.previous ket = previous_component.out(previous_component_output_side) - alpha = round(ket[0]) - beta = round(ket[1]) + alpha = round(ket[0],3) + beta = round(ket[1],3) print(f"|{name}⟩ = {alpha}|0⟩ + {beta}|1⟩") def out(self): previous_component, previous_component_output_side = self.previous return previous_component.out(previous_component_output_side) + def temporality(self): + previous_component, previous_component_output_side = self.previous + return previous_component.temporality(previous_component_output_side) + ### ------ Classic Elements ------ @@ -33,6 +41,19 @@ class Element: self.previous = previous ## (as in {"left": (Object 1, "label of chosen output of object 1"), "right": (obj2, "label2")}) self.next = None +class Path(Element): + def __init__(self, previous, time): + Element.__init__(self, previous) + self.time = time + + def out(self, side): + previous_component, previous_component_output_side = self.previous + return previous_component.out(previous_component_output_side) + + def temporality(self, side): + previous_component, previous_component_output_side = self.previous + return previous_component.temporality(previous_component_output_side) + self.time + class Plate(Element): def __init__(self, previous, jones_matrix = np.eye(2, dtype='complex')): Element.__init__(self, previous) @@ -44,6 +65,10 @@ class Plate(Element): previous_element_output_side = self.previous[1] self.output_vector = self.jones_matrix @ previous_element.out(previous_element_output_side) # matrix-vector product : jones matrix times state vector given by the out(side) method of the previous component return self.output_vector + + def temporality(self, side): + previous_component, previous_component_output_side = self.previous + return previous_component.temporality(previous_component_output_side) class BeamSplitter(Element): """matrix is a 4x4 complex ndarray, two first dimensions regard left input, two next are right. So it's blockwise defined.""" @@ -77,3 +102,30 @@ class BeamSplitter(Element): _, ans = np.split(self.output_vector, 2) return ans + + def temporality(self, side): + if self.previous["left"] is None and self.previous["right"] is None: + return 0 + + elif self.previous["left"] is None: + right_previous_component = self.previous["right"][0] + right_previous_component_output_side = self.previous["right"][1] + right_time = right_previous_component.temporality(right_previous_component_output_side) + return right_time + + elif self.previous["right"] is None: + left_previous_component = self.previous["left"][0] + left_previous_component_output_side = self.previous["left"][1] + left_time = left_previous_component.temporality(left_previous_component_output_side) + return left_time + else: + right_previous_component = self.previous["right"][0] + right_previous_component_output_side = self.previous["right"][1] + right_time = right_previous_component.temporality(right_previous_component_output_side) + left_previous_component = self.previous["left"][0] + left_previous_component_output_side = self.previous["left"][1] + left_time = left_previous_component.temporality(left_previous_component_output_side) + if side == "left": + return left_time + elif side == "right": + return right_time \ No newline at end of file diff --git a/graphics.py b/graphics.py index e69de29..796eb8e 100644 --- a/graphics.py +++ b/graphics.py @@ -0,0 +1 @@ +import matplotlib.pyplot as plt diff --git a/matrices.py b/matrices.py index 6662709..8fb6268 100644 --- a/matrices.py +++ b/matrices.py @@ -73,3 +73,8 @@ HALF_MIRROR_RIGHT_ACTIVE = (1/np.sqrt(2))*np.array([[1, 0, 1, 0], [0, -1, 0, 1], [1, 0, -1, 0], [0, 1, 0, 1]], dtype='complex') + +POLARISER_BS = np.array([[1, 0, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1], + [0, 1, 0, 0]], dtype='complex') -- GitLab