Skip to content

Commit 0c45fa5

Browse files
joelkurienpre-commit-ci[bot]cclauss
authored
Logical Implementation of Shor Algorithm (#12317)
* Added Shor Algorithm for RSA n factorization * updating DIRECTORY.md * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added referred website and handle type hints * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Developed the Logical implementation of Shor Algo #12318 * Apply suggestion from @cclauss --------- Co-authored-by: joelkurien <joelkurien@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 1f27232 commit 0c45fa5

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,7 @@
14081408

14091409
## [Quantum](quantum)
14101410
* [Q Fourier Transform](quantum/q_fourier_transform.py)
1411+
* [Shor Algorithm](quantum/shor_algorithm.py)
14111412

14121413
## [Scheduling](scheduling)
14131414
* [Cpuschedulingalgorithms](scheduling/cpuschedulingalgorithms.py)

quantum/shor_algorithm.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import math
2+
import random
3+
4+
"""
5+
Shor Algorithm is one of the basic quantum computing algorithm
6+
that is used in breaking the RSA cryptography protocol, by finding the
7+
prime numbers that are used to create the public key value, n
8+
9+
In this implementation, I have used a very simple construct without
10+
the use of qiskit or cirq to help understand how Shor algorithm's
11+
idea actually works.
12+
13+
Website referred for shor algorithm:
14+
https://www.geeksforgeeks.org/shors-factorization-algorithm/
15+
16+
"""
17+
18+
19+
class Shor:
20+
def period_find(self, num: int, number: int) -> int:
21+
"""
22+
Find the period of a^x mod N.
23+
24+
>>> shor = Shor()
25+
>>> shor.period_find(2, 15)
26+
4
27+
>>> shor.period_find(3, 7)
28+
6
29+
"""
30+
start: int = 1
31+
while pow(num, start, number) != 1:
32+
start += 1
33+
return start
34+
35+
def shor_algorithm(self, number: int) -> tuple[int, int]:
36+
"""
37+
Run Shor's algorithm to factor a number.
38+
>>> shor = Shor()
39+
>>> random.seed(0)
40+
>>> factors = shor.shor_algorithm(15)
41+
>>> isinstance(factors, tuple) and len(factors) == 2
42+
True
43+
>>> factors
44+
(3, 5)
45+
"""
46+
if number % 2 == 0:
47+
return 2, number // 2
48+
while True:
49+
random.seed(0)
50+
num: int = random.randint(2, number - 1)
51+
gcd_number_num: int = math.gcd(number, num)
52+
if gcd_number_num > 1:
53+
return gcd_number_num, number // gcd_number_num
54+
55+
result: int = self.period_find(num, number)
56+
if not result % 2:
57+
start: int = pow(num, result // 2, number)
58+
if start != number - 1:
59+
p_value: int = math.gcd(start - 1, number)
60+
q_value: int = math.gcd(start + 1, number)
61+
if p_value > 1 and q_value > 1:
62+
return p_value, q_value
63+
64+
65+
if __name__ == "__main__":
66+
shor = Shor()
67+
print(shor.shor_algorithm(15))

0 commit comments

Comments
 (0)