improved controlled gate
This commit is contained in:
parent
371fd92c56
commit
be9252e1c0
2 changed files with 11 additions and 12 deletions
|
|
@ -108,16 +108,14 @@ 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]])
|
m = matrix._Matrix__m
|
||||||
|
a, b, c, d = m[2][2], m[2][3], m[3][2], m[3][3]
|
||||||
j = 0
|
j = 0
|
||||||
while j < self.__n:
|
while j < 2**self.__n:
|
||||||
if j == i:
|
x3, x4 = self.__state[j+2], self.__state[j+3]
|
||||||
M *= matrix
|
self.__state[j+2] = a*x3+b*x4
|
||||||
j += 2
|
self.__state[j+3] = c*x3+d*x4
|
||||||
else:
|
j += 4
|
||||||
M *= Matrix.I()
|
|
||||||
j += 1
|
|
||||||
self.__state = M._Matrix__apply(self.__state)
|
|
||||||
|
|
||||||
def __getProb(self, i: int) -> float:
|
def __getProb(self, i: int) -> float:
|
||||||
"""Probs for i to be zero"""
|
"""Probs for i to be zero"""
|
||||||
|
|
@ -171,7 +169,7 @@ class MuBit:
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
for i in range(self.__n):
|
for i in range(self.__n):
|
||||||
l.append(state%2)
|
l.insert(0, state%2)
|
||||||
state -= state%2
|
state -= state%2
|
||||||
state //= 2
|
state //= 2
|
||||||
return l
|
return l
|
||||||
|
|
@ -403,9 +401,9 @@ def SWAP(q: MuBit, n1: int, n2: int) -> None:
|
||||||
nmin = min(n1, n2)
|
nmin = min(n1, n2)
|
||||||
nmax = max(n1, n2)
|
nmax = max(n1, n2)
|
||||||
for i in range(nmin, nmax):
|
for i in range(nmin, nmax):
|
||||||
q._MuBit__mapply(Matrix.SWAP(), i)
|
q._MuBit__SWITCH(i)
|
||||||
for i in range(nmax-2, nmin-1, -1):
|
for i in range(nmax-2, nmin-1, -1):
|
||||||
q._MuBit__mapply(Matrix.SWAP(), i)
|
q._MuBit__SWITCH(i)
|
||||||
|
|
||||||
def Cu(q: MuBit, u: list[list[complex]], n1: int, n2: int) -> None:
|
def Cu(q: MuBit, u: list[list[complex]], n1: int, n2: int) -> None:
|
||||||
if type(q) is not MuBit:
|
if type(q) is not MuBit:
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ pip install QElephant
|
||||||
In order to work, QElephant is using the following libraries:
|
In order to work, QElephant is using the following libraries:
|
||||||
- `math`
|
- `math`
|
||||||
- `random`
|
- `random`
|
||||||
|
- `numpy`
|
||||||
|
|
||||||
They are automatically managed when installing QElephant.
|
They are automatically managed when installing QElephant.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue