Skip to content

add-mps-concept-encoding: §1, §3-§7 — hierarchical polysemy example, demo, docs - #46

Merged
jascal merged 1 commit into
mainfrom
add-mps-concept-encoding-hierarchical
May 1, 2026
Merged

add-mps-concept-encoding: §1, §3-§7 — hierarchical polysemy example, demo, docs#46
jascal merged 1 commit into
mainfrom
add-mps-concept-encoding-hierarchical

Conversation

@jascal

@jascal jascal commented May 1, 2026

Copy link
Copy Markdown
Owner

Summary

Lands the rung-1 companion to larql-polysemantic-clusters: a 12-concept two-level hierarchy (3 super-groups × 2 sub-clusters × 2 concepts) prepared as bond-dimension-2 matrix product states via a Ry; CNOT; Ry; CNOT; Ry staircase on a 3-qubit register.

The CNOT staircase entangles adjacent qubits, lifting the flat block diagonal of rung 0 into a graded two-level hierarchy. The Gram matrix splits into four ordered tiers — self 1.000 / sub-cluster-mate 0.882 / super-group-sibling [0.47, 0.54] / cross-group [0.12, 0.25] — one tier richer than rung 0.

This PR completes the implementation phase of the add-mps-concept-encoding change started by #45 (which shipped the compute_concept_gram_mps helper in §2). Sections 1, 3, 4, 5, 6, 7 are now landed; only §8 (archive) remains for after merge.

What ships

  • examples/larql-polysemantic-hierarchical.q.orca.md — 15 states, 25 transitions, 2 parametric actions. 13 query/prepare call sites stamped from a single bond-2 MPS template. Leading paragraph documents the angle design, analytic per-tier band table, an ASCII Gram heatmap, and the polysemy column for |f⟩ = |dog⟩. Uses measurement_collapse_allowed for the convergent done [final] state.

  • demos/larql_polysemantic_hierarchical/demo.py — parse + verify → compile (Mermaid + QASM + Qiskit) → 12 independent Qiskit circuits at 1024 shots each. Prints the analytic Gram matrix as a 4-tier ASCII heatmap, compares empirical vs. analytic polysemy, prints a side-by-side rung-0 vs rung-1 comparison.

  • tests/test_examples.pytest_larql_polysemantic_hierarchical_pipeline pins parametric-action signature shape on both prepare_concept and query_concept (three angle params each), 12 query call sites, qubit[3] q; in QASM, QuantumCircuit(3) plus 39 ry / 26 cx in the Qiskit script, diagonal == 1, and the four off-diagonal tiers with strict sub→super (≥ 0.20) and super→cross (≥ 0.15) separation.

  • README.md — new "Hierarchical polysemy" sub-heading under "Parametric actions" linking the example, demo, and back to larql-polysemantic-clusters as the flat-tier variant. Examples table grows the new entry.

  • CHANGELOG.md — Unreleased / Added bullet covering the new example, demo, and compute_concept_gram_mps helper coverage.

  • openspec/changes/add-mps-concept-encoding/tasks.md — marks tasks 1.1–1.3, 3.1–3.3, 4.1–4.3, 5.1–5.4, 6.1–6.3, 7.1–7.4 done with measured-result completion notes (per the §5.1 convention from tech-debt-backlog).

Test plan

  • pytest — 771 passed, 6 skipped (full suite, including the new pipeline test)
  • Ruff clean across touched files
  • openspec validate add-mps-concept-encoding --strict passes
  • Demo run locally PASSes (max_err 0.0225 < threshold 0.0469 = 3·MC-std bound at 1024 shots)
  • Four-tier Gram structure recovered with the expected separation gaps (sub→super 0.347, super→cross 0.222)
  • CI green on the PR

🤖 Generated with Claude Code

…demo, docs

Lands the rung-1 companion to larql-polysemantic-clusters: a 12-concept
two-level hierarchy (3 super-groups × 2 sub-clusters × 2 concepts)
prepared as bond-dimension-2 matrix product states via a Ry; CNOT; Ry;
CNOT; Ry staircase on a 3-qubit register.

The CNOT staircase entangles adjacent qubits, lifting the flat block
diagonal of rung 0 into a graded two-level hierarchy. The Gram matrix
splits into four ordered tiers — self 1.000 / sub-cluster-mate 0.882 /
super-group-sibling [0.47, 0.54] / cross-group [0.12, 0.25] — one tier
richer than rung 0.

What ships:

- examples/larql-polysemantic-hierarchical.q.orca.md — 13 transitions
  (1 prepare + 12 query call sites), 15 states, 2 parametric actions
  with three-angle signatures. Leading paragraph documents the angle
  design, the analytic per-tier band table, an ASCII Gram heatmap, and
  the polysemy column for |f> = |dog>. Uses
  measurement_collapse_allowed for the convergent done [final] state.

- demos/larql_polysemantic_hierarchical/demo.py — parse + verify →
  compile (Mermaid + QASM + Qiskit) → 12 independent Qiskit circuits at
  1024 shots each → polysemy column print. Prints the analytic Gram
  matrix as a 4-tier ASCII heatmap, compares empirical vs analytic
  polysemy, and prints a side-by-side rung-0 (flat) vs rung-1
  (hierarchical) comparison. PASSes with max_err 0.0225 < threshold
  0.0469 (3 × Monte-Carlo std bound at 1024 shots).

- tests/test_examples.py: test_larql_polysemantic_hierarchical_pipeline
  pins parametric-action signature shape on both prepare_concept and
  query_concept (3 angle params each), 12 query call sites, qubit[3] q;
  in QASM, QuantumCircuit(3) plus 39 ry / 26 cx in the Qiskit script,
  diagonal == 1, and the four off-diagonal tiers with strict sub→super
  (≥ 0.20) and super→cross (≥ 0.15) separation.

- README.md: new "Hierarchical polysemy" sub-heading under Parametric
  actions linking the example, demo, and back to clusters as the
  flat-tier variant. Examples table grows the new entry.

- CHANGELOG.md: Unreleased / Added bullet describing the new example,
  demo, and compute_concept_gram_mps helper coverage.

- openspec/changes/add-mps-concept-encoding/tasks.md: marks tasks
  1.1–1.3, 3.1–3.3, 4.1–4.3, 5.1–5.4, 6.1–6.3, 7.1–7.4 done with
  measured-result completion notes (per the §5.1 convention from
  tech-debt-backlog).

Verification:

- 771 passed, 6 skipped (full pytest)
- Ruff clean across touched files
- openspec validate add-mps-concept-encoding --strict passes
- Demo PASSes locally (max_err 0.0225 < threshold 0.0469)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@jascal
jascal merged commit 1ae325e into main May 1, 2026
6 checks passed
jascal added a commit that referenced this pull request May 2, 2026
#49)

Bumps version 0.6.0 → 0.7.0 and lands the post-merge documentation
sync after PR #46 (`add-mps-concept-encoding`) and PR #48
(`fix-mps-encoding-non-factorizing`).

- README: move MPS concept encoding (rung 1) from "Near-term —
  implementation pending" into "Recently shipped" with PR refs and
  the cross-coupled-by-sum encoding formula.
- CHANGELOG: convert "Unreleased" → "0.7.0 (2026-05-01)"; correct
  the stale tier values (`super-group-sibling [0.47, 0.54]` →
  `{0.335, 0.593, 0.753}`, `cross-group [0.12, 0.25]` → `[0.000,
  0.178]`) and the encoding (single-bound-param → cross-coupled-by-
  sum) in the hierarchical-polysemy entry; add Changed entry for the
  generalized angle parser; add Fixed entry for
  `fix-mps-encoding-non-factorizing` referencing PR #48 and tech-
  debt §5.7.
- Research doc: link the rung-1 entry in
  `docs/research/polysemantic-encoding-beyond-product-states.md` to
  the merged PRs (#46 + #48) and the post-mortem in
  `add-mps-concept-encoding/design.md`.
- OpenSpec: archive `add-mps-concept-encoding` (25/26 — task 4.2
  superseded by `fix-mps-encoding-non-factorizing`) and
  `fix-mps-encoding-non-factorizing` (✓ Complete) under
  `openspec/changes/archive/2026-05-02-…`. Both updates flow
  through to `openspec/specs/{compiler,language}/spec.md`.
- Tech debt: append §5.8–§5.15 from a post-merge self-review of
  PR #48 (HIGH: scientific-notation regression in
  `_split_linear_combination`; MEDIUM: error-message drift, etc.).

Note: the demo-smoke step in `verify-examples.yml` is split out of
this commit (PAT lacks workflow scope) — to be added via the GitHub
UI as a follow-up.

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