channels
Pauli noise channels and error sampling infrastructure.
Channel dataclass
Channel(probs: ndarray, unique_col_ids: tuple[int, ...])
A probability distribution over error outcomes.
Outcome indices: bit position i corresponds to 1 << i in probs. For example, in a 2-bit channel, index 1 (0b01) is bit pattern bit0=1, bit1=0 and index 2 (0b10) is bit0=0, bit1=1.
Attributes:
| Name | Type | Description |
|---|---|---|
probs | ndarray | Shape |
unique_col_ids | tuple[int, ...] | Tuple of column IDs. Entry |
num_bits property
num_bits: int
Number of bits in the channel (k where probs has shape 2^k).
__post_init__
__post_init__() -> None
Validate channel probabilities.
Source code in src/tsim/noise/channels.py
30 31 32 33 34 35 36 37 38 39 | |
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. Within each channel, probability-array bit 0 corresponds to the first produced error bit, bit 1 to the second, and so on.
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. Folding duplicate column IDs, i.e. channels whose column signatures contain duplicate IDs. 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
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | |
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
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | |
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
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
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. - probs[0] is the probability that no branch fires. - probs[1 << i] is the probability that branch i fires after all previous branches did not fire.
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
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 | |
error_probs
error_probs(p: float) -> np.ndarray
Single-bit error channel.
Returns [P(bit0=0), P(bit0=1)].
Source code in src/tsim/noise/channels.py
47 48 49 50 51 52 | |
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 column IDs must be a strict subset of target_col_ids when considered as sets, and both tuples must be sorted. New target bit positions are treated as always zero.
Duplicate source column IDs are allowed. When multiple source bits map to the same target bit, their contribution is XORed, matching GF(2) composition. Duplicate target column IDs are not allowed; channels with duplicate IDs should be canonicalized before subset absorption.
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
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
fold_duplicate_channel_bits
fold_duplicate_channel_bits(
channels: list[Channel],
) -> list[Channel]
Canonicalize channels by XOR-folding duplicate column IDs.
If two bits in the same channel have identical column signatures, sampling both bits only affects the reduced error basis through their parity. This replaces those duplicate bits with one bit whose probability is the sum of all old outcomes with the same XOR-folded value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels | list[Channel] | List of channels with sorted unique_col_ids | required |
Returns:
| Type | Description |
|---|---|
list[Channel] | List of channels whose unique_col_ids contain no duplicates |
Source code in src/tsim/noise/channels.py
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 | |
heralded_pauli_channel_1_probs
heralded_pauli_channel_1_probs(
pi: float, px: float, py: float, pz: float
) -> np.ndarray
Heralded single-qubit Pauli channel. Returns shape (8,).
Bit layout: - bit 0: herald bit, written to the measurement record - bit 1: Z error component - bit 2: X error component
The non-zero outcomes are: - index 0 (0b000): no herald, no Pauli error - index 1 (0b001): herald + I - index 3 (0b011): herald + Z - index 5 (0b101): herald + X - index 7 (0b111): herald + Y, represented as X+Z
Source code in src/tsim/noise/channels.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
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
398 399 400 401 402 403 404 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 | |
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. The probability tensor is transposed using the same axis permutation so little-endian outcome bits continue to refer to the matching column IDs.
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
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 308 | |
pauli_channel_1_probs
pauli_channel_1_probs(
px: float, py: float, pz: float
) -> np.ndarray
Single-qubit Pauli channel. Returns shape (4,).
Bit layout: - bit 0: Z error component - bit 1: X error component
The outcomes are: - index 0 (0b00): I - index 1 (0b01): Z - index 2 (0b10): X - index 3 (0b11): Y
Source code in src/tsim/noise/channels.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
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,).
Bit layout: - bit 0: Z error component on qubit_i - bit 1: X error component on qubit_i - bit 2: Z error component on qubit_j - bit 3: X error component on qubit_j
With that layout, index z_i + 2*x_i + 4*z_j + 8*x_j stores the probability for the corresponding two-qubit Pauli outcome. The arguments follow Stim's naming convention: pix is I on qubit_i and X on qubit_j, pzi is Z on qubit_i and I on qubit_j, etc.
Source code in src/tsim/noise/channels.py
97 98 99 100 101 102 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 132 133 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 161 162 163 164 165 166 | |
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
230 231 232 233 234 235 236 237 238 239 240 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 274 275 276 277 | |
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, folding, 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
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
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
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |