RIR 序列化¶
oracq.infrastructure.serialization
Versioned dual-format YAML/JSON encoding, YAML by default; module definitions and call edges survive the round trip unchanged.
- oracq.infrastructure.serialization.TYPES = {'Adjoint': <class 'oracq.infrastructure.ir.Adjoint'>, 'Call': <class 'oracq.infrastructure.ir.Call'>, 'Control': <class 'oracq.infrastructure.ir.Control'>, 'Load': <class 'oracq.infrastructure.ir.Load'>, 'Module': <class 'oracq.infrastructure.ir.Module'>, 'Primitive': <class 'oracq.infrastructure.ir.Primitive'>, 'Program': <class 'oracq.infrastructure.ir.Program'>, 'QRAM': <class 'oracq.infrastructure.ir.QRAM'>, 'Ref': <class 'oracq.infrastructure.ir.Ref'>, 'RegType': <class 'oracq.infrastructure.ir.RegType'>, 'Register': <class 'oracq.infrastructure.ir.Register'>, 'Repeat': <class 'oracq.infrastructure.ir.Repeat'>, 'Resource': <class 'oracq.infrastructure.ir.Resource'>, 'Span': <class 'oracq.infrastructure.ir.Span'>, 'Store': <class 'oracq.infrastructure.ir.Store'>}¶
Mapping from node
tagvalues toirdataclasses;decoderestores node types from it.
- oracq.infrastructure.serialization.encode(value)[源代码]¶
Encode an RIR value into a structure compatible with both YAML and JSON.
Dataclasses encode as objects carrying a
tagfield, tuples encode as arrays, and scalars andNonepass through unchanged.- 参数:
value -- dataclass node, tuple or scalar to encode.
- 返回:
text-format-compatible structure built from dict, list and scalars.
- 抛出:
ValidationError -- the value belongs to a non-serializable type.
- oracq.infrastructure.serialization.decode(value)[源代码]¶
Rebuild an encoded structure into an RIR dataclass tree.
- 参数:
value -- structure produced by
encode; arrays are restored to tuples.- 返回:
rebuilt dataclass node, tuple or scalar.
- 抛出:
ValidationError -- an unknown
tagis encountered, or node fields are missing or extra.
- oracq.infrastructure.serialization.dumps(program, *, format='yaml')[源代码]¶
Serialize a program to canonical YAML (default) or canonical JSON text.
Both formats use sorted keys, two-space indentation, UTF-8 and one trailing newline; module definitions are sorted by module name, with signature parameters and instruction order preserved. YAML uses block style, indents nested sequences relative to their owning key, and uses no anchors or aliases; floats must be finite. Open bodies stay null, and binding-origin attributes such as binding_captures are saved as-is; binding and resource analysis reports are not mixed into executable nodes.
- 参数:
program -- program to serialize.
format -- output text format,
yaml(default) orjson.
- 返回:
canonical text.
- 返回类型:
str
- 抛出:
ValidationError -- the program failed structural or semantic validation, or contains non-serializable values.
- oracq.infrastructure.serialization.loads(text)[源代码]¶
Parse YAML or JSON text and rebuild a validated program.
The format is auto-detected from the text: text starting with
{is first parsed as strict JSON, falling back to YAML on failure (YAML is a superset of JSON); other text is parsed as YAML. Both paths reject duplicate keys and non-finite numbers; the YAML path additionally rejects implicit scalar types such as dates.- 参数:
text -- YAML or JSON text produced by
dumps.- 返回:
rebuilt program that passed semantic validation.
- 返回类型:
- 抛出:
ValidationError -- the text syntax or node structure is invalid, the root node is not a
Program, or validation failed.