From 47caa54c0d80707198fabe70d683e6bdc7383741 Mon Sep 17 00:00:00 2001 From: Didictateur Date: Mon, 23 Oct 2023 18:28:02 +0200 Subject: [PATCH] improved single gate --- QElephant/QuBit.py | 26 +++--- docs/Gate.md | 172 ------------------------------------- docs/Matrix.md | 196 ------------------------------------------- docs/QuBit/IQuBit.md | 48 ----------- docs/QuBit/MuBit.md | 63 -------------- docs/QuBit/QuBit.md | 42 ---------- setup.py | 2 +- 7 files changed, 11 insertions(+), 538 deletions(-) delete mode 100644 docs/Gate.md delete mode 100644 docs/Matrix.md delete mode 100644 docs/QuBit/IQuBit.md delete mode 100644 docs/QuBit/MuBit.md delete mode 100644 docs/QuBit/QuBit.md diff --git a/QElephant/QuBit.py b/QElephant/QuBit.py index a2b2ddb..ec01007 100644 --- a/QElephant/QuBit.py +++ b/QElephant/QuBit.py @@ -78,15 +78,13 @@ class MuBit: raise TypeError(f"can only manipulate MuBit with Matrix, not {type(matrix)}") i = i%self.__n - M = Matrix([[1]]) + H_ = np.kron(matrix._Matrix__m, np.identity(2**(self.__n-i-1))) + nstate = [] - for j in range(self.__n): - if j == i: - M *= matrix - else: - M *= Matrix.I() + for k in range(2**(i)): + nstate += np.dot(H_, self.__state[k*2**(self.__n-i):(k+1)*2**(self.__n-i)]).tolist() - self.__state = M._Matrix__apply(self.__state) + self.__state = nstate def __mapply(self, matrix: Matrix, i: int) -> None: if type(i) is not int: @@ -116,15 +114,11 @@ class MuBit: raise IndexError("MuBit index out of range") i = i%self.__n - M = Matrix([[1]]) - - for j in range(self.__n): - if j == i: - M *= Matrix([[1, 0], [0, 0]]) - else: - M *= Matrix.I() - - l = M._Matrix__apply(self.__state) + H_ = np.kron([[1, 0], [0, 0]], np.identity(2**(self.__n-i-1))) + l = [] + for k in range(2**(i)): + l += np.dot(H_, self.__state[k*2**(self.__n-i):(k+1)*2**(self.__n-i)]).tolist() + return sum([abs(x)**2 for x in l]) def observe(self) -> list[int]: diff --git a/docs/Gate.md b/docs/Gate.md deleted file mode 100644 index 1b6d363..0000000 --- a/docs/Gate.md +++ /dev/null @@ -1,172 +0,0 @@ -# Gates - -Here is the list of all the main gates in quantum algorithm available in the library. In the following formulas, `i` represents the complex number. - -## H(q: QuBit) -> None -`Hadamard's gate`. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -\frac {1} {\sqrt 2} & \frac {1} {\sqrt 2}\\ -\frac {1} {\sqrt 2} & -\frac {1} {\sqrt 2} -\end{pmatrix} -``` - -## X(q: QuBit) -> None -`Pauli-X gate` or `NOT gate`. Inplementes a rotation aroud the x-axis of $\pi$ radians. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -0 & 1\\ -1 & 0 -\end{pmatrix} -``` - -## Y(q: QuBit) -> None -`Pauli-Y gate`. Inplementes a rotation aroud the y-axis of $\pi$ radians. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -0 & -i\\ -i & 0 -\end{pmatrix} -``` - -## Z(q: QuBit) -> None -`Pauli-Z gate`. Inplementes a rotation aroud z-axis of $\pi$ radians. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -1 & 0\\ -0 & -1 -\end{pmatrix} -``` - -## S(q: QuBit) -> None -`NOT gate`. Invert the state |0> and |1> of the QuiBit q. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -1 & 0\\ -0 & i -\end{pmatrix} -``` - -## T(q: QuBit) -> None -`NOT gate`. Invert the state |0> and |1> of the QuiBit q. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -1 & 0\\ -0 & e^{i\frac\pi4} -\end{pmatrix} -``` - -## Rx(q: QuBit, phi: float) -> None -`NOT gate`. Inplementes a rotation aroud x-axis of $\phi$ radians. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -\cos(\frac\phi2) & -\sin(\frac\phi2)\\ -\sin(\frac\phi2) & \cos(\frac\phi2) -\end{pmatrix} -``` - -## Ry(q: QuBit, phi: float) -> None -`NOT gate`. Inplementes a rotation aroud y-axis of $\pi$ radians. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -e^{-i\frac\phi2} & 0\\ -0 & e^{i\frac\phi2} -\end{pmatrix} -``` - -## R1(q: QuBit, phi: float) -> None -`NOT gate`. Invert the state |0> and |1> of the QuiBit q. - -- q: the qubit manipulated by the gate. This function is in-place. - -- matrix: -```math -\begin{pmatrix} -1 & 0\\ -0 & e^{i\frac\phi2} -\end{pmatrix} -``` - -## CNOT(q: MuBit, n1: int, n2: int) -> None -`X-controlled gate`. Invert the state |0> and |1> of the QuiBit in n2 if the state of the QuBit in n1 is 1. - -- q: the qubit manipulated by the gate. This function is in-place. -- n1: the first QuBit to manipualte. -- n2: the second QuBit to manipulate - -- matrix: -```math -\begin{pmatrix} -1 & 0 & 0 & 0\\ -0 & 1 & 0 & 0\\ -0 & 0 & 0 & 1\\ -0 & 0 & 1 & 0 -\end{pmatrix} -``` - -## SWAP(q: MuBit, n1: int, n2: int) -> None -`NOT gate`. Invert the state |0> and |1> of the QuiBit q. - -- q: the qubit manipulated by the gate. This function is in-place. -- n1: the first QuBit to manipualte. -- n2: the second QuBit to manipulate - -- matrix: -```math -\begin{pmatrix} -1 & 0 & 0 & 0\\ -0 & 0 & 1 & 0\\ -0 & 1 & 0 & 0\\ -0 & 0 & 0 & 1 -\end{pmatrix} -``` - -## Cu(q: MuBit, u: list[list[float]], n1: int, n2: int) -> None -`controlled-u gate`. Applies the gate u to the QuBit in n2 if the Qubit in n1 is 1. - -- q: the qubit manipulated by the gate. This function is in-place. -- the list respresentation of a matrix of the size 2 by 2. -- n1: the first QuBit to manipualte. -- n2: the second QuBit to manipulate - -- matrix: -```math -\begin{pmatrix} -1 & 0 & 0 & 0\\ -0 & 1 & 0 & 0\\ -0 & 0 & u_{00} & u_{01}\\ -0 & 0 & u_{10} & u_{11} -\end{pmatrix} -``` diff --git a/docs/Matrix.md b/docs/Matrix.md deleted file mode 100644 index 03a1d5d..0000000 --- a/docs/Matrix.md +++ /dev/null @@ -1,196 +0,0 @@ -## Matrix - -Matrix(l: list[list[complexe]]) - -- l: representation of the matrix in a list - -### Attributes - -- ```__m: list[list[complexe]]``` - - the values contained in the matrix. - -- ```__size: tuple[int, int]``` - - the size of the matrix - -### Methode - -- ```__init__(l: list[list[complex]]=[]) -> None``` - - initiates the matrix - -- ```__mul__(__value: "Matrix") -> Matrix``` - - defines the natural multiplication as the Koeneger product for matrices - -- ```__apply(x: list[complex]) -> list[complex]``` - - takes a list to represent a vector in a colonne, and return the proctuct of the matrix by this vector - -- ```__str__() -> str``` - - returns a string representation of the matrix - - -### Staticmethods - -- ```I() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & 0\\ -0 & 1 -\end{pmatrix} -``` - -- ```H() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -\frac {1} {\sqrt 2} & \frac {1} {\sqrt 2}\\ -\frac {1} {\sqrt 2} & -\frac{1} {\sqrt 2} -\end{pmatrix} -``` - -- ```X() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -0 & 1\\ -1 & 0 -\end{pmatrix} -``` - -- ```Y() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -0 & -i\\ -i & 0 -\end{pmatrix} -``` - -- ```Z() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & 0\\ -0 & -1 -\end{pmatrix} -``` - -- ```S() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & 0\\ -0 & i -\end{pmatrix} -``` - -- ```T() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & 0\\ -0 & e^{i\frac {\pi} {4}} -\end{pmatrix} -``` - -- ```Rx(phi: float) -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -\cos(\frac \phi 2) & -i\sin(\frac \phi 2)\\ --i\sin(\frac \phi 2) & \cos(\frac \phi 2) -\end{pmatrix} -``` - -- ```Ry(phi: float) -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -\cos(\frac \phi 2) & -\sin(\frac \phi 2)\\ -\sin(\frac \phi 2) & \cos(\frac \phi 2) -\end{pmatrix} -``` - -- ```Rz(phi: float) -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -e^{-i\frac \phi 2} & 0\\ -0 & e^{i\frac \phi 2} -\end{pmatrix} -``` - -- ```R1(phi: float) -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & p\\ -0 & e^{i\phi} -\end{pmatrix} -``` - -- ```CNOT() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & 0 & 0 & 0\\ -0 & 1 & 0 & 0\\ -0 & 0 & 0 & 1\\ -0 & 0 & 1 & 0 -\end{pmatrix} -``` - -- ```SWAP() -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & 0 & 0 & 0\\ -0 & 0 & 1 & 0\\ -0 & 1 & 0 & 0\\ -0 & 0 & 0 & 0 -\end{pmatrix} -``` - -- ```Cu(u: Matrix) -> Matrix``` - - returns the matrix - -```math -\begin{pmatrix} -1 & 0 & 0 & 0\\ -0 & 1 & 0 & 0\\ -0 & 0 & u_{00} & u_{01}\\ -0 & 0 & u_{10} & u_{11} -\end{pmatrix} -``` diff --git a/docs/QuBit/IQuBit.md b/docs/QuBit/IQuBit.md deleted file mode 100644 index 41304db..0000000 --- a/docs/QuBit/IQuBit.md +++ /dev/null @@ -1,48 +0,0 @@ -## IQuBit - -IQuBit(n: int, mb: MuBit) - -IQuBit(QuBit) - -- n: the position of the QuBit in the list of intricated QuBits -- mb: the MuBit which organises the intricated QuBit - -### Attributes - -- ```__n: int``` - - the position of the QuBit in the list of intricated QuBits - -- ```__Mubit: MuBit``` - the MuBit which organises the intricated QuBit - -- ```__intricated: bool``` - - tells if the QuBit is intricated or not - - -### Methode - -- ```__init__(n: int, mb: MuBit) -> None``` - - initiates the qubit - -- ```is_intricated() -> bool``` - - returns the value of __intricated - -- ```get_MuBit() -> MuBit``` - - returns the MuBit in wich the QuBit is intricated. - -- ```__str__() -> str``` - - returns a string representation of the list of __state - -- ```__apply(m: Matrix) -> None``` - - takes a Matrix and modifies the QuBit according the matrix - -- ```observe() -> list[int]``` - - forces the QuBit in a state, where the probabilities are given throught __state. Returns the new __state obtained. diff --git a/docs/QuBit/MuBit.md b/docs/QuBit/MuBit.md deleted file mode 100644 index a22a68c..0000000 --- a/docs/QuBit/MuBit.md +++ /dev/null @@ -1,63 +0,0 @@ -## MuBit - -MuBit(n: int) - -- n: number of intricated QuBits - -### Attributes - -- ```__n: int``` - - the number of intricated QuBits represented by this MuBit - -- ```__state: list[complexe]``` - - list of the values for the state |0...0>, |0...01>, |0...010>, |0...011> etc... - -### Methode - -- ```__init__(n: int=2) -> None``` - - initiates the mubit with n QuBits. - -- ```get_size() -> int``` - - return __n - -- ```__str__() -> str``` - - returns a string representation of the list of __state. Each state is named and draw in a new line. - -- ```__set(i: int, value: int)``` - - force the i-th QuBit of the MuBit to take the state value. Value must be 0 or 1. - -- ```__iter__() -> Iterator[IQuBit]``` - - creates an interator of __state. - -- ```__getitem__(item: int) -> IQubIt``` - - creates an intricated QuBit corresponding to the item-th QuBit. - -- ```__apply(m: Matrix) -> None``` - - takes a Matrix and modifies the QuBits according the matrix. - -- ```__mapply(m: Matrix, i: int) -> None``` - - takes a Matrix and modifies the i-th QuBit according the matrix. - -- ```__getProbs(i: int) -> float``` - - returns the probability to get the i-th QuBit in the state |0>. - -- ```observe() -> list[int]``` - - forces the QuBits in a state, where the probabilities are given throught __state. Returns the list of the state of each QuBit. - -### Staticmethods - -- ```intricateThem(*args: QuBit) -> MuBit``` - - returns a new QuBit, independent of the args, but corresponding to the intrication of all given QuBits \ No newline at end of file diff --git a/docs/QuBit/QuBit.md b/docs/QuBit/QuBit.md deleted file mode 100644 index f972fae..0000000 --- a/docs/QuBit/QuBit.md +++ /dev/null @@ -1,42 +0,0 @@ -## QuBit - -QuBit(alpha: complexe, beta: complexe) - -- alpha: complexe value for |0> -- beta: complexe value for |1> - -### Attributes - -- ```__state: list[complexe]``` - - list of the values for the state |0> and |1> - -- ```__intricated: bool``` - - tells if the qubit is intricated or not - -### Methode - -- ```__init__(alpha: complexe=1, beta: complexe=0) -> None``` - - initiates the qubit - -- ```is_intricated() -> bool``` - - returns the value of __intricated - -- ```get_MuBit() -> None``` - - returns the MuBit in wich the QuBit is intricated. If not intricated, rerurns None - -- ```__str__() -> str``` - - returns a string representation of the list of __state - -- ```__apply(m: Matrix) -> None``` - - takes a Matrix and modifies the QuBit according the matrix - -- ```observe() -> list[int]``` - - forces the QuBit in a state, where the probabilities are given throught __state. Returns the state obtained. diff --git a/setup.py b/setup.py index 11e3437..2fbbb97 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name="QElephant", - version="1.0.4", + version="1.0.5", description="A small python module simulating a quantum computer", author="Didictateur", author_email="decosse.adrien@gmail.com",