From fd01f8971646e0adf0af1d16473e23b0bcdc2d1f Mon Sep 17 00:00:00 2001 From: Rafael Haenel Date: Fri, 13 Nov 2026 12:57:42 -0700 Subject: [PATCH] refactor(gpu): drop the unused observable expectations Nothing in Tsim asks the Metal sampler for observable expectations: they served only tests. Remove them from the shared tape and noise split and from the Metal kernels and sampler, and merge sample_accepted into the private _accepted, which always takes columns. _select no longer commits an empty command buffer when it dispatches nothing. Co-Authored-By: Claude Opus 5.5 --- src/tsim/statevector/gpu/noise.py | 15 +- src/tsim/statevector/gpu/tape.py | 68 +------ src/tsim/statevector/metal/kernels.py | 34 +--- src/tsim/statevector/metal/sampler.py | 245 +++++++------------------ test/integration/test_metal_sampler.py | 117 +----------- test/unit/statevector/test_gpu.py | 50 ++--- 6 files changed, 108 insertions(+), 421 deletions(-) diff --git a/src/tsim/statevector/gpu/noise.py b/src/tsim/statevector/gpu/noise.py index 15e8b8d6..f8eafaf5 100644 --- a/src/tsim/statevector/gpu/noise.py +++ b/src/tsim/statevector/gpu/noise.py @@ -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 @@ -221,15 +220,12 @@ 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 @@ -237,13 +233,6 @@ def split( 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: diff --git a/src/tsim/statevector/gpu/tape.py b/src/tsim/statevector/gpu/tape.py index 22e6a97a..fdd91450 100644 --- a/src/tsim/statevector/gpu/tape.py +++ b/src/tsim/statevector/gpu/tape.py @@ -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 @@ -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) @@ -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), @@ -691,7 +670,6 @@ def tape_function( program: LaneProgram, index: int, tb: int, - observables: dict[int, int], dialect: Dialect, ) -> str: """Return ``component_``: one shot's run of a component's tape. @@ -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. """ @@ -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): @@ -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(): @@ -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]) @@ -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: diff --git a/src/tsim/statevector/metal/kernels.py b/src/tsim/statevector/metal/kernels.py index 885dff02..bcc9f947 100644 --- a/src/tsim/statevector/metal/kernels.py +++ b/src/tsim/statevector/metal/kernels.py @@ -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. """ @@ -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}; @@ -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}; @@ -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]; - }} -}} """ @@ -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) @@ -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), @@ -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 diff --git a/src/tsim/statevector/metal/sampler.py b/src/tsim/statevector/metal/sampler.py index eead872d..3b802da6 100644 --- a/src/tsim/statevector/metal/sampler.py +++ b/src/tsim/statevector/metal/sampler.py @@ -31,7 +31,7 @@ from __future__ import annotations import threading -from dataclasses import dataclass, field +from dataclasses import dataclass from typing import Any import numpy as np @@ -103,7 +103,8 @@ class _Module: psm_words: np.ndarray passing: float """Estimated share of shots that pass the screen.""" - splits: dict[bool, tuple[_Launch, _Launch] | None] = field(default_factory=dict) + split: tuple[_Launch, _Launch] | None + """A postselected batch's noise passes (screened, deferred), or None.""" @dataclass @@ -122,7 +123,6 @@ class _Slot: counts: runtime.Buffer totals: runtime.Buffer index: runtime.Buffer - values: runtime.Buffer | None = None @dataclass @@ -133,11 +133,8 @@ class _Call: slot: _Slot constant: runtime.Buffer split: tuple[_Launch, _Launch] | None - expectations: bool rows: runtime.Buffer """The accepted rows, bit-packed.""" - values: runtime.Buffer | None - """The accepted rows' expectations, with ``expectations``.""" class MetalShotSampler: @@ -145,8 +142,7 @@ class MetalShotSampler: It keeps ``ShotSampler``'s interface (``sample``, ``direct_bools``, ``reference``, ``plan``, ``layout``); ``sample`` returns only the shots that - pass postselection. ``sample_accepted`` can also return each observable's - expectation. + pass postselection. """ supports = staticmethod(tape.supports) @@ -179,8 +175,6 @@ def __init__( self._lock = threading.RLock() self._host = host layout = host.layout - self._num_detectors = num_detectors - self._observables = np.arange(num_detectors, layout.num_outputs) self._rng = np.random.default_rng(seed) self._channels = channels self._noise = noise_tables(channels, layout.noise, layout.num_words) @@ -188,8 +182,8 @@ def __init__( self._all = self._launch(self._noise.groups) self._modules: dict[bytes, _Module] = {} self._constants: dict[bytes, runtime.Buffer] = {} - self._slots: dict[tuple[int, bool], _Slot] = {} - self._shares: dict[tuple, float] = {} + self._slots: dict[int, _Slot] = {} + self._shares: dict[ShotPlan, float] = {} self._cpu_only = False @property @@ -233,10 +227,8 @@ def _module(self, mask: np.ndarray | None) -> _Module: plan = self.plan(mask) layout = self.layout tb = _threads_per_shot(layout) - parts = tape.tape_kernel(layout, plan, mask, self._num_detectors, tb, METAL) - source = kernels.source( - layout, self._noise, parts, tb, max(1, len(self._observables)) - ) + parts = tape.tape_kernel(layout, plan, mask, tb, METAL) + source = kernels.source(layout, self._noise, parts, tb) library = runtime.Library(source) for name in kernels.names(self._noise.shared_words): library.pipeline(name) @@ -248,6 +240,7 @@ def _module(self, mask: np.ndarray | None) -> _Module: plan=plan, psm_words=parts.psm_words, passing=screen_pass_rate(self._channels, layout, plan), + split=self._split(plan, parts.psm_words, mask is not None), ) self._modules[key] = compiled return compiled @@ -262,31 +255,19 @@ def _launch(self, groups: NoiseGroups) -> _Launch: ) def _split( - self, module: _Module, expectations: bool + self, plan: ShotPlan, psm_words: np.ndarray, has_mask: bool ) -> tuple[_Launch, _Launch] | None: """Return a postselected batch's noise passes (screened, deferred), or None. - As ``gpu.noise.split``; with ``expectations`` the observables' bits are - read too. + As ``gpu.noise.split``. """ # NOTE: rows too wide for threadgroup memory draw all their noise at once. - if not module.has_mask or not self._noise.shared_words: + if not has_mask or not self._noise.shared_words: return None - if expectations not in module.splits: - found = split( - self._noise, - self._channels, - self.layout, - module.plan, - module.psm_words, - self._num_detectors if expectations else None, - ) - module.splits[expectations] = ( - None - if found is None - else (self._launch(found[0]), self._launch(found[1])) - ) - return module.splits[expectations] + found = split(self._noise, self._channels, self.layout, plan, psm_words) + if found is None: + return None + return self._launch(found[0]), self._launch(found[1]) # -- execution -------------------------------------------------------------- @@ -304,12 +285,11 @@ def _batch_shots(self, shots: int, batch_size: int | None) -> int: cap = max(1 << 10, BATCH_BYTES // (8 * self.layout.num_words)) return max(1, min(batch_size or BATCH_SHOTS, BATCH_SHOTS, cap, shots)) - def _slot(self, shots: int, expectations: bool) -> _Slot: + def _slot(self, shots: int) -> _Slot: """Return the buffers for batches of up to ``shots`` shots.""" - key = (shots, expectations) - slot = self._slots.get(key) + slot = self._slots.get(shots) if slot is None: - nw, no = self.layout.num_words, max(1, len(self._observables)) + nw = self.layout.num_words slot = _Slot( words=runtime.buffer(shots * nw, np.uint64), queue=runtime.buffer(shots, np.uint32), @@ -318,10 +298,7 @@ def _slot(self, shots: int, expectations: bool) -> _Slot: totals=runtime.buffer(-(-shots // 32 // SCAN_THREADS) + 1, np.uint32), index=runtime.buffer(shots, np.uint32), ) - if expectations: - slot.values = runtime.buffer(shots * no, np.float32) - self._slots = {k: v for k, v in self._slots.items() if k[1] != expectations} - self._slots[key] = slot + self._slots = {shots: slot} return slot def _encode_batch( @@ -332,7 +309,6 @@ def _encode_batch( Only the shots that pass the screen get their words written. """ module, slot, constant = call.module, call.slot, call.constant - expectations = call.expectations launch = call.split[0] if call.split else self._all library = module.library words = slot.words @@ -368,9 +344,7 @@ def _encode_batch( ) if self.layout.components: tape_args = np.zeros(1, dtype=_TAPE_ARGS) - flags = int(module.has_mask) | (2 if expectations else 0) - tape_args[0] = (first_shot, seed, flags, 0) - values = slot.values if slot.values is not None else slot.counts + tape_args[0] = (first_shot, seed, int(module.has_mask), 0) threads = shots << module.tb encoder.dispatch( library.pipeline("tape"), @@ -381,7 +355,6 @@ def _encode_batch( _count(slot, _SURVIVORS), slot.accepted, _count(slot, _ACCEPTED), - values, _count(slot, _DEVIATION), ], -(-threads // TAPE_THREADS), @@ -397,16 +370,14 @@ def _encode_pack( ) -> None: """Encode the listing and packing of a batch's accepted rows into ``call.rows``. - They follow the rows of the call's earlier batches (at the cursor), and - their expectations go to ``call.values``; ``fixed`` holds the batch's - shots, which a call without a mask returns. + They follow the rows of the call's earlier batches (at the cursor); + ``fixed`` holds the batch's shots, which a call without a mask returns. """ module, slot, split = call.module, call.slot, call.split - expectations, rows, values = call.expectations, call.rows, call.values + rows = call.rows library = module.library S = self._noise.block_shots nb = -(-self.layout.num_outputs // 8) - no = len(self._observables) if module.has_mask: words = -(-shots // 32) blocks = -(-words // SCAN_THREADS) @@ -468,22 +439,6 @@ def _encode_pack( -(-threads // COPY_THREADS), COPY_THREADS, ) - if expectations and no and self.layout.components: - assert slot.values is not None and values is not None - encoder.dispatch( - library.pipeline("gather_float"), - [ - slot.values, - count, - np.int32(no), - slot.index, - values, - cursor, - np.int32(module.has_mask), - ], - COPY_THREADS, - COPY_THREADS, - ) encoder.dispatch(library.pipeline("advance"), [cursor, count], 1, 1) def sample( @@ -504,20 +459,15 @@ def sample( forked after its parent used the GPU, the call samples on the CPU, or raises if the sampler is ``required`` on the GPU (``_or_cpu``). """ - - def on_gpu(): - outputs, _, deviation = self.sample_accepted( + return self._or_cpu( + lambda: self._accepted( shots, + columns, mask=mask, flip=flip, batch_size=batch_size, bools=not bit_packed, - columns=columns, - ) - return outputs, deviation - - return self._or_cpu( - on_gpu, + ), lambda: self._host.sample( shots, columns, @@ -560,7 +510,7 @@ def _or_cpu(self, on_gpu, on_cpu): self._cpu_only = True return on_cpu() - def _capacity(self, key: tuple, module: _Module, shots: int) -> int: + def _capacity(self, key: ShotPlan, module: _Module, shots: int) -> int: """Return the rows to make room for: the expected accepted shots and a margin. The share comes from the previous call of ``key``, or else the screen's @@ -571,7 +521,7 @@ def _capacity(self, key: tuple, module: _Module, shots: int) -> int: return min(shots, int(1.05 * expected + 8 * np.sqrt(expected) + 4096)) def _run_accepted(self, call: _Call, shots: int, batch: int, capacity: int) -> None: - """Run a call's batches into ``call.rows`` (and ``values``) of ``capacity`` rows.""" + """Run a call's batches into ``call.rows`` of ``capacity`` rows.""" slot = call.slot starts = range(0, shots, batch) fixed = runtime.buffer(len(starts), np.uint32) @@ -622,35 +572,27 @@ def _settle(self, commands: list[Any]) -> None: except KeyboardInterrupt: continue - def sample_accepted( + def _accepted( self, shots: int, + columns: list[np.ndarray], *, mask: np.ndarray | None = None, flip: np.ndarray | None = None, - expectations: bool = False, batch_size: int | None = None, bools: bool = False, - columns: list[np.ndarray] | None = None, - ) -> tuple[Any, np.ndarray | None, float]: + ) -> tuple[list[np.ndarray], float]: """Sample ``shots`` shots; return only those that pass postselection. A shot passes when none of the ``mask``'s detectors is set (every shot - passes without a mask). Rows keep their shots' order and hold every output; - with ``bools``, one boolean per output, unpacked on the GPU. With - ``columns``, the outputs are a list of arrays instead, one per entry: its - outputs, in its order, selected on the GPU. + passes without a mask). Rows keep their shots' order. Each entry of + ``columns`` is an array of its outputs, in its order, selected on the + GPU: bit-packed as ``np.packbits(..., bitorder="little")`` would + (``uint8``, in shared memory), or with ``bools`` one boolean per output. Returns: - ``(outputs, expectations, deviation)``: the passing shots' outputs, - bit-packed as ``np.packbits(..., bitorder="little")`` would (``uint8``, - in shared memory), as booleans, or with ``columns`` a list of such - arrays; with ``expectations`` each observable's - expectation of ``(-1)^observable`` given the shot's history before - the measurement that settles it, or its sampled ``±1`` when a - postselected output of its component is tested at or after that - measurement (else None), and the largest normalization deviation - beyond float32 rounding. + ``(outputs, deviation)``: the passing shots' arrays and the largest + normalization deviation beyond float32 rounding. Raises: MemoryError: If the GPU runs out of memory. @@ -658,68 +600,46 @@ def sample_accepted( """ if shots > MAX_CALL_SHOTS: parts = [ - self.sample_accepted( + self._accepted( min(MAX_CALL_SHOTS, shots - start), + columns, mask=mask, flip=flip, - expectations=expectations, batch_size=batch_size, bools=bools, - columns=columns, ) for start in range(0, shots, MAX_CALL_SHOTS) ] - found = [p[1] for p in parts if p[1] is not None] - values = np.concatenate(found) if expectations else None - if columns is None: - outputs = np.concatenate([p[0] for p in parts]) - else: - outputs = [ - np.concatenate(cut) - for cut in zip(*(p[0] for p in parts), strict=True) - ] - return outputs, values, max(p[2] for p in parts) + outputs = [ + np.concatenate(cut) for cut in zip(*(p[0] for p in parts), strict=True) + ] + return outputs, max(p[1] for p in parts) with self._lock: return self._sample_call( - shots, - mask=mask, - flip=flip, - expectations=expectations, - batch_size=batch_size, - bools=bools, - columns=columns, + shots, columns, mask=mask, flip=flip, batch_size=batch_size, bools=bools ) def _sample_call( self, shots: int, + columns: list[np.ndarray], *, - mask: np.ndarray | None = None, - flip: np.ndarray | None = None, - expectations: bool = False, - batch_size: int | None = None, - bools: bool = False, - columns: list[np.ndarray] | None = None, - ) -> tuple[Any, np.ndarray | None, float]: - """One call of ``sample_accepted``, of at most ``MAX_CALL_SHOTS`` shots.""" + mask: np.ndarray | None, + flip: np.ndarray | None, + batch_size: int | None, + bools: bool, + ) -> tuple[list[np.ndarray], float]: + """One call of ``_accepted``, of at most ``MAX_CALL_SHOTS`` shots.""" module = self._module(mask) - nb, no = -(-self.layout.num_outputs // 8), len(self._observables) + nb = -(-self.layout.num_outputs // 8) if not shots: - values = np.zeros((0, no), np.float32) if expectations else None - if columns is not None: - if bools: - empty = [np.zeros((0, len(c)), bool) for c in columns] - else: - empty = [np.zeros((0, -(-len(c) // 8)), np.uint8) for c in columns] - return empty, values, 0.0 if bools: - return np.zeros((0, self.layout.num_outputs), bool), values, 0.0 - return np.zeros((0, nb), np.uint8), values, 0.0 + return [np.zeros((0, len(c)), bool) for c in columns], 0.0 + return [np.zeros((0, -(-len(c) // 8)), np.uint8) for c in columns], 0.0 constant = self._constant(flip) - split = self._split(module, expectations) batch = self._batch_shots(shots, batch_size) - slot = self._slot(batch, expectations) - key = (module.plan, expectations) + slot = self._slot(batch) + key = module.plan state = self._rng.bit_generator.state capacity = self._capacity(key, module, shots) while True: @@ -727,12 +647,7 @@ def _sample_call( # output has room for the expected rows only; a call that overflows # it runs again, on the same streams, with room for every shot. rows = runtime.buffer(capacity * nb, np.uint8, recycle=True) - values = ( - runtime.buffer(capacity * no, np.float32, recycle=True) - if expectations - else None - ) - call = _Call(module, slot, constant, split, expectations, rows, values) + call = _Call(module, slot, constant, module.split, rows) self._run_accepted(call, shots, batch, capacity) total = int(slot.counts.array[_CURSOR]) if total <= capacity: @@ -742,19 +657,7 @@ def _sample_call( self._shares[key] = total / max(shots, 1) # NOTE: fast math records a NaN total as NaN (not inf), which must warn. deviation = tape.deviation(slot.counts.array[_DEVIATION]) - packed = rows.array[: total * nb].reshape(total, nb) - expected = None - if values is not None: - expected = values.array[: total * no].reshape(total, no) - if not self.layout.components: - expected[:] = 0 - self._direct_expectations(packed, expected) - if columns is not None: - selected = self._select(module, rows, total, columns, bools) - return selected, expected, deviation - if bools: - return self._unpack(module, rows, total), expected, deviation - return packed, expected, deviation + return self._select(module, rows, total, columns, bools), deviation def _unpack(self, module: _Module, rows: runtime.Buffer, total: int) -> np.ndarray: """Return the first ``total`` packed ``rows`` as booleans, unpacked on the GPU.""" @@ -794,7 +697,8 @@ def _select( num_outputs = self.layout.num_outputs nb = -(-num_outputs // 8) packed = rows.array[: total * nb].reshape(total, nb) - selected, encoder, dispatched = [], runtime.Encoder(), False + selected: list[np.ndarray] = [] + encoder = None for chosen in columns: count = len(chosen) leading = np.array_equal(chosen, np.arange(count)) @@ -813,31 +717,19 @@ def _select( if total * width: threads = min(total * width, MAX_COPY_THREADS) arguments = np.array([total, count, width, int(bools)], np.uint32) + encoder = encoder or runtime.Encoder() encoder.dispatch( module.library.pipeline("select_rows"), [rows, out, runtime.upload(chosen.astype(np.uint32)), arguments], -(-threads // COPY_THREADS), COPY_THREADS, ) - dispatched = True array = out.array[: total * width].reshape(total, width) selected.append(array.view(bool) if bools else array) - commands = encoder.commit() - if dispatched: - self._wait_all([commands]) + if encoder is not None: + self._wait_all([encoder.commit()]) return selected - def _direct_expectations(self, rows: np.ndarray, values: np.ndarray) -> None: - """Set the expectations of observables outside the components: ``±1``.""" - layout = self.layout - in_components = {i for c in layout.components for i in c.output_indices} - for column, index in enumerate(self._observables): - if int(index) in in_components: - continue - k = int(index) - bit = (rows[:, k >> 3] >> np.uint8(k & 7)) & np.uint8(1) - values[:, column] = 1.0 - 2.0 * bit.astype(np.float32) - def direct_bools( self, shots: int, @@ -845,10 +737,11 @@ def direct_bools( batch_size: int | None = None, ) -> np.ndarray: """Return every output bit of a program without components, as booleans.""" + every = [np.arange(self.layout.num_outputs)] return self._or_cpu( - lambda: self.sample_accepted( - shots, flip=flip, batch_size=batch_size, bools=True - )[0], + lambda: self._accepted( + shots, every, flip=flip, batch_size=batch_size, bools=True + )[0][0], lambda: self._host.direct_bools(shots, flip, batch_size), ) diff --git a/test/integration/test_metal_sampler.py b/test/integration/test_metal_sampler.py index 9627ac1a..e7e9823b 100644 --- a/test/integration/test_metal_sampler.py +++ b/test/integration/test_metal_sampler.py @@ -49,8 +49,7 @@ DETECTOR rec[-1] """ -# NOTE: a T state measured in X after a noisy idle: its observable's expectation -# is cos(π/4) = 0.707 before noise. +# NOTE: a T state measured in X after a noisy idle. T_STATE_X = """ R 0 H 0 @@ -321,35 +320,12 @@ def test_metal_defers_the_noise_that_postselection_cannot_see(): mask[[0, 2]] = True sampler = _gpu(circuit.compile_detector_sampler(seed=12)) got = _accepted(sampler, 200_000, mask) - assert _gpu_shots(sampler)._module(mask).splits[False] is not None + assert _gpu_shots(sampler)._module(mask).split is not None full = _accepted(_cpu(circuit.compile_detector_sampler, seed=13), 200_000, mask) assert abs(len(got) - len(full)) <= 5 * np.sqrt(len(full)) assert_counts_match(got, full) -def _expectations(sampler, shots, mask=None): - """Return the passing rows and expectations of ``sample_accepted``.""" - rows, values, _ = _gpu_shots(sampler).sample_accepted( - shots, mask=mask, expectations=True - ) - assert values is not None - return rows, values - - -def test_deferred_noise_leaves_the_expectations_unbiased(): - # NOTE: with expectations, the noise on the observable's direct qubit is not - # deferred: the tape reads the observable's bit for its sign. - circuit = Circuit(UNREAD_NOISE) - mask = np.zeros(circuit.num_detectors, dtype=bool) - mask[[0, 2]] = True - sampler = _gpu(circuit.compile_detector_sampler(seed=14)) - _, values = _expectations(sampler, 200_000, mask) - assert _gpu_shots(sampler)._module(mask).splits[True] is not None - full = _accepted(_cpu(circuit.compile_detector_sampler, seed=15), 200_000, mask) - expected = np.mean(1 - 2 * full[:, -1].astype(float)) - assert abs(values.mean() - expected) < 5 / np.sqrt(len(full)) - - @pytest.mark.parametrize("text", [NOISY_DETECTORS, UNREAD_NOISE]) def test_metal_batches_return_the_rows_of_one_pass(monkeypatch, text): # NOTE: 1000-shot batches, queued on one set of buffers; without deferral, a @@ -377,40 +353,6 @@ def packed(mask=None): np.testing.assert_array_equal(rows, full[passing]) -def test_observable_expectations_average_to_the_sampled_observable(): - circuit = Circuit(T_STATE_X) - sampler = _gpu(circuit.compile_detector_sampler(seed=6)) - packed, values = _expectations(sampler, 200_000) - assert values.shape == (len(packed), 1) - # NOTE: given the shot's Z error, the expectation is ±cos(π/4). - np.testing.assert_allclose(np.abs(values), np.sqrt(0.5), atol=1e-5) - observed = 1 - 2 * np.unpackbits(packed, axis=1, bitorder="little")[:, 0].astype( - float - ) - sampled = _cpu(circuit.compile_detector_sampler, seed=7).sample( - 200_000, append_observables=True - )[:, 0] - expected = np.mean(1 - 2 * sampled.astype(float)) - assert abs(values.mean() - expected) < 5 * 1 / np.sqrt(200_000) - # NOTE: the returned observable bit is sampled from the same state. - assert abs(observed.mean() - values.mean()) < 5 / np.sqrt(200_000) - - -# NOTE: the T state's X outcome is copied onto qubit 1, whose postselected (and -# noisy) detector is settled after the observable's measurement. -T_STATE_X_THEN_DETECTOR = """ -R 0 1 -H 0 -T 0 -H 0 -CNOT 0 1 -X_ERROR(0.1) 1 -M 0 -OBSERVABLE_INCLUDE(0) rec[-1] -M 1 -DETECTOR rec[-1] -""" - # NOTE: a postselected T-state detector settled before the observable's measurement. T_STATE_X_AFTER_DETECTOR = """ R 0 1 @@ -426,45 +368,6 @@ def test_observable_expectations_average_to_the_sampled_observable(): """ -def _postselected_expectations(text, shots): - circuit = Circuit(text) - mask = np.ones(circuit.num_detectors, dtype=bool) - sampler = _gpu(circuit.compile_detector_sampler(seed=9)) - _, values = _expectations(sampler, shots, mask) - detectors, observables = _cpu(circuit.compile_detector_sampler, seed=10).sample( - shots, separate_observables=True - ) - kept = observables[~(detectors & mask).any(axis=1), 0] - return values[:, 0], np.mean(1 - 2 * kept.astype(float)), len(kept) - - -def test_expectations_stay_unbiased_under_later_postselection(): - values, expected, kept = _postselected_expectations( - T_STATE_X_THEN_DETECTOR, 200_000 - ) - # NOTE: the expectation before the measurement would average to cos(π/4). - assert abs(values.mean() - expected) < 5 * np.sqrt(1 / len(values) + 1 / kept) - np.testing.assert_array_equal(np.abs(values), 1) - - -def test_expectations_are_exact_after_earlier_postselection(): - values, expected, kept = _postselected_expectations( - T_STATE_X_AFTER_DETECTOR, 200_000 - ) - np.testing.assert_allclose(np.abs(values), np.sqrt(0.5), atol=1e-5) - assert abs(values.mean() - expected) < 5 * np.sqrt(1 / len(values) + 1 / kept) - - -def test_clifford_observables_have_expectations_of_plus_or_minus_one(): - circuit = Circuit( - "R 0 1\nX_ERROR(0.3) 0\nCNOT 0 1\nM 0 1\nDETECTOR rec[-1]\nOBSERVABLE_INCLUDE(0) rec[-2]" - ) - sampler = _gpu(circuit.compile_detector_sampler(seed=8)) - packed, values = _expectations(sampler, 10_000) - bits = np.unpackbits(packed, axis=1, bitorder="little") - np.testing.assert_array_equal(values[:, 0], 1 - 2 * bits[:, 1].astype(np.float32)) - - @pytest.mark.parametrize("reference", [False, True]) def test_metal_samples_programs_without_components(reference): circuit = Circuit( @@ -597,21 +500,19 @@ def packed(): np.testing.assert_array_equal(rows, expected) -def test_metal_unmasked_expectations_after_a_masked_call(): +def test_metal_unmasked_rows_after_a_masked_call(): # NOTE: a masked call lists its accepted shots in the batch buffers; an - # unmasked call on the same buffers must still read every row's own value. + # unmasked call on the same buffers must still read every row of its own. circuit = Circuit(T_STATE_X_AFTER_DETECTOR) mask = np.ones(circuit.num_detectors, dtype=bool) sampler = _gpu(circuit.compile_detector_sampler(seed=5)) - _expectations(sampler, 20_000, mask) + _packed(sampler, 20_000, mask) shots = _gpu_shots(sampler) state = shots._rng.bit_generator.state - rows, values = _expectations(sampler, 20_000) + rows = _packed(sampler, 20_000) shots._rng.bit_generator.state = state shots._slots.clear() - again, values_again = _expectations(sampler, 20_000) - np.testing.assert_array_equal(again, rows) - np.testing.assert_array_equal(values_again, values) + np.testing.assert_array_equal(_packed(sampler, 20_000), rows) def test_metal_samples_rows_too_wide_for_threadgroup_memory(): @@ -806,7 +707,7 @@ def test_calls_out_of_gpu_memory_fall_back_to_the_cpu(monkeypatch, text): def out_of_memory(*args, **kwargs): raise MemoryError("the GPU ran out of memory") - monkeypatch.setattr(_gpu_shots(sampler), "sample_accepted", out_of_memory) + monkeypatch.setattr(_gpu_shots(sampler), "_accepted", out_of_memory) with warnings.catch_warnings(): warnings.simplefilter("error") got = sampler.sample(20_000, append_observables=True) @@ -815,7 +716,7 @@ def out_of_memory(*args, **kwargs): ) assert_counts_match(got, expected) required = _gpu(circuit.compile_detector_sampler(seed=3, device="metal")) - monkeypatch.setattr(_gpu_shots(required), "sample_accepted", out_of_memory) + monkeypatch.setattr(_gpu_shots(required), "_accepted", out_of_memory) with pytest.raises(MemoryError, match="the GPU ran out of memory"): required.sample(10, append_observables=True) diff --git a/test/unit/statevector/test_gpu.py b/test/unit/statevector/test_gpu.py index 61c561dd..5b003f41 100644 --- a/test/unit/statevector/test_gpu.py +++ b/test/unit/statevector/test_gpu.py @@ -310,13 +310,13 @@ def test_tapes_break_out_of_a_block_without_goto(): programs = _programs(CIRCUITS["t_ghz"]) assert any(p.pruned.any() or p.prune_first.any() for p in programs) for i, program in enumerate(programs): - source = tape.tape_function(program, i, 0, {}, TEST_DIALECT) + source = tape.tape_function(program, i, 0, TEST_DIALECT) assert "goto" not in source and "done:" not in source assert source.count(" do {") == source.count("} while (false);") == 1 if program.pruned.any(): assert "break; }" in source goto = tape.tape_function( - program, i, 0, {}, tape.Dialect(**{**TEST_DIALECT.__dict__, "goto": True}) + program, i, 0, tape.Dialect(**{**TEST_DIALECT.__dict__, "goto": True}) ) assert "do {" not in goto and "done:" in goto @@ -324,14 +324,13 @@ def test_tapes_break_out_of_a_block_without_goto(): def test_unrolled_tapes_split_their_amplitudes_into_chunks(): (program,) = _programs(_wide_unrolled(), mask=False) assert program.width == 11 and not tape.loops(program, 5, 0) - source = tape.tape_function(program, 0, 5, {}, TEST_DIALECT) + source = tape.tape_function(program, 0, 5, TEST_DIALECT) assert re.search(r"\ba\[", source) is None assert "float2 a0[32], a1[32];" in source and "a1[31]" in source whole = tape.tape_function( program, 0, 5, - {}, tape.Dialect(**{**TEST_DIALECT.__dict__, "amplitude_chunk": None}), ) assert "float2 a[64];" in whole @@ -340,8 +339,8 @@ def test_unrolled_tapes_split_their_amplitudes_into_chunks(): def test_projections_read_pairs_or_every_partner_first(): (program,) = _programs(_wide_unrolled(), mask=False) unpaired = tape.Dialect(**{**TEST_DIALECT.__dict__, "pair_projections": False}) - paired = tape.tape_function(program, 0, 5, {}, TEST_DIALECT) - whole = tape.tape_function(program, 0, 5, {}, unpaired) + paired = tape.tape_function(program, 0, 5, TEST_DIALECT) + whole = tape.tape_function(program, 0, 5, unpaired) # NOTE: a non-diagonal projection loads its partners as q: all of them # before it updates any, or a pair in a block of its own. assert re.search(r"\n float2 q63 = ", whole) @@ -419,16 +418,16 @@ def test_projections_read_pairs_or_every_partner_first(): def _compile_tapes(sources: list[str], directory) -> list: - """Compile tape functions ``component_0`` into ``run_(words, shots, nwords, seed, grave, ev, nev, deviation)``.""" + """Compile tape functions ``component_0`` into ``run_(words, shots, nwords, seed, grave, deviation)``.""" parts = [CPP_PRELUDE] for k, source in enumerate(sources): parts.append( f"namespace v{k} {{\n{source}\n" f'extern "C" void run_{k}(u64* words, long shots, long nwords, u64 seed,' - " int* grave, float* ev, long nev, unsigned* deviation) {\n" + " int* grave, unsigned* deviation) {\n" " for (long s = 0; s < shots; ++s)\n" " grave[s] = component_0(words + s * nwords, (u64)s, seed, 0," - " nev ? ev + s * nev : nullptr, deviation);\n}\n}" + " deviation);\n}\n}" ) cpp, lib = directory / "tapes.cpp", directory / "tapes.so" cpp.write_text("\n".join(parts)) @@ -446,11 +445,10 @@ def _compile_tapes(sources: list[str], directory) -> list: return functions -def _run_tape(f, words, seed, nev): +def _run_tape(f, words, seed): words = words.copy() shots, nwords = words.shape grave = np.empty(shots, np.int32) - ev = np.zeros((shots, max(nev, 1)), np.float32) deviation = np.zeros(1, np.uint32) f( words.ctypes.data_as(ctypes.c_void_p), @@ -458,20 +456,17 @@ def _run_tape(f, words, seed, nev): ctypes.c_long(nwords), ctypes.c_uint64(seed), grave.ctypes.data_as(ctypes.c_void_p), - ev.ctypes.data_as(ctypes.c_void_p), - ctypes.c_long(nev), deviation.ctypes.data_as(ctypes.c_void_p), ) assert tape.deviation(deviation[0]) == 0.0 - return words, grave, ev[:, :nev] + return words, grave def _looping_circuit(n=5, layers=5, seed=0): """A width-``n`` component with every kind of action that a looping tape runs. T gates (diagonal rotations), U3 (others), X, Z and Pauli-product mid-circuit - measurements with feedforward, noise, postselected detectors, and two - observables. + measurements with feedforward, noise, and postselected detectors. """ rng = np.random.default_rng(seed) lines = ["R " + " ".join(map(str, range(n))), "H " + " ".join(map(str, range(n)))] @@ -513,14 +508,7 @@ def test_looping_and_unrolled_tapes_run_as_the_cpu_executor( layout = host.layout num_detectors = Circuit(text).num_detectors plan = host.plan(np.ones(num_detectors, bool)) - (component,) = layout.components (program,) = plan.programs - observables = { - o: index - num_detectors - for o, index in enumerate(component.output_indices) - if index >= num_detectors - } - assert observables # NOTE: unrolled in each dialect variant, and looping. variants = [ CPP_DIALECT, @@ -529,10 +517,10 @@ def test_looping_and_unrolled_tapes_run_as_the_cpu_executor( tape.Dialect(**{**CPP_DIALECT.__dict__, "pair_projections": False}), ] assert not tape.loops(program, 0, 0) - sources = [tape.tape_function(program, 0, 0, observables, d) for d in variants] + sources = [tape.tape_function(program, 0, 0, d) for d in variants] monkeypatch.setattr(tape, "UNROLL_BUDGET", 0) assert tape.loops(program, 0, 0) - looping = tape.tape_function(program, 0, 0, observables, CPP_DIALECT) + looping = tape.tape_function(program, 0, 0, CPP_DIALECT) assert "#pragma unroll 1" in looping *unrolled, looped = _compile_tapes([*sources, looping], tmp_path) @@ -556,15 +544,13 @@ def test_looping_and_unrolled_tapes_run_as_the_cpu_executor( 1, ) assert 0 < alive.sum() < shots - got, grave, ev = _run_tape(looped, words, seed, len(observables)) + got, grave = _run_tape(looped, words, seed) np.testing.assert_array_equal(grave == np.iinfo(np.int32).max, alive) np.testing.assert_array_equal(got[alive], expected[alive]) - assert np.abs(ev).max() > 0 for f in unrolled: - other, other_grave, other_ev = _run_tape(f, words, seed, len(observables)) + other, other_grave = _run_tape(f, words, seed) np.testing.assert_array_equal(other_grave, grave) np.testing.assert_array_equal(other, got) - np.testing.assert_allclose(other_ev, ev, atol=1e-5) @pytest.mark.parametrize(("p", "qubits"), [(2e-8, 192), (1e-9, 3200)]) @@ -595,16 +581,14 @@ def test_tape_kernel_fills_in_every_component(): mask = np.ones(num_detectors, bool) plan = host.plan(mask) tb = tape.threads_per_shot(layout) - kernel = tape.tape_kernel(layout, plan, mask, num_detectors, tb, TEST_DIALECT) + kernel = tape.tape_kernel(layout, plan, mask, tb, TEST_DIALECT) count = len(plan.programs) assert count == len(layout.components) > 1 assert kernel.calls.count("grave = min(grave, component_") == count assert kernel.seeds.count("const u64 seed_") == count assert all(f"component_{i}(" in kernel.functions for i in range(count)) assert kernel.hits != "0" and kernel.psm_words.any() - unmasked = tape.tape_kernel( - layout, host.plan(None), None, num_detectors, tb, TEST_DIALECT - ) + unmasked = tape.tape_kernel(layout, host.plan(None), None, tb, TEST_DIALECT) assert unmasked.hits == "0" and not unmasked.psm_words.any() assert tape.supports(layout)