1+ """Scalability benchmark for Grover's algorithm.
2+
3+ Sweeps n=2-10 qubits, measures circuit depth, gate counts,
4+ and execution time for each configuration. Saves depth and
5+ time plots to outputs/.
6+ """
7+
8+ import sys
19import time
210from pathlib import Path
311
12+ sys .path .insert (0 , str (Path (__file__ ).resolve ().parents [1 ])) # scripts/ is not on sys.path; src/ is only findable from project root
13+
414import matplotlib .pyplot as plt
515from qiskit_aer import AerSimulator
616
@@ -49,6 +59,28 @@ def plot_benchmark(results: list[dict], title: str, ylabel: str, filepath: str)
4959 plt .close (fig )
5060
5161
62+ def plot_gate_benchmark (results : list [dict ], filepath : str ) -> None :
63+ """Plot total gates and CX gates vs. qubits."""
64+ xs = [r ["num_qubits" ] for r in results ]
65+ totals = [r ["total_gates" ] for r in results ]
66+ cxs = [r ["cx_gates" ] for r in results ]
67+
68+ fig , ax = plt .subplots (figsize = (8 , 5 ))
69+ ax .plot (xs , totals , "o-" , label = "Total Gates" , color = "#3498db" , linewidth = 2 , markersize = 6 )
70+ ax .plot (xs , cxs , "s--" , label = "CX Gates (Part of total gates)" , color = "#e74c3c" , linewidth = 2 , markersize = 6 )
71+ ax .set_xlabel ("Number of Qubits (n)" )
72+ ax .set_ylabel ("Gate Count" )
73+ ax .set_title ("Grover's Algorithm - Gate Counts vs. Qubits" )
74+ ax .legend ()
75+ ax .grid (True , alpha = 0.3 )
76+ plt .tight_layout ()
77+
78+ Path (filepath ).parent .mkdir (parents = True , exist_ok = True )
79+ fig .savefig (filepath , dpi = 150 )
80+ print (f" Plot saved: { filepath } " )
81+ plt .close (fig )
82+
83+
5284def main () -> None :
5385 print ()
5486 print ("=" * 60 )
@@ -58,10 +90,12 @@ def main() -> None:
5890
5991 depth_results = []
6092 time_results = []
93+ info_list = []
6194
6295 for n in SWEEP_QUBITS :
6396 marked = "1" * n
6497 info = measure_circuit_stats (n , marked )
98+ info_list .append (info )
6599 circuit = build_grover_circuit (n , marked )
66100 elapsed = measure_execution_time (circuit )
67101
@@ -88,6 +122,8 @@ def main() -> None:
88122 f"Execution Time (s) - { SHOTS_PER_POINT } shots" ,
89123 "outputs/benchmark_time.png" ,
90124 )
125+ # Needs a dual-line plot (total vs CX); single-line plot_benchmark() won't do this
126+ plot_gate_benchmark (info_list , "outputs/benchmark_gates.png" )
91127
92128 print ()
93129 print (" Done. Plots saved to outputs/." )
0 commit comments