Skip to content

Add measurements without discard and index classical bits by measurement - #644

Open
thierry-martinez wants to merge 5 commits into
TeamGraphix:masterfrom
thierry-martinez:measure-without-discard
Open

thierry-martinez wants to merge 5 commits into
TeamGraphix:masterfrom
thierry-martinez:measure-without-discard

Conversation

@thierry-martinez

Copy link
Copy Markdown
Collaborator

Instruction.M gets a keyword-only attribute discard (default True), exposed by Circuit.m(..., discard=...). With discard=False, the measured qubit stays active, in the eigenstate corresponding to the outcome.

Since a qubit can now be measured several times, the domains of CONDINSTR refer to classical bits instead of measured qubits: bit i is the outcome of the i-th measurement (Circuit.measurement_count). This also fixes #640: transpile_swaps renames qubits, but domains no longer refer to qubits.

  • transpile_discard replaces measurements without discard with measurements with discard followed by the preparation of a fresh ancilla in the corresponding eigenstate; Circuit.transpile applies it automatically.
  • Circuit.simulate and transpile_to_qasm_gates support measurements without discard.
  • QASM export of instructions becomes a method of ToQASM3, which keeps track of the next classical bit; measurement statements are now terminated by a semicolon.
  • Circuit.visit(copy=True), transpile_ancilla_state and transpile_swaps(copy=False) keep the validator state in sync.
  • DataclassReprMixin always names keyword-only fields.

`Instruction.M` gets a keyword-only attribute `discard` (default `True`),
exposed by `Circuit.m(..., discard=...)`. With `discard=False`, the
measured qubit stays active, in the eigenstate corresponding to the
outcome.

Since a qubit can now be measured several times, the domains of
`CONDINSTR` refer to classical bits instead of measured qubits: bit `i`
is the outcome of the `i`-th measurement (`Circuit.measurement_count`).
This also fixes TeamGraphix#640: `transpile_swaps` renames qubits, but domains no
longer refer to qubits.

- `transpile_discard` replaces measurements without discard with
  measurements with discard followed by the preparation of a fresh
  ancilla in the corresponding eigenstate; `Circuit.transpile` applies
  it automatically.
- `Circuit.simulate` and `transpile_to_qasm_gates` support measurements
  without discard.
- QASM export of instructions becomes a method of `ToQASM3`, which
  keeps track of the next classical bit; measurement statements are now
  terminated by a semicolon.
- `Circuit.visit(copy=True)`, `transpile_ancilla_state` and
  `transpile_swaps(copy=False)` keep the validator state in sync.
- `DataclassReprMixin` always names keyword-only fields.

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

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.05%. Comparing base (5fa0763) to head (9a0ab39).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #644      +/-   ##
==========================================
+ Coverage   89.98%   90.05%   +0.06%     
==========================================
  Files          49       49              
  Lines        8447     8524      +77     
==========================================
+ Hits         7601     7676      +75     
- Misses        846      848       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@matulni

matulni commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

To explore further: reset instructions

@thierry-martinez

Copy link
Copy Markdown
Collaborator Author

I opened the issue #648 for reset instructions.

@matulni matulni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for this addition!

Comment thread graphix/transpiler.py Resolved
Comment thread graphix/transpiler.py Resolved
Comment thread tests/test_transpiler.py Resolved
Comment thread tests/test_transpiler.py Resolved
- `transpile_discard` becomes a method of `Circuit`, like the other
  `transpile_*` methods. It appends SWAP gates that restore the order of
  the output qubits, so the result has the same semantics as the
  original circuit, and `TranspileDiscardResult` is removed.
- The method always returns a new circuit, even when there is no
  measurement without discard.
- The new method `Circuit.reorder_active_qubits_with_swaps` appends these
  SWAP gates. `Pattern.to_circuit` uses it too.
- Ancillas are transpiled to |+> only if they are in another state.

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

@matulni matulni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the mods. LGTM!

Comment thread graphix/transpiler.py Resolved

This branch has not been deployed

No deployments
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.

[Bug]: transpile_swaps doesn't rename CONDINSTR domains after a SWAP

3 participants