Skip to content

Commit faddb0c

Browse files
authored
Vlad Shor + Grover (#81)
* Shor's algorithm added * groovers algo init * shor, added link * Grover fixed formulas * added links for interaction
1 parent bc6356e commit faddb0c

File tree

5 files changed

+270
-4
lines changed

5 files changed

+270
-4
lines changed

content/Basic Cryptography/Basic Cryptography.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- [[ElGamal]]
1313
- [[ECDSA]]
1414
- [[Post-Quantum Cryptography]]
15+
- [[Shor's Algorithm]]
16+
- [[Grover’s Algorithm]]
1517
- [[Learning with Errors(LWE)]]
1618
- [[Ring Learning with Errors(RLWE)]]
1719
- [[Module Learning with Errors(MLWE)]]
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Grover’s Algorithm
2+
3+
Grover’s Algorithm is a **quantum** algorithm discovered by Lov Grover in 1996. It provides a *quadratic speed-up* over classical methods for **unstructured search** problems. While this may not sound as dramatic as Shor’s *exponential* advantage in factoring (see [[Shor's Algorithm]]), Grover’s Algorithm is extremely important in scenarios where one must check each item in a large, unsorted list or database.
4+
5+
---
6+
7+
## 1. The Problem It Solves
8+
9+
In the **unstructured search** setting, you have:
10+
- A “black box” or “oracle” function $ f(x) $ that returns:
11+
$$
12+
f(x) = \begin{cases}
13+
1 & \text{if } x = x_\text{target}, \\
14+
0 & \text{otherwise}.
15+
\end{cases}
16+
$$
17+
- A database of size $ N $ (which is unstructured or unsorted).
18+
19+
**Classically**, finding $$ x_{\text{target}} $$ by querying the oracle could take $ O(N) $ tries (in the worst case). **Grover’s Algorithm**, running on a quantum computer, needs on the order of $ O(\sqrt{N}) $ queries—a quadratic improvement.
20+
21+
---
22+
23+
## 2. High-Level Overview
24+
25+
1. **State Initialization**
26+
- You start with a register of $\log_2(N)$ qubits.
27+
- Apply the Hadamard transform to create a uniform superposition over all possible database indices.
28+
29+
2. **Oracle Query (Phase Flip)**
30+
- The algorithm has access to an *oracle* (a quantum circuit) that flips the phase of the state
31+
$$
32+
\lvert x_{\text{target}} \rangle.
33+
$$
34+
- If the register is in the state $\lvert x_{\text{target}} \rangle$, the oracle applies a phase of $-1$ to that amplitude.
35+
36+
3. **Grover Diffusion (Amplitude Amplification)**
37+
- Another subroutine (often called the “Grover diffusion” or “inversion about the mean”) increases the amplitude of the “target” state while decreasing the amplitude of other states.
38+
39+
4. **Repetition**
40+
- The **phase flip** + **diffusion** sequence is repeated approximately $\frac{\pi}{4}\sqrt{N}$ times.
41+
- This concentrates the probability amplitude onto the target state.
42+
43+
5. **Measurement**
44+
- Finally, measuring the qubits collapses the state, yielding $ x_{\text{target}} $ with high probability.
45+
46+
---
47+
48+
## 3. Complexity and Performance
49+
50+
- **Classical**: Searching an unsorted list of $N $ items requires on average $O(N) $ checks.
51+
- **Quantum (Grover’s Algorithm)**: Finds the target in about **$ O(\sqrt{N}) $** oracle calls.
52+
53+
This quadratic speed-up, while not exponential, is still significant for large databases or multiple search passes.
54+
55+
### Example
56+
57+
- If $ N = 1{,}000{,}000 $ (a million items), **classical** approaches might need ~1,000,000 queries in the worst case.
58+
- **Grover’s** approach would need on the order of ~1,000 queries—much faster, though still not polynomial vs. exponential.
59+
60+
---
61+
62+
## 4. Use Cases
63+
64+
1. **Database or Pattern Search**
65+
- Any time you have an unstructured search problem (like searching through a cryptographic keyspace), Grover’s Algorithm offers a generic speed-up.
66+
67+
2. **Cryptanalysis (Key Search)**
68+
- Brute-forcing an $n$-bit key classically takes $2^n $ steps.
69+
- With **Grover’s**, you need on the order of $2^{n/2} $ queries. This effectively *doubles* the key length required to maintain the same security against a quantum adversary.
70+
71+
3. **Quantum Subroutine for Other Algorithms**
72+
- Grover’s “amplitude amplification” concept can be adapted in more complex quantum algorithms to boost the probability of finding a desired solution.
73+
74+
---
75+
76+
## 5. Oracle Construction
77+
78+
Unlike [[Shor’s Algorithm]]—where the arithmetic is explicit—Grover’s Algorithm requires designing a **quantum oracle**:
79+
- The oracle identifies the target item $x_{\text{target}} $ by flipping its amplitude’s phase.
80+
- In practical applications, building such an oracle might itself be non-trivial.
81+
82+
---
83+
84+
## 6. Practical Considerations
85+
86+
1. **Noise and Decoherence**
87+
- Like all quantum algorithms, Grover’s Algorithm requires *[coherence](https://en.wikipedia.org/wiki/Coherence_(physics))* over a sequence of operations.
88+
- In current “noisy intermediate-scale quantum” (NISQ) devices, the depth (number of gates in the circuit) must remain low to keep errors manageable.
89+
90+
2. **Exact vs. Approximate Counting**
91+
- If the oracle flags **multiple** solutions, variants of Grover’s Algorithm can estimate the number of solutions or find one of them at random.
92+
93+
3. **Scaling Up**
94+
- Realistically, implementing Grover’s Algorithm at large $N $ on near-term hardware is challenging due to qubit quality and error rates.
95+
- Nevertheless, small proofs of concept (like searching lists of size up to a few thousand) are feasible in emerging quantum systems.
96+
97+
---
98+
99+
## 7. Example Code Snippet (Pseudocode, Qiskit-Style)
100+
101+
Below is a simplified look at how **Grover’s** might be implemented with [Qiskit](https://qiskit.org/).
102+
*Note: This is just conceptual; actual code would be more detailed.*
103+
More info [here](https://github.com/Qiskit/textbook/blob/main/notebooks/ch-algorithms/grover.ipynb).
104+
105+
```python
106+
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, Aer, execute
107+
import numpy as np
108+
109+
def grover_circuit(num_qubits, oracle_circuit):
110+
# num_qubits: log2(N)
111+
# oracle_circuit: implements the phase flip for the target state
112+
113+
# Initialize registers
114+
qc = QuantumCircuit(num_qubits, num_qubits)
115+
116+
# 1. Create uniform superposition
117+
qc.h(range(num_qubits))
118+
119+
# 2. Apply the oracle + diffuser multiple times
120+
iterations = int(np.floor((np.pi/4)*np.sqrt(2**num_qubits)))
121+
122+
for _ in range(iterations):
123+
# Oracle phase flip
124+
qc.compose(oracle_circuit, inplace=True)
125+
# Diffusion
126+
qc.h(range(num_qubits))
127+
qc.x(range(num_qubits))
128+
qc.h(num_qubits - 1)
129+
qc.mct(list(range(num_qubits-1)), num_qubits - 1) # multi-controlled Toffoli
130+
qc.h(num_qubits - 1)
131+
qc.x(range(num_qubits))
132+
qc.h(range(num_qubits))
133+
134+
# 3. Measure
135+
qc.measure(range(num_qubits), range(num_qubits))
136+
137+
return qc
138+
```
139+
Example usage (very simplified):
140+
Suppose our 'oracle_circuit' flips the phase of a known target state |101>.
141+
142+
In a real scenario, you build the oracle_circuit so that it knows which state(s) to flip. Then you run grover_circuit and measure.

content/Basic Cryptography/Post-Quantum Cryptography.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ The public key cryptosystems currently in widespread use, such as [[RSA]], **Ell
1515
- **Integer Factorization**: The basis for [[RSA]] encryption.
1616
- **Discrete Logarithm Problem**: Used in [[Diffie-Hellman]] and ECC.
1717

18-
Quantum computers threaten these systems because of algorithms like **Shor's algorithm**, which can solve these problems in polynomial time, effectively breaking the security of these cryptosystems.
18+
Quantum computers threaten these systems because of algorithms like **[[Shor's algorithm]]**, which can solve these problems in polynomial time, effectively breaking the security of these cryptosystems.
1919

20-
#### Shor's Algorithm
20+
#### [[Shor's Algorithm]]
2121

2222
- **Developed by**: Peter Shor in 1994.
2323
- **Capability**: Efficiently factors large integers and computes discrete logarithms.
2424
- **Impact**: Can break [[RSA]], ECC, and other cryptosystems relying on these hard problems.
2525

26-
#### Grover's Algorithm
26+
#### [[Grover's Algorithm]]
2727

2828
- **Developed by**: Lov Grover in 1996.
2929
- **Capability**: Provides a quadratic speedup for unstructured search problems.
@@ -89,7 +89,7 @@ Post-quantum cryptographic algorithms are based on mathematical problems believe
8989

9090
6. **Symmetric-Key Quantum Resistance**
9191

92-
- **Approach**: Doubling key sizes in symmetric algorithms to counteract Grover's algorithm.
92+
- **Approach**: Doubling key sizes in symmetric algorithms to counteract [[Grover's algorithm]].
9393
- **Examples**: Using AES-256 instead of AES-128.
9494

9595
## NIST Post-Quantum Cryptography Standardization Program
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Shor's Algorithm
2+
3+
## Definition
4+
5+
**Shor's Algorithm** is a *quantum* algorithm discovered by Peter Shor in 1994, designed to factor large integers and solve discrete logarithms in **polynomial time** on a quantum computer. This capability poses a significant threat to **classical cryptography** schemes, such as [[RSA]] and [[Diffie-Hellman]], which rely on the infeasibility of factoring large numbers or computing discrete logarithms with classical computers.
6+
7+
Shor’s Algorithm exploits the power of **quantum parallelism** and the **Quantum Fourier Transform (QFT)** to find the period of a function related to the integer in question. By determining this period, it becomes possible to derive non-trivial factors of large numbers—or solve discrete logs—exponentially faster compared to any known classical algorithm.
8+
9+
## Use Cases
10+
11+
Despite often being described in the context of *breaking* cryptography, Shor's Algorithm has broader implications:
12+
13+
- **Cryptanalysis**: It is primarily famous for breaking [[RSA]], [[Elliptic Curves]] **Cryptography (ECC)**, and other discrete-log-based systems if (and when) large-scale quantum computers become practical.
14+
- [[Post-Quantum Cryptography]] **(PQC) Research**: Shor’s Algorithm directly motivates the field of **post-quantum** or **quantum-resistant** cryptography, prompting the design of new cryptographic schemes believed to be secure against quantum attacks.
15+
- **Scientific and Academic**: In quantum computing courses and research, Shor’s Algorithm is often used as a central illustration of how quantum algorithms can vastly outperform classical algorithms for certain problems.
16+
17+
## Advantages
18+
19+
1. **Exponential Speed-Up**
20+
- Classically, factoring an $n$-bit integer grows super-polynomially. Shor's Algorithm can factor an $n$-bit integer in roughly $O(n^3) $ steps (or better with optimizations), showcasing a significant theoretical advantage.
21+
22+
2. **Demonstration of Quantum Superiority**
23+
- Alongside [[Grover’s Algorithm]] for unstructured search, Shor’s Algorithm remains a cornerstone example of how quantum computers can outperform classical systems for specific tasks.
24+
25+
3. **Driving Innovation in Cryptography**
26+
- The cryptographic world now intensively researches **lattice-based**, **code-based**, or other **post-quantum** cryptosystems to stay ahead of the quantum threat.
27+
28+
## Challenges
29+
30+
1. **Quantum Hardware Requirements**
31+
- To factor large numbers (e.g., 2048-bit RSA moduli), you need **thousands to millions** of *logical* qubits with fault tolerance and low error rates—well beyond current quantum hardware capabilities.
32+
33+
2. **Error Correction**
34+
- Real quantum systems suffer from *noise* and *decoherence*, necessitating robust quantum error correction schemes (e.g., the **surface code**). These in turn require many *physical* qubits to create a single *logical* qubit.
35+
36+
3. **Implementation Complexity**
37+
- Designing, programming, and stabilizing a large-scale quantum circuit for Shor’s Algorithm is complex. Even with today’s quantum libraries, only small numbers (tens to hundreds of bits) can be factored reliably.
38+
39+
## Applying Shor’s Algorithm to Cryptography
40+
41+
### Challenges in Classical Cryptography
42+
43+
Modern cryptosystems like [[RSA]], [[Diffie–Hellman]] **Key Exchange**, and[[Elliptic Curves]] Cryptography rely on the hardness of factoring large integers or computing discrete logarithms. A classical computer cannot feasibly solve these problems for properly chosen key sizes (e.g., 2048-bit or higher). However, with Shor’s Algorithm:
44+
45+
- **Factoring RSA Moduli**: Breaking RSA requires factoring a large semi-prime $N = p \times q$. Shor’s Algorithm can do this in polynomial time, undermining RSA’s security assumptions.
46+
- **Discrete Log Problems**: Similar logic applies to discrete log variants (e.g., [[Diffie–Hellman]] and ECC), making them vulnerable to quantum attacks.
47+
48+
### How Shor’s Algorithm Works (High-Level)
49+
50+
1. **Choose a Random $a$**
51+
- Select an integer $a$ coprime to $N$ (the integer to be factored).
52+
2. **Quantum Period-Finding**
53+
- Construct and evaluate the function $f(x) = a^x $mod N$ within a *quantum* circuit.
54+
- Use the **Quantum Fourier Transform (QFT)** to find the *period* $r$ of this function in *superposition*.
55+
3. **Classical GCD**
56+
- Once the period $r$ is found, compute $\gcd(a^{r/2} \pm 1, N)$ to discover non-trivial factors of $N$.
57+
![[Shor's Algorithm.png]]
58+
59+
## Example: Factoring a Small Number with Shor's Algorithm
60+
61+
Below is a simplified Python-style pseudo-code using [Qiskit](https://qiskit.org/). This example can factor very small numbers (like 15, 21), illustrating core concepts rather than large-scale factorization. More info [here](https://github.com/Qiskit/textbook/blob/main/notebooks/ch-algorithms/shor.ipynb)
62+
63+
```python
64+
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, Aer, execute
65+
from qiskit.circuit.library import QFT
66+
67+
def shor_factor(N):
68+
"""
69+
Pseudo-code function to demonstrate the structure
70+
of a Shor's Algorithm circuit for a small integer N.
71+
For large N, a significantly more complex circuit and
72+
quantum resources are required.
73+
"""
74+
75+
# 1. Choose a random 'a' coprime to N
76+
a = find_random_coprime(N) # Placeholder function
77+
78+
# 2. Determine number of qubits needed for the range [0 .. 2^m-1] ~ N^2
79+
m = 4 # For demonstration, a small circuit
80+
81+
# Quantum registers: 'x' for superposition, 'f' to store a^x mod N
82+
x = QuantumRegister(m, name='x')
83+
f = QuantumRegister(m, name='f')
84+
c = ClassicalRegister(m, name='c')
85+
circuit = QuantumCircuit(x, f, c)
86+
87+
# 3. Initialize x register to superposition (Hadamard transforms)
88+
for qubit in x:
89+
circuit.h(qubit)
90+
91+
# 4. Implement the modular exponentiation a^x mod N on the 'f' register
92+
# (Pseudo-code: in reality, this is done via repeated modular multiplications)
93+
circuit = apply_mod_exp(circuit, x, f, a, N)
94+
95+
# 5. Apply Quantum Fourier Transform (QFT) to the x register
96+
qft_dagger = QFT(num_qubits=m, do_swaps=False).inverse()
97+
circuit.append(qft_dagger.to_instruction(), x[:])
98+
99+
# 6. Measure x register
100+
circuit.measure(x, c)
101+
102+
# 7. Run on a simulator
103+
simulator = Aer.get_backend('aer_simulator')
104+
result = execute(circuit, simulator, shots=1024).result()
105+
counts = result.get_counts()
106+
107+
# Use classical post-processing on the measurement result (period-finding)
108+
# Try GCD steps with a^(r/2) ± 1 to find non-trivial factors of N
109+
# ...
110+
return possible_factors
111+
112+
113+
if __name__ == "__main__":
114+
# Example usage:
115+
N = 15 # For demonstration (factors: 3 and 5)
116+
factors = shor_factor(N)
117+
print("Candidate factors:", factors)
118+
```
119+
120+
121+
### Original Paper:
122+
https://arxiv.org/abs/quant-ph/9508027
118 KB
Loading

0 commit comments

Comments
 (0)