From 9aaebd4b340eba1129be8421a3acde50e56a8dbe Mon Sep 17 00:00:00 2001 From: Didictateur Date: Mon, 23 Oct 2023 13:11:12 +0200 Subject: [PATCH] exceptions Matrix --- QElephant/Matrix.py | 49 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/QElephant/Matrix.py b/QElephant/Matrix.py index 0e056b1..65304fc 100644 --- a/QElephant/Matrix.py +++ b/QElephant/Matrix.py @@ -4,12 +4,25 @@ I = complex(0, 1) class Matrix: def __init__(self, l: list[list[complex]]=[]) -> None: + if type(l) is not list: + raise ValueError(f"l was expected to be list[list[complex]], no {type(l)}") + for u_ in l: + if type(u_) is not list: + raise ValueError(f"l was expected to be list[list[complex]]. A {type(u_)} has been found") + for l_ in l: + for x in l_: + if type(x) not in {int, float, complex}: + raise ValueError(f"l was expected to be list[list[complex]]. A {type(x)} has been found") + self.__m = l if l == []: self.__m = [[0, 0], [0, 0]] self.__size = (len(self.__m), len(self.__m[0])) def __mul__(self, __value: "Matrix") -> "Matrix": + if type(__value) is not Matrix: + raise ValueError(f"cannot multiply Matrix with {type(__value)}") + l: list[list[complex]] = [] for i in range(self.__size[0]*__value.__size[0]): l.append([0]*(self.__size[1]*__value.__size[1])) @@ -24,6 +37,11 @@ class Matrix: return Matrix(l) def __apply(self, x: list[complex]) -> list[complex]: + if type(x) is not list: + raise TypeError(f"can apply a Matrix to a list[complex, not to a {type(x)}]") + for y in x: + if type(y) not in {int, float, complex}: + TypeError(f"can apply a Matrix to a ") assert(self.__size[1] == len(x)) y: list[complex] = [0]*self.__size[0] @@ -88,6 +106,9 @@ class Matrix: @staticmethod def Rx(phi: float) -> "Matrix": + if type(phi) not in {int, float}: + TypeError(f"an angle must be integer or float, not {type(phi)}") + c = math.cos(phi/2) s = math.sin(phi/2) return Matrix([ @@ -97,6 +118,9 @@ class Matrix: @staticmethod def Ry(phi: float) -> "Matrix": + if type(phi) not in {int, float}: + TypeError(f"an angle must be integer or float, not {type(phi)}") + c = math.cos(phi/2) s = math.sin(phi/2) return Matrix([ @@ -106,6 +130,9 @@ class Matrix: @staticmethod def Rz(phi: float) -> "Matrix": + if type(phi) not in {int, float}: + TypeError(f"an angle must be integer or float, not {type(phi)}") + s = math.e**(I*phi/2) return Matrix([ [1/s, 0], @@ -114,6 +141,9 @@ class Matrix: @staticmethod def R1(phi: float) -> "Matrix": + if type(phi) not in {int, float}: + TypeError(f"an angle must be integer or float, not {type(phi)}") + s = math.e**(I*phi) return Matrix([ [1, 0], @@ -139,8 +169,21 @@ class Matrix: ]) @staticmethod - def Cu(u: "Matrix") -> "Matrix": - assert(u.size == (2, 2)) + def Cu(u: list[list[complex]]) -> "Matrix": + if type(u) is not list: + raise ValueError(f"u was expected to be list[list[complex]], not {type(u)}") + for u_ in u: + if type(u_) is not list: + raise ValueError(f"u was expected to be list[list[complex]]. A {type(u_)} has been found") + for l in u: + for x in l: + if type(x) not in {int, float, complex}: + raise ValueError(f"u was expected to be list[list[complex]]. A {type(x)} has been found") + if len(u) != 2 or len(u[0]) != 2: + if len(u) == 0: + ValueError(f"the size of the matrix was expected to be (2, 2). A matrix of size (0, .) has been given") + ValueError(f"the size of the matrix was expected to be (2, 2). A matrix of size ({len(u)}, {u[0]}) has been given") + l = [ [1, 0, 0, 0], [0, 1, 0, 0], @@ -148,6 +191,8 @@ class Matrix: [0, 0, u[1][0], u[1][1]] ] + return Matrix(l) + if __name__=="__main__": m1 = Matrix([[1]]) m2 = Matrix([[1, 1], [0, 1]])