From 7655bf3f65279c97bb4c178652f889b02593d571 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Tue, 29 Oct 2024 21:31:59 +1100 Subject: [PATCH 1/6] Added Shor Algorithm for RSA n factorization --- quantum/shor_algorithm.py | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 quantum/shor_algorithm.py diff --git a/quantum/shor_algorithm.py b/quantum/shor_algorithm.py new file mode 100644 index 000000000..96a5b3205 --- /dev/null +++ b/quantum/shor_algorithm.py @@ -0,0 +1,64 @@ +import math +import random + +""" +Shor Algorithm is one of the basic quantum computing algorithm +that is used in breaking the RSA cryptography protocol, by finding the +prime numbers that are used to create the public key value, n + +In this implementation, I have used a very simple construct without +the use of qiskit or cirq to help understand how Shor algorithm's +idea actually works. +""" +class Shor: + def period_find(self, num: int, number: int) -> int: + """ + Find the period of a^x mod N. + + >>> shor = Shor() + >>> shor.period_find(2, 15) + 4 + >>> shor.period_find(3, 7) + 6 + """ + start:int = 1 + while pow(num, start, number) != 1: + start += 1 + return start + + def shor_algorithm(self, number:int) -> list[int]: + """ + Run Shor's algorithm to factor a number. + >>> shor = Shor() + >>> random.seed(0) + >>> factors = shor.shor_algorithm(15) + >>> isinstance(factors, tuple) and len(factors) == 2 + True + >>> factors + (3, 5) + """ + if number%2 == 0: + return 2, number//2 + while True: + random.seed(0) + num:int = random.randint(2, number-1) + gcd_number_num:int = math.gcd(number, num) + if gcd_number_num > 1: + return gcd_number_num, number//gcd_number_num + + result:int = self.period_find(num, number) + if not result%2: + start:int = pow(num, result//2, number) + if start != number-1: + p_value:int = math.gcd(start-1, number) + q_value:int = math.gcd(start+1, number) + if p_value > 1 and q_value > 1: + return p_value, q_value + + +shor = Shor() +print(shor.shor_algorithm(15)) + + + + \ No newline at end of file From a616dc90abd286c688112686218a8e097f5ee1f6 Mon Sep 17 00:00:00 2001 From: joelkurien Date: Tue, 29 Oct 2024 10:32:19 +0000 Subject: [PATCH 2/6] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553..814cf568f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1192,6 +1192,7 @@ ## Quantum * [Q Fourier Transform](quantum/q_fourier_transform.py) + * [Shor Algorithm](quantum/shor_algorithm.py) ## Scheduling * [First Come First Served](scheduling/first_come_first_served.py) From 656c6b512ef5caca0e151107c1374cd5ba9aec15 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:33:42 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- quantum/shor_algorithm.py | 40 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/quantum/shor_algorithm.py b/quantum/shor_algorithm.py index 96a5b3205..9c43ce3b3 100644 --- a/quantum/shor_algorithm.py +++ b/quantum/shor_algorithm.py @@ -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 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 idea actually works. """ + + class Shor: def period_find(self, num: int, number: int) -> int: """ @@ -21,12 +23,12 @@ class Shor: >>> shor.period_find(3, 7) 6 """ - start:int = 1 + start: int = 1 while pow(num, start, number) != 1: start += 1 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. >>> shor = Shor() @@ -37,28 +39,24 @@ class Shor: >>> factors (3, 5) """ - if number%2 == 0: - return 2, number//2 + if number % 2 == 0: + return 2, number // 2 while True: random.seed(0) - num:int = random.randint(2, number-1) - gcd_number_num:int = math.gcd(number, num) + num: int = random.randint(2, number - 1) + gcd_number_num: int = math.gcd(number, num) if gcd_number_num > 1: - return gcd_number_num, number//gcd_number_num - - result:int = self.period_find(num, number) - if not result%2: - start:int = pow(num, result//2, number) - if start != number-1: - p_value:int = math.gcd(start-1, number) - q_value:int = math.gcd(start+1, number) + return gcd_number_num, number // gcd_number_num + + result: int = self.period_find(num, number) + if not result % 2: + start: int = pow(num, result // 2, number) + if start != number - 1: + p_value: int = math.gcd(start - 1, number) + q_value: int = math.gcd(start + 1, number) if p_value > 1 and q_value > 1: return p_value, q_value - + shor = Shor() print(shor.shor_algorithm(15)) - - - - \ No newline at end of file From 17ec57437ee4f221df99962709a236d0f9ed90c7 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Tue, 29 Oct 2024 21:35:43 +1100 Subject: [PATCH 4/6] Added referred website and handle type hints --- quantum/shor_algorithm.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/quantum/shor_algorithm.py b/quantum/shor_algorithm.py index 96a5b3205..1cc5a5714 100644 --- a/quantum/shor_algorithm.py +++ b/quantum/shor_algorithm.py @@ -9,6 +9,9 @@ prime numbers that are used to create the public key value, n In this implementation, I have used a very simple construct without the use of qiskit or cirq to help understand how Shor algorithm's idea actually works. + +Website referred for shor algorithm: +https://www.geeksforgeeks.org/shors-factorization-algorithm/ """ class Shor: def period_find(self, num: int, number: int) -> int: @@ -26,7 +29,7 @@ class Shor: start += 1 return start - def shor_algorithm(self, number:int) -> list[int]: + def shor_algorithm(self, number:int) -> tuple[int, int]: """ Run Shor's algorithm to factor a number. >>> shor = Shor() From 73ffa2485d61b1b248c8dc11d87e4b287c673fa7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:37:02 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- quantum/shor_algorithm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/shor_algorithm.py b/quantum/shor_algorithm.py index 28d6d2fd1..6058259fd 100644 --- a/quantum/shor_algorithm.py +++ b/quantum/shor_algorithm.py @@ -30,8 +30,8 @@ class Shor: while pow(num, start, number) != 1: start += 1 return start - - def shor_algorithm(self, number:int) -> tuple[int, int]: + + def shor_algorithm(self, number: int) -> tuple[int, int]: """ Run Shor's algorithm to factor a number. >>> shor = Shor() From 30d41fbd50234119edbbe3eeaaa3c04a14b394a0 Mon Sep 17 00:00:00 2001 From: Joelkurien Date: Tue, 29 Oct 2024 22:43:16 +1100 Subject: [PATCH 6/6] Developed the Logical implementation of Shor Algo #12318 --- quantum/shor_algorithm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/quantum/shor_algorithm.py b/quantum/shor_algorithm.py index 28d6d2fd1..f4085c578 100644 --- a/quantum/shor_algorithm.py +++ b/quantum/shor_algorithm.py @@ -12,6 +12,7 @@ idea actually works. Website referred for shor algorithm: https://www.geeksforgeeks.org/shors-factorization-algorithm/ + """