Source code for 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']
[docs]
class SparseStateVector:
"""
Exact amplitudes over arbitrary-width basis indices (dict-backed).
"""
def __init__(self):
...
[docs]
def amplitude(self, basis: int) -> complex:
...
[docs]
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):
...
[docs]
def conditioned_by_bit(self, pairs):
...
[docs]
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`).
"""
[docs]
def X_Bool(self, name: str, bit_index: int) -> WordXOperator:
...
def __init__(self, machine_word_bits: int = 64):
...
[docs]
def make_state(self) -> SparseStateVector:
...
[docs]
def position(self, pair: tuple[str, int]) -> int:
...
[docs]
def register(self, *views) -> None:
"""
Touch every coordinate so later state creation sees the layout.
"""
[docs]
def word_slot(self, name: str) -> int:
...
[docs]
class InplaceWordBackend(ReferenceWordBackend):
"""
Protocol variant whose ``conditioned_by_bit`` returns None (pure mutation).
"""
class _InplaceX(WordXOperator):
def conditioned_by_bit(self, pairs):
...
[docs]
def X_Bool(self, name, bit_index):
...
[docs]
def encode_basis(basis: int, pairs, value: int, backend: ReferenceWordBackend) -> int:
"""
OR the little-endian ``value`` into ``basis`` at the given coordinates.
"""
[docs]
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.
"""
[docs]
MACHINE_WORD_BITS: int = 64