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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
in `CS 0 0`, raise an error naming the gate instead of its decomposition.

### Changed
- `strategy="active_statevector"` may run a component's measurements later
where that frees no axis that a later rotation reuses and leaves more of the
tape before its first shot-dependent action, which a GPU backend runs once for
all shots. The samples follow the same distribution, but some circuits' seeds
give other samples than in 1.0.
- `ChannelSampler.signature_matrix` is now a `tsim.utils.linalg.SparseGF2Matrix`
instead of a dense uint8 array; `np.asarray` gives the dense matrix.

Expand Down
14 changes: 14 additions & 0 deletions src/tsim/statevector/gpu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""What the statevector executor's GPU backends share.

A GPU backend runs the noise sampler and every component's tape as kernels of
its own language. The host side is common to them: the noise tables that the
noise kernels read (``noise``), the tape generator, which writes a component's
tape in any of their languages (``tape``), and the selection of outputs from
the rows that the kernels return (``rows``).
"""

FORKED = (
"This process was forked after its parent used the GPU, so it samples on the "
"CPU; start it with the 'spawn' or 'forkserver' method to sample on the GPU."
)
"""The warning of a GPU sampler in a process forked after its parent used the GPU."""
386 changes: 386 additions & 0 deletions src/tsim/statevector/gpu/noise.py

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/tsim/statevector/gpu/rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""The rows of outputs that the GPU backends return."""

from __future__ import annotations

import numpy as np


def select(
rows: np.ndarray, columns: np.ndarray, num_outputs: int, bit_packed: bool
) -> np.ndarray:
"""Return the outputs ``columns`` of rows of every output, as ``read_outputs`` does.

The rows are bit-packed as ``np.packbits`` does, with zero bits past the
outputs; leading outputs in order are a view of them, or of whole bytes.
"""
count = len(columns)
if bit_packed and np.array_equal(columns, np.arange(count)):
packed = rows[:, : -(-count // 8)]
if count == num_outputs or count % 8 == 0:
return packed
packed = packed.copy()
packed[:, -1] &= np.uint8((1 << (count % 8)) - 1)
return packed
bits = np.unpackbits(rows, axis=1, count=num_outputs, bitorder="little")
bits = bits.view(np.bool_)[:, columns]
return np.packbits(bits, axis=1, bitorder="little") if bit_packed else bits
Loading
Loading