improved single gate

This commit is contained in:
Didictateur 2023-10-23 18:28:02 +02:00
parent 4dd7c77c20
commit 47caa54c0d
7 changed files with 11 additions and 538 deletions

View file

@ -78,15 +78,13 @@ class MuBit:
raise TypeError(f"can only manipulate MuBit with Matrix, not {type(matrix)}") raise TypeError(f"can only manipulate MuBit with Matrix, not {type(matrix)}")
i = i%self.__n 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): for k in range(2**(i)):
if j == i: nstate += np.dot(H_, self.__state[k*2**(self.__n-i):(k+1)*2**(self.__n-i)]).tolist()
M *= matrix
else:
M *= Matrix.I()
self.__state = M._Matrix__apply(self.__state) self.__state = nstate
def __mapply(self, matrix: Matrix, i: int) -> None: def __mapply(self, matrix: Matrix, i: int) -> None:
if type(i) is not int: if type(i) is not int:
@ -116,15 +114,11 @@ class MuBit:
raise IndexError("MuBit index out of range") raise IndexError("MuBit index out of range")
i = i%self.__n i = i%self.__n
M = Matrix([[1]]) H_ = np.kron([[1, 0], [0, 0]], np.identity(2**(self.__n-i-1)))
l = []
for j in range(self.__n): for k in range(2**(i)):
if j == i: l += np.dot(H_, self.__state[k*2**(self.__n-i):(k+1)*2**(self.__n-i)]).tolist()
M *= Matrix([[1, 0], [0, 0]])
else:
M *= Matrix.I()
l = M._Matrix__apply(self.__state)
return sum([abs(x)**2 for x in l]) return sum([abs(x)**2 for x in l])
def observe(self) -> list[int]: def observe(self) -> list[int]:

View file

@ -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}
```

View file

@ -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}
```

View file

@ -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.

View file

@ -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

View file

@ -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.

View file

@ -2,7 +2,7 @@ from setuptools import setup
setup( setup(
name="QElephant", name="QElephant",
version="1.0.4", version="1.0.5",
description="A small python module simulating a quantum computer", description="A small python module simulating a quantum computer",
author="Didictateur", author="Didictateur",
author_email="decosse.adrien@gmail.com", author_email="decosse.adrien@gmail.com",