Skip to content

Support simple IfElseOp conditionals in OpenQASM 2 export#14556

Merged
kaldag merged 5 commits into
Qiskit:mainfrom
jakelishman:qasm2/if
Dec 11, 2025
Merged

Support simple IfElseOp conditionals in OpenQASM 2 export#14556
kaldag merged 5 commits into
Qiskit:mainfrom
jakelishman:qasm2/if

Conversation

@jakelishman
Copy link
Copy Markdown
Member

Summary

With the removal of Instruction.c_if in gh-13506, we lost the ability to export the sort of simple conditionals that OpenQASM 2 can represent. This restores the ability, for the subset of IfElseOp objects that OpenQASM 2 can represent.

It is possible to make the exporter slightly more permissive in some cases, if desired/necessary.

Details and comments

Fix #13848

With the removal of `Instruction.c_if` in Qiskitgh-13506, we lost the ability
to export the sort of simple conditionals that OpenQASM 2 can represent.
This restores the ability, for the subset of `IfElseOp` objects that
OpenQASM 2 can represent.

It is possible to make the exporter slightly more permissive in some
cases, if desired/necessary.
@jakelishman jakelishman added this to the 2.2.0 milestone Jun 6, 2025
@jakelishman jakelishman requested a review from a team as a code owner June 6, 2025 17:30
@jakelishman jakelishman added Changelog: Added Add an "Added" entry in the GitHub Release changelog. mod: qasm2 Relating to OpenQASM 2 import or export labels Jun 6, 2025
@qiskit-bot
Copy link
Copy Markdown
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@coveralls
Copy link
Copy Markdown

coveralls commented Jun 6, 2025

Pull Request Test Coverage Report for Build 20142533553

Details

  • 33 of 33 (100.0%) changed or added relevant lines in 1 file are covered.
  • 16 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.01%) to 88.315%

Files with Coverage Reduction New Missed Lines %
crates/transpiler/src/passes/unitary_synthesis.rs 1 93.32%
crates/qasm2/src/lex.rs 3 92.8%
crates/circuit/src/parameter/symbol_expr.rs 12 72.82%
Totals Coverage Status
Change from base Build 20140426167: -0.01%
Covered Lines: 96515
Relevant Lines: 109285

💛 - Coveralls

Copy link
Copy Markdown
Contributor

@kaldag kaldag left a comment

Choose a reason for hiding this comment

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

Are register arrays supposed to throw up an error? Does the error OpenQASM 2 only supports register-equality conditions refer to that condition.

I do see this line but wanted clarification:

 if isinstance(operation.condition, expr.Expr) or isinstance(
    operation.condition[0], Clbit
)

Comment thread qiskit/qasm2/export.py
Comment thread test/python/qasm2/test_export.py
kaldag
kaldag previously approved these changes Sep 4, 2025
Copy link
Copy Markdown
Contributor

@kaldag kaldag left a comment

Choose a reason for hiding this comment

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

This is good to merge.

@raynelfss raynelfss modified the milestones: 2.2.0, 2.3.0 Sep 22, 2025
@github-project-automation github-project-automation Bot moved this to Ready in Qiskit 2.3 Oct 7, 2025
@kaldag kaldag enabled auto-merge October 10, 2025 15:03
Copy link
Copy Markdown
Collaborator

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

1 question and 1 tiny comment otherwise LGTM

Comment thread qiskit/qasm2/export.py Outdated
Comment thread qiskit/qasm2/export.py
if len(body.data) != 1:
# This could be relaxed in the future by "unrolling" the block and putting
# individual `if` conditions on each instruction.
raise QASM2ExportError(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's stretching my memory, but were we previously able to do c_if on InstructionSets (i.e. multiple instructions) or do we have the same functionality as before if we only allow a single instruction?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The old InstructionSet.c_if (which doesn't exist any more) used to set the .condition field individually, which corresponded to the OQ2 model (and this one) of only allowing if (<reg> == <val>) <gate>; - there's no blocks in OQ2.

@Cryoris Cryoris moved this from Ready to In review in Qiskit 2.3 Dec 10, 2025
@kaldag kaldag added this pull request to the merge queue Dec 11, 2025
Merged via the queue into Qiskit:main with commit e8e3893 Dec 11, 2025
23 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in Qiskit 2.3 Dec 11, 2025
@jakelishman jakelishman deleted the qasm2/if branch December 11, 2025 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Added Add an "Added" entry in the GitHub Release changelog. mod: qasm2 Relating to OpenQASM 2 import or export

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add path to export IfElseOp in Qasm2

6 participants