From fb673e4963dc4a9fc392911138c61fb143ca6364 Mon Sep 17 00:00:00 2001 From: Lou Bernabeu <lou.bernabeu@student-cs.fr> Date: Tue, 5 Jan 2021 21:05:00 +0100 Subject: [PATCH] Added basic classes of PLate and BeamSplitter --- Circuit/__init__.py => .gitignore | 0 Elements/__init__.py => __main__.py | 0 elements/__init__.py | 2 + elements/main.py | 58 +++++++++++++++++++++++++++++ {Graphics => graphics}/__init__.py | 0 main.py => requirements.txt | 0 6 files changed, 60 insertions(+) rename Circuit/__init__.py => .gitignore (100%) rename Elements/__init__.py => __main__.py (100%) create mode 100644 elements/__init__.py create mode 100644 elements/main.py rename {Graphics => graphics}/__init__.py (100%) rename main.py => requirements.txt (100%) diff --git a/Circuit/__init__.py b/.gitignore similarity index 100% rename from Circuit/__init__.py rename to .gitignore diff --git a/Elements/__init__.py b/__main__.py similarity index 100% rename from Elements/__init__.py rename to __main__.py diff --git a/elements/__init__.py b/elements/__init__.py new file mode 100644 index 0000000..9664db0 --- /dev/null +++ b/elements/__init__.py @@ -0,0 +1,2 @@ +import numpy as np +import cmath diff --git a/elements/main.py b/elements/main.py new file mode 100644 index 0000000..ddc33e2 --- /dev/null +++ b/elements/main.py @@ -0,0 +1,58 @@ +class Source: + def __init__(self, initial_vector=np.array([0., 0.+0j])): + self.photon = initial_vector + self.next = None + + +class Screen: + def __init__(self, previous=(None, None)): + self.previous = previous + + +class Element: + def __init__(self, previous): ## previous is either a single (previous_object, output) tuple, or a 2-dictionary of tuples + self.previous = previous ## (as in {"left": (Object 1, "label of chosen output of object 1"), "right": (obj2, "label2")}) + self.next = None + +class Plate(Element): + def __init__(self, jones_matrix = np.eye((2, 2), dtype='complex')): + self.jones_matrix = jones_matrix + self.output_vector = None + + def out(self, side): + previous_element = self.previous[0] + previous_element_output_side = self.previous[1] + self.output_vector = self.jones_matrix @ previous_element(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 + +class BeamSplitter(Element): + """matrix is a 4x4 complex ndarray, two first dimensions regard left input, two next are right. So it's blockwise defined.""" + def __init__(self, matrix = np.eye((4, 4), dtype='complex')): + self.matrix = matrix + self.output_vector = None + + def out(self, side): + if self.previous["left"] is None: + left_input_state = np.zeros((2,), dtype="complex") # if no input on the left, then |\psi> = 0 + else: + left_input_element = self.previous["left"][0] + left_input_element_output_side = self.previous["left"][1] + left_input_state = left_input_element.out(left_input_element_output_side) + + if self.previous["right"] is None: + left_input_state = np.zeros((2,), dtype="complex") # if no input on the right, then |\psi> = 0 + else: + right_input_element = self.previous["right"][0] + right_input_element_output_side = self.previous["right"][1] + right_input_state = right_input_element.out(right_input_element_output_side) + + input_state = np.concatenate((left_input_state, right_input_state)) + self.output_vector = self.matrix @ input_state + + if side == "left": + ans, _ = np.split(self.output_vector) + + elif side == "right": + _, ans = np.split(self.output_vector) + + return ans diff --git a/Graphics/__init__.py b/graphics/__init__.py similarity index 100% rename from Graphics/__init__.py rename to graphics/__init__.py diff --git a/main.py b/requirements.txt similarity index 100% rename from main.py rename to requirements.txt -- GitLab