Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions code/qec/color_code/memory_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,18 +552,18 @@ def add_reset(self, qubits, basis='Z'):

# Apply preparation errors
if self.noise_model is not None:
# Use 22-parameter noise model: explicit X_ERROR and Z_ERROR
# For Z-basis prep (|0>): X error flips to |1>
# For X-basis prep (|+>): Z error flips to |->
if basis == 'Z' and self.noise_model.p_prep_X > 0:
# 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_X
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_Z > 0:
elif basis == 'X' and self.noise_model.p_prep_X > 0:
c += self.margin
c += 'Z_ERROR(%.10f) ' % self.noise_model.p_prep_Z
c += 'Z_ERROR(%.10f) ' % self.noise_model.p_prep_X
for q in qubits:
c += '%d ' % q
c += '\n'
Expand All @@ -585,10 +585,13 @@ 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_X
elif error_type == 'Z':
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:
Expand Down Expand Up @@ -776,9 +779,10 @@ def add_measure_reset(self, qubits, error_free_reset=False):

# Measurement error (before measurement)
if self.noise_model is not None:
if self.noise_model.p_meas_X > 0:
# 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_X
c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_Z
for q in qubits:
c += '%d ' % q
c += '\n'
Expand All @@ -798,9 +802,10 @@ 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:
if self.noise_model.p_prep_X > 0:
# 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_X
c += 'X_ERROR(%.10f) ' % self.noise_model.p_prep_Z
for q in qubits:
c += '%d ' % q
c += '\n'
Expand Down Expand Up @@ -831,25 +836,25 @@ def add_measure(self, qubits, basis='Z', include_reset=False):
When NoiseModel is set, uses explicit X_ERROR/Z_ERROR with measurement probabilities.
Otherwise uses spam_error for backwards compatibility.

Convention:
- Z-basis measurement: X error before measurement flips the outcome
- X-basis measurement: Z error before measurement flips the outcome
Convention (basis-labeled, same as the surface-code builder):
- Z-basis measurement: X error before measurement flips the outcome (p_meas_Z)
- X-basis measurement: Z error before measurement flips the outcome (p_meas_X)
"""
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_X > 0:
if basis == 'Z' and self.noise_model.p_meas_Z > 0:
c += self.margin
c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_X
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_Z > 0:
elif basis == 'X' and self.noise_model.p_meas_X > 0:
c += self.margin
c += 'Z_ERROR(%.10f) ' % self.noise_model.p_meas_Z
c += 'Z_ERROR(%.10f) ' % self.noise_model.p_meas_X
for q in qubits:
c += '%d ' % q
c += '\n'
Expand Down Expand Up @@ -1326,16 +1331,18 @@ def _add_stabilizer_round(
if not state_prep:
if logical_measurement and orig_noise_model is not None:
# 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_Z)
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_X)
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:
Expand Down
10 changes: 6 additions & 4 deletions code/qec/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ class NoiseModel:
25-Parameter Noise Model for circuit-level noise simulation.

Attributes:
p_prep_X: X error probability after state preparation
p_prep_Z: Z error probability after state preparation
p_meas_X: X error probability before measurement
p_meas_Z: Z error probability before measurement
p_prep_X: X-basis (|+>) state-preparation failure probability (applied as Z after reset)
p_prep_Z: Z-basis (|0>) state-preparation failure probability (applied as X after reset)
p_meas_X: X-basis measurement failure probability (applied as Z before measurement)
p_meas_Z: Z-basis measurement failure probability (applied as X before measurement)
The four keys above are named by the failing basis, not by the applied
Pauli, in both the surface-code and the color-code circuits.
p_idle_cnot_X/Y/Z: Idle Pauli errors during bulk/CNOT layers (single-qubit Pauli channel)
p_idle_spam_X/Y/Z: Idle Pauli errors on data qubits during ancilla prep/reset window.
NOTE: In noise-model mode we intentionally do NOT apply data-qubit
Expand Down
11 changes: 7 additions & 4 deletions code/qec/precompute_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,20 +1651,23 @@ 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_Z_nm if prep_basis == 0 else p_prep_X_nm
p_err[int(eidx)] = p_prep_X_nm if prep_basis == 0 else p_prep_Z_nm
elif is_ancilla_meas:
meas_basis = int(meas_basis_map[(round_idx, q)])
p_err[int(eidx)] = p_meas_Z_nm if meas_basis == 0 else p_meas_X_nm
p_err[int(eidx)] = p_meas_X_nm if meas_basis == 0 else p_meas_Z_nm
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_Z_nm if prep_basis == 0 else p_meas_X_nm)
p_err[int(eidx)] = (p_meas_X_nm if prep_basis == 0 else p_meas_Z_nm)
else:
p_err[int(eidx)] = (p_prep_Z_nm if prep_basis == 0 else p_prep_X_nm)
p_err[int(eidx)] = (p_prep_X_nm if prep_basis == 0 else p_prep_Z_nm)
elif is_data_spam_idle:
p_err[int(eidx)] = nm_idle_spam.get(err_type, 0.0)
else:
Expand Down
18 changes: 9 additions & 9 deletions code/tests/test_color_augmented_dem_precompute.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ def test_color_precompute_p_vector_uses_25p_values(basis):
"data_prep_probability",
),
[
("X", "Z", 0.0044, "Z", 0.0022),
("Z", "X", 0.0033, "X", 0.0011),
("X", "Z", 0.0033, "Z", 0.0011),
("Z", "X", 0.0044, "X", 0.0022),
],
)
def test_color_single_qubit_noise_probabilities_follow_pauli_labels(
def test_color_single_qubit_noise_probabilities_follow_basis_labels(
basis,
final_error_type,
final_probability,
Expand Down Expand Up @@ -157,10 +157,10 @@ def test_color_single_qubit_noise_probabilities_follow_pauli_labels(
np.testing.assert_allclose(
probabilities[7:11],
[
noise_model.p_prep_Z,
noise_model.p_meas_Z,
noise_model.p_prep_X,
noise_model.p_meas_X,
noise_model.p_prep_Z,
noise_model.p_meas_Z,
],
rtol=0.0,
atol=1e-9,
Expand Down Expand Up @@ -277,8 +277,8 @@ def test_color_noiseless_context_restores_noise_state_normally():
"final_probability",
),
[
("X", "Z_ERROR", "Z", 0.023, 0.047),
("Z", "X_ERROR", "X", 0.019, 0.031),
("X", "Z_ERROR", "Z", 0.019, 0.031),
("Z", "X_ERROR", "X", 0.023, 0.047),
],
)
def test_color_stim_and_dem_data_noise_locations_match(
Expand Down Expand Up @@ -359,10 +359,10 @@ def test_color_stim_and_dem_data_noise_locations_match(
sorted([data_prep_probability, final_probability])
)
assert sorted(stim_xcheck_error_ops) == pytest.approx(
sorted([noise_model.p_prep_Z, noise_model.p_meas_Z] * (n_rounds - 1))
sorted([noise_model.p_prep_X, noise_model.p_meas_X] * (n_rounds - 1))
)
assert sorted(stim_zcheck_error_ops) == pytest.approx(
sorted([noise_model.p_prep_X, noise_model.p_meas_X] * (n_rounds - 1))
sorted([noise_model.p_prep_Z, noise_model.p_meas_Z] * (n_rounds - 1))
)

layout = _extract_color_round_layout(
Expand Down
Loading
Loading