Loading...
Loading...
Factoring & Cryptography
Shor's algorithm factors integers in polynomial time, offering an exponential speedup over the best known classical algorithms. It poses a fundamental threat to RSA encryption and remains the most celebrated example of quantum computational advantage.
Given a large integer that is the product of two primes, finding those prime factors is believed to be intractable for classical computers. The best known classical algorithm, the General Number Field Sieve, has a sub-exponential complexity that grows rapidly with the number of digits.
This hardness assumption underpins modern public-key cryptography. RSA, Diffie-Hellman, and elliptic-curve cryptography all rely on the difficulty of factoring or related problems. Shor's algorithm breaks this assumption by solving factoring efficiently on a quantum computer.
Shor's algorithm reduces factoring to an order-finding (period-finding) problem. For a random integer coprime to , the function is periodic. If we can find the period , we can factor using classical number theory.
The quantum speedup comes from Quantum Phase Estimation (QPE) applied to a modular exponentiation circuit. QPE extracts the period from a superposition of states in polynomial time — something no classical algorithm can do efficiently.
Modular Exponentiation
Period Relation
QPE Unitary
Eigenvalue Structure
Factor Recovery
Shor's algorithm requires a fully fault-tolerant quantum computer with thousands of logical qubits and deep circuits. Current NISQ devices can only factor trivially small numbers (e.g., ), which are easily handled by classical computers.
Researchers are actively working on optimizing the algorithm for smaller qubit counts and developing post-quantum cryptographic standards (like lattice-based cryptography) to prepare for the post-Shor era.
Runnable implementations you can copy and experiment with.
A simplified QPE circuit that forms the core of Shor's period-finding subroutine. This example uses 3 counting qubits to estimate a phase.
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
import numpy as np
# QPE with 3 counting qubits for a toy unitary
n_count = 3
qc = QuantumCircuit(n_count + 1, n_count)
# Initialize eigenstate |1> on auxiliary qubit
qc.x(n_count)
# Create superposition on counting register
qc.h(range(n_count))
# Controlled-U operations (toy: rotation by pi/4)
angle = np.pi / 4
for i in range(n_count):
qc.cp(angle * (2 ** i), i, n_count)
# Inverse QFT on counting register
for j in range(n_count // 2):
qc.swap(j, n_count - j - 1)
for j in range(n_count):
for m in range(j):
qc.cp(-np.pi / (2 ** (j - m)), m, j)
qc.h(j)
# Measure
qc.measure(range(n_count), range(n_count))
simulator = AerSimulator()
job = simulator.run(qc, shots=2048)
print(job.result().get_counts())A PennyLane QPE circuit that estimates the phase of a toy unitary, forming the core of Shor's period-finding subroutine.
import pennylane as qml
import numpy as np
n_count = 3
dev = qml.device('default.qubit', wires=n_count + 1)
def c_u_power(k):
angle = np.pi / 4 * (2 ** k)
qml.ControlledPhaseShift(angle, wires=[k, n_count])
@qml.qnode(dev)
def qpe_circuit():
qml.PauliX(wires=n_count)
for i in range(n_count):
qml.Hadamard(wires=i)
for i in range(n_count):
c_u_power(i)
qml.adjoint(qml.QFT)(wires=range(n_count))
return qml.sample(wires=range(n_count))
samples = qpe_circuit()
print("QPE samples:", samples)The QFT step applied directly to the period-4 'comb' state left behind after measuring the second register for , , which gives order . Peaks appear at , each with probability .
from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import QFTGate
from qiskit_aer import AerSimulator
qc = QuantumCircuit(4, 4)
qc.h([2, 3]) # period-4 "comb" state: |0>, |4>, |8>, |12>
qc.append(QFTGate(4), [0, 1, 2, 3])
qc.measure(range(4), range(4))
simulator = AerSimulator()
job = simulator.run(transpile(qc, simulator), shots=4000)
counts = job.result().get_counts()
print(counts)
# Four peaks at y = 0, 4, 8, 12, each ~25%The same period-4 comb-state QFT example, in PennyLane.
import pennylane as qml
dev = qml.device('default.qubit', wires=4)
@qml.qnode(dev)
def shor_period4_qft():
# period-4 "comb" state: |0>, |4>, |8>, |12>
qml.Hadamard(wires=2)
qml.Hadamard(wires=3)
qml.QFT(wires=range(4))
return qml.probs(wires=range(4))
probs = shor_period4_qft()
print(probs)
# Four peaks at index 0, 4, 8, 12, each ~25%