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
15 changes: 2 additions & 13 deletions src/tsim/statevector/gpu/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import numpy as np

from tsim.noise.channels import ChannelSampler, WordTables
from tsim.statevector.shared import pack_bits

if TYPE_CHECKING:
from tsim.statevector.shots import ShotLayout, ShotPlan
Expand Down Expand Up @@ -221,29 +220,19 @@ def split(
layout: ShotLayout,
plan: ShotPlan,
psm_words: np.ndarray,
observables_from: int | None = None,
) -> tuple[NoiseGroups, NoiseGroups] | None:
"""Return a postselected batch's noise passes ``(screened, deferred)``, or None.

The deferred channels flip none of the bits that the screen, the tapes or
the postselection ``psm_words`` read: the postselected outputs, the keys,
and, with ``observables_from``, the components' outputs from that index on
(the observables, whose expectations the tapes return). They are drawn
apart when they are at least ``DEFER_SHARE`` of the expected faults.
the postselection ``psm_words`` read: the postselected outputs and the keys.
They are drawn apart when they are at least ``DEFER_SHARE`` of the expected faults.
"""
read = np.zeros(layout.num_words, dtype=np.uint64)
read[: layout.output_words] = psm_words | plan.screen
for program in plan.programs:
start = program.key_start
read[start : start + program.key_words] = ~np.uint64(0)
np.bitwise_or.at(read, program.ps_word, program.ps_mask.astype(np.uint64))
if observables_from is not None:
bits = np.zeros(64 * layout.output_words, dtype=bool)
for component in layout.components:
for index in component.output_indices:
if index >= observables_from:
bits[index] = True
read[: layout.output_words] |= pack_bits(bits, layout.output_words)
deferred = deferred_channels(channels, layout.noise, read)
later = tables.subset(deferred)
if later.faults > 0 and later.faults >= DEFER_SHARE * tables.groups.faults:
Expand Down
68 changes: 7 additions & 61 deletions src/tsim/statevector/gpu/tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@
The actions before the first one that reads a lane symbol run alike for every
shot (``shared.common_prefix``): they run once, on the host, and every shot
starts from the amplitudes they leave.

With ``expectations``, the tape also returns, for each observable output, the
expectation of ``(-1)^output`` given the shot's history before the measurement
that sets the observable's last branch bit ``b``:
``(-1)^(output ⊕ b) (w0 - w1) / (w0 + w1)``. A fair coin's is 0 and an output
without branch bits has ``±1``. The expectation ignores postselection that
follows it, so when a postselected output of the component is tested at or
after that measurement, the tape returns the sampled ``(-1)^output`` instead:
the expectation's average over the accepted shots would be biased, the sample's
is not.
"""

from __future__ import annotations
Expand Down Expand Up @@ -220,25 +210,14 @@ def tape_kernel(
layout: ShotLayout,
plan: ShotPlan,
mask: np.ndarray | None,
num_detectors: int,
tb: int,
dialect: Dialect,
) -> TapeKernel:
"""Return the tape kernel's parts for ``plan`` of postselection ``mask``.

The outputs from ``num_detectors`` on are observables, whose expectations
the tapes return in that order.
"""
functions = []
for i, (program, component) in enumerate(
zip(plan.programs, layout.components, strict=True)
):
observables = {
o: index - num_detectors
for o, index in enumerate(component.output_indices)
if index >= num_detectors
}
functions.append(tape_function(program, i, tb, observables, dialect))
"""Return the tape kernel's parts for ``plan`` of postselection ``mask``."""
functions = [
tape_function(program, i, tb, dialect)
for i, program in enumerate(plan.programs)
]
lanes = "t, gmask" if dialect.lane_mask else "t"
count = len(plan.programs)
bits = np.zeros(64 * layout.output_words, dtype=bool)
Expand All @@ -258,7 +237,7 @@ def tape_kernel(
for i in range(count)
),
calls="\n".join(
f" grave = min(grave, component_{i}(w, shot, seed_{i}, {lanes}, ev, deviation));"
f" grave = min(grave, component_{i}(w, shot, seed_{i}, {lanes}, deviation));"
for i in range(count)
),
stores="\n".join(f" row[{w}] = w[{w}];" for w in written),
Expand Down Expand Up @@ -691,7 +670,6 @@ def tape_function(
program: LaneProgram,
index: int,
tb: int,
observables: dict[int, int],
dialect: Dialect,
) -> str:
"""Return ``component_<index>``: one shot's run of a component's tape.
Expand All @@ -700,8 +678,6 @@ def tape_function(
program: The component's lane program.
index: The component's index (names the function).
tb: Log2 of the threads per shot.
observables: Slot of each of the component's observable outputs
(output slot in the program -> column of the expectations).
dialect: The language to write it in.

"""
Expand All @@ -726,16 +702,9 @@ def tape_function(
num_coins = p.coin_pattern.shape[0]
slots = A + (num_coins + 63) // 64
ready = p.ready
# NOTE: an expectation is exact only if no postselected output of the component
# is tested at or after its measurement (other components and direct outputs
# depend on it only through the f-bits, which it is conditioned on).
exact = {o: ready[o] < 0 or not p.pruned[ready[o] :].any() for o in observables}
# NOTE: the measurements whose weights an observable's expectation reads.
watched = {int(ready[o]) for o in observables if ready[o] >= 0 and exact[o]}

mask = "unsigned gmask, " if d.lane_mask else ""
emit(
f"{d.inline} int component_{index}({d.private}u64* words, u64 shot, u64 seed, int t, {mask}{d.private}float* ev, {d.deviation} deviation) {{"
f"{d.inline} int component_{index}({d.private}u64* words, u64 shot, u64 seed, int t, {mask}{d.deviation} deviation) {{"
)
emit(f" u64 sym[{p.lane_words}];")
for w in range(p.key_words):
Expand Down Expand Up @@ -769,8 +738,6 @@ def tape_function(
emit(" if (t == 0) a[0].x = 1.f;")
emit(" int grave = 0x7fffffff;")
emit(" float w0, w1, total, weight, scale, s; int outcome, f;")
for a in sorted(watched):
emit(f" float ev_{a} = 0.f; int b_{a} = 0;")
if not d.goto:
emit(" do {")
if p.prune_first.any():
Expand Down Expand Up @@ -804,10 +771,6 @@ def tape_function(
f" else outcome = draw_one(draw(seed, shot, {slots}, {a}), w0, w1, total);"
)
emit(" weight = outcome ? w1 : w0;")
if a in watched:
emit(
f" ev_{a} = total > {TINY}f ? (w0 - w1) / total : 0.f; b_{a} = outcome;"
)
if kind != MEASURE_DORMANT:
tape.project(a)
bit = int(p.branch[a])
Expand Down Expand Up @@ -843,23 +806,6 @@ def tape_function(
emit(
f" if (grave < {int(ready[o])}) words[{w}] &= ~{d.hex64(1 << int(p.output_bit[o]))};"
)
if observables:
emit(" if (ev != nullptr) {")
for o, column in observables.items():
w = int(p.out_words[p.output_word[o]])
bit = f"(int)((words[{w}] >> {int(p.output_bit[o])}) & 1)"
r = int(ready[o])
if r >= 0 and not p.splits[r]:
# NOTE: a fair coin that no postselected output reads.
value = "0.f"
elif r < 0 or not exact[o]:
value = f"({bit} ? -1.f : 1.f)"
elif int(p.kind[r]) == MEASURE_DORMANT:
value = "0.f"
else:
value = f"((({bit}) ^ b_{r}) ? -ev_{r} : ev_{r})"
emit(f" ev[{column}] = grave < {max(r, 0)} ? 0.f : {value};")
emit(" }")
emit(" return grave;")
emit("}")
if split:
Expand Down
34 changes: 4 additions & 30 deletions src/tsim/statevector/metal/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

``tape`` runs the components' tapes on the queued shots; ``count_bits`` and
``list_bits`` list the accepted shots from the bitmap; ``gather_rows``,
``unpack_rows``, ``select_rows`` and ``gather_float`` copy out rows, booleans,
chosen outputs and expectations;
``unpack_rows`` and ``select_rows`` copy out rows, booleans and chosen outputs;
``reset`` and ``advance`` keep a call's counters on the GPU.
"""

Expand All @@ -21,13 +20,12 @@
TEMPLATE = r"""
struct TapeArgs {{ long first_shot; u64 seed; int flags; }};

// flags: 1 clears rejected shots from ``survivors`` and counts the accepted ones,
// 2 writes the expectations.
// flags: 1 clears rejected shots from ``survivors`` and counts the accepted ones.
kernel void tape(
device u64* out [[buffer(0)]], constant TapeArgs& args [[buffer(1)]],
device const uint* queue [[buffer(2)]], device const uint* count [[buffer(3)]],
device atomic_uint* survivors [[buffer(4)]], device atomic_uint* acc_count [[buffer(5)]],
device float* expectations [[buffer(6)]], device atomic_uint* deviation [[buffer(7)]],
device atomic_uint* deviation [[buffer(6)]],
uint gid [[thread_position_in_grid]], uint lane [[thread_index_in_simdgroup]])
{{
int t = lane & {T1};
Expand All @@ -40,13 +38,10 @@
device u64* row = out + (long)row_index * {NW};
u64 w[{NW}];
for (int i = 0; i < {NW}; ++i) w[i] = row[i];
float ev[{NO}];
int grave = 0x7fffffff;
{calls}
if (t == 0) {{
{stores}
if (args.flags & 2)
for (int i = 0; i < {NO}; ++i) expectations[(long)row_index * {NO} + i] = ev[i];
}}
if (args.flags & 1) {{
u64 hit = {hits};
Expand Down Expand Up @@ -194,25 +189,6 @@
dst[i] = uchar(v);
}}
}}

// Observable expectations of rows ``index[j]`` (or of row ``j``, without
// ``has_index``) for ``j < *n``, densely, after the ``*cursor`` rows of the
// call's earlier batches.
kernel void gather_float(
device const float* values [[buffer(0)]], device const uint* n [[buffer(1)]],
constant int& width [[buffer(2)]], device const uint* index [[buffer(3)]],
device float* dst [[buffer(4)]], device const uint* cursor [[buffer(5)]],
constant int& has_index [[buffer(6)]],
uint gid [[thread_position_in_grid]], uint grid [[threads_per_grid]])
{{
long total = min((long)*n, (long)cursor[1] - (long)cursor[0]) * width;
dst += (long)*cursor * width;
for (long k = gid; k < total; k += grid) {{
long j = k / width; int c = (int)(k - j * width);
long r = has_index ? (long)index[j] : j;
dst[k] = values[r * width + c];
}}
}}
"""


Expand All @@ -221,7 +197,6 @@ def source(
tables: NoiseTables,
parts: TapeKernel,
tb: int,
num_observables: int,
) -> str:
"""Return the MSL source of every kernel, for tapes of ``2^tb`` threads a shot."""
nb = -(-layout.num_outputs // 8)
Expand All @@ -234,7 +209,6 @@ def source(
tb=tb,
NW=layout.num_words,
SCAN=SCAN_THREADS,
NO=num_observables,
NB=max(nb, 1),
RB=8 * layout.num_words,
NOUT=max(layout.num_outputs, 1),
Expand All @@ -250,5 +224,5 @@ def source(
def names(shared_words: bool) -> list[str]:
"""Return the kernels of a source; ``noise_kept`` needs ``shared_words``."""
found = ["noise", "tape", "count_bits", "list_bits", "gather_rows"]
found += ["gather_float", "reset", "advance", "unpack_rows", "select_rows"]
found += ["reset", "advance", "unpack_rows", "select_rows"]
return [*found, "noise_kept"] if shared_words else found
Loading
Loading