Skip to content

Implement add-classical-context-updates (grammar + verifier + compiler) - #21

Merged
jascal merged 1 commit into
mainfrom
add-classical-context-updates
Apr 20, 2026
Merged

Implement add-classical-context-updates (grammar + verifier + compiler)#21
jascal merged 1 commit into
mainfrom
add-classical-context-updates

Conversation

@jascal

@jascal jascal commented Apr 20, 2026

Copy link
Copy Markdown
Owner

Summary

Implements the add-classical-context-updates OpenSpec change (proposal landed in #19). Ships the language/AST/verifier/compiler facets of classical context-update effects — the primitive the QPC research proposal flagged as the one missing piece. Does not ship shot-to-shot runtime execution; that's parked for a follow-up change (run-context-updates).

New grammar in the Effect column of ## actions:

<effect>   ::= <mutation> | if bits[i] == v: <mut_seq> [else: <mut_seq>]
<mutation> ::= <ident>[<int>]? (= | += | -=) (<literal> | <ident>)

Example: if bits[0] == 1: theta[0] -= eta else: theta[0] += eta.

What changed

  • AST (q_orca/ast.py): QContextMutation, QEffectContextUpdate, context_update field on QActionSignature.
  • Parser (q_orca/parser/markdown_parser.py): _parse_context_update_from_effect + mixed-effect rejection (H(qs[0]); iteration += 1) + nested-conditional rejection.
  • Verifier (q_orca/verifier/classical_context.py, new): stage runs between determinism and quantum-static. Enforces UNDECLARED_CONTEXT_FIELD, CONTEXT_FIELD_TYPE_MISMATCH, CONTEXT_INDEX_OUT_OF_RANGE, and BIT_READ_BEFORE_WRITE via acyclic-path enumeration from initial. Adds VerifyOptions.skip_classical_context.
  • Compiler: QASM and Qiskit emit // context_update: ... / # context_update: ... at the action site and a file-level banner when any context-update is present. Mermaid unchanged (transition label is the action name, as for any action).
  • Docs: research doc pointer updated.
  • Tests (tests/test_context_updates.py, 23 cases): parser forms & error modes, verifier error codes & happy path, compiler emission & banner gating.

Test plan

  • .venv/bin/python -m pytest tests/ -q --ignore=tests/test_cuquantum_backend.py --ignore=tests/test_cudaq_backend.py — 468 passed, 5 skipped
  • .venv/bin/q-orca verify on all 10 examples/*.q.orca.md — all VALID, none regress
  • openspec validate add-classical-context-updates --strict — valid

Co-Authored-By: Claude Opus 4.7 noreply@anthropic.com

Ships the language/AST/verifier/compiler facets of classical context
updates — the one primitive the QPC research proposal called out as
missing. Grammar is:

  <effect>   ::= <mutation> | if bits[i] == v: <mut_seq> [else: <mut_seq>]
  <mutation> ::= <ident>[<int>]? (= | += | -=) (<literal> | <ident>)

Shot-to-shot runtime execution of the mutation remains out of scope —
compilers emit annotations and a banner flagging the no-op, per the
spec.

- AST: QContextMutation, QEffectContextUpdate, context_update field on
  QActionSignature.
- Parser: _parse_context_update_from_effect + mixed-effect rejection
  + nested-conditional rejection.
- Verifier: new classical_context stage wired between determinism and
  quantum-static; enforces UNDECLARED_CONTEXT_FIELD,
  CONTEXT_FIELD_TYPE_MISMATCH, CONTEXT_INDEX_OUT_OF_RANGE, and
  BIT_READ_BEFORE_WRITE via acyclic-path enumeration from initial.
  VerifyOptions.skip_classical_context added.
- Compiler: QASM and Qiskit emit `// context_update: ...` /
  `# context_update: ...` at the action site plus a file-level banner
  when any context-update is present. Mermaid unchanged.
- Tests: tests/test_context_updates.py (23 cases across parser,
  verifier, and compiler); full suite 468 passed, 5 skipped; all 10
  examples still verify.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jascal
jascal merged commit a31a217 into main Apr 20, 2026
6 checks passed
@jascal
jascal deleted the add-classical-context-updates branch April 20, 2026 13:31
jascal added a commit that referenced this pull request Apr 20, 2026
Rolling collection point for small items that surface during code
review and QA but aren't worth their own proposal. Seeded with two
low-severity observations from Hermes QA on PR #21:

- parser: rename `_has_trailing_mutation` to match what it detects
- verifier: audit CUDA-Q backend severity/valid consistency

The backlog is intentionally long-lived and has no specs deltas
(so `openspec validate --strict` will fail on it, by design).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jascal jascal mentioned this pull request Apr 20, 2026
2 tasks
jascal added a commit that referenced this pull request Apr 20, 2026
Rolling collection point for small items that surface during code
review and QA but aren't worth their own proposal. Seeded with two
low-severity observations from Hermes QA on PR #21:

- parser: rename `_has_trailing_mutation` to match what it detects
- verifier: audit CUDA-Q backend severity/valid consistency

The backlog is intentionally long-lived and has no specs deltas
(so `openspec validate --strict` will fail on it, by design).

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
jascal added a commit that referenced this pull request Apr 22, 2026
Both shipped earlier but were never moved out of the active changes
folder. Relocate to match the archive pattern:

- add-classical-context-updates → archive/2026-04-20-... (PR #21)
- harden-completeness-detection  → archive/2026-04-19-... (PR #18)

Pure folder rename; no code impact.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jascal jascal mentioned this pull request Apr 22, 2026
2 tasks
jascal added a commit that referenced this pull request Apr 22, 2026
* Archive run-context-updates proposal

Shipped in PR #24 (6c5dbfa). Move the folder from active to
`openspec/changes/archive/2026-04-21-run-context-updates/` to match the
pattern of previously shipped proposals, and update the design-doc
breadcrumb in `runtime/iterative.py` to the new path.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Archive add-classical-context-updates and harden-completeness-detection

Both shipped earlier but were never moved out of the active changes
folder. Relocate to match the archive pattern:

- add-classical-context-updates → archive/2026-04-20-... (PR #21)
- harden-completeness-detection  → archive/2026-04-19-... (PR #18)

Pure folder rename; no code impact.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
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