Skip to content
Draft
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
173 changes: 42 additions & 131 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,62 +571,20 @@ def add_reset(self, qubits, basis='Z'):
c += '%d ' % q
c += '\n'

# 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:
c += self.margin
c += 'X_ERROR(%.10f) ' % self.noise_model.p_prep_X
for q in qubits:
c += '%d ' % q
c += '\n'
elif basis == 'X' and self.noise_model.p_prep_Z > 0:
c += self.margin
c += 'Z_ERROR(%.10f) ' % self.noise_model.p_prep_Z
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:
if error_type == 'X':
error_prob = self.noise_model.p_prep_X
elif error_type == 'Z':
error_prob = self.noise_model.p_prep_Z
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 @@ -772,22 +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:
if self.noise_model.p_meas_X > 0:
c += self.margin
c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_X
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 @@ -797,19 +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:
if self.noise_model.p_prep_X > 0:
c += self.margin
c += 'X_ERROR(%.10f) ' % self.noise_model.p_prep_X
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 All @@ -831,38 +784,14 @@ 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:
c += self.margin
c += 'X_ERROR(%.10f) ' % self.noise_model.p_meas_X
for q in qubits:
c += '%d ' % q
c += '\n'
elif basis == 'X' and self.noise_model.p_meas_Z > 0:
c += self.margin
c += 'Z_ERROR(%.10f) ' % self.noise_model.p_meas_Z
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 @@ -1326,22 +1255,11 @@ 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.
if self.basis.upper() == 'X':
p_fake = float(orig_noise_model.p_meas_Z)
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)
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
# - X-basis data measurement failure -> apply Z with prob p_meas_X
# - Z-basis data measurement failure -> apply X with prob p_meas_Z
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 @@ -1356,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
37 changes: 33 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 Expand Up @@ -606,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
81 changes: 28 additions & 53 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 @@ -1652,19 +1632,14 @@ def build_single_p_marginal_color_code(
p_err[int(eidx)] = nm_cnot.get(err_type, 0.0)
elif len(err_type) == 1:
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_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_Z_nm if meas_basis == 0 else p_meas_X_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_Z_nm if prep_basis == 0 else p_meas_X_nm)
else:
p_err[int(eidx)] = (p_prep_Z_nm if prep_basis == 0 else p_prep_X_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