added more gates
This commit is contained in:
parent
9fd8e8c231
commit
a3cb9de05b
4 changed files with 163 additions and 6 deletions
|
|
@ -64,6 +64,13 @@ class Matrix:
|
||||||
[0, 1],
|
[0, 1],
|
||||||
[1, 0]
|
[1, 0]
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def SQRTX() -> "Matrix":
|
||||||
|
return Matrix([
|
||||||
|
[(1+I)/2, (1-I)/2],
|
||||||
|
[(1-I)/2, (1+I)/2]
|
||||||
|
])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def Y() -> "Matrix":
|
def Y() -> "Matrix":
|
||||||
|
|
@ -141,7 +148,7 @@ class Matrix:
|
||||||
])
|
])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def CNOT() -> "Matrix":
|
def CX() -> "Matrix":
|
||||||
return Matrix([
|
return Matrix([
|
||||||
[1, 0, 0, 0],
|
[1, 0, 0, 0],
|
||||||
[0, 1, 0, 0],
|
[0, 1, 0, 0],
|
||||||
|
|
@ -149,6 +156,24 @@ class Matrix:
|
||||||
[0, 0, 1, 0]
|
[0, 0, 1, 0]
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def CY() -> "Matrix":
|
||||||
|
return Matrix([
|
||||||
|
[1, 0, 0, 0],
|
||||||
|
[0, 1, 0, 0],
|
||||||
|
[0, 0, 0, -I],
|
||||||
|
[0, 0, I, 0]
|
||||||
|
])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def CZ() -> "Matrix":
|
||||||
|
return Matrix([
|
||||||
|
[1, 0, 0, 0],
|
||||||
|
[0, 1, 0, 0],
|
||||||
|
[0, 0, 1, 0],
|
||||||
|
[0, 0, 0, -1]
|
||||||
|
])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def SWAP() -> "Matrix":
|
def SWAP() -> "Matrix":
|
||||||
return Matrix([
|
return Matrix([
|
||||||
|
|
|
||||||
|
|
@ -256,7 +256,7 @@ class IQuBit(QuBit):
|
||||||
if type(matrix) is not Matrix:
|
if type(matrix) is not Matrix:
|
||||||
raise TypeError(f"can only manipulate QuBit with Matrix, not {type(matrix)}")
|
raise TypeError(f"can only manipulate QuBit with Matrix, not {type(matrix)}")
|
||||||
|
|
||||||
self.__muBit._MuBit__apply(self.__n, matrix)
|
self.__muBit._MuBit__apply(matrix, self.__n)
|
||||||
|
|
||||||
def observe(self) -> int:
|
def observe(self) -> int:
|
||||||
r = rd.random()
|
r = rd.random()
|
||||||
|
|
@ -288,6 +288,14 @@ def X(q: QuBit) -> None:
|
||||||
else:
|
else:
|
||||||
raise TypeError(f"a QuBit was expected, but a {type(q)} was given")
|
raise TypeError(f"a QuBit was expected, but a {type(q)} was given")
|
||||||
|
|
||||||
|
def SQRTX(q: QuBit) -> None:
|
||||||
|
if type(q) == IQuBit:
|
||||||
|
q._IQuBit__apply(Matrix.SQRTX())
|
||||||
|
elif type(q) == QuBit:
|
||||||
|
q._QuBit__apply(Matrix.SQRTX())
|
||||||
|
else:
|
||||||
|
raise TypeError(f"a QuBit was expected, but a {type(q)} was given")
|
||||||
|
|
||||||
def Y(q: QuBit) -> None:
|
def Y(q: QuBit) -> None:
|
||||||
if type(q) == IQuBit:
|
if type(q) == IQuBit:
|
||||||
q._IQuBit__apply(Matrix.Y())
|
q._IQuBit__apply(Matrix.Y())
|
||||||
|
|
@ -364,7 +372,7 @@ def R1(q: QuBit, phi: float) -> None:
|
||||||
else:
|
else:
|
||||||
raise TypeError(f"a QuBit was expected, but a {type(q)} was given")
|
raise TypeError(f"a QuBit was expected, but a {type(q)} was given")
|
||||||
|
|
||||||
def CNOT(q: MuBit, n1: int, n2: int) -> None:
|
def CX(q: MuBit, n1: int, n2: int) -> None:
|
||||||
if type(q) is not MuBit:
|
if type(q) is not MuBit:
|
||||||
TypeError(f"a MuBit was expected, but a {type(q)} was given")
|
TypeError(f"a MuBit was expected, but a {type(q)} was given")
|
||||||
if type(n1) is not int:
|
if type(n1) is not int:
|
||||||
|
|
@ -380,7 +388,47 @@ def CNOT(q: MuBit, n1: int, n2: int) -> None:
|
||||||
|
|
||||||
SWAP(q, 0, n1)
|
SWAP(q, 0, n1)
|
||||||
SWAP(q, 1, n2)
|
SWAP(q, 1, n2)
|
||||||
q._MuBit__mapply(Matrix.CNOT(), 0)
|
q._MuBit__mapply(Matrix.CX(), 0)
|
||||||
|
SWAP(q, 0, n1)
|
||||||
|
SWAP(q, 1, n2)
|
||||||
|
|
||||||
|
def CY(q: MuBit, n1: int, n2: int) -> None:
|
||||||
|
if type(q) is not MuBit:
|
||||||
|
TypeError(f"a MuBit was expected, but a {type(q)} was given")
|
||||||
|
if type(n1) is not int:
|
||||||
|
TypeError(f"MuBit indices must be intergers, not {type(n1)}")
|
||||||
|
if type(n2) is not int:
|
||||||
|
TypeError(f"MuBit indices must be intergers, not {type(n2)}")
|
||||||
|
if n1 > q._MuBit__n or n2 > q._MuBit__n:
|
||||||
|
ValueError("Mubit index out of range")
|
||||||
|
n1 = n1%q._MuBit__n
|
||||||
|
n2 = n2%q._MuBit__n
|
||||||
|
if n1==n2:
|
||||||
|
ValueError("the CNOT gate must be applied on two differents QuBits")
|
||||||
|
|
||||||
|
SWAP(q, 0, n1)
|
||||||
|
SWAP(q, 1, n2)
|
||||||
|
q._MuBit__mapply(Matrix.CY(), 0)
|
||||||
|
SWAP(q, 0, n1)
|
||||||
|
SWAP(q, 1, n2)
|
||||||
|
|
||||||
|
def CZ(q: MuBit, n1: int, n2: int) -> None:
|
||||||
|
if type(q) is not MuBit:
|
||||||
|
TypeError(f"a MuBit was expected, but a {type(q)} was given")
|
||||||
|
if type(n1) is not int:
|
||||||
|
TypeError(f"MuBit indices must be intergers, not {type(n1)}")
|
||||||
|
if type(n2) is not int:
|
||||||
|
TypeError(f"MuBit indices must be intergers, not {type(n2)}")
|
||||||
|
if n1 > q._MuBit__n or n2 > q._MuBit__n:
|
||||||
|
ValueError("Mubit index out of range")
|
||||||
|
n1 = n1%q._MuBit__n
|
||||||
|
n2 = n2%q._MuBit__n
|
||||||
|
if n1==n2:
|
||||||
|
ValueError("the CNOT gate must be applied on two differents QuBits")
|
||||||
|
|
||||||
|
SWAP(q, 0, n1)
|
||||||
|
SWAP(q, 1, n2)
|
||||||
|
q._MuBit__mapply(Matrix.CZ(), 0)
|
||||||
SWAP(q, 0, n1)
|
SWAP(q, 0, n1)
|
||||||
SWAP(q, 1, n2)
|
SWAP(q, 1, n2)
|
||||||
|
|
||||||
|
|
|
||||||
49
docs/Gate.md
49
docs/Gate.md
|
|
@ -28,6 +28,19 @@ Here is the list of all the main gates in quantum algorithm available in the lib
|
||||||
\end{pmatrix}
|
\end{pmatrix}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## SQRTX(q: QuBit) -> None
|
||||||
|
`Square-Root of X`. Inplementes the square root of the X-gate.
|
||||||
|
|
||||||
|
- q: the qubit manipulated by the gate. This function is in-place.
|
||||||
|
|
||||||
|
- matrix:
|
||||||
|
```math
|
||||||
|
\begin{pmatrix}
|
||||||
|
\frac {1+i} {2} & \frac {1-i} {2}\\
|
||||||
|
\frac {1-i} {2} & \frac {1+i} {2}
|
||||||
|
\end{pmatrix}
|
||||||
|
```
|
||||||
|
|
||||||
## Y(q: QuBit) -> None
|
## Y(q: QuBit) -> None
|
||||||
`Pauli-Y gate`. Inplementes a rotation aroud the y-axis of $\pi$ radians.
|
`Pauli-Y gate`. Inplementes a rotation aroud the y-axis of $\pi$ radians.
|
||||||
|
|
||||||
|
|
@ -119,7 +132,7 @@ e^{-i\frac\phi2} & 0\\
|
||||||
\end{pmatrix}
|
\end{pmatrix}
|
||||||
```
|
```
|
||||||
|
|
||||||
## CNOT(q: MuBit, n1: int, n2: int) -> None
|
## CX(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.
|
`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.
|
- q: the qubit manipulated by the gate. This function is in-place.
|
||||||
|
|
@ -136,6 +149,40 @@ e^{-i\frac\phi2} & 0\\
|
||||||
\end{pmatrix}
|
\end{pmatrix}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## CY(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 & -i\\
|
||||||
|
0 & 0 & i & 0
|
||||||
|
\end{pmatrix}
|
||||||
|
```
|
||||||
|
|
||||||
|
## CZ(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 & 1 & 0\\
|
||||||
|
0 & 0 & 0 & -1
|
||||||
|
\end{pmatrix}
|
||||||
|
```
|
||||||
|
|
||||||
## SWAP(q: MuBit, n1: int, n2: int) -> None
|
## SWAP(q: MuBit, n1: int, n2: int) -> None
|
||||||
`NOT gate`. Invert the state |0> and |1> of the QuiBit q.
|
`NOT gate`. Invert the state |0> and |1> of the QuiBit q.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,17 @@ Matrix(l: list[list[complexe]])
|
||||||
\end{pmatrix}
|
\end{pmatrix}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- ```SQRTX() -> Matrix```
|
||||||
|
|
||||||
|
returns the matrix
|
||||||
|
|
||||||
|
```math
|
||||||
|
\begin{pmatrix}
|
||||||
|
\frac {1+i} {2} & \frac {1-i} {2}\\
|
||||||
|
\frac {1-i} {2} & \frac {1+i} {2}
|
||||||
|
\end{pmatrix}
|
||||||
|
```
|
||||||
|
|
||||||
- ```Y() -> Matrix```
|
- ```Y() -> Matrix```
|
||||||
|
|
||||||
returns the matrix
|
returns the matrix
|
||||||
|
|
@ -156,7 +167,7 @@ e^{-i\frac \phi 2} & 0\\
|
||||||
\end{pmatrix}
|
\end{pmatrix}
|
||||||
```
|
```
|
||||||
|
|
||||||
- ```CNOT() -> Matrix```
|
- ```CX() -> Matrix```
|
||||||
|
|
||||||
returns the matrix
|
returns the matrix
|
||||||
|
|
||||||
|
|
@ -169,6 +180,32 @@ e^{-i\frac \phi 2} & 0\\
|
||||||
\end{pmatrix}
|
\end{pmatrix}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- ```CY() -> Matrix```
|
||||||
|
|
||||||
|
returns the matrix
|
||||||
|
|
||||||
|
```math
|
||||||
|
\begin{pmatrix}
|
||||||
|
1 & 0 & 0 & 0\\
|
||||||
|
0 & 1 & 0 & 0\\
|
||||||
|
0 & 0 & 0 & -i\\
|
||||||
|
0 & 0 & i & 0
|
||||||
|
\end{pmatrix}
|
||||||
|
```
|
||||||
|
|
||||||
|
- ```CZ() -> Matrix```
|
||||||
|
|
||||||
|
returns the matrix
|
||||||
|
|
||||||
|
```math
|
||||||
|
\begin{pmatrix}
|
||||||
|
1 & 0 & 0 & 0\\
|
||||||
|
0 & 1 & 0 & 0\\
|
||||||
|
0 & 0 & 1 & 0\\
|
||||||
|
0 & 0 & 0 & -1
|
||||||
|
\end{pmatrix}
|
||||||
|
```
|
||||||
|
|
||||||
- ```SWAP() -> Matrix```
|
- ```SWAP() -> Matrix```
|
||||||
|
|
||||||
returns the matrix
|
returns the matrix
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue