qram_simulator.word_backend 源代码

"""
Reference native backend for the ideal sparse wide-word query chain.

Executes the frozen ``SparseWordMemory.apply_xor`` operator protocol (see
:mod:`qram_simulator.word_memory`) on an exact dict-backed statevector
whose basis indices are arbitrary-precision Python ints. Registers are
named machine words of ``MACHINE_WORD_BITS`` bits, so logical registers
wider than 64 bits genuinely span several words — the same layout
discipline SparQSim's production native backend follows.

This module is QRAM-Simulator's own verification and product-demo
executor. Production-scale native execution is provided by the caller's
backend (SparQSim); both are validated against the shared fixture
``bindings/python/test/fixtures/word_query_fixture.json``.
"""
from __future__ import annotations
__all__: list[str] = ['InplaceWordBackend', 'MACHINE_WORD_BITS', 'ReferenceWordBackend', 'SparseStateVector', 'WordXOperator', 'encode_basis', 'word_layout']
[文档] class SparseStateVector: """ Exact amplitudes over arbitrary-width basis indices (dict-backed). """ def __init__(self): ...
[文档] def amplitude(self, basis: int) -> complex: ...
[文档] class WordXOperator: """ A native X on one machine-word bit, optionally controlled on |1>. Follows the frozen protocol: ``conditioned_by_bit`` accepts the bit coordinate list, mutates the operator and returns ``self``. """ def __call__(self, state: SparseStateVector) -> None: ... def __init__(self, backend: 'ReferenceWordBackend', position: int): ...
[文档] def conditioned_by_bit(self, pairs): ...
[文档] class ReferenceWordBackend: """ Named machine-word register file with exact X / controlled-X execution. Every operation is an exact unitary permutation of the amplitudes — a genuine statevector execution, not a call recorder. Bit indices are confined to one machine word; wider logical registers span several word registers (see :func:`word_layout`). """
[文档] def X_Bool(self, name: str, bit_index: int) -> WordXOperator: ...
def __init__(self, machine_word_bits: int = 64): ...
[文档] def make_state(self) -> SparseStateVector: ...
[文档] def position(self, pair: tuple[str, int]) -> int: ...
[文档] def register(self, *views) -> None: """ Touch every coordinate so later state creation sees the layout. """
[文档] def word_slot(self, name: str) -> int: ...
[文档] class InplaceWordBackend(ReferenceWordBackend): """ Protocol variant whose ``conditioned_by_bit`` returns None (pure mutation). """ class _InplaceX(WordXOperator): def conditioned_by_bit(self, pairs): ...
[文档] def X_Bool(self, name, bit_index): ...
[文档] def encode_basis(basis: int, pairs, value: int, backend: ReferenceWordBackend) -> int: """ OR the little-endian ``value`` into ``basis`` at the given coordinates. """
[文档] def word_layout(width: int, prefix: str, machine_word_bits: int = 64) -> list[tuple[str, int]]: """ Little-endian coordinates of one logical register over machine words. """
[文档] MACHINE_WORD_BITS: int = 64