Resource estimation: ## resources, resource invariants, stage 4c - #37
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements
add-resource-estimationend-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.## resourcestable declares which of the five static cost numbers a machine cares about (gate_count,depth,cx_count,t_count,logical_qubits). The## invariantsblock accepts the same five identifiers as the LHS of a comparison against an integer literal.q_orca.compiler.resourcesmodule exposesestimate_resources(machine),compile_with_resources(machine), andformat_resource_report(machine, resources). One transpile pass per machine, memoized byid(machine).resource_bounds) auto-runs whenever a machine has at least one resource invariant; emitsRESOURCE_BOUND_EXCEEDED(error) on violation andRESOURCE_BOUND_INDETERMINATE(warning) when the metric is"unknown". Skipped viaVerifyOptions(skip_resource_bounds=True)or the new--skip-resource-boundsCLI flag.bell-entangler,qaoa-maxcut, andvqe-heisenbergnow pin their gate budgets.docs/language/resources.md; README's stage table and CLI flag table updated; CHANGELOG entry added.Test plan
openspec validate add-resource-estimation --strict— greenruff check— clean--stricttests/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