|
| 1 | +# benchmark_neighbors.py |
| 2 | +""" |
| 3 | +A script to benchmark the performance of neighbor-finding algorithms |
| 4 | +for the CustomizeLattice class. |
| 5 | +
|
| 6 | +This script compares the KDTree-based method against the baseline |
| 7 | +all-to-all distance matrix method across a range of lattice sizes. |
| 8 | +
|
| 9 | +To run this script from the project root directory: |
| 10 | +python benchmark_neighbors.py |
| 11 | +""" |
| 12 | +import logging |
| 13 | +import timeit |
| 14 | +from typing import Any, Dict, List |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +logging.getLogger("tensorcircuit.templates.lattice").setLevel(logging.WARNING) |
| 19 | + |
| 20 | + |
| 21 | +def run_benchmark() -> None: |
| 22 | + """ |
| 23 | + Executes the benchmark test and prints the results in a formatted table. |
| 24 | + """ |
| 25 | + site_counts: List[int] = [10, 50, 100, 200, 500, 1000, 1500, 2000] |
| 26 | + max_k: int = 1 |
| 27 | + number_of_runs: int = 5 |
| 28 | + results: List[Dict[str, Any]] = [] |
| 29 | + |
| 30 | + print("=" * 75) |
| 31 | + print("Starting neighbor finding benchmark for CustomizeLattice...") |
| 32 | + print(f"Each test will be run {number_of_runs} times to get a stable average.") |
| 33 | + print("=" * 75) |
| 34 | + print( |
| 35 | + f"{'Sites (N)':>10} | {'KDTree Time (s)':>18} | {'Baseline Time (s)':>20} | {'Speedup':>10}" |
| 36 | + ) |
| 37 | + print("-" * 75) |
| 38 | + |
| 39 | + for n_sites in site_counts: |
| 40 | + setup_code = f""" |
| 41 | +import numpy as np |
| 42 | +from tensorcircuit.templates.lattice import CustomizeLattice |
| 43 | +
|
| 44 | +np.random.seed(42) |
| 45 | +coords = np.random.rand({n_sites}, 2) |
| 46 | +ids = list(range({n_sites})) |
| 47 | +lat = CustomizeLattice(dimensionality=2, identifiers=ids, coordinates=coords) |
| 48 | +""" |
| 49 | + stmt_kdtree = f"lat._build_neighbors(max_k={max_k})" |
| 50 | + stmt_baseline = f"lat._build_neighbors_by_distance_matrix(max_k={max_k})" |
| 51 | + |
| 52 | + try: |
| 53 | + time_kdtree = ( |
| 54 | + timeit.timeit(stmt=stmt_kdtree, setup=setup_code, number=number_of_runs) |
| 55 | + / number_of_runs |
| 56 | + ) |
| 57 | + time_baseline = ( |
| 58 | + timeit.timeit( |
| 59 | + stmt=stmt_baseline, setup=setup_code, number=number_of_runs |
| 60 | + ) |
| 61 | + / number_of_runs |
| 62 | + ) |
| 63 | + |
| 64 | + speedup = time_baseline / time_kdtree if time_kdtree > 0 else float("inf") |
| 65 | + results.append( |
| 66 | + { |
| 67 | + "n_sites": n_sites, |
| 68 | + "time_kdtree": time_kdtree, |
| 69 | + "time_baseline": time_baseline, |
| 70 | + "speedup": speedup, |
| 71 | + } |
| 72 | + ) |
| 73 | + print( |
| 74 | + f"{n_sites:>10} | {time_kdtree:>18.6f} | {time_baseline:>20.6f} | {speedup:>9.2f}x" |
| 75 | + ) |
| 76 | + |
| 77 | + except Exception as e: |
| 78 | + print(f"An error occurred at N={n_sites}: {e}") |
| 79 | + break |
| 80 | + |
| 81 | + print("-" * 75) |
| 82 | + print("Benchmark complete.") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + run_benchmark() |
0 commit comments