Skip to content

About

A concise, hands-on Python + Jupyter workshop for the Ciencias Naturales y Tecnología (CNYT) course at Escuela Colombiana de Ingeniería. Implements Shor’s factorization pipeline (modular arithmetic, period-finding, GCD factor extraction), interactive matplotlib visualizations-ideal for learning the theory and practice behind quantum period-finding

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

🔐 Shor's Algorithm Workshop

Quantum Computing Python Jupyter License

A comprehensive implementation and exploration of Shor's groundbreaking quantum factorization algorithm

About • Features • Installation • Usage • Theory • Examples • Contributing


🌟 About

Shor's Algorithm, developed by Peter Shor in 1994, represents one of the most revolutionary breakthroughs in quantum computing. This algorithm demonstrates the exponential advantage quantum computers hold over classical computers for the integer factorization problem—a foundation of modern cryptography like RSA encryption.

This workshop provides:

  • 📚 Complete theoretical foundation of modular arithmetic and period-finding
  • 💻 Hands-on implementation in Python with detailed examples
  • 🎯 Solved exercises demonstrating the algorithm's power
  • 📊 Visual representations of modular exponentiation functions
  • 🔬 Step-by-step breakdown of the quantum circuit approach

✨ Features

  • ✅ Modular Arithmetic Fundamentals: Learn the mathematical foundations
  • ✅ Period Finding Algorithm: Efficient recursive implementation
  • ✅ Factor Extraction: Complete Euclidean GCD implementation
  • ✅ Interactive Visualizations: matplotlib-based plotting of periodic functions
  • ✅ Real-world Examples: Factor numbers like 15, 247, and 371
  • ✅ Comprehensive Exercises: With detailed solutions and explanations
  • ✅ Quantum Circuit Overview: Understanding the quantum advantage

🚀 Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • Jupyter Notebook/Lab

Quick Start

  1. Clone the repository

    git clone https://github.com/AnderssonProgramming/shor-algorithm-py.git
    cd shor-algorithm-py
  2. Install dependencies

    pip install -r Requirements.txt
  3. Launch Jupyter Notebook

    jupyter notebook Shors_Algorithm_Workshop.ipynb

📖 Usage

Running the Workshop

Open Shors_Algorithm_Workshop.ipynb in Jupyter and execute cells sequentially. The notebook is structured as follows:

  1. Introduction - Historical context and significance
  2. Modular Arithmetic - Basic operations and congruence
  3. Modular Exponentiation - Computing powers efficiently
  4. Period Finding - The heart of Shor's algorithm
  5. Factor Extraction - Converting periods to factors
  6. Quantum Algorithm - Understanding the quantum circuit
  7. Exercises - Hands-on problems with complete solutions

Code Examples

Finding the Period of a Modular Function

N = 15
a = 2

# Compute and visualize the function f(x) = a^x mod N
computeAndDrawPowersAModuloN(a, N)

Factoring a Number

N = 247
a = 2

# Find the period
period = findPeriod(a, N)

# Extract factors using GCD
factor1 = gcd((a ** (period // 2)) + 1, N)
factor2 = gcd((a ** (period // 2)) - 1, N)

print(f"Factors of {N}: {factor1} and {factor2}")

🧮 Theory

The Mathematical Foundation

Shor's algorithm exploits the periodic nature of modular exponentiation:

Given: An integer N to factor and a coprime integer a < N

Find: The period r such that a^r ≡ 1 (mod N)

Extract Factors:

  • If r is even and a^(r/2) ≢ -1 (mod N), then:
  • Factor₁ = GCD(a^(r/2) + 1, N)
  • Factor₂ = GCD(a^(r/2) - 1, N)

Why Quantum?

Classical algorithms require exponential time to find the period. Shor's quantum algorithm uses:

  1. Quantum Fourier Transform (QFT) - Finds periodicity in superposition
  2. Quantum parallelism - Evaluates function for multiple inputs simultaneously
  3. Interference - Amplifies periodic patterns

Time Complexity:

  • Classical: O(exp(∛(log N)))
  • Quantum: O((log N)² (log log N) (log log log N))

📊 Examples

Example 1: Factoring 15

N = 15, a = 2
Period r = 4
Factors: GCD(2² + 1, 15) = 5 and GCD(2² - 1, 15) = 3
Result: 15 = 3 × 5 ✓

Example 2: Factoring 247

N = 247, a = 2
Period r = 30
Factors: GCD(2¹⁵ + 1, 247) = 13 and GCD(2¹⁵ - 1, 247) = 19
Result: 247 = 13 × 19 ✓

Example 3: Factoring 371

N = 371, a = 24
Period r = 39
Factors: GCD(24^(39/2) + 1, 371) = 7 and GCD(24^(39/2) - 1, 371) = 53
Result: 371 = 7 × 53 ✓

🎯 Workshop Exercises

The notebook includes solved exercises:

Exercise 1: Proving Congruence Relations

  • Prove: 1977 ≡ 1 (mod 247)
  • Prove: 16183 ≡ 15442 (mod 247)

Solution provided with both direct computation and divisibility proof methods.

Exercise 2: Complete Factorization

  • Factor N = 247 using a = 2
  • Step-by-step implementation with visualization

Solution includes:

  • GCD verification
  • Period computation
  • Evenness check
  • Factor extraction
  • Visual plot of the periodic function

🔬 Key Concepts Covered

Concept Description
Modular Arithmetic Remainder operations and congruence relations
GCD Algorithm Euclidean method for finding greatest common divisors
Period Finding Detecting cycles in modular exponentiation
Coprimality Numbers with no common factors except 1
Quantum Superposition Evaluating multiple inputs simultaneously
Quantum Fourier Transform Extracting periodicity from quantum states

📚 Further Reading

  • Yanofsky & Mannucci - Quantum Computing for Computer Scientists
  • Nielsen & Chuang - Quantum Computation and Quantum Information
  • Kaye, Laflamme & Mosca - An Introduction to Quantum Computing
  • Qiskit Documentation - IBM's quantum computing framework

🛠️ Technologies Used

  • Python - Core implementation language
  • NumPy - Numerical computations
  • Matplotlib - Data visualization
  • Jupyter - Interactive computing environment

🤝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Ideas for Contributions

  • Implement quantum circuit simulation using Qiskit
  • Add more complex factorization examples
  • Optimize the period-finding algorithm
  • Create interactive visualizations with Plotly
  • Add unit tests for all functions

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.


👨‍💻 Author

AnderssonProgramming


🌟 Acknowledgments

  • Peter Shor for the revolutionary algorithm
  • The quantum computing community for continued research
  • Open-source contributors to Python scientific libraries
  • ESCUELA COLOMBIANA DE INGENIERIA JULIO GARAVITO - CNYT Course

⭐ If you found this workshop helpful, please consider giving it a star! ⭐

Made with ❤️ and quantum superposition

About

A concise, hands-on Python + Jupyter workshop for the Ciencias Naturales y Tecnología (CNYT) course at Escuela Colombiana de Ingeniería. Implements Shor’s factorization pipeline (modular arithmetic, period-finding, GCD factor extraction), interactive matplotlib visualizations-ideal for learning the theory and practice behind quantum period-finding

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages