|
| 1 | +"""Tests for syndrome database generation module.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pathlib |
| 6 | +import tempfile |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pytest |
| 10 | +import stim |
| 11 | + |
| 12 | +from bpdecoderplus.circuit import generate_circuit |
| 13 | +from bpdecoderplus.syndrome import ( |
| 14 | + generate_syndrome_database_from_circuit, |
| 15 | + load_syndrome_database, |
| 16 | + sample_syndromes, |
| 17 | + save_syndrome_database, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class TestSampleSyndromes: |
| 22 | + """Tests for sample_syndromes function.""" |
| 23 | + |
| 24 | + def test_basic_sampling(self): |
| 25 | + """Test basic syndrome sampling.""" |
| 26 | + circuit = generate_circuit(distance=3, rounds=3, p=0.01, task="z") |
| 27 | + syndromes, observables = sample_syndromes(circuit, num_shots=10) |
| 28 | + |
| 29 | + assert syndromes.shape[0] == 10 |
| 30 | + assert observables.shape == (10,) |
| 31 | + assert syndromes.dtype == np.uint8 |
| 32 | + assert observables.dtype == np.uint8 |
| 33 | + |
| 34 | + def test_without_observables(self): |
| 35 | + """Test sampling without observables.""" |
| 36 | + circuit = generate_circuit(distance=3, rounds=3, p=0.01, task="z") |
| 37 | + syndromes, observables = sample_syndromes( |
| 38 | + circuit, num_shots=10, include_observables=False |
| 39 | + ) |
| 40 | + |
| 41 | + assert syndromes.shape[0] == 10 |
| 42 | + assert observables is None |
| 43 | + |
| 44 | + def test_num_detectors(self): |
| 45 | + """Test that number of detectors matches circuit.""" |
| 46 | + circuit = generate_circuit(distance=3, rounds=3, p=0.01, task="z") |
| 47 | + dem = circuit.detector_error_model() |
| 48 | + syndromes, _ = sample_syndromes(circuit, num_shots=5) |
| 49 | + |
| 50 | + assert syndromes.shape[1] == dem.num_detectors |
| 51 | + |
| 52 | + |
| 53 | +class TestSaveSyndromeDatabase: |
| 54 | + """Tests for save_syndrome_database function.""" |
| 55 | + |
| 56 | + def test_save_with_observables(self): |
| 57 | + """Test saving database with observables.""" |
| 58 | + syndromes = np.random.randint(0, 2, size=(10, 24), dtype=np.uint8) |
| 59 | + observables = np.random.randint(0, 2, size=10, dtype=np.uint8) |
| 60 | + |
| 61 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 62 | + output_path = pathlib.Path(tmpdir) / "test.npz" |
| 63 | + save_syndrome_database(syndromes, observables, output_path) |
| 64 | + |
| 65 | + assert output_path.exists() |
| 66 | + |
| 67 | + def test_save_without_observables(self): |
| 68 | + """Test saving database without observables.""" |
| 69 | + syndromes = np.random.randint(0, 2, size=(10, 24), dtype=np.uint8) |
| 70 | + |
| 71 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 72 | + output_path = pathlib.Path(tmpdir) / "test.npz" |
| 73 | + save_syndrome_database(syndromes, None, output_path) |
| 74 | + |
| 75 | + assert output_path.exists() |
| 76 | + |
| 77 | + def test_save_with_metadata(self): |
| 78 | + """Test saving database with metadata.""" |
| 79 | + syndromes = np.random.randint(0, 2, size=(10, 24), dtype=np.uint8) |
| 80 | + observables = np.random.randint(0, 2, size=10, dtype=np.uint8) |
| 81 | + metadata = {"distance": 3, "rounds": 3, "p": 0.01} |
| 82 | + |
| 83 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 84 | + output_path = pathlib.Path(tmpdir) / "test.npz" |
| 85 | + save_syndrome_database(syndromes, observables, output_path, metadata) |
| 86 | + |
| 87 | + assert output_path.exists() |
| 88 | + |
| 89 | + |
| 90 | +class TestLoadSyndromeDatabase: |
| 91 | + """Tests for load_syndrome_database function.""" |
| 92 | + |
| 93 | + def test_load_with_observables(self): |
| 94 | + """Test loading database with observables.""" |
| 95 | + syndromes = np.random.randint(0, 2, size=(10, 24), dtype=np.uint8) |
| 96 | + observables = np.random.randint(0, 2, size=10, dtype=np.uint8) |
| 97 | + |
| 98 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 99 | + output_path = pathlib.Path(tmpdir) / "test.npz" |
| 100 | + save_syndrome_database(syndromes, observables, output_path) |
| 101 | + |
| 102 | + loaded_syndromes, loaded_observables, _ = load_syndrome_database(output_path) |
| 103 | + |
| 104 | + np.testing.assert_array_equal(loaded_syndromes, syndromes) |
| 105 | + np.testing.assert_array_equal(loaded_observables, observables) |
| 106 | + |
| 107 | + def test_load_without_observables(self): |
| 108 | + """Test loading database without observables.""" |
| 109 | + syndromes = np.random.randint(0, 2, size=(10, 24), dtype=np.uint8) |
| 110 | + |
| 111 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 112 | + output_path = pathlib.Path(tmpdir) / "test.npz" |
| 113 | + save_syndrome_database(syndromes, None, output_path) |
| 114 | + |
| 115 | + loaded_syndromes, loaded_observables, _ = load_syndrome_database(output_path) |
| 116 | + |
| 117 | + np.testing.assert_array_equal(loaded_syndromes, syndromes) |
| 118 | + assert loaded_observables is None |
| 119 | + |
| 120 | + def test_load_with_metadata(self): |
| 121 | + """Test loading database with metadata.""" |
| 122 | + syndromes = np.random.randint(0, 2, size=(10, 24), dtype=np.uint8) |
| 123 | + observables = np.random.randint(0, 2, size=10, dtype=np.uint8) |
| 124 | + metadata = {"distance": 3, "rounds": 3, "p": 0.01} |
| 125 | + |
| 126 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 127 | + output_path = pathlib.Path(tmpdir) / "test.npz" |
| 128 | + save_syndrome_database(syndromes, observables, output_path, metadata) |
| 129 | + |
| 130 | + _, _, loaded_metadata = load_syndrome_database(output_path) |
| 131 | + |
| 132 | + assert loaded_metadata == metadata |
| 133 | + |
| 134 | + |
| 135 | +class TestGenerateSyndromeDatabaseFromCircuit: |
| 136 | + """Tests for generate_syndrome_database_from_circuit function.""" |
| 137 | + |
| 138 | + def test_generate_from_circuit_file(self): |
| 139 | + """Test generating database from circuit file.""" |
| 140 | + circuit = generate_circuit(distance=3, rounds=3, p=0.01, task="z") |
| 141 | + |
| 142 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 143 | + circuit_path = pathlib.Path(tmpdir) / "test.stim" |
| 144 | + circuit_path.write_text(str(circuit)) |
| 145 | + |
| 146 | + db_path = generate_syndrome_database_from_circuit(circuit_path, num_shots=20) |
| 147 | + |
| 148 | + assert db_path.exists() |
| 149 | + assert db_path.suffix == ".npz" |
| 150 | + |
| 151 | + # Load and verify |
| 152 | + syndromes, observables, metadata = load_syndrome_database(db_path) |
| 153 | + assert syndromes.shape[0] == 20 |
| 154 | + assert observables.shape == (20,) |
| 155 | + assert metadata["num_shots"] == 20 |
| 156 | + assert metadata["circuit_file"] == "test.stim" |
| 157 | + |
| 158 | + def test_custom_output_path(self): |
| 159 | + """Test generating database with custom output path.""" |
| 160 | + circuit = generate_circuit(distance=3, rounds=3, p=0.01, task="z") |
| 161 | + |
| 162 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 163 | + circuit_path = pathlib.Path(tmpdir) / "test.stim" |
| 164 | + circuit_path.write_text(str(circuit)) |
| 165 | + |
| 166 | + custom_output = pathlib.Path(tmpdir) / "custom_db.npz" |
| 167 | + db_path = generate_syndrome_database_from_circuit( |
| 168 | + circuit_path, num_shots=15, output_path=custom_output |
| 169 | + ) |
| 170 | + |
| 171 | + assert db_path == custom_output |
| 172 | + assert db_path.exists() |
0 commit comments