You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A pure Python implementation of Shor's quantum factorization algorithm using classical simulation of the period-finding step.
4
6
The project supports both explicit matrix simulation for very small inputs and a faster distribution-based simulation for the ideal first-register measurement probabilities.
5
7
@@ -48,6 +50,8 @@ A quantum circuit sketch for Shor's Algorithm using 8 qubits:
48
50
-**Visualization**: Plots probability distributions to visualize quantum measurements
@@ -104,64 +109,77 @@ cd Shors_Algorithm_Simulation
104
109
pip install -r requirements.txt
105
110
```
106
111
107
-
`qiskit` and `pylatexenc` are included so `src/quantum_part/quantum_circuit.py` can regenerate the illustrative circuit diagram.
112
+
`requirements.txt` includes the simulator dependencies plus `pytest` for local test runs. Circuit diagram generation uses optional Qiskit dependencies:
113
+
114
+
```bash
115
+
pip install -r requirements-circuits.txt
116
+
# or, for editable/package installs:
117
+
pip install ".[circuits]"
118
+
```
108
119
109
120
### Example Usage
110
121
111
122
**Terminal inputs**:
112
123
113
124
```python
114
-
python examples/factorisation_example.py # single run with plot output
115
-
python examples/no_plot_example.py # single run without plotting
116
-
python examples/multiple_cases_example.py # batch of small deterministic cases
`distribution` mode is the default and is appropriate for the documented examples. `matrix` mode is intended for the smallest cases because explicit gate matrices grow quickly.
155
-
`shors_simulation` returns a dictionary containing `success`, `N`, `a`, `mode`, `period`, `factors`, `message`, and `classical_precheck`.
176
+
`shors_simulation` returns a dictionary containing `success`, `N`, `a`, `mode`, `period`, `factors`, `message`, `classical_precheck`, `shots`, `measurement_counts`, and `attempts`.
156
177
157
178
**Output**:
158
179
159
180
```
160
181
N = 35
161
-
Running Classical Checks...
162
-
Classical checks passed.
163
-
a = 2.
164
-
Proceeding to quantum algorithm...
182
+
Attempt 1: a = 2
165
183
The period r = 12 is even.
166
184
a^(r/2) + 1 = 30, and gcd(30, 35) = 5
167
185
a^(r/2) - 1 = 28, and gcd(28, 35) = 7
@@ -180,6 +198,14 @@ This also saves the plot to the "images" directory as "first_register_probabilit
180
198
- continued-fraction candidate plot and CSV table
181
199
- matrix mode vs distribution mode comparison for a small case
182
200
201
+
`examples/shots_sweep_example.py` repeats sampled period recovery for multiple shot counts and saves a CSV plus a success-rate plot. It is intended to show how empirical measurement histograms converge toward the ideal distribution as shots increase.
202
+
203
+
### What Is Simulated
204
+
205
+
`mode="matrix"` constructs the full simulated state evolution for tiny examples, so it is useful for checking the gate-level model but grows quickly.
206
+
`mode="distribution"` computes the ideal post-IQFT first-register probability distribution directly from the periodic oracle values. It does not build a scalable quantum computer or simulate hardware noise.
207
+
When `shots` is provided, the simulator samples measurement counts from that ideal distribution and then runs the same continued-fraction recovery on the empirical histogram.
208
+
183
209
### Tests
184
210
185
211
```bash
@@ -190,6 +216,7 @@ pytest -q
190
216
191
217
-**Exponential Runtime/Memory**: `mode="matrix"` scales exponentially with the number of simulated qubits and is only practical for tiny cases.
192
218
-**Distribution Mode Is Idealized**: `mode="distribution"` avoids full matrices by computing the ideal first-register distribution directly, which is still a classical simulation of the period-finding output.
219
+
-**Shot Sampling Is Synthetic**: `shots` samples from the ideal distribution; it does not model device noise, decoherence, or imperfect gates.
193
220
-**Small Numbers Only**: Practical for factoring small educational examples, not cryptographic integers.
194
221
-**Educational Purpose**: Not suitable for large numbers practically used for low-bit RSA
195
222
-**Multiple Runs**: May require multiple runs if classical checks on $N, a$ or $r$ fail
0 commit comments