diff --git a/code/qec/color_code/memory_circuit.py b/code/qec/color_code/memory_circuit.py index dd3007d..3bf783f 100644 --- a/code/qec/color_code/memory_circuit.py +++ b/code/qec/color_code/memory_circuit.py @@ -538,6 +538,27 @@ def add_tick(self): self.circuit += c return c + def _spam_probability(self, kind, basis): + """Failure probability of a 'prep' or 'meas' operation in ``basis`` ('X' or 'Z').""" + if self.noise_model is not None: + return self.noise_model.spam_probability(kind, basis) + return self.spam_error + + def _flip_error(self, qubits, basis, probability): + """ + Instruction that flips a ``basis`` prep/meas outcome on ``qubits``. + + A Z-basis outcome is flipped by X_ERROR, an X-basis outcome by Z_ERROR. + Returns '' when the probability is zero. Does not append to the circuit. + """ + if not probability > 0.: + return '' + c = self.margin + c += ('X_ERROR(%.10f) ' if basis == 'Z' else 'Z_ERROR(%.10f) ') % probability + for q in qubits: + c += '%d ' % q + return c + '\n' + def add_reset(self, qubits, basis='Z'): basis = basis.upper() @@ -550,65 +571,20 @@ def add_reset(self, qubits, basis='Z'): c += '%d ' % q c += '\n' - # Apply preparation errors - if self.noise_model is not None: - # Basis-labeled semantics (same as the surface-code builder): - # - Z-basis prep (|0>): X flips to |1> -> use p_prep_Z (Z-basis prep failure) - # - X-basis prep (|+>): Z flips to |-> -> use p_prep_X (X-basis prep failure) - if basis == 'Z' and self.noise_model.p_prep_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_prep_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif basis == 'X' and self.noise_model.p_prep_X > 0: - c += self.margin - c += 'Z_ERROR(%.10f) ' % self.noise_model.p_prep_X - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - # Fallback to simple mode - c += self.margin - if basis == 'Z': - c += 'X_ERROR(%.10f) ' % self.spam_error - elif basis == 'X': - c += 'Z_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + # Preparation error: a flip of the freshly prepared state. + c += self._flip_error(qubits, basis, self._spam_probability('prep', basis)) self.circuit += c return c def add_single_error(self, qubits, error_type): - """Add a single-qubit error (X or Z) to specified qubits.""" - # Determine error probability - if self.noise_model is not None: - # Basis-labeled semantics: - # - Applying X corresponds to Z-basis prep failure -> p_prep_Z - # - Applying Z corresponds to X-basis prep failure -> p_prep_X - if error_type == 'X': - error_prob = self.noise_model.p_prep_Z - elif error_type == 'Z': - error_prob = self.noise_model.p_prep_X - else: - error_prob = 0. - else: - error_prob = self.spam_error - - if error_prob == 0.: + """Add a prep-like single-qubit error (X or Z) to specified qubits.""" + # An X error is a Z-basis prep failure, a Z error an X-basis prep failure. + basis = {'X': 'Z', 'Z': 'X'}.get(error_type) + if basis is None: return '' - c = self.margin - if error_type == 'X': - c += 'X_ERROR(%.10f) ' % error_prob - elif error_type == 'Z': - c += 'Z_ERROR(%.10f) ' % error_prob - for q in qubits: - c += '%d ' % q - c += '\n' - + c = self._flip_error(qubits, basis, self._spam_probability('prep', basis)) self.circuit += c return c @@ -775,23 +751,8 @@ def add_measure_reset(self, qubits, error_free_reset=False): When NoiseModel is set, uses explicit measurement and prep error probabilities. """ - c = '' - # Measurement error (before measurement) - if self.noise_model is not None: - # Z-basis measurement failure is modeled as a pre-measurement X flip. - if self.noise_model.p_meas_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + c = self._flip_error(qubits, 'Z', self._spam_probability('meas', 'Z')) c += self.margin c += 'MR ' # Measure and reset to |0> @@ -801,20 +762,7 @@ def add_measure_reset(self, qubits, error_free_reset=False): # Reset error (after reset, if not error-free) if not error_free_reset: - if self.noise_model is not None: - # Z-basis reset failure is modeled as a post-reset X flip. - if self.noise_model.p_prep_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_prep_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + c += self._flip_error(qubits, 'Z', self._spam_probability('prep', 'Z')) self.circuit += c return c @@ -842,32 +790,8 @@ def add_measure(self, qubits, basis='Z', include_reset=False): """ basis = basis.upper() - c = '' - # Apply measurement errors (before measurement) - if self.noise_model is not None: - # Use 22-parameter noise model: explicit X_ERROR or Z_ERROR - if basis == 'Z' and self.noise_model.p_meas_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif basis == 'X' and self.noise_model.p_meas_X > 0: - c += self.margin - c += 'Z_ERROR(%.10f) ' % self.noise_model.p_meas_X - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - # Fallback to simple mode - c += self.margin - if basis == 'Z': - c += 'X_ERROR(%.10f) ' % self.spam_error - elif basis == 'X': - c += 'Z_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + # Measurement error (before measurement) + c = self._flip_error(qubits, basis, self._spam_probability('meas', basis)) c += self.margin @@ -1333,22 +1257,9 @@ def _add_stabilizer_round( # Inject ONLY a "fake data-measurement SPAM" error on data qubits. # - X-basis data measurement failure -> apply Z with prob p_meas_X # - Z-basis data measurement failure -> apply X with prob p_meas_Z - if self.basis.upper() == 'X': - p_fake = float(orig_noise_model.p_meas_X) - if p_fake > 0: - c = self.margin + 'Z_ERROR(%.10f) ' % p_fake - for q in self.code.data_qubits: - c += '%d ' % q - c += '\n' - self.circuit += c - else: # 'Z' - p_fake = float(orig_noise_model.p_meas_Z) - if p_fake > 0: - c = self.margin + 'X_ERROR(%.10f) ' % p_fake - for q in self.code.data_qubits: - c += '%d ' % q - c += '\n' - self.circuit += c + data_basis = 'X' if self.basis.upper() == 'X' else 'Z' + p_fake = float(orig_noise_model.spam_probability('meas', data_basis)) + self.circuit += self._flip_error(self.code.data_qubits, data_basis, p_fake) else: if self.noise_model is not None: # NoiseModel semantics (drift/decomposition): IGNORE data-idle during ancilla prep/reset. @@ -1363,15 +1274,8 @@ def _add_stabilizer_round( # Use orig spam_error (saved before zeroing) to mimic measurement error on data qubits. # X-basis measurement -> Z errors flip the outcome; Z-basis -> X errors flip the outcome. p_fake = orig[3] # orig = (idle_error, sqgate_error, tqgate_error, spam_error) - if p_fake > 0: - if self.basis.upper() == 'X': - c = self.margin + 'Z_ERROR(%.10f) ' % p_fake - else: - c = self.margin + 'X_ERROR(%.10f) ' % p_fake - for q in self.code.data_qubits: - c += '%d ' % q - c += '\n' - self.circuit += c + data_basis = 'X' if self.basis.upper() == 'X' else 'Z' + self.circuit += self._flip_error(self.code.data_qubits, data_basis, p_fake) else: self.add_idle(self.code.data_qubits, logical_measurement=logical_measurement) diff --git a/code/qec/noise_model.py b/code/qec/noise_model.py index 9904daf..dc3d84a 100644 --- a/code/qec/noise_model.py +++ b/code/qec/noise_model.py @@ -608,6 +608,33 @@ def to_stim_pauli_channel_1_args_spam(self) -> Tuple[float, float, float]: """Args (p_X,p_Y,p_Z) for PAULI_CHANNEL_1 during SPAM-window data-idle (ancilla prep/reset).""" return (self.p_idle_spam_X, self.p_idle_spam_Y, self.p_idle_spam_Z) + def spam_probability(self, kind: str, basis: str) -> float: + """ + Failure probability of a state preparation or a measurement in a given basis. + + The four prep/meas keys are named by the basis that fails, not by the Pauli + that is applied: a Z-basis failure is an X flip, an X-basis failure a Z flip. + This is the single place that maps (operation, basis) to a key. + + Args: + kind: 'prep' (state preparation / reset) or 'meas' (measurement) + basis: 'X' or 'Z' + + Raises: + ValueError: if ``kind`` or ``basis`` is not one of the values above. + """ + probabilities = { + ('prep', 'X'): self.p_prep_X, + ('prep', 'Z'): self.p_prep_Z, + ('meas', 'X'): self.p_meas_X, + ('meas', 'Z'): self.p_meas_Z, + } + if (kind, basis) not in probabilities: + raise ValueError( + f"Expected kind in ('prep', 'meas') and basis in ('X', 'Z'), got {kind!r}, {basis!r}" + ) + return probabilities[(kind, basis)] + def to_stim_pauli_channel_2_args(self) -> Tuple[float, ...]: """ Get arguments for Stim's PAULI_CHANNEL_2 instruction. diff --git a/code/qec/precompute_dem.py b/code/qec/precompute_dem.py index 7860f21..2cdf92d 100644 --- a/code/qec/precompute_dem.py +++ b/code/qec/precompute_dem.py @@ -594,10 +594,12 @@ def build_single_p_marginal( "IX", "IY", "IZ", "XI", "XX", "XY", "XZ", "YI", "YX", "YY", "YZ", "ZI", "ZX", "ZY", "ZZ" ]: _nm_cnot[ab] = float(getattr(nm, f"p_cnot_{ab}")) - p_prep_X = float(nm.p_prep_X) - p_prep_Z = float(nm.p_prep_Z) - p_meas_X = float(nm.p_meas_X) - p_meas_Z = float(nm.p_meas_Z) + # Prep/meas failure probabilities and the Pauli that causes the failure, + # indexed by basis code: 0 is the X basis (flipped by Z), 1 the Z basis + # (flipped by X). + p_prep = tuple(float(nm.spam_probability("prep", b)) for b in "XZ") + p_meas = tuple(float(nm.spam_probability("meas", b)) for b in "XZ") + flip_pauli = ("Z", "X") else: spam_error = float(p_scalar) * 2.0 / 3.0 combined_meas_error = 2.0 * spam_error * (1.0 - spam_error) @@ -651,42 +653,20 @@ def build_single_p_marginal( # fault channel, consistent with get_grouped_totals' "X/Z prep # and measurement are separate one-Pauli fault channels" rule # and with linear behaviour under uniform noise upscaling. - prep_basis = int(prep_basis_map[(r, q)]) - if prep_basis == 0: - allowed = (et == "Z") - else: - allowed = (et == "X") - p_err[eidx] = float( - p_prep_X if prep_basis == 0 else p_prep_Z - ) if allowed else 0.0 + b = int(prep_basis_map[(r, q)] != 0) + p_err[eidx] = p_prep[b] if et == flip_pauli[b] else 0.0 elif is_ancilla_meas: - meas_basis = int(meas_basis_map[(r, q)]) - if meas_basis == 0: - allowed = (et == "Z") - else: - allowed = (et == "X") - p_err[eidx] = float( - p_meas_X if meas_basis == 0 else p_meas_Z - ) if allowed else 0.0 + b = int(meas_basis_map[(r, q)] != 0) + p_err[eidx] = p_meas[b] if et == flip_pauli[b] else 0.0 elif is_data_prep: - prep_basis = int(prep_basis_map[(r, q)]) - if prep_basis == 0: - allowed = (et == "Z") - else: - allowed = (et == "X") - if is_final_round: - # The circuit represents noisy data readout as a fake - # data-measurement flip injected at the start of the final - # perfect round, at the *measurement* rates: Z_ERROR(p_meas_X) - # for X-basis readout, X_ERROR(p_meas_Z) for Z-basis (see - # MemoryCircuit's logical_measurement injection). - p_err[eidx] = float( - p_meas_X if prep_basis == 0 else p_meas_Z - ) if allowed else 0.0 - else: - p_err[eidx] = float( - p_prep_X if prep_basis == 0 else p_prep_Z - ) if allowed else 0.0 + # The circuit represents noisy data readout as a fake + # data-measurement flip injected at the start of the final + # perfect round, at the *measurement* rates: Z_ERROR(p_meas_X) + # for X-basis readout, X_ERROR(p_meas_Z) for Z-basis (see + # MemoryCircuit's logical_measurement injection). + b = int(prep_basis_map[(r, q)] != 0) + p_spam = p_meas if is_final_round else p_prep + p_err[eidx] = p_spam[b] if et == flip_pauli[b] else 0.0 elif is_data_spam_idle: if is_final_round: p_err[eidx] = 0.0 @@ -1574,10 +1554,10 @@ def build_single_p_marginal_color_code( "ZZ" ] } - p_prep_X_nm = float(nm.p_prep_X) - p_prep_Z_nm = float(nm.p_prep_Z) - p_meas_X_nm = float(nm.p_meas_X) - p_meas_Z_nm = float(nm.p_meas_Z) + # Prep/meas failure probabilities indexed by basis code: 0 is the X basis + # (Z fault), 1 the Z basis (X fault). + p_prep_nm = tuple(float(nm.spam_probability("prep", b)) for b in "XZ") + p_meas_nm = tuple(float(nm.spam_probability("meas", b)) for b in "XZ") else: p_scalar = float(p_scalar) spam_error = p_scalar * 2.0 / 3.0 @@ -1651,23 +1631,15 @@ def build_single_p_marginal_color_code( if len(err_type) == 2: p_err[int(eidx)] = nm_cnot.get(err_type, 0.0) elif len(err_type) == 1: - # Basis-labeled keys, as in the surface-code path and in the - # color MemoryCircuit: basis 0 is X (Z fault, p_*_X), basis 1 is - # Z (X fault, p_*_Z). if is_ancilla_prep: - prep_basis = int(prep_basis_map[(round_idx, q)]) - p_err[int(eidx)] = p_prep_X_nm if prep_basis == 0 else p_prep_Z_nm + p_err[int(eidx)] = p_prep_nm[int(prep_basis_map[(round_idx, q)])] elif is_ancilla_meas: - meas_basis = int(meas_basis_map[(round_idx, q)]) - p_err[int(eidx)] = p_meas_X_nm if meas_basis == 0 else p_meas_Z_nm + p_err[int(eidx)] = p_meas_nm[int(meas_basis_map[(round_idx, q)])] elif is_data_prep: - prep_basis = int(prep_basis_map[(round_idx, q)]) - if is_final_round: - # The final perfect round models noisy data readout with a - # fake Pauli error at tt=0 (see MemoryCircuit). - p_err[int(eidx)] = (p_meas_X_nm if prep_basis == 0 else p_meas_Z_nm) - else: - p_err[int(eidx)] = (p_prep_X_nm if prep_basis == 0 else p_prep_Z_nm) + # The final perfect round models noisy data readout with a + # fake Pauli error at tt=0 (see MemoryCircuit). + p_spam = p_meas_nm if is_final_round else p_prep_nm + p_err[int(eidx)] = p_spam[int(prep_basis_map[(round_idx, q)])] elif is_data_spam_idle: p_err[int(eidx)] = nm_idle_spam.get(err_type, 0.0) else: diff --git a/code/qec/surface_code/memory_circuit.py b/code/qec/surface_code/memory_circuit.py index 7f63216..3fdf6fc 100644 --- a/code/qec/surface_code/memory_circuit.py +++ b/code/qec/surface_code/memory_circuit.py @@ -403,6 +403,27 @@ def add_tick(self): self.circuit += c return c + def _spam_probability(self, kind, basis): + """Failure probability of a 'prep' or 'meas' operation in ``basis`` ('X' or 'Z').""" + if self.noise_model is not None: + return self.noise_model.spam_probability(kind, basis) + return self.spam_error + + def _flip_error(self, qubits, basis, probability): + """ + Instruction that flips a ``basis`` prep/meas outcome on ``qubits``. + + A Z-basis outcome is flipped by X_ERROR, an X-basis outcome by Z_ERROR. + Returns '' when the probability is zero. Does not append to the circuit. + """ + if not probability > 0.: + return '' + c = self.margin + c += ('X_ERROR(%.10f) ' if basis == 'Z' else 'Z_ERROR(%.10f) ') % probability + for q in qubits: + c += '%d ' % q + return c + '\n' + def add_reset(self, qubits, basis='Z'): basis = basis.upper() @@ -415,65 +436,20 @@ def add_reset(self, qubits, basis='Z'): c += '%d ' % q c += '\n' - # Apply preparation errors (basis-labeled semantics): - # - Z-basis prep (|0>): X flips outcome -> use p_prep_Z (Z-basis prep failure) - # - X-basis prep (|+>): Z flips outcome -> use p_prep_X (X-basis prep failure) - if self.noise_model is not None: - # Use explicit X_ERROR/Z_ERROR with the appropriate basis-labeled probability. - if basis == 'Z' and self.noise_model.p_prep_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_prep_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif basis == 'X' and self.noise_model.p_prep_X > 0: - c += self.margin - c += 'Z_ERROR(%.10f) ' % self.noise_model.p_prep_X - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - # Fallback to simple mode - c += self.margin - if basis == 'Z': - c += 'X_ERROR(%.10f) ' % self.spam_error - elif basis == 'X': - c += 'Z_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + # Preparation error: a flip of the freshly prepared state. + c += self._flip_error(qubits, basis, self._spam_probability('prep', basis)) self.circuit += c return c def add_single_error(self, qubits, error_type): - """Add a single-qubit error (X or Z) to specified qubits.""" - # Determine error probability - if self.noise_model is not None: - # Basis-labeled semantics: - # - Applying X corresponds to Z-basis prep failure -> p_prep_Z - # - Applying Z corresponds to X-basis prep failure -> p_prep_X - if error_type == 'X': - error_prob = self.noise_model.p_prep_Z - elif error_type == 'Z': - error_prob = self.noise_model.p_prep_X - else: - error_prob = 0. - else: - error_prob = self.spam_error - - if error_prob == 0.: + """Add a prep-like single-qubit error (X or Z) to specified qubits.""" + # An X error is a Z-basis prep failure, a Z error an X-basis prep failure. + basis = {'X': 'Z', 'Z': 'X'}.get(error_type) + if basis is None: return '' - c = self.margin - if error_type == 'X': - c += 'X_ERROR(%.10f) ' % error_prob - elif error_type == 'Z': - c += 'Z_ERROR(%.10f) ' % error_prob - for q in qubits: - c += '%d ' % q - c += '\n' - + c = self._flip_error(qubits, basis, self._spam_probability('prep', basis)) self.circuit += c return c @@ -640,23 +616,8 @@ def add_measure_reset(self, qubits, error_free_reset=False): When NoiseModel is set, uses explicit measurement and prep error probabilities. """ - c = '' - # Measurement error (before measurement) - if self.noise_model is not None: - # Z-basis measurement failure is modeled as a pre-measurement X flip. - if self.noise_model.p_meas_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + c = self._flip_error(qubits, 'Z', self._spam_probability('meas', 'Z')) c += self.margin c += 'MR ' # Measure and reset to |0> @@ -666,20 +627,7 @@ def add_measure_reset(self, qubits, error_free_reset=False): # Reset error (after reset, if not error-free) if not error_free_reset: - if self.noise_model is not None: - # Z-basis reset failure is modeled as a post-reset X flip. - if self.noise_model.p_prep_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_prep_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + c += self._flip_error(qubits, 'Z', self._spam_probability('prep', 'Z')) self.circuit += c return c @@ -707,34 +655,8 @@ def add_measure(self, qubits, basis='Z', include_reset=False): """ basis = basis.upper() - c = '' - # Apply measurement errors (before measurement) - if self.noise_model is not None: - # Basis-labeled semantics: - # - Z-basis measurement failure -> apply X with prob p_meas_Z - # - X-basis measurement failure -> apply Z with prob p_meas_X - if basis == 'Z' and self.noise_model.p_meas_Z > 0: - c += self.margin - c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_Z - for q in qubits: - c += '%d ' % q - c += '\n' - elif basis == 'X' and self.noise_model.p_meas_X > 0: - c += self.margin - c += 'Z_ERROR(%.10f) ' % self.noise_model.p_meas_X - for q in qubits: - c += '%d ' % q - c += '\n' - elif self.spam_error > 0.: - # Fallback to simple mode - c += self.margin - if basis == 'Z': - c += 'X_ERROR(%.10f) ' % self.spam_error - elif basis == 'X': - c += 'Z_ERROR(%.10f) ' % self.spam_error - for q in qubits: - c += '%d ' % q - c += '\n' + # Measurement error (before measurement) + c = self._flip_error(qubits, basis, self._spam_probability('meas', basis)) c += self.margin @@ -1144,22 +1066,9 @@ def _add_stabilizer_round( # Basis-labeled semantics: # - X-basis data measurement failure -> apply Z with prob p_meas_X # - Z-basis data measurement failure -> apply X with prob p_meas_Z - if self.basis.upper() == 'X': - p_fake = float(orig_noise_model.p_meas_X) - if p_fake > 0: - c = self.margin + 'Z_ERROR(%.10f) ' % p_fake - for q in self.code.data_qubits: - c += '%d ' % q - c += '\n' - self.circuit += c - else: # 'Z' - p_fake = float(orig_noise_model.p_meas_Z) - if p_fake > 0: - c = self.margin + 'X_ERROR(%.10f) ' % p_fake - for q in self.code.data_qubits: - c += '%d ' % q - c += '\n' - self.circuit += c + data_basis = 'X' if self.basis.upper() == 'X' else 'Z' + p_fake = float(orig_noise_model.spam_probability('meas', data_basis)) + self.circuit += self._flip_error(self.code.data_qubits, data_basis, p_fake) else: if self.noise_model is not None: # NoiseModel semantics (drift/decomposition): IGNORE data-idle during ancilla prep/reset. diff --git a/code/tests/test_noise_model.py b/code/tests/test_noise_model.py index a24a43f..42f6251 100644 --- a/code/tests/test_noise_model.py +++ b/code/tests/test_noise_model.py @@ -442,6 +442,24 @@ def test_no_double_measurement_noise_in_final_data_qubit_readout(self): ) +class TestNoiseModelSpamProbability(unittest.TestCase): + """NoiseModel.spam_probability maps (operation, basis) to the basis-labeled key.""" + + def test_returns_key_named_by_failing_basis(self): + nm = NoiseModel(p_prep_X=0.011, p_prep_Z=0.022, p_meas_X=0.033, p_meas_Z=0.044) + self.assertEqual(nm.spam_probability('prep', 'X'), 0.011) + self.assertEqual(nm.spam_probability('prep', 'Z'), 0.022) + self.assertEqual(nm.spam_probability('meas', 'X'), 0.033) + self.assertEqual(nm.spam_probability('meas', 'Z'), 0.044) + + def test_rejects_unknown_kind_or_basis(self): + nm = NoiseModel(p_prep_X=0.011, p_prep_Z=0.022, p_meas_X=0.033, p_meas_Z=0.044) + for kind, basis in (('reset', 'Z'), ('prep', 'Y'), ('meas', 'z'), ('', '')): + with self.subTest(kind=kind, basis=basis): + with self.assertRaises(ValueError): + nm.spam_probability(kind, basis) + + class TestNoiseModelUpscaling(unittest.TestCase): """Tests for surface-code training noise model upscaling (get_training_upscaled_noise_model)."""