Skip to content

Commit 67b721c

Browse files
action failure fix, and black and ruff
1 parent eb0baba commit 67b721c

29 files changed

Lines changed: 423 additions & 174 deletions

.github/workflows/tests.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ on:
55
pull_request:
66

77
jobs:
8+
lint:
9+
name: Lint and Format
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Check out repository
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: "3.12"
20+
cache: pip
21+
22+
- name: Install lint tools
23+
run: |
24+
python -m pip install --upgrade pip
25+
python -m pip install -e ".[dev]"
26+
27+
- name: Ruff
28+
run: ruff check .
29+
30+
- name: Black
31+
run: black --check .
32+
833
tests:
934
name: Tests Python ${{ matrix.python-version }}
1035
runs-on: ubuntu-latest

examples/benchmark_runtime.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from shors_algorithm_simulation.plotting.runtime import benchmark_runtime_table
22

3-
43
if __name__ == "__main__":
54
"""Benchmark selected deterministic cases and save a CSV summary."""
65
benchmark_cases = [
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from shors_algorithm_simulation.quantum.circuits import main
22

3-
43
if __name__ == "__main__":
54
main()

examples/factorisation_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from shors_algorithm_simulation import compute_probs, shors_simulation
22
from shors_algorithm_simulation.plotting.probabilities import plot_probs
33

4-
54
if __name__ == "__main__":
65
"""Run a deterministic factorisation example with plot output."""
76
result = shors_simulation(N=35, a=2, sparse=True, mode="distribution")

examples/multiple_cases_example.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from shors_algorithm_simulation import shors_simulation
22

3-
43
if __name__ == "__main__":
54
"""Run multiple semiprime examples with fixed bases for quick experimentation."""
65
cases = [
@@ -15,10 +14,14 @@
1514
print(f"Running example with N={N}, a={a}, mode={mode}")
1615
print("=" * 60)
1716
result = shors_simulation(N=N, a=a, sparse=True, mode=mode)
18-
print(f"Structured result: success={result['success']}, factors={result['factors']}, period={result['period']}")
17+
print(
18+
f"Structured result: success={result['success']}, factors={result['factors']}, period={result['period']}"
19+
)
1920

2021
print("\n" + "=" * 60)
2122
print("Expected retry case with N=33, a=2")
2223
print("=" * 60)
2324
result = shors_simulation(N=33, a=2, sparse=True, mode="distribution")
24-
print(f"Structured result: success={result['success']}, message={result['message']}")
25+
print(
26+
f"Structured result: success={result['success']}, message={result['message']}"
27+
)

examples/no_plot_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from shors_algorithm_simulation import shors_simulation
22

3-
43
if __name__ == "__main__":
54
"""Run a deterministic example without opening plots."""
65
result = shors_simulation(N=21, a=2, sparse=True, mode="distribution")

examples/runtimes_test.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
from shors_algorithm_simulation.plotting.runtime import run_runtime_analysis
22

3-
43
if __name__ == "__main__":
54
"""Choose coprime (N, a) for non-trivial runs"""
65
test_cases = [
7-
(15, 2), # 3 * 5
8-
(21, 2), # 3 * 7
9-
(33, 5), # 3 * 11
10-
(35, 2), # 5 * 7
11-
(39, 2), # 3 * 13
12-
(51, 2), # 3 * 17
13-
(55, 2), # 5 * 11
14-
(57, 5), # 3 * 19
15-
(65, 3), # 5 * 13
16-
(69, 2), # 3 * 23
17-
(77, 2), # 7 * 11
18-
(85, 2), # 5 * 17
19-
(87, 2), # 3 * 29
20-
(91, 5), # 7 * 13
21-
(93, 2), # 3 * 31
22-
(95, 2), # 5 * 19
23-
(111, 2), # 3 * 37
24-
(115, 2), # 5 * 23
25-
(119, 2), # 7 * 17
26-
(123, 2), # 3 * 41
27-
(129, 7), # 3 * 43
28-
(133, 2), # 7 * 19
29-
(141, 2), # 3 * 47
30-
(161, 6), # 7 * 23
31-
]
6+
(15, 2), # 3 * 5
7+
(21, 2), # 3 * 7
8+
(33, 5), # 3 * 11
9+
(35, 2), # 5 * 7
10+
(39, 2), # 3 * 13
11+
(51, 2), # 3 * 17
12+
(55, 2), # 5 * 11
13+
(57, 5), # 3 * 19
14+
(65, 3), # 5 * 13
15+
(69, 2), # 3 * 23
16+
(77, 2), # 7 * 11
17+
(85, 2), # 5 * 17
18+
(87, 2), # 3 * 29
19+
(91, 5), # 7 * 13
20+
(93, 2), # 3 * 31
21+
(95, 2), # 5 * 19
22+
(111, 2), # 3 * 37
23+
(115, 2), # 5 * 23
24+
(119, 2), # 7 * 17
25+
(123, 2), # 3 * 41
26+
(129, 7), # 3 * 43
27+
(133, 2), # 7 * 19
28+
(141, 2), # 3 * 47
29+
(161, 6), # 7 * 23
30+
]
3231

3332
try:
34-
N_vals, qubits, times, stds = run_runtime_analysis(test_cases, 3, sparse=True, mode="distribution")
33+
N_vals, qubits, times, stds = run_runtime_analysis(
34+
test_cases, 3, sparse=True, mode="distribution"
35+
)
3536
print("\nAnalysis complete!")
36-
37+
3738
except KeyboardInterrupt:
3839
print("\nAnalysis interrupted by user.")
3940

examples/shots_sweep_example.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from shors_algorithm_simulation import shors_simulation
99
from shors_algorithm_simulation.plotting.matplotlib_helpers import get_pyplot
1010

11-
1211
DEFAULT_SHOTS = (16, 32, 64, 128, 256, 512, 1024)
1312

1413

@@ -49,7 +48,10 @@ def run_shots_sweep(
4948

5049
csv_path = os.path.join(output_dir, f"shots_sweep_N={N}_a={a}_trials={trials}.csv")
5150
with open(csv_path, "w", newline="", encoding="utf-8") as handle:
52-
writer = csv.DictWriter(handle, fieldnames=["N", "a", "shots", "trials", "successes", "success_rate"])
51+
writer = csv.DictWriter(
52+
handle,
53+
fieldnames=["N", "a", "shots", "trials", "successes", "success_rate"],
54+
)
5355
writer.writeheader()
5456
writer.writerows(rows)
5557

@@ -80,13 +82,32 @@ def _plot_shots_sweep(rows: list[dict[str, object]], output_file: str) -> None:
8082

8183

8284
def parse_args() -> argparse.Namespace:
83-
parser = argparse.ArgumentParser(description="Plot sampled-measurement success rate versus shots.")
84-
parser.add_argument("--N", type=int, default=21, help="Composite integer to factor.")
85-
parser.add_argument("--a", type=int, default=2, help="Base for modular exponentiation.")
86-
parser.add_argument("--shots", nargs="+", type=int, default=list(DEFAULT_SHOTS), help="Shot counts to test.")
85+
parser = argparse.ArgumentParser(
86+
description="Plot sampled-measurement success rate versus shots."
87+
)
88+
parser.add_argument(
89+
"--N", type=int, default=21, help="Composite integer to factor."
90+
)
91+
parser.add_argument(
92+
"--a", type=int, default=2, help="Base for modular exponentiation."
93+
)
94+
parser.add_argument(
95+
"--shots",
96+
nargs="+",
97+
type=int,
98+
default=list(DEFAULT_SHOTS),
99+
help="Shot counts to test.",
100+
)
87101
parser.add_argument("--trials", type=int, default=20, help="Trials per shot count.")
88-
parser.add_argument("--seed", type=int, default=0, help="Base random seed for reproducible sampling.")
89-
parser.add_argument("--output-dir", default="images", help="Directory for generated CSV and plot.")
102+
parser.add_argument(
103+
"--seed",
104+
type=int,
105+
default=0,
106+
help="Base random seed for reproducible sampling.",
107+
)
108+
parser.add_argument(
109+
"--output-dir", default="images", help="Directory for generated CSV and plot."
110+
)
90111
return parser.parse_args()
91112

92113

examples/visualizations_example.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99
plot_oracle_period_pattern,
1010
)
1111

12-
1312
PLOT_CHOICES = ["oracle", "marked", "continued", "comparison", "all"]
1413

1514

1615
def parse_args():
17-
parser = argparse.ArgumentParser(description="Generate Shor period-finding visualizations.")
18-
parser.add_argument("--N", type=int, default=21, help="Semiprime for oracle/probability/candidate plots.")
19-
parser.add_argument("--a", type=int, default=2, help="Base for oracle/probability/candidate plots.")
16+
parser = argparse.ArgumentParser(
17+
description="Generate Shor period-finding visualizations."
18+
)
19+
parser.add_argument(
20+
"--N",
21+
type=int,
22+
default=21,
23+
help="Semiprime for oracle/probability/candidate plots.",
24+
)
25+
parser.add_argument(
26+
"--a", type=int, default=2, help="Base for oracle/probability/candidate plots."
27+
)
2028
parser.add_argument(
2129
"--mode",
2230
choices=["distribution", "matrix"],
@@ -30,11 +38,34 @@ def parse_args():
3038
default=["all"],
3139
help="Plots to generate.",
3240
)
33-
parser.add_argument("--output-dir", default="images", help="Directory for generated plots and CSV files.")
34-
parser.add_argument("--top-n", type=int, default=12, help="Number of continued-fraction candidates to plot.")
35-
parser.add_argument("--comparison-N", type=int, default=15, help="N for matrix/distribution comparison.")
36-
parser.add_argument("--comparison-a", type=int, default=2, help="a for matrix/distribution comparison.")
37-
parser.add_argument("--dense-comparison", action="store_true", help="Use dense matrices for comparison plot.")
41+
parser.add_argument(
42+
"--output-dir",
43+
default="images",
44+
help="Directory for generated plots and CSV files.",
45+
)
46+
parser.add_argument(
47+
"--top-n",
48+
type=int,
49+
default=12,
50+
help="Number of continued-fraction candidates to plot.",
51+
)
52+
parser.add_argument(
53+
"--comparison-N",
54+
type=int,
55+
default=15,
56+
help="N for matrix/distribution comparison.",
57+
)
58+
parser.add_argument(
59+
"--comparison-a",
60+
type=int,
61+
default=2,
62+
help="a for matrix/distribution comparison.",
63+
)
64+
parser.add_argument(
65+
"--dense-comparison",
66+
action="store_true",
67+
help="Use dense matrices for comparison plot.",
68+
)
3869
return parser.parse_args()
3970

4071

@@ -47,7 +78,9 @@ def selected_plots(plot_args):
4778

4879
def print_output(name, value):
4980
if isinstance(value, dict):
50-
print(f"{name}: plot={value['plot']}, csv={value['csv']}, rows={len(value['rows'])}")
81+
print(
82+
f"{name}: plot={value['plot']}, csv={value['csv']}, rows={len(value['rows'])}"
83+
)
5184
else:
5285
print(f"{name}: {value}")
5386

@@ -62,7 +95,9 @@ def main():
6295
probabilities = compute_probs(args.N, args.a, mode=args.mode)
6396

6497
if "oracle" in plots:
65-
outputs["oracle"] = plot_oracle_period_pattern(args.N, args.a, output_dir=args.output_dir)
98+
outputs["oracle"] = plot_oracle_period_pattern(
99+
args.N, args.a, output_dir=args.output_dir
100+
)
66101

67102
if "continued" in plots:
68103
outputs["continued"] = plot_continued_fraction_diagnostics(

main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from shors_algorithm_simulation.cli import main
22

3-
43
if __name__ == "__main__":
54
raise SystemExit(main())

0 commit comments

Comments
 (0)