Skip to content

Added classical implementation of Shor’s Algorithm #12545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 29, 2025
commit b0833eb96b223522993eafe98b6b0ec8f40b84c9
5 changes: 5 additions & 0 deletions algorithms/cryptography/shor_algorithm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import math

Check failure on line 1 in algorithms/cryptography/shor_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

algorithms/cryptography/shor_algorithm.py:1:1: INP001 File `algorithms/cryptography/shor_algorithm.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in algorithms/cryptography/shor_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

algorithms/cryptography/shor_algorithm.py:1:8: F401 `math` imported but unused
import random


def gcd(a, b):
"""Computes the greatest common divisor using Euclidean algorithm."""
while b:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be while b == 0:, not while b: to make sure b is zero.

a, b = b, a % b
return a


def modular_exponentiation(base, exp, mod):
"""Computes (base^exp) % mod using fast modular exponentiation."""
result = 1
Expand All @@ -17,7 +19,8 @@
exp //= 2
return result


def find_order(a, N):

Check failure on line 23 in algorithms/cryptography/shor_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

algorithms/cryptography/shor_algorithm.py:23:19: N803 Argument name `N` should be lowercase
"""Finds the smallest r such that a^r ≡ 1 (mod N)"""
r = 1
while modular_exponentiation(a, r, N) != 1:
Expand All @@ -26,8 +29,9 @@
return None
return r


def shor_algorithm(N):

Check failure on line 33 in algorithms/cryptography/shor_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

algorithms/cryptography/shor_algorithm.py:33:20: N803 Argument name `N` should be lowercase
"""Simulates Shor’s Algorithm classically to factorize N."""

Check failure on line 34 in algorithms/cryptography/shor_algorithm.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

algorithms/cryptography/shor_algorithm.py:34:22: RUF002 Docstring contains ambiguous `’` (RIGHT SINGLE QUOTATION MARK). Did you mean ``` (GRAVE ACCENT)?
if N % 2 == 0:
return 2, N // 2 # Trivial case if N is even

Expand All @@ -49,6 +53,7 @@
if 1 < factor2 < N:
return factor2, N // factor2


# Example usage
if __name__ == "__main__":
N = 15 # You can test with 21, 35, 55, etc.
Expand Down
Loading