fixed SWAP

This commit is contained in:
Didictateur 2023-10-17 00:09:43 +02:00
parent 201a023b6c
commit 421f43b9e1

View file

@ -27,8 +27,8 @@ class QuBit:
class MuBit: class MuBit:
def __init__(self, n: int) -> None: def __init__(self, n: int) -> None:
self.__n = n self.n = n
self.__state: list[complex] = [0]*(2**self.__n) self.__state: list[complex] = [0]*(2**self.n)
self.__state[0] = 1 self.__state[0] = 1
def __str__(self) -> str: def __str__(self) -> str:
@ -41,8 +41,8 @@ class MuBit:
return (next(N[:-1]))+"0" return (next(N[:-1]))+"0"
txt = "" txt = ""
N = "0"*self.__n N = "0"*self.n
for i in range(2**self.__n): for i in range(2**self.n):
txt += f"{round(self.__state[i], 3)} |{N}>\n" txt += f"{round(self.__state[i], 3)} |{N}>\n"
N = next(N) N = next(N)
return txt return txt
@ -52,7 +52,7 @@ class MuBit:
assert(value in {0, 1}) assert(value in {0, 1})
M = Matrix([[1]]) M = Matrix([[1]])
for j in range(self.__n): for j in range(self.n):
if j == i: if j == i:
M *= Matrix([[1-value, 0], [0, value]]) M *= Matrix([[1-value, 0], [0, value]])
else: else:
@ -62,7 +62,7 @@ class MuBit:
self.__state = [x/norm for x in self.__state] self.__state = [x/norm for x in self.__state]
def __iter__(self): def __iter__(self):
return iter([IQuBit(i, self) for i in range(self.__n)]) return iter([IQuBit(i, self) for i in range(self.n)])
def __getitem__(self, item: int) -> "IQuBit": def __getitem__(self, item: int) -> "IQuBit":
return IQuBit(item, self) return IQuBit(item, self)
@ -70,7 +70,7 @@ class MuBit:
def __apply(self, i: int, matrix: Matrix) -> None: def __apply(self, i: int, matrix: Matrix) -> None:
M = Matrix([[1]]) M = Matrix([[1]])
for j in range(self.__n): for j in range(self.n):
if j == i: if j == i:
M *= matrix M *= matrix
else: else:
@ -78,14 +78,23 @@ class MuBit:
self.__state = M._Matrix__apply(self.__state) self.__state = M._Matrix__apply(self.__state)
def __mapply(self, matrix: Matrix) -> None: def __mapply(self, matrix: Matrix, i: int) -> None:
self.__state = matrix.__MuBit__apply(self.__state) M = Matrix([[1]])
j = 0
while j < self.n:
if j == i:
M *= matrix
j += 2
else:
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"""
M = Matrix([[1]]) M = Matrix([[1]])
for j in range(self.__n): for j in range(self.n):
if j == i: if j == i:
M *= Matrix([[1, 0], [0, 0]]) M *= Matrix([[1, 0], [0, 0]])
else: else:
@ -96,24 +105,24 @@ class MuBit:
def observe(self) -> list[0]: def observe(self) -> list[0]:
l = [] l = []
for i in range(self.__n): for i in range(self.n):
l.append(IQuBit(i, self).observe()) l.append(IQuBit(i, self).observe())
return l return l
class IQuBit: class IQuBit:
def __init__(self, n: int, mb: MuBit) -> None: def __init__(self, n: int, mb: MuBit) -> None:
self.__n = n self.n = n
self.__muBit = mb self.__muBit = mb
def __apply(self, matrix: Matrix) -> None: def __apply(self, matrix: Matrix) -> None:
self.__muBit._MuBit__apply(self.__n, matrix) self.__muBit._MuBit__apply(self.n, matrix)
def observe(self) -> int: def observe(self) -> int:
r = rd.random() r = rd.random()
if r < self.__muBit._MuBit__getProb(self.__n): if r < self.__muBit._MuBit__getProb(self.n):
self.__muBit._MuBit__set(self.__n, 0) self.__muBit._MuBit__set(self.n, 0)
return 0 return 0
self.__muBit._MuBit__set(self.__n, 1) self.__muBit._MuBit__set(self.n, 1)
return 1 return 1
def H(q: (QuBit | IQuBit)) -> None: def H(q: (QuBit | IQuBit)) -> None:
@ -179,8 +188,13 @@ def R1(q: (QuBit | IQuBit), phi: float) -> None:
def CNOT(q: MuBit) -> None: def CNOT(q: MuBit) -> None:
q._MuBit__mapply(Matrix.CNOT()) q._MuBit__mapply(Matrix.CNOT())
def SWAP(q: MuBit) -> None: def SWAP(q: MuBit, n1: int, n2: int) -> None:
q._MuBit__mapply(Matrix.SWAP()) nmin = min(n1, n2)
nmax = max(n1, n2)
for i in range(nmin, nmax):
q._MuBit__mapply(Matrix.SWAP(), i)
for i in range(nmax-2, nmin-1, -1):
q._MuBit__mapply(Matrix.SWAP(), i)
def Cu(q: MuBit, u: list[list[complex]]) -> None: def Cu(q: MuBit, u: list[list[complex]]) -> None:
q._MuBit__mapply(Matrix.Cu(u)) q._MuBit__mapply(Matrix.Cu(u))
@ -194,7 +208,11 @@ def test(q: QuBit) -> None:
print(varName) print(varName)
if __name__=="__main__": if __name__=="__main__":
q1 = QuBit() q = MuBit(3)
q2 = QuBit() H(q[0])
print(q)
test(q1) SWAP(q, 0, 1)
SWAP(q, 1, 2)
print(q)