Skip to content

🐛 Enforce terminal measurements in Base QIR - #2449

Merged
burgholzer merged 6 commits into
mainfrom
codex/2287-qir-base-measurement-order
Sep 7, 2026
Merged

🐛 Enforce terminal measurements in Base QIR#2449
burgholzer merged 6 commits into
mainfrom
codex/2287-qir-base-measurement-order

Conversation

@simon1hofmann

@simon1hofmann simon1hofmann commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

Description

Base QIR lowering moved measurements behind all gates, so measure q; x q; became x q; measure q;. Lowering now preserves instruction order, checks canonical qubit pointers in one forward scan, and moves measurements only after validation succeeds. It rejects gates, measured controls, and repeated measurements on an already measured qubit, as required by the Base profile. Gates on independent qubits remain supported.

Repeated loads of a constant register element reuse the same qubit pointer. Unresolved register references, runtime or out-of-bounds indices, and mixed static/allocation modes are rejected during resource lowering. QC and QCO use the same validation path; a measured QCO qubit can be inserted into a QTensor and then freed. Adaptive measurement behavior is unchanged.

Addresses the Base-profile ordering finding in #2287.

Local validation: 136 Base conversion tests, 152 Adaptive conversion tests, and 165 compiler tests passed with LLVM/MLIR 23.1.0. uvx nox -s lint and uvx nox -s cpp-lint -- 80abe20f444d231e91e06df3deba5ff665ab3f55 passed; the C++ check covers every line of all three changed C++ source/test files with clang-tidy 23.1.1. Hosted CI must validate the new head.

GPT-6 via Codex assisted the implementation, tests, and PR text.

Checklist

  • The pull request only contains commits that are focused and relevant to this change.
  • I have added appropriate tests that cover the new/changed functionality.
  • I have updated the documentation to reflect these changes.
  • Changelog entries are not required for this unreleased v4 functionality.
  • Migration instructions are not required for this unreleased v4 functionality.
  • The changes follow the project's style guidelines and introduce no new warnings.
  • The changes are fully tested and pass the CI checks.
  • I have reviewed my own code changes.

If PR contains AI-assisted content:

  • Any agent that created, edited, or submitted GitHub content was explicitly authorized for that scope, as required by our AI Usage Guidelines.
  • Every agent-authored or agent-edited public text body begins with the visible disclosure 🤖 *AI text below* 🤖 (titles are exempt).
  • I have disclosed AI assistance in the PR description.
  • I confirm that I have personally reviewed and understood all AI-generated content, and accept full responsibility for it.

Reject gates on measured qubits before moving measurements to the final
block. Track static and register aliases while allowing later gates on
independent qubits.

Assisted-by: GPT-6 via Codex
@simon1hofmann simon1hofmann added bug Something isn't working MLIR Anything related to MLIR QIR Anything related to QIR labels Sep 7, 2026
@simon1hofmann simon1hofmann self-assigned this Sep 7, 2026
Keep the static measurement-order helper outside the anonymous namespace without changing its internal linkage or behavior.

Assisted-by: GPT-6 via Codex

@burgholzer burgholzer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in principle. One quick comment on the scope here that could potentially be simplified.
Feel free to resolve, then merge

Comment thread mlir/lib/Conversion/QCToQIR/QIRBase/QCToQIRBase.cpp Outdated
@codecov

codecov Bot commented Sep 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Keep constant-index register aliases, which Base lowering already
accepts. Remove runtime-index alias tracking and its test and docs
from this static-qubit ordering fix.

Assisted-by: GPT-6 via Codex
@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Important

Approval pending

CodeRabbit has no unresolved comments, but it has not reviewed the latest commit.

Use the checkbox below to review the latest commit. CodeRabbit will approve the changes if it finds no blocking issues.

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Preserve quantum instruction order until canonical qubit pointers are
checked, then move measurements into the Base measurement block. Resolve
repeated register loads once during lowering and reject unresolved or
out-of-bounds indices and mixed allocation modes.

Reject repeated Base measurements while retaining Adaptive support and
QCO measurements whose qubits are inserted into a freed tensor.

Assisted-by: GPT-6 via Codex
@burgholzer burgholzer changed the title 🐛 Reject unsafe Base-profile measurement order 🐛 Enforce terminal measurements in Base QIR Sep 7, 2026
@burgholzer burgholzer added fix Fix for something that isn't working and removed bug Something isn't working labels Sep 7, 2026
@burgholzer burgholzer self-assigned this Sep 7, 2026

@burgholzer burgholzer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned this up a little. Should be good to go now :shipit:

@burgholzer
burgholzer enabled auto-merge (squash) September 7, 2026 21:18
@burgholzer
burgholzer merged commit d0a2f7e into main Sep 7, 2026
25 of 26 checks passed
@burgholzer
burgholzer deleted the codex/2287-qir-base-measurement-order branch September 7, 2026 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Fix for something that isn't working MLIR Anything related to MLIR QIR Anything related to QIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants