|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from shadowsim.benchmarking import Benchmark |
| 7 | +from shadowsim.core import Hamiltonian |
| 8 | +from shadowsim.core import State |
| 9 | +from shadowsim.simulators.simulator import Simulator |
| 10 | + |
| 11 | + |
| 12 | +Z = np.array([[1, 0], [0, -1]], dtype=np.complex128) |
| 13 | + |
| 14 | + |
| 15 | +class StubSimulator(Simulator): |
| 16 | + """Minimal simulator with canned expectation traces.""" |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + *, |
| 21 | + total_time: float = 1.0, |
| 22 | + time_steps: int = 3, |
| 23 | + curves: list[np.ndarray] | None = None, |
| 24 | + id: str = "stub", |
| 25 | + ): |
| 26 | + super().__init__( |
| 27 | + [Hamiltonian(Z)], |
| 28 | + [], |
| 29 | + State(np.array([1.0, 0.0], dtype=np.complex128), 1), |
| 30 | + 1, |
| 31 | + total_time, |
| 32 | + time_steps, |
| 33 | + id, |
| 34 | + ) |
| 35 | + if curves is None: |
| 36 | + t = self.tlist |
| 37 | + curves = [np.cos(t), np.sin(t)] |
| 38 | + self._curves = [np.asarray(c, dtype=float) for c in curves] |
| 39 | + |
| 40 | + def simulate(self): |
| 41 | + self.results = list(self._curves) |
| 42 | + return self.results |
| 43 | + |
| 44 | + |
| 45 | +def _pair(**kwargs): |
| 46 | + return StubSimulator(id="a", **kwargs), StubSimulator(id="b", **kwargs) |
| 47 | + |
| 48 | + |
| 49 | +def test_benchmark_rejects_mismatched_tlist(): |
| 50 | + a = StubSimulator(time_steps=3) |
| 51 | + b = StubSimulator(time_steps=4) |
| 52 | + with pytest.raises(ValueError, match="same time grid"): |
| 53 | + Benchmark(a, b) |
| 54 | + |
| 55 | + |
| 56 | +def test_benchmark_run_and_get_results(): |
| 57 | + a, b = _pair() |
| 58 | + benchmark = Benchmark(a, b) |
| 59 | + |
| 60 | + assert np.array_equal(benchmark.get_tlist(), a.tlist) |
| 61 | + |
| 62 | + benchmark.run() |
| 63 | + all_a, all_b = benchmark.get_results() |
| 64 | + assert len(all_a) == len(all_b) == 2 |
| 65 | + assert np.allclose(all_a[0], a.get_results(0)) |
| 66 | + assert np.allclose(all_b[1], b.get_results(1)) |
| 67 | + |
| 68 | + first_a, first_b = benchmark.get_results(0) |
| 69 | + assert np.allclose(first_a, a.get_results(0)) |
| 70 | + assert np.allclose(first_b, b.get_results(0)) |
| 71 | + |
| 72 | + |
| 73 | +def test_benchmark_save_result_plot_writes_three_files(tmp_path, monkeypatch): |
| 74 | + monkeypatch.chdir(tmp_path) |
| 75 | + a, b = _pair() |
| 76 | + # Distinct curves so the abs-diff plot is nontrivial. |
| 77 | + b._curves = [np.cos(b.tlist) + 0.1, np.sin(b.tlist) - 0.1] |
| 78 | + benchmark = Benchmark(a, b) |
| 79 | + benchmark.run() |
| 80 | + |
| 81 | + pa, pb, pdiff = benchmark.save_result_plot( |
| 82 | + labels=["cavity", "emitter"], |
| 83 | + title_a="A", |
| 84 | + title_b="B", |
| 85 | + title="diff", |
| 86 | + dpi=80, |
| 87 | + ) |
| 88 | + |
| 89 | + assert pa.is_file() |
| 90 | + assert pb.is_file() |
| 91 | + assert pdiff.is_file() |
| 92 | + assert pa.parent == Path("results") |
| 93 | + assert pdiff.name.startswith("benchmark_abs_diff_") |
| 94 | + |
| 95 | + |
| 96 | +def test_benchmark_abs_diff_requires_results(): |
| 97 | + a, b = _pair() |
| 98 | + benchmark = Benchmark(a, b) |
| 99 | + with pytest.raises(ValueError, match="Results are not available"): |
| 100 | + benchmark._save_abs_diff_plot(indices=None, dpi=80, title=None) |
| 101 | + |
| 102 | + |
| 103 | +def test_benchmark_abs_diff_with_explicit_indices_and_no_title(tmp_path, monkeypatch): |
| 104 | + monkeypatch.chdir(tmp_path) |
| 105 | + a, b = _pair() |
| 106 | + a.results = a._curves |
| 107 | + b.results = b._curves |
| 108 | + benchmark = Benchmark(a, b) |
| 109 | + |
| 110 | + path = benchmark._save_abs_diff_plot(indices=[0], dpi=80, title=None) |
| 111 | + assert path.is_file() |
| 112 | + |
| 113 | + |
| 114 | +def test_benchmark_abs_diff_rejects_mismatched_observable_counts(): |
| 115 | + a = StubSimulator(id="a", curves=[np.zeros(3), np.ones(3)]) |
| 116 | + b = StubSimulator(id="b", curves=[np.zeros(3)]) |
| 117 | + a.results = a._curves |
| 118 | + b.results = b._curves |
| 119 | + benchmark = Benchmark(a, b) |
| 120 | + |
| 121 | + with pytest.raises(ValueError, match="Mismatched observable counts"): |
| 122 | + benchmark._save_abs_diff_plot(indices=None, dpi=80, title=None) |
| 123 | + |
| 124 | + |
| 125 | +def test_benchmark_str_and_repr(): |
| 126 | + a, b = _pair() |
| 127 | + benchmark = Benchmark(a, b) |
| 128 | + |
| 129 | + assert str(benchmark) == "Benchmark(simulator_a=StubSimulator, simulator_b=StubSimulator)" |
| 130 | + assert "StubSimulator(" in repr(benchmark) |
0 commit comments