channels
Pauli noise channels and error sampling infrastructure.
Channel dataclass
Channel(probs: ndarray, unique_col_ids: tuple[int, ...])
A probability distribution over error outcomes.
Attributes:
| Name | Type | Description |
|---|---|---|
probs | ndarray | Shape (2^k,) probability array, sums to 1, dtype float64 |
unique_col_ids | tuple[int, ...] | Tuple of column IDs, where each ID corresponds to a bit of the channel. |
num_bits property
num_bits: int
Number of bits in the channel (k where probs has shape 2^k).
ChannelSampler
ChannelSampler(
channel_probs: list[ndarray],
error_transform: ndarray,
seed: int | None = None,
)
Samples from multiple error channels and transforms to a reduced basis.
This class combines multiple error channels (each producing error bits e0, e1, ...) and applies a linear transformation over GF(2) to convert samples from the original "e" basis to a reduced "f" basis using geometric-skip sampling optimized for low-noise regimes.
f_i = error_transform_ij * e_j mod 2
Channels are automatically simplified by: 1. Removing bits e_i that do not affect any f-variable (i.e. all-zero columns in error_transform) 2. Merging channels with identical column signatures, i.e. channels whose corresponding columns in error_transform are identical. 3. Absorbing channels whose signatures are subsets of others, i.e. channels whose corresponding columns in error_transform are a strict subset of another channel's columns.
Example
probs = [error_probs(0.1), error_probs(0.2)] # two 1-bit channels transform = np.array([[1, 1]]) # f0 = e0 XOR e1 sampler = ChannelSampler(probs, transform) samples = sampler.sample(1000) # shape (1000, 1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_probs | list[ndarray] | List of probability arrays. Channel i has shape (2^k_i,) and produces k_i error bits starting at index sum(k_0:k_{i-1}). For example, if channels have shapes [(4,), (2,), (4,)], they produce variables [e0,e1], [e2], [e3,e4]. | required |
error_transform | ndarray | Binary matrix of shape (num_f, num_e) where entry [i, j] = 1 means f_i depends on e_j. For example, if row 0 is [0, 1, 0, 1], then f0 = e1 XOR e3. | required |
seed | int | None | Random seed for sampling. If None, a random seed is generated. | None |
Source code in src/tsim/noise/channels.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
sample
sample(num_samples: int = 1) -> np.ndarray
Sample from all error channels and transform to new error basis.
Uses geometric-skip sampling, optimized for low-noise regimes where P(non-identity) << 1 per channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_samples | int | Number of samples to draw. | 1 |
Returns:
| Type | Description |
|---|---|
ndarray | NumPy array of shape (num_samples, num_f) with uint8 values indicating |
ndarray | which f-variables are set for each sample. |
Source code in src/tsim/noise/channels.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
absorb_subset_channels
absorb_subset_channels(
channels: list[Channel], max_bits: int = 4
) -> list[Channel]
Absorb channels whose signatures are subsets of others.
If channel A's signatures are a strict subset of channel B's signatures, and |B| <= max_bits, then A is absorbed into B.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels | list[Channel] | List of channels | required |
max_bits | int | Maximum number of bits allowed per channel | 4 |
Returns:
| Type | Description |
|---|---|
list[Channel] | List with no channel being a strict subset of another |
Source code in src/tsim/noise/channels.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
correlated_error_probs
correlated_error_probs(
probabilities: list[float],
) -> np.ndarray
Build probability distribution for correlated error chain.
Given conditional probabilities [p1, p2, ..., pk] from a chain of CORRELATED_ERROR(p1) ELSE_CORRELATED_ERROR(p2) ... ELSE_CORRELATED_ERROR(pk), computes the joint probability distribution over 2^k outcomes.
Since errors are mutually exclusive, only outcomes with at most one bit set have non-zero probability: - P(0) = (1-p1)(1-p2)...(1-pk) (no error) - P(2^i) = (1-p1)...(1-p_i) * p_{i+1} (error i+1 occurred)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probabilities | list[float] | List of conditional probabilities [p1, p2, ..., pk] | required |
Returns:
| Type | Description |
|---|---|
ndarray | Array of shape (2^k,) with probabilities for each outcome. |
Source code in src/tsim/noise/channels.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
error_probs
error_probs(p: float) -> np.ndarray
Single-bit error channel. Returns shape (2,).
Source code in src/tsim/noise/channels.py
30 31 32 | |
expand_channel
expand_channel(
channel: Channel, target_col_ids: tuple[int, ...]
) -> Channel
Expand a channel's distribution to a larger signature set.
The channel's existing col_ids must be a strict subset of target_col_ids. Both must be sorted. New bit positions are treated as "don't care" (always 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel | Channel | Channel to expand (must have sorted unique_col_ids) | required |
target_col_ids | tuple[int, ...] | Target signature set (must be sorted superset) | required |
Returns:
| Type | Description |
|---|---|
Channel | New channel with expanded distribution |
Source code in src/tsim/noise/channels.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
merge_identical_channels
merge_identical_channels(
channels: list[Channel],
) -> list[Channel]
Merge all channels with identical signature sets.
Groups channels by their unique_col_ids and convolves all channels in each group into a single channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels | list[Channel] | List of channels | required |
Returns:
| Type | Description |
|---|---|
list[Channel] | List with at most one channel per unique signature set |
Source code in src/tsim/noise/channels.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
normalize_channels
normalize_channels(
channels: list[Channel],
) -> list[Channel]
Normalize channels by sorting unique_col_ids, permuting probs accordingly.
This ensures channels affecting the same set of columns have identical unique_col_ids tuples, enabling merge_identical_channels to group them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels | list[Channel] | List of channels | required |
Returns:
| Type | Description |
|---|---|
list[Channel] | List of channels with sorted unique_col_ids |
Source code in src/tsim/noise/channels.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
pauli_channel_1_probs
pauli_channel_1_probs(
px: float, py: float, pz: float
) -> np.ndarray
Single-qubit Pauli channel. Returns shape (4,).
Order: [I, Z, X, Y] mapped to bits [00, 01, 10, 11].
Source code in src/tsim/noise/channels.py
35 36 37 38 39 40 | |
pauli_channel_2_probs
pauli_channel_2_probs(
pix: float,
piy: float,
piz: float,
pxi: float,
pxx: float,
pxy: float,
pxz: float,
pyi: float,
pyx: float,
pyy: float,
pyz: float,
pzi: float,
pzx: float,
pzy: float,
pzz: float,
) -> np.ndarray
Two-qubit Pauli channel. Returns shape (16,).
Source code in src/tsim/noise/channels.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
reduce_null_bits
reduce_null_bits(
channels: list[Channel], null_col_id: int | None = None
) -> list[Channel]
Remove bits corresponding to the null column (all-zero column).
If a channel has bits mapped to null_col_id (representing an all-zero column in the transform matrix), those bits don't affect any f-variable and can be marginalized out by summing over them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels | list[Channel] | List of channels | required |
null_col_id | int | None | Column ID representing the all-zero column, or None if there is no all-zero column. | None |
Returns:
| Type | Description |
|---|---|
list[Channel] | List of channels with null bits marginalized out. Channels with all |
list[Channel] | null entries are removed entirely (they have no effect on outputs). |
Source code in src/tsim/noise/channels.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
simplify_channels
simplify_channels(
channels: list[Channel],
max_bits: int = 4,
null_col_id: int | None = None,
) -> list[Channel]
Simplify channels by removing null columns, merging identical and absorbing subsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels | list[Channel] | List of channels to simplify | required |
max_bits | int | Maximum number of bits allowed per channel | 4 |
null_col_id | int | None | Column ID representing the all-zero column, or None if there is no all-zero column. | None |
Returns:
| Type | Description |
|---|---|
list[Channel] | Simplified list of channels |
Source code in src/tsim/noise/channels.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
xor_convolve
xor_convolve(
probs_a: ndarray, probs_b: ndarray
) -> np.ndarray
XOR convolution of two probability distributions.
Computes P(A XOR B = o) = sum_{a ^ b = o} P(A=a) * P(B=b)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probs_a | ndarray | Shape (2^k,) probabilities for channel A | required |
probs_b | ndarray | Shape (2^k,) probabilities for channel B (same size as A) | required |
Returns:
| Type | Description |
|---|---|
ndarray | Shape (2^k,) probabilities for the combined channel |
Source code in src/tsim/noise/channels.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |