{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://haiqu.ai/schemas/qecx/v0.1/qecx.schema.json",
  "title": "QEC Exchange Format",
  "description": "Schema for the QEC Exchange Format (.qecx, inline or archive container). Validates structural correctness of quantum error correction code implementation files. Cross-field dimension checks and mathematical validity (CSS orthogonality, GF(2) rank) are delegated to the semantic validator.",
  "type": "object",
  "required": ["qec_version", "code", "register_template", "operations", "error_correction_cycle"],
  "additionalProperties": true,
  "properties": {
    "qec_version": {
      "description": "Format version string in 'major.minor' format. Version compatibility is checked by the semantic validator.",
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+$"
    },
    "code": {
      "$ref": "#/$defs/code_section"
    },
    "register_template": {
      "$ref": "#/$defs/register_template_section"
    },
    "operations": {
      "$ref": "#/$defs/operations_section"
    },
    "error_correction_cycle": {
      "$ref": "#/$defs/error_correction_cycle_section"
    },
    "decoder_config": {
      "$ref": "#/$defs/decoder_config_section"
    },
    "qubit_layout": {
      "$ref": "#/$defs/qubit_layout_section"
    },
    "properties": {
      "$ref": "#/$defs/properties_section"
    }
  },

  "$defs": {

    "binary_matrix_dense": {
      "description": "Dense binary matrix: 2D array of 0/1 integers with dimensions [m, n].",
      "type": "array",
      "items": {
        "type": "array",
        "items": {
          "type": "integer",
          "enum": [0, 1]
        }
      }
    },

    "binary_matrix_sparse": {
      "description": "Sparse binary matrix: object with 'rows' (list of per-row column indices of nonzero entries) and 'shape' ([m, n]).",
      "type": "object",
      "required": ["rows", "shape"],
      "properties": {
        "rows": {
          "type": "array",
          "items": {
            "type": "array",
            "items": {
              "type": "integer",
              "minimum": 0
            }
          }
        },
        "shape": {
          "type": "array",
          "items": {
            "type": "integer",
            "minimum": 0
          },
          "minItems": 2,
          "maxItems": 2
        }
      },
      "additionalProperties": false
    },

    "binary_matrix": {
      "description": "Binary matrix in either dense or sparse representation.",
      "oneOf": [
        { "$ref": "#/$defs/binary_matrix_dense" },
        { "$ref": "#/$defs/binary_matrix_sparse" }
      ]
    },

    "ref_object": {
      "description": "External reference object for the archive container. The $ref value is a relative path within the archive.",
      "type": "object",
      "required": ["$ref"],
      "properties": {
        "$ref": {
          "type": "string"
        }
      }
    },

    "binary_matrix_or_ref": {
      "description": "Binary matrix (dense or sparse) or an external $ref.",
      "oneOf": [
        { "$ref": "#/$defs/binary_matrix" },
        { "$ref": "#/$defs/ref_object" }
      ]
    },

    "pauli_string": {
      "description": "A Pauli string over the alphabet {I, X, Y, Z}.",
      "type": "string",
      "pattern": "^[IXYZ]+$"
    },

    "pauli_string_list": {
      "description": "Array of Pauli strings.",
      "type": "array",
      "items": {
        "$ref": "#/$defs/pauli_string"
      }
    },

    "pauli_string_list_or_ref": {
      "description": "Array of Pauli strings or an external $ref.",
      "oneOf": [
        { "$ref": "#/$defs/pauli_string_list" },
        { "$ref": "#/$defs/ref_object" }
      ]
    },

    "logical_operators": {
      "description": "Logical operator representatives: k Pauli strings for X-type and k for Z-type.",
      "type": "object",
      "required": ["X", "Z"],
      "properties": {
        "X": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/pauli_string"
          },
          "minItems": 1
        },
        "Z": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/pauli_string"
          },
          "minItems": 1
        }
      },
      "additionalProperties": false
    },

    "distance": {
      "description": "Code distance: either a positive integer (symmetric) or an object with distinct X and Z distances.",
      "oneOf": [
        {
          "type": "integer",
          "minimum": 1
        },
        {
          "type": "object",
          "required": ["X", "Z"],
          "properties": {
            "X": {
              "type": "integer",
              "minimum": 1
            },
            "Z": {
              "type": "integer",
              "minimum": 1
            }
          },
          "additionalProperties": false
        }
      ]
    },

    "register_purpose": {
      "description": "Role a register plays in the QEC protocol.",
      "type": "string",
      "enum": [
        "logical_data",
        "logical_measurement",
        "syndrome",
        "flag",
        "cat",
        "verification",
        "magic_state",
        "transversal_readout",
        "custom"
      ]
    },

    "register_entry": {
      "description": "A register declaration with size and optional purpose annotation.",
      "type": "object",
      "required": ["size"],
      "properties": {
        "size": {
          "type": "integer",
          "minimum": 1
        },
        "purpose": {
          "$ref": "#/$defs/register_purpose"
        },
        "purpose_description": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },

    "named_register_map": {
      "description": "Map from register name to register entry.",
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/register_entry"
      }
    },

    "operation_qreg_entry": {
      "description": "Quantum register declaration within an operation variant. Polymorphic on scope: global/block require 'ref', scratch/output require 'size'. Logical-addressing block entries also carry 'block_index'.",
      "type": "object",
      "required": ["scope"],
      "properties": {
        "scope": {
          "type": "string",
          "enum": ["global", "block", "scratch", "output"]
        },
        "ref": {
          "type": "string"
        },
        "size": {
          "type": "integer",
          "minimum": 1
        },
        "block_index": {
          "type": "integer",
          "minimum": 0
        },
        "purpose": {
          "$ref": "#/$defs/register_purpose"
        },
        "purpose_description": {
          "type": "string"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "scope": { "const": "global" } }
          },
          "then": {
            "required": ["ref"]
          }
        },
        {
          "if": {
            "properties": { "scope": { "const": "block" } }
          },
          "then": {
            "required": ["ref"]
          }
        },
        {
          "if": {
            "properties": { "scope": { "const": "scratch" } }
          },
          "then": {
            "required": ["size"]
          }
        },
        {
          "if": {
            "properties": { "scope": { "const": "output" } }
          },
          "then": {
            "required": ["size"]
          }
        }
      ],
      "additionalProperties": true
    },

    "operation_creg_entry": {
      "description": "Classical register declaration within an operation variant. Polymorphic on scope: global/block require 'ref', scratch/output require 'size'.",
      "type": "object",
      "required": ["scope"],
      "properties": {
        "scope": {
          "type": "string",
          "enum": ["global", "block", "scratch", "output"]
        },
        "ref": {
          "type": "string"
        },
        "size": {
          "type": "integer",
          "minimum": 1
        },
        "block_index": {
          "type": "integer",
          "minimum": 0
        },
        "purpose": {
          "$ref": "#/$defs/register_purpose"
        },
        "purpose_description": {
          "type": "string"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "scope": { "const": "global" } }
          },
          "then": {
            "required": ["ref"]
          }
        },
        {
          "if": {
            "properties": { "scope": { "const": "block" } }
          },
          "then": {
            "required": ["ref"]
          }
        },
        {
          "if": {
            "properties": { "scope": { "const": "scratch" } }
          },
          "then": {
            "required": ["size"]
          }
        },
        {
          "if": {
            "properties": { "scope": { "const": "output" } }
          },
          "then": {
            "required": ["size"]
          }
        }
      ],
      "additionalProperties": true
    },

    "operation_qregs_map": {
      "description": "Map from QASM register name to quantum register declaration.",
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/operation_qreg_entry"
      }
    },

    "operation_cregs_map": {
      "description": "Map from QASM register name to classical register declaration.",
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/operation_creg_entry"
      }
    },

    "qasm_string_or_ref": {
      "description": "OpenQASM 3.0 circuit fragment: inline string or external $ref.",
      "oneOf": [
        { "type": "string" },
        { "$ref": "#/$defs/ref_object" }
      ]
    },

    "variant_base": {
      "description": "Common fields present on every operation variant.",
      "type": "object",
      "required": ["fault_tolerant", "qasm"],
      "properties": {
        "description": {
          "type": "string"
        },
        "fault_tolerant": {
          "type": "boolean"
        },
        "qregs": {
          "$ref": "#/$defs/operation_qregs_map"
        },
        "cregs": {
          "$ref": "#/$defs/operation_cregs_map"
        },
        "qasm": {
          "$ref": "#/$defs/qasm_string_or_ref"
        },
        "references": {
          "description": "List of citation keys that resolve to entries in top-level properties.references. Semantic validator enforces resolution.",
          "type": "array",
          "items": { "type": "string" }
        },
        "syndrome_cregs": {
          "description": "Readout declarations holding one bit per check. Optional on every operation kind; syndrome_measurement requires this field or transversal_readout_cregs.",
          "type": "array",
          "items": {
            "$ref": "#/$defs/syndrome_creg_entry"
          }
        },
        "transversal_readout_cregs": {
          "description": "Readout declarations holding one bit per block qubit read. Optional on every operation kind; syndrome_measurement requires this field or syndrome_cregs.",
          "type": "array",
          "items": {
            "$ref": "#/$defs/transversal_readout_creg_entry"
          }
        }
      },
      "additionalProperties": true
    },

    "state_preparation_variant": {
      "description": "State preparation variant: base fields only. The target logical state is specified by the outer key of the keyed container.",
      "allOf": [
        { "$ref": "#/$defs/variant_base" }
      ]
    },

    "check_frame": {
      "description": "Frame a readout declaration names its checks in: 'entry' for the block as it enters the operation, 'exit' for the state the operation produces. When absent, the operation kind's default applies; a reset entry carries the key.",
      "type": "string",
      "enum": ["entry", "exit"]
    },

    "syndrome_creg_entry": {
      "description": "Maps an output classical register to a code section source field, one bit per check.",
      "type": "object",
      "required": ["name", "source"],
      "properties": {
        "name": {
          "type": "string"
        },
        "source": {
          "type": "string"
        },
        "indices": {
          "type": "array",
          "items": {
            "type": "integer",
            "minimum": 0
          }
        },
        "block_index": {
          "type": "integer",
          "minimum": 0
        },
        "frame": {
          "$ref": "#/$defs/check_frame"
        }
      },
      "additionalProperties": false
    },

    "transversal_readout_creg_entry": {
      "description": "Maps an output classical register to a code section source field, one bit per block qubit read.",
      "type": "object",
      "required": ["name", "source"],
      "properties": {
        "name": {
          "type": "string"
        },
        "source": {
          "type": "string"
        },
        "indices": {
          "type": "array",
          "items": {
            "type": "integer",
            "minimum": 0
          }
        },
        "qubits": {
          "type": "array",
          "items": {
            "type": "integer",
            "minimum": 0
          }
        },
        "block_index": {
          "type": "integer",
          "minimum": 0
        },
        "frame": {
          "$ref": "#/$defs/check_frame"
        }
      },
      "additionalProperties": false
    },

    "syndrome_measurement_variant": {
      "description": "Syndrome measurement variant: base fields, with at least one of syndrome_cregs and transversal_readout_cregs present. Repetition is controlled by error_correction_cycle.",
      "allOf": [
        { "$ref": "#/$defs/variant_base" },
        {
          "type": "object",
          "anyOf": [
            { "required": ["syndrome_cregs"] },
            { "required": ["transversal_readout_cregs"] }
          ]
        }
      ]
    },

    "data_measurement_variant": {
      "description": "Data measurement variant: base fields plus result_creg. The measurement basis is specified by the outer key of the keyed container.",
      "allOf": [
        { "$ref": "#/$defs/variant_base" },
        {
          "type": "object",
          "required": ["result_creg"],
          "properties": {
            "result_creg": {
              "type": "string"
            }
          }
        }
      ]
    },

    "reset_variant": {
      "description": "Reset variant: base fields only. The target logical state is specified by the outer key of the keyed container.",
      "allOf": [
        { "$ref": "#/$defs/variant_base" }
      ]
    },

    "logical_measurement_variant": {
      "description": "Logical measurement variant: base fields plus result_creg.",
      "allOf": [
        { "$ref": "#/$defs/variant_base" },
        {
          "type": "object",
          "required": ["result_creg"],
          "properties": {
            "result_creg": {
              "type": "string"
            }
          }
        }
      ]
    },

    "state_key_string": {
      "description": "Target-state outer key for state_preparation / reset. k=1 codes use a single character; k>1 codes use a length-k string (one char per logical qubit). Length-vs-code.k is enforced by the semantic validator.",
      "type": "string",
      "pattern": "^[01+\\-ij]+$"
    },

    "basis_key_string": {
      "description": "Measurement-basis outer key for data_measurement. Restricted to a single Pauli character applied uniformly across the block.",
      "type": "string",
      "enum": ["Z", "X", "Y"]
    },

    "default_variants_container_state_preparation_inner": {
      "description": "Inner default/variants container for a single state_preparation outer key.",
      "type": "object",
      "required": ["default", "variants"],
      "properties": {
        "default": {
          "type": "string"
        },
        "variants": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/state_preparation_variant"
          },
          "minProperties": 1
        }
      },
      "additionalProperties": false
    },

    "default_variants_container_syndrome_measurement": {
      "description": "Flat default/variants container for syndrome_measurement.",
      "type": "object",
      "required": ["default", "variants"],
      "properties": {
        "default": {
          "type": "string"
        },
        "variants": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/syndrome_measurement_variant"
          },
          "minProperties": 1
        }
      },
      "additionalProperties": false
    },

    "default_variants_container_data_measurement_inner": {
      "description": "Inner default/variants container for a single data_measurement basis outer key.",
      "type": "object",
      "required": ["default", "variants"],
      "properties": {
        "default": {
          "type": "string"
        },
        "variants": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/data_measurement_variant"
          },
          "minProperties": 1
        }
      },
      "additionalProperties": false
    },

    "default_variants_container_reset_inner": {
      "description": "Inner default/variants container for a single reset outer key.",
      "type": "object",
      "required": ["default", "variants"],
      "properties": {
        "default": {
          "type": "string"
        },
        "variants": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/reset_variant"
          },
          "minProperties": 1
        }
      },
      "additionalProperties": false
    },

    "keyed_default_variants_container_state_preparation": {
      "description": "Keyed default/variants container for state_preparation. Outer key is a target-state string; value is an inner flat default/variants container.",
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/state_key_string"
      },
      "additionalProperties": {
        "$ref": "#/$defs/default_variants_container_state_preparation_inner"
      },
      "minProperties": 1
    },

    "keyed_default_variants_container_data_measurement": {
      "description": "Keyed default/variants container for data_measurement. Outer key is a basis character; value is an inner flat default/variants container.",
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/basis_key_string"
      },
      "additionalProperties": {
        "$ref": "#/$defs/default_variants_container_data_measurement_inner"
      },
      "minProperties": 1
    },

    "keyed_default_variants_container_reset": {
      "description": "Keyed default/variants container for reset. Outer key is a target-state string; value is an inner flat default/variants container.",
      "type": "object",
      "propertyNames": {
        "$ref": "#/$defs/state_key_string"
      },
      "additionalProperties": {
        "$ref": "#/$defs/default_variants_container_reset_inner"
      },
      "minProperties": 1
    },

    "operand_entry": {
      "description": "Logical qubit operand in a logical-addressing operand group.",
      "type": "object",
      "required": ["block", "logical_qubit"],
      "properties": {
        "block": {
          "type": "integer",
          "minimum": 0
        },
        "logical_qubit": {
          "oneOf": [
            {
              "type": "integer",
              "minimum": 0
            },
            {
              "type": "null"
            }
          ]
        },
        "role": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },

    "operand_group": {
      "description": "An operand group in a logical-addressing operation: operands specification plus default/variants for that operand signature.",
      "type": "object",
      "required": ["operands", "default", "variants"],
      "properties": {
        "operands": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/operand_entry"
          },
          "minItems": 1
        },
        "default": {
          "type": "string"
        },
        "variants": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/variant_base"
          },
          "minProperties": 1
        }
      },
      "additionalProperties": false
    },

    "operand_group_logical_measurement": {
      "description": "An operand group for logical_measurement: same as operand_group but variants require result_creg.",
      "type": "object",
      "required": ["operands", "default", "variants"],
      "properties": {
        "operands": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/operand_entry"
          },
          "minItems": 1
        },
        "default": {
          "type": "string"
        },
        "variants": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/logical_measurement_variant"
          },
          "minProperties": 1
        }
      },
      "additionalProperties": false
    },

    "cell_complex": {
      "description": "Pure combinatorial cell complex: vertices, edges, and optional faces.",
      "type": "object",
      "required": ["num_vertices", "edges"],
      "properties": {
        "num_vertices": {
          "type": "integer",
          "minimum": 1
        },
        "edges": {
          "type": "array",
          "items": {
            "type": "array",
            "items": { "type": "integer", "minimum": 0 },
            "minItems": 2,
            "maxItems": 2
          }
        },
        "faces": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["vertices"],
            "properties": {
              "vertices": {
                "type": "array",
                "items": { "type": "integer", "minimum": 0 },
                "minItems": 3
              },
              "color": {
                "type": "integer",
                "minimum": 0
              }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },

    "embedding": {
      "description": "Geometric embedding of a cell complex into Euclidean space.",
      "type": "object",
      "required": ["dimension", "coordinate_type", "vertex_coordinates"],
      "properties": {
        "dimension": {
          "type": "integer",
          "minimum": 1
        },
        "coordinate_type": {
          "type": "string"
        },
        "vertex_coordinates": {
          "oneOf": [
            {
              "type": "array",
              "items": {
                "type": "array",
                "items": { "type": "number" }
              }
            },
            {
              "$ref": "#/$defs/ref_object"
            }
          ]
        }
      },
      "additionalProperties": false
    },

    "qubit_placement_cell": {
      "description": "Register placement on a cell complex element.",
      "type": "object",
      "required": ["cell", "mapping"],
      "properties": {
        "cell": {
          "type": "string",
          "enum": ["vertex", "edge", "face"]
        },
        "mapping": {
          "type": "array",
          "items": { "type": "integer", "minimum": 0 }
        }
      },
      "additionalProperties": false
    },

    "qubit_placement_coordinates": {
      "description": "Register placement via explicit coordinates (not on the cell complex).",
      "type": "object",
      "required": ["coordinates"],
      "properties": {
        "coordinates": {
          "oneOf": [
            {
              "type": "array",
              "items": {
                "type": "array",
                "items": { "type": "number" }
              }
            },
            {
              "$ref": "#/$defs/ref_object"
            }
          ]
        }
      },
      "additionalProperties": false
    },

    "parametric_ref": {
      "description": "Reference to a field in the document, resolved at load time.",
      "type": "object",
      "required": ["ref"],
      "properties": {
        "ref": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },

    "integer_or_ref": {
      "description": "Either a concrete integer or a parametric reference that resolves to an integer.",
      "oneOf": [
        { "type": "integer", "minimum": 1 },
        { "$ref": "#/$defs/parametric_ref" }
      ]
    },

    "error_correction_cycle_section": {
      "description": "Required section defining how syndrome measurement variants compose into complete error correction cycles.",
      "type": "object",
      "required": ["default", "variants"],
      "properties": {
        "default": {
          "type": "string"
        },
        "variants": {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": {
            "$ref": "#/$defs/ec_cycle_variant"
          }
        }
      },
      "additionalProperties": false
    },

    "ec_cycle_variant": {
      "description": "A single error correction cycle variant.",
      "type": "object",
      "required": ["steps"],
      "properties": {
        "description": {
          "type": "string"
        },
        "fault_tolerant": {
          "type": "boolean"
        },
        "steps": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/ec_cycle_step"
          }
        },
        "repeat": {
          "$ref": "#/$defs/integer_or_ref"
        },
        "references": {
          "description": "List of citation keys that resolve to entries in top-level properties.references. Semantic validator enforces resolution.",
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "additionalProperties": false
    },

    "ec_cycle_step": {
      "description": "A single step in an error correction cycle, referencing a syndrome measurement variant.",
      "type": "object",
      "required": ["measurement"],
      "properties": {
        "measurement": {
          "type": "string"
        },
        "repeat": {
          "$ref": "#/$defs/integer_or_ref"
        }
      },
      "additionalProperties": false
    },

    "decoder_config_section": {
      "description": "Optional syndrome-decoder declaration: maps a decoder slug to its config body. Semantic validity (anchor names, lengths, cross-section references) is delegated to the semantic validator.",
      "type": "object",
      "additionalProperties": { "$ref": "#/$defs/decoder_entry" }
    },

    "decoder_entry": {
      "description": "One decoder slug's config body: optional global parameters plus per-detector tag anchor-maps.",
      "type": "object",
      "properties": {
        "parameters": { "type": "object" }
      },
      "additionalProperties": { "$ref": "#/$defs/decoder_tag_anchor_map" }
    },

    "decoder_tag_anchor_map": {
      "description": "A per-detector tag: source-anchored value lists plus an optional syndrome_measurement override.",
      "type": "object",
      "properties": {
        "syndrome_measurement": { "$ref": "#/$defs/decoder_syndrome_override" }
      },
      "additionalProperties": { "$ref": "#/$defs/decoder_anchor_value" }
    },

    "decoder_anchor_value": {
      "description": "A value list (primitives, optionally with element-wise parametric refs) or a whole-value parametric ref.",
      "oneOf": [
        { "$ref": "#/$defs/decoder_value_list" },
        { "$ref": "#/$defs/parametric_ref" }
      ]
    },

    "decoder_value_list": {
      "description": "A list with one entry per syndrome operator (or per register bit for an override). Each entry is one per-detector tag value: a primitive, a structured value such as a vector or map, or a parametric reference resolving element-wise. The entry's type is decoder-defined; the decoder-specific validator checks it.",
      "type": "array",
      "items": {}
    },

    "decoder_syndrome_override": {
      "description": "Per-register override keyed by variant then classical register name.",
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "additionalProperties": { "$ref": "#/$defs/decoder_anchor_value" }
      }
    },

    "code_section": {
      "description": "Algebraic definition of the quantum error correcting code.",
      "type": "object",
      "required": ["code_type", "n", "k", "d", "logical_operators"],
      "properties": {
        "code_type": {
          "type": "string",
          "enum": ["css", "stabilizer", "subsystem_css", "subsystem_stabilizer"]
        },
        "name": {
          "type": "string"
        },
        "n": {
          "type": "integer",
          "minimum": 1
        },
        "k": {
          "type": "integer",
          "minimum": 1
        },
        "d": {
          "$ref": "#/$defs/distance"
        },
        "logical_operators": {
          "$ref": "#/$defs/logical_operators"
        },
        "H_X": {
          "$ref": "#/$defs/binary_matrix_or_ref"
        },
        "H_Z": {
          "$ref": "#/$defs/binary_matrix_or_ref"
        },
        "stabilizer_generators": {
          "$ref": "#/$defs/pauli_string_list_or_ref"
        },
        "gauge_generators": {
          "$ref": "#/$defs/pauli_string_list_or_ref"
        },
        "gauge_X": {
          "$ref": "#/$defs/binary_matrix_or_ref"
        },
        "gauge_Z": {
          "$ref": "#/$defs/binary_matrix_or_ref"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "code_type": { "const": "css" } },
            "required": ["code_type"]
          },
          "then": {
            "required": ["H_X", "H_Z"]
          }
        },
        {
          "if": {
            "properties": { "code_type": { "const": "stabilizer" } },
            "required": ["code_type"]
          },
          "then": {
            "required": ["stabilizer_generators"]
          }
        },
        {
          "if": {
            "properties": { "code_type": { "const": "subsystem_css" } },
            "required": ["code_type"]
          },
          "then": {
            "required": ["gauge_X", "gauge_Z"]
          }
        },
        {
          "if": {
            "properties": { "code_type": { "const": "subsystem_stabilizer" } },
            "required": ["code_type"]
          },
          "then": {
            "required": ["gauge_generators"]
          }
        }
      ],
      "additionalProperties": true
    },

    "register_template_section": {
      "description": "Declarative register layout: defines the registers used by circuit operations.",
      "type": "object",
      "required": ["per_code_block"],
      "properties": {
        "global": {
          "type": "object",
          "properties": {
            "ancilla_qregs": {
              "$ref": "#/$defs/named_register_map"
            },
            "cregs": {
              "$ref": "#/$defs/named_register_map"
            }
          },
          "additionalProperties": false
        },
        "per_code_block": {
          "type": "object",
          "required": ["data_qreg"],
          "properties": {
            "data_qreg": {
              "$ref": "#/$defs/register_entry"
            },
            "ancilla_qregs": {
              "$ref": "#/$defs/named_register_map"
            },
            "cregs": {
              "$ref": "#/$defs/named_register_map"
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },

    "operations_section": {
      "description": "Circuit implementations for all operation types.",
      "type": "object",
      "required": [
        "state_preparation",
        "syndrome_measurement",
        "logical_gates",
        "data_measurement"
      ],
      "properties": {
        "state_preparation": {
          "$ref": "#/$defs/keyed_default_variants_container_state_preparation"
        },
        "syndrome_measurement": {
          "$ref": "#/$defs/default_variants_container_syndrome_measurement"
        },
        "logical_gates": {
          "description": "Logical-addressing: object mapping gate names to arrays of operand groups.",
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/operand_group"
            },
            "minItems": 1
          }
        },
        "data_measurement": {
          "$ref": "#/$defs/keyed_default_variants_container_data_measurement"
        },
        "logical_measurement": {
          "description": "Logical-addressing for non-destructive logical measurements, keyed by observable Pauli string.",
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/operand_group_logical_measurement"
            },
            "minItems": 1
          }
        },
        "reset": {
          "$ref": "#/$defs/keyed_default_variants_container_reset"
        }
      },
      "additionalProperties": true
    },

    "reference_entry": {
      "description": "A single bibliographic reference. 'key' is required and unique within the file; at least one of 'doi', 'arxiv', or 'url' must be present.",
      "type": "object",
      "required": ["key"],
      "properties": {
        "key": {
          "type": "string",
          "pattern": "^[A-Za-z][A-Za-z0-9_-]*$",
          "description": "Citation key; unique within the file."
        },
        "title": { "type": "string", "description": "Paper or resource title." },
        "doi":   { "type": "string", "description": "DOI without the URL prefix." },
        "arxiv": { "type": "string", "description": "arXiv identifier." },
        "url":   { "type": "string", "format": "uri", "description": "Direct URL; fallback when neither DOI nor arXiv is given." },
        "note":  { "type": "string", "description": "Short note describing the reference's role." },
        "authors": { "type": "string", "description": "Verbatim author list, rendered as-is." },
        "year":  { "type": "integer", "description": "Publication year." }
      },
      "additionalProperties": false,
      "anyOf": [
        { "required": ["doi"] },
        { "required": ["arxiv"] },
        { "required": ["url"] }
      ]
    },

    "properties_section": {
      "description": "Optional metadata and provenance. All keys are optional; unknown keys are accepted.",
      "type": "object",
      "properties": {
        "uuid": {
          "type": "string"
        },
        "author": {
          "oneOf": [
            { "type": "string" },
            {
              "type": "array",
              "items": { "type": "string" }
            }
          ]
        },
        "creator": {
          "type": "string"
        },
        "license": {
          "type": "string"
        },
        "created_date": {
          "type": "string"
        },
        "references": {
          "description": "Bibliographic references for this code file. Convention: first entry is the primary (code-defining) reference; subsequent entries are auxiliary.",
          "type": "array",
          "items": { "$ref": "#/$defs/reference_entry" }
        },
        "description": {
          "type": "string"
        }
      },
      "additionalProperties": true
    },

    "qubit_layout_section": {
      "description": "Optional spatial geometry and structural metadata for codes with geometric structure.",
      "type": "object",
      "properties": {
        "tiling_type": {
          "description": "Tiling/tessellation type (e.g. 'square', 'hexagonal', '4.8.8').",
          "type": "string"
        },
        "topology": {
          "description": "Manifold topology (e.g. 'planar', 'torus', 'periodic').",
          "type": "string"
        },
        "cell_complex": {
          "oneOf": [
            { "$ref": "#/$defs/cell_complex" },
            { "$ref": "#/$defs/ref_object" }
          ]
        },
        "embedding": {
          "$ref": "#/$defs/embedding"
        },
        "qubit_placement": {
          "description": "Maps registers to cell complex elements or provides explicit coordinates.",
          "type": "object",
          "additionalProperties": {
            "oneOf": [
              { "$ref": "#/$defs/qubit_placement_cell" },
              { "$ref": "#/$defs/qubit_placement_coordinates" }
            ]
          }
        }
      },
      "additionalProperties": true
    }
  }
}
