|
| 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 exposing simulator seeding on stabilizer backends.""" |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from pecos_rslib import SparseSim, Stab |
| 18 | + |
| 19 | + |
| 20 | +def _measurement_sequence(sim_cls, *, seed=None, reseed=None, rounds=32): |
| 21 | + sim = sim_cls(1, seed=seed) if seed is not None else sim_cls(1) |
| 22 | + if reseed is not None: |
| 23 | + sim.set_seed(reseed) |
| 24 | + |
| 25 | + outcomes = [] |
| 26 | + for _ in range(rounds): |
| 27 | + sim.reset() |
| 28 | + sim.run_1q_gate("H", 0) |
| 29 | + outcomes.append(sim.run_1q_gate("MZ", 0)) |
| 30 | + return outcomes |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("sim_cls", [SparseSim, Stab]) |
| 34 | +def test_seeded_constructor_repeats_measurement_sequence(sim_cls) -> None: |
| 35 | + assert _measurement_sequence(sim_cls, seed=42) == _measurement_sequence(sim_cls, seed=42) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("sim_cls", [SparseSim, Stab]) |
| 39 | +def test_set_seed_repeats_measurement_sequence(sim_cls) -> None: |
| 40 | + assert _measurement_sequence(sim_cls, reseed=42) == _measurement_sequence(sim_cls, reseed=42) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("sim_cls", [SparseSim, Stab]) |
| 44 | +def test_different_seeds_change_measurement_sequence(sim_cls) -> None: |
| 45 | + assert _measurement_sequence(sim_cls, seed=42) != _measurement_sequence(sim_cls, seed=43) |
0 commit comments