Skip to content
Merged
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
170 changes: 37 additions & 133 deletions code/qec/color_code/memory_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

Expand Down Expand Up @@ -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>
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand Down
27 changes: 27 additions & 0 deletions code/qec/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
84 changes: 28 additions & 56 deletions code/qec/precompute_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading
Loading