{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.certbounds.local/v1/cell.schema.json",
  "title": "QECC Parameter Cell Record",
  "type": "object",
  "required": ["schema_version", "record_type", "cell_id", "parameters", "default_family"],
  "anyOf": [
    { "required": ["claimed"] },
    { "required": ["certified"] },
    { "required": ["subfamily_bounds"], "properties": { "subfamily_bounds": { "minItems": 1 } } }
  ],
  "properties": {
    "schema_version": { "$ref": "common.schema.json#/$defs/SchemaVersion" },
    "record_type": { "type": "string", "const": "cell" },
    "cell_id": { "$ref": "common.schema.json#/$defs/CellId" },
    "parameters": {
      "type": "object",
      "required": ["q", "n", "k"],
      "properties": {
        "q": { "type": "integer", "minimum": 2, "maximum": 64 },
        "n": { "type": "integer", "minimum": 1, "maximum": 4096 },
        "k": { "type": "integer", "minimum": 0, "maximum": 4096 }
      },
      "additionalProperties": false
    },
    "default_family": { "type": "string", "enum": ["additive_stabilizer", "general_quantum"], "default": "additive_stabilizer" },
    "claimed": {
      "type": "object",
      "minProperties": 1,
      "properties": {
        "d_lower": { "$ref": "#/$defs/ClaimedCellBound" },
        "d_upper": { "$ref": "#/$defs/ClaimedCellBound" }
      },
      "additionalProperties": false
    },
    "certified": {
      "type": "object",
      "minProperties": 1,
      "properties": {
        "d_lower": { "$ref": "#/$defs/CertifiedLowerBound" },
        "d_upper": { "$ref": "#/$defs/CertifiedUpperBound" }
      },
      "additionalProperties": false
    },
    "subfamily_bounds": { "type": "array", "items": { "$ref": "#/$defs/SubfamilyBoundEntry" }, "default": [] },
    "history": { "type": "array", "items": { "$ref": "#/$defs/HistoricalCellBound" }, "default": [] },
    "disputes": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/DisputeEntry" }, "default": [] }
  },
  "allOf": [
    {
      "if": { "required": ["parameters"], "properties": { "parameters": { "required": ["k"], "properties": { "k": { "const": 0 } } } } },
      "then": {
        "properties": {
          "claimed": {
            "properties": {
              "d_lower": { "properties": { "scope": { "properties": { "distance_mode": { "const": "pure" } } } } },
              "d_upper": { "properties": { "scope": { "properties": { "distance_mode": { "const": "pure" } } } } }
            }
          },
          "certified": {
            "properties": {
              "d_lower": { "properties": { "scope": { "properties": { "distance_mode": { "const": "pure" } } } } },
              "d_upper": { "properties": { "scope": { "properties": { "distance_mode": { "const": "pure" } } } } }
            }
          },
          "subfamily_bounds": { "items": { "properties": { "bound": { "properties": { "scope": { "properties": { "distance_mode": { "const": "pure" } } } } } } } },
          "history": { "items": { "properties": { "bound": { "properties": { "scope": { "properties": { "distance_mode": { "const": "pure" } } } } } } } }
        }
      }
    },
    {
      "if": { "required": ["parameters"], "properties": { "parameters": { "required": ["k"], "properties": { "k": { "minimum": 1 } } } } },
      "then": {
        "properties": {
          "claimed": {
            "properties": {
              "d_lower": { "properties": { "scope": { "properties": { "code_family": { "not": { "const": "graph_state" } } } } } },
              "d_upper": { "properties": { "scope": { "properties": { "code_family": { "not": { "const": "graph_state" } } } } } }
            }
          },
          "certified": {
            "properties": {
              "d_lower": { "properties": { "scope": { "properties": { "code_family": { "not": { "const": "graph_state" } } } } } },
              "d_upper": { "properties": { "scope": { "properties": { "code_family": { "not": { "const": "graph_state" } }, "distance_mode": { "const": "degenerate" } } } } }
            }
          },
          "subfamily_bounds": { "items": { "properties": { "bound": { "properties": { "scope": { "properties": { "code_family": { "not": { "const": "graph_state" } } } } } } } } },
          "history": { "items": { "properties": { "bound": { "properties": { "scope": { "properties": { "code_family": { "not": { "const": "graph_state" } } } } } } } } }
        }
      }
    },
    {
      "if": { "required": ["default_family"], "properties": { "default_family": { "const": "general_quantum" } } },
      "then": {
        "properties": {
          "claimed": {
            "properties": {
              "d_upper": { "properties": { "scope": { "properties": { "code_family": { "const": "general_quantum" } } } } }
            }
          },
          "certified": {
            "properties": {
              "d_upper": { "properties": { "scope": { "properties": { "code_family": { "const": "general_quantum" } } } } }
            }
          }
        }
      }
    },
    {
      "if": { "required": ["default_family"], "properties": { "default_family": { "const": "additive_stabilizer" } } },
      "then": {
        "properties": {
          "claimed": {
            "properties": {
              "d_upper": { "properties": { "scope": { "properties": { "code_family": { "enum": ["general_quantum", "additive_stabilizer"] } } } } }
            }
          },
          "certified": {
            "properties": {
              "d_lower": { "properties": { "scope": { "properties": { "code_family": { "$ref": "common.schema.json#/$defs/StabilizerFamily" } } } } },
              "d_upper": { "properties": { "scope": { "properties": { "code_family": { "enum": ["general_quantum", "additive_stabilizer"] } } } } }
            }
          }
        }
      }
    }
  ],
  "additionalProperties": false,
  "$defs": {
    "ClaimedCellBound": {
      "type": "object",
      "required": ["value", "trust_level", "source_type", "sources"],
      "properties": {
        "value": { "type": "integer", "minimum": 1 },
        "trust_level": { "type": "string", "enum": ["L0", "L1"] },
        "source_type": { "type": "string", "enum": ["codetables_import", "paper_claim", "reproducible_run", "analytic_bound"] },
        "source_claim_id": { "$ref": "common.schema.json#/$defs/ClaimRef" },
        "sources": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/BibliographicSource" }, "minItems": 1 },
        "scope": { "$ref": "common.schema.json#/$defs/Scope" },
        "rerun_spec": { "$ref": "common.schema.json#/$defs/RerunSpec" },
        "credits": { "$ref": "common.schema.json#/$defs/Credits" }
      },
      "allOf": [
        {
          "if": { "required": ["trust_level"], "properties": { "trust_level": { "const": "L1" } } },
          "then": { "required": ["rerun_spec", "scope", "credits"] }
        }
      ],
      "additionalProperties": false
    },
    "CertifiedBoundBase": {
      "allOf": [
        { "$ref": "common.schema.json#/$defs/TrustAndFormulaGuards" },
        {
          "type": "object",
          "required": ["value", "trust_level", "verification_id", "scope", "certificate", "credits", "verified_at"],
          "properties": {
            "value": { "type": "integer", "minimum": 1 },
            "trust_level": { "$ref": "common.schema.json#/$defs/CertifiedTrustLevel" },
            "source_claim_id": { "$ref": "common.schema.json#/$defs/ClaimRef" },
            "verification_id": { "$ref": "common.schema.json#/$defs/VerificationId" },
            "scope": { "allOf": [{ "$ref": "common.schema.json#/$defs/Scope" }, { "properties": { "assumptions": { "type": "array", "maxItems": 0 } } }] },
            "certificate": { "$ref": "common.schema.json#/$defs/CertificatePointer" },
            "credits": { "$ref": "common.schema.json#/$defs/Credits" },
            "verified_at": { "$ref": "common.schema.json#/$defs/IsoDateTime" }
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "CertifiedLowerBound": {
      "allOf": [
        { "$ref": "#/$defs/CertifiedBoundBase" },
        {
          "properties": {
            "certificate": {
              "required": ["certificate_kind"],
              "properties": {
                "certificate_kind": { "enum": ["stabilizer_and_lrat", "propagation_step", "lean_theorem"] },
                "propagation": { "properties": { "direction": { "const": "lower" } } }
              }
            }
          }
        }
      ],
      "unevaluatedProperties": false
    },
    "CertifiedUpperBound": {
      "allOf": [
        { "$ref": "#/$defs/CertifiedBoundBase" },
        {
          "properties": {
            "scope": { "not": { "required": ["subfamily"] } },
            "certificate": {
              "required": ["certificate_kind"],
              "properties": {
                "certificate_kind": { "enum": ["class_nonexistence_lrat", "propagation_step", "lean_theorem", "closed_form_bound"] },
                "propagation": { "properties": { "direction": { "const": "upper" } } }
              }
            }
          }
        },
        {
          "if": {
            "required": ["certificate"],
            "properties": {
              "certificate": {
                "required": ["certificate_kind"],
                "properties": { "certificate_kind": { "const": "propagation_step" } }
              }
            }
          },
          "then": { "properties": { "scope": { "required": ["distance_mode"], "properties": { "distance_mode": { "const": "degenerate" } } } } }
        }
      ],
      "unevaluatedProperties": false
    },
    "SubfamilyBoundEntry": {
      "type": "object",
      "required": ["direction", "bound"],
      "properties": {
        "direction": { "type": "string", "enum": ["lower", "upper"] },
        "bound": { "$ref": "#/$defs/CertifiedBoundBase" }
      },
      "allOf": [
        {
          "if": { "required": ["direction"], "properties": { "direction": { "const": "lower" } } },
          "then": { "properties": { "bound": { "$ref": "#/$defs/CertifiedLowerBound" } } }
        },
        {
          "if": { "required": ["direction"], "properties": { "direction": { "const": "upper" } } },
          "then": {
            "properties": {
              "bound": {
                "allOf": [
                  { "$ref": "#/$defs/CertifiedBoundBase" },
                  {
                    "properties": {
                      "certificate": {
                        "required": ["certificate_kind"],
                        "properties": {
                          "certificate_kind": { "enum": ["class_nonexistence_lrat", "propagation_step", "lean_theorem", "closed_form_bound"] },
                          "propagation": { "properties": { "direction": { "const": "upper" } } }
                        }
                      }
                    }
                  },
                  {
                    "if": {
                      "required": ["certificate"],
                      "properties": {
                        "certificate": {
                          "required": ["certificate_kind"],
                          "properties": { "certificate_kind": { "const": "propagation_step" } }
                        }
                      }
                    },
                    "then": { "properties": { "scope": { "required": ["distance_mode"], "properties": { "distance_mode": { "const": "degenerate" } } } } }
                  }
                ]
              }
            }
          }
        }
      ],
      "additionalProperties": false
    },
    "HistoricalCellBound": {
      "type": "object",
      "required": ["direction", "superseded_at", "bound"],
      "properties": {
        "direction": { "type": "string", "enum": ["lower", "upper"] },
        "superseded_at": { "$ref": "common.schema.json#/$defs/IsoDateTime" },
        "bound": { "$ref": "#/$defs/CertifiedBoundBase" }
      },
      "allOf": [
        {
          "if": { "required": ["direction"], "properties": { "direction": { "const": "lower" } } },
          "then": { "properties": { "bound": { "$ref": "#/$defs/CertifiedLowerBound" } } }
        },
        {
          "if": { "required": ["direction"], "properties": { "direction": { "const": "upper" } } },
          "then": { "properties": { "bound": { "$ref": "#/$defs/CertifiedUpperBound" } } }
        }
      ],
      "additionalProperties": false
    }
  }
}
