|
| 1 | +# Copyright 2026 The PECOS Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 4 | +# in compliance with the License. You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 | +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 | +# or implied. See the License for the specific language governing permissions and limitations under |
| 11 | +# the License. |
| 12 | + |
| 13 | +"""Tests for simulator seeding via the pecos_rslib bindings.""" |
| 14 | + |
| 15 | +import pytest |
| 16 | +from pecos_rslib.simulators import SparseSim, Stab |
| 17 | + |
| 18 | + |
| 19 | +def _measure_sequence(sim_cls: type, *, seed: int, rounds: int = 16) -> list: |
| 20 | + """Create a seeded simulator, apply H then MZ repeatedly, return outcomes.""" |
| 21 | + sim = sim_cls(1, seed=seed) |
| 22 | + outcomes = [] |
| 23 | + for _ in range(rounds): |
| 24 | + sim.reset() |
| 25 | + sim.run_1q_gate("H", 0) |
| 26 | + outcomes.append(sim.run_1q_gate("MZ", 0)) |
| 27 | + return outcomes |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize("sim_cls", [SparseSim, Stab]) |
| 31 | +class TestSimulatorSeeding: |
| 32 | + """Verify that seeded stabilizer simulators produce reproducible results.""" |
| 33 | + |
| 34 | + def test_constructor_seed_is_reproducible(self, sim_cls: type) -> None: |
| 35 | + """Same seed in constructor gives same measurement sequence.""" |
| 36 | + assert _measure_sequence(sim_cls, seed=42) == _measure_sequence(sim_cls, seed=42) |
| 37 | + |
| 38 | + def test_different_seeds_differ(self, sim_cls: type) -> None: |
| 39 | + """Different seeds give different measurement sequences.""" |
| 40 | + assert _measure_sequence(sim_cls, seed=42) != _measure_sequence(sim_cls, seed=99) |
| 41 | + |
| 42 | + def test_set_seed_is_reproducible(self, sim_cls: type) -> None: |
| 43 | + """Calling set_seed after construction gives reproducible results.""" |
| 44 | + sim_a = sim_cls(1) |
| 45 | + sim_a.set_seed(42) |
| 46 | + outcomes_a = [sim_a.run_1q_gate("H", 0) for _ in range(8)] |
| 47 | + |
| 48 | + sim_b = sim_cls(1) |
| 49 | + sim_b.set_seed(42) |
| 50 | + outcomes_b = [sim_b.run_1q_gate("H", 0) for _ in range(8)] |
| 51 | + |
| 52 | + assert outcomes_a == outcomes_b |
0 commit comments