QRAM memory definition (qram YAML)¶
English · 简体中文
Memory data is not written into the program serialization text (see RIR); it is bound separately as an execution input. This document defines the format of the binding file *.qram.yaml: one file describes a set of QRAM segments (the qram_segments list), cross-checked against the program by entry resource name at execution time.
qram_segments:
- name: values # storage name, indexed by this name in code
address_length: 2 # address bit width
word_length: 3 # data word bit width
type: uint # data type: uint / sint / fixedpoint
data: [1, 2, 4, 7] # concrete data, dense array, index is the address
Fields¶
field |
type |
constraint |
description |
|---|---|---|---|
|
list |
required, sole top-level key |
a set of QRAM definitions |
|
string |
non-empty, unique within the file |
storage name; matched exactly against entry resource names at execution |
|
integer |
>= 1 |
address bit width; corresponds to |
|
integer |
>= 1 |
data word bit width; corresponds to |
|
string |
|
source data type of |
|
string |
optional; only |
absent means the legacy dense array |
|
array |
see the table below |
dense values, or |
Value ranges and encodings of the three data types:
type |
data element range |
encoded on load as an unsigned word |
|---|---|---|
|
unsigned integer, 0 .. 2^ |
unchanged |
|
signed integer, −2^( |
two’s-complement bit pattern |
|
fixed-point number in [0, 1) |
multiplied by 2^ |
Semantic rules¶
At load time each source datum is validated locally against
word_length(ranges in the table above); an out-of-range value is an error. The length ofdatamust not exceed 2^address_length; when shorter, high-address cells are zero-padded (padding).Segment names must be unique within the file.
At execution time the QRAM declarations inside the program are authoritative:
check_memoryrequires the segment resource-name set to match the entry declarations exactly, no more and no less; encoded words must fall within the program-declared bit widths, and anaddress_length/word_lengthdisagreeing with the program declaration is not reported as a separate error.Segments produced by
dump_qram_yamlalways have typeuint(the executor-side words are already unsigned integers within the declared bit width).
Python API¶
from oracq import dump_qram_yaml, load_qram_yaml, simulate
memory = load_qram_yaml("memory.qram.yaml") # -> {"values": [1, 2, 4, 7]}
state = simulate(program, memory) # fully compatible with the existing memory argument
text = dump_qram_yaml(program, memory) # emits text after canonical validation; the caller writes it to disk
load_qram_yaml returns a mapping from resource names to dense word arrays or sparse address/word dictionaries (already encoded per type), shaped like the memory parameter of simulate, run_pysparq, and run_originir. By default, dump_qram_yaml first cross-validates through check_memory and densifies the sparse dictionary, then emits segments in the order of the entry’s resource declarations; when the entry declares no QRAM resource it emits an empty qram_segments.
CLI¶
oracq run closed.rir.yaml --memory memory.qram.yaml
--memory accepts only this format. When the structure or data is invalid it reports a validation error with exit code 2 and never truncates silently. See Command line for all subcommands.
Schema¶
The structural constraints are in qram-memory.schema.json, including the data element type constraints branched on type. Cross-field constraints (array length versus address_length, word value range versus word_length and type, segment-name uniqueness) cannot be expressed in JSON Schema and are enforced by the Python loader.
Sparse snapshots (memory schema 0.4)¶
For a large address space, opt in with dump_qram_yaml(program, memory, sparse=True).
The writer emits compact JSON, which is valid YAML, using explicit pairs rather than
object keys. Existing dense output remains the default.
qram_segments:
- name: bank
address_length: 32
word_length: 8
type: uint
encoding: sparse
data: [[0, 7], [4294967295, 255]]
The loader returns {"bank": {0: 7, 4294967295: 255}} without allocating a dense
array. Omitted addresses read zero. Addresses must be unique integers in the
declared range; values retain the existing uint/sint/fixedpoint rules.
QramSegment.addresses is None for a dense segment and a tuple of explicit
addresses for a sparse segment; data retains the corresponding source values.
Read/write work scales with stored records, not 2**address_length. Sparse files
require this reader version; older readers retain support for dense files only.
Schema 0.4 lifts the former 64-bit maximum on address_length and word_length;
every document valid under 0.3 stays valid, and older validators keep rejecting
wide segments. Sparse interchange still does not imply execution: the PySparQ
adapter materializes dense banks only up to 20 address bits, and wider banks run
through the logical-word sparse query interface when the installed pysparq
provides it, with an explicit capability error otherwise.
Optional QRAM-assisted compilation¶
compile_qram(program) returns (compiled, extra_memory, report). Execute or
export both the returned program and merged memory. This is an explicit
resource tradeoff; it does not declare QRAM to have zero latency.
The compiler recognizes exact dyadic, bit-controlled Ry words and shares one phase-gradient register across the coherent entry execution. Gradient preparation and inverse preparation are in the circuit. Controlled modular addition preserves the angle-word-dependent phase, including negative outer controls. Arbitrary non-dyadic rotations remain unchanged. Clean carry, padding and predicate work registers count toward the algorithmic logical peak.
A four-word, one-bit read-only AND table ({3: 1}, other words zero) replaces
nonlinear Boolean gates by reversible XOR queries. Its nominal capacity, query
count and routing requirements remain external QRAM resources. Applications must
explicitly choose their timing policy; removing those queries from a gate-only
time total is a model assumption, not a hardware speed measurement.
Calls and Repeat nodes stay compressed. The public register interface, angle
words, output protocol and coherent complex amplitudes are preserved. Programs
with writes, unresolved modules, or reserved __qr_ names are rejected. The
transformation is optional (phase_gradient=False or boolean=False) and
idempotent. Sparse-state simulation of a large gradient can itself be expensive;
this does not change the compressed circuit resource estimate.
Phase-gradient kickback and reversible arithmetic follow standard constructions; see Gidney, 2018 and Babbush et al., 2018. This implementation keeps its preparation/cleanup and coherent-control phase in the serialized RIR.