Skip to content

Resource estimation: ## resources, resource invariants, stage 4c - #37

Merged
jascal merged 4 commits into
mainfrom
add-resource-estimation
Apr 26, 2026
Merged

Resource estimation: ## resources, resource invariants, stage 4c#37
jascal merged 4 commits into
mainfrom
add-resource-estimation

Conversation

@jascal

@jascal jascal commented Apr 26, 2026

Copy link
Copy Markdown
Owner

Summary

Implements add-resource-estimation end-to-end: a static resource-bound surface for Q-Orca machines, layered cleanly on top of the existing parser, compiler, and verifier without changing any runtime behavior.

  • Surface: optional ## resources table declares which of the five static cost numbers a machine cares about (gate_count, depth, cx_count, t_count, logical_qubits). The ## invariants block accepts the same five identifiers as the LHS of a comparison against an integer literal.
  • Compiler: new q_orca.compiler.resources module exposes estimate_resources(machine), compile_with_resources(machine), and format_resource_report(machine, resources). One transpile pass per machine, memoized by id(machine).
  • Verifier: new stage 4c (resource_bounds) auto-runs whenever a machine has at least one resource invariant; emits RESOURCE_BOUND_EXCEEDED (error) on violation and RESOURCE_BOUND_INDETERMINATE (warning) when the metric is "unknown". Skipped via VerifyOptions(skip_resource_bounds=True) or the new --skip-resource-bounds CLI flag.
  • Examples: bell-entangler, qaoa-maxcut, and vqe-heisenberg now pin their gate budgets.
  • Docs: full reference at docs/language/resources.md; README's stage table and CLI flag table updated; CHANGELOG entry added.

Test plan

  • openspec validate add-resource-estimation --strict — green
  • Full pytest — 727 passed, 6 skipped
  • ruff check — clean
  • All 15 bundled examples verify under --strict
  • New tests: tests/test_resource_estimation.py (7), tests/test_parser.py::TestResourcesSection (4), tests/test_parser.py::TestResourceInvariants (6), tests/test_verifier.py::TestResourceInvariantVerification (3)

🤖 Generated with Claude Code

jascal and others added 4 commits April 26, 2026 16:18
Additive AST surface for the resource-estimation work:

- `QMachineDef.resource_metrics: list[str]` — declared in `## resources`,
  empty means "use the default five-metric set" downstream.
- `Invariant.metric: Optional[str]` — only populated for
  `kind="resource"`; existing `entanglement` and `schmidt_rank`
  invariants continue to leave it `None`.

No parser, compiler, or verifier behavior changes yet — those land in
later sections.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
`## resources` table — optional 2- or 3-column form with `Metric` as
the first column. Recognized metric names: `gate_count`, `depth`,
`cx_count`, `t_count`, `logical_qubits`. Unknown names append a
structured `unknown_resource_metric: '<name>'` error to the parse-error
sink. Absence leaves `QMachineDef.resource_metrics=[]`.

`## invariants` grammar — extended to recognize the same five
identifiers as the LHS of `<op> <int>`, producing
`Invariant(kind="resource", metric=..., op=..., value=...)`. Existing
`entanglement` and `schmidt_rank` invariants continue to parse
identically.

Adds `TestResourcesSection` (4 tests) and `TestResourceInvariants`
(6 tests, parametrized over metric × operator). Test count 707 → 717.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- `q_orca/compiler/resources.py` (NEW): `estimate_resources(machine)`
  builds the Qiskit circuit via `build_circuit_for_iteration` and
  computes `gate_count`, `depth`, `cx_count` (via `transpile` to
  `['u3','cx']`), `t_count` (via `transpile` to
  `['h','s','cx','t','tdg']`), and `logical_qubits`. Memoized by
  `id(machine)`. Adds `compile_with_resources` and
  `format_resource_report` helpers.
- `q_orca/verifier/resources.py` (NEW): `check_resource_invariants`
  evaluates each `Invariant(kind="resource")` against the estimator,
  emits `RESOURCE_BOUND_EXCEEDED` (error) or
  `RESOURCE_BOUND_INDETERMINATE` (warning). Located in its own
  module rather than `dynamic.py` because the check is purely static
  (no QuTiP/Qiskit-Aer simulation).
- `q_orca/verifier/__init__.py`: Stage 4c dispatch alongside
  unitarity/completeness/etc., gated by `skip_resource_bounds`.
- `q_orca/__init__.py`: re-exports `estimate_resources`,
  `compile_with_resources`, `format_resource_report`.
- Tests: `tests/test_resource_estimation.py` (NEW, 7 tests covering
  Bell/GHZ/QAOA pinned numbers, memoization, default-metrics
  fallback, unknown-metric error, format_resource_report). Verifier
  tests added under `TestResourceInvariantVerification` covering
  exceeded/satisfied/skipped paths. Suite 717 → 727.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
- Pin gate budgets in bell-entangler, qaoa-maxcut, vqe-heisenberg
- New docs/language/resources.md (full surface reference)
- README: stage 4c row + --skip-resource-bounds CLI row
- CLI: wire --skip-resource-bounds through VerifyOptions
- CHANGELOG entry for the additive resource-estimation surface
- Inline source for the no-resources-section test (bell now
  declares one)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jascal
jascal merged commit 302b86f into main Apr 26, 2026
6 checks passed
@jascal
jascal deleted the add-resource-estimation branch April 26, 2026 22:27
@jascal jascal mentioned this pull request Apr 26, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant