[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-10-29 10:33:42 +00:00
parent a616dc90ab
commit 656c6b512e

View File

@ -6,10 +6,12 @@ Shor Algorithm is one of the basic quantum computing algorithm
that is used in breaking the RSA cryptography protocol, by finding the that is used in breaking the RSA cryptography protocol, by finding the
prime numbers that are used to create the public key value, n prime numbers that are used to create the public key value, n
In this implementation, I have used a very simple construct without In this implementation, I have used a very simple construct without
the use of qiskit or cirq to help understand how Shor algorithm's the use of qiskit or cirq to help understand how Shor algorithm's
idea actually works. idea actually works.
""" """
class Shor: class Shor:
def period_find(self, num: int, number: int) -> int: def period_find(self, num: int, number: int) -> int:
""" """
@ -21,12 +23,12 @@ class Shor:
>>> shor.period_find(3, 7) >>> shor.period_find(3, 7)
6 6
""" """
start:int = 1 start: int = 1
while pow(num, start, number) != 1: while pow(num, start, number) != 1:
start += 1 start += 1
return start return start
def shor_algorithm(self, number:int) -> list[int]: def shor_algorithm(self, number: int) -> list[int]:
""" """
Run Shor's algorithm to factor a number. Run Shor's algorithm to factor a number.
>>> shor = Shor() >>> shor = Shor()
@ -37,28 +39,24 @@ class Shor:
>>> factors >>> factors
(3, 5) (3, 5)
""" """
if number%2 == 0: if number % 2 == 0:
return 2, number//2 return 2, number // 2
while True: while True:
random.seed(0) random.seed(0)
num:int = random.randint(2, number-1) num: int = random.randint(2, number - 1)
gcd_number_num:int = math.gcd(number, num) gcd_number_num: int = math.gcd(number, num)
if gcd_number_num > 1: if gcd_number_num > 1:
return gcd_number_num, number//gcd_number_num return gcd_number_num, number // gcd_number_num
result:int = self.period_find(num, number) result: int = self.period_find(num, number)
if not result%2: if not result % 2:
start:int = pow(num, result//2, number) start: int = pow(num, result // 2, number)
if start != number-1: if start != number - 1:
p_value:int = math.gcd(start-1, number) p_value: int = math.gcd(start - 1, number)
q_value:int = math.gcd(start+1, number) q_value: int = math.gcd(start + 1, number)
if p_value > 1 and q_value > 1: if p_value > 1 and q_value > 1:
return p_value, q_value return p_value, q_value
shor = Shor() shor = Shor()
print(shor.shor_algorithm(15)) print(shor.shor_algorithm(15))