Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Expr support to OpenQASM 3 exporter #10408

Merged
merged 5 commits into from
Jul 20, 2023
Merged

Conversation

jakelishman
Copy link
Member

Summary

This adds support for Expr nodes in condition fields and SwitchCaseOp.target. The general c_if condition checking is lifted into an Expr node as part of the OQ3 export, so the form of them automatically becomes like the new-style outputs, depending on how expr.lift_legacy_condition chooses to raise them to Expr nodes.

As part of the refactor of the Expression subtree of the temporary AST, I removed SubroutineCall. Its definition was severely out-of-date (a form from before even the initial arXiv draft), where it was statement-like instead of expression-like. There is currently no support in Qiskit for representing these sorts of subroutines properly, and the OQ3 exporter would produce invalid OQ3 if it encountered something that it thought should be a a subroutine call. This commit fixes that bug, correctly raising an exception that non-unitary instructions are not yet supported for export.

This commit also removes the special casing from the CXGate into the exporter itself; it was more convenient in the new structure of some internal components, and it's a data model we don't want to support anyway.

Details and comments

Close #10225. Depends on #10249 and #10358. Further changelog in #10331.

I haven't verified the exact output formats from this against what the QSS stack supports yet, but will do early next week and make whatever tweaks are required.

@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog Changelog: Bugfix Include in the "Fixed" section of the changelog mod: qasm3 Related to OpenQASM 3 import or export labels Jul 7, 2023
@jakelishman jakelishman added this to the 0.25.0 milestone Jul 7, 2023
@jakelishman jakelishman requested a review from a team as a code owner July 7, 2023 18:28
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented Jul 7, 2023

Pull Request Test Coverage Report for Build 5605101679

  • 145 of 166 (87.35%) changed or added relevant lines in 3 files are covered.
  • 199 unchanged lines in 14 files lost coverage.
  • Overall coverage decreased (-0.03%) to 86.029%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qasm3/ast.py 62 67 92.54%
qiskit/qasm3/printer.py 32 38 84.21%
qiskit/qasm3/exporter.py 51 61 83.61%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/controlflow/switch_case.py 1 94.35%
qiskit/qasm3/exporter.py 1 94.87%
qiskit/transpiler/instruction_durations.py 1 92.96%
qiskit/transpiler/passmanager_config.py 1 97.26%
crates/qasm2/src/lex.rs 2 90.89%
qiskit/extensions/quantum_initializer/squ.py 2 80.0%
qiskit/qasm3/ast.py 3 94.89%
qiskit/circuit/controlflow/builder.py 4 95.3%
qiskit/providers/fake_provider/fake_backend.py 4 91.12%
qiskit/qasm3/printer.py 5 88.15%
Totals Coverage Status
Change from base Build 5597428209: -0.03%
Covered Lines: 72585
Relevant Lines: 84373

💛 - Coveralls

@kdk kdk added the on hold Can not fix yet label Jul 18, 2023
This adds support for `Expr` nodes in `condition` fields and
`SwitchCaseOp.target`.  The general `c_if` condition checking is lifted
into an `Expr` node as part of the OQ3 export, so the form of them
automatically becomes like the new-style outputs, depending on how
`expr.lift_legacy_condition` chooses to raise them to `Expr` nodes.

As part of the refactor of the `Expression` subtree of the temporary
AST, I removed `SubroutineCall`.  Its definition was severely
out-of-date (a form from before even the initial arXiv draft), where it
was statement-like instead of expression-like.  There is currently no
support in Qiskit for representing these sorts of subroutines properly,
and the OQ3 exporter would produce invalid OQ3 if it encountered
something that it thought should be a a subroutine call.  This commit
fixes that bug, correctly raising an exception that non-unitary
instructions are not yet supported for export.

This commit also removes the special casing from the `CXGate` into the
exporter itself; it was more convenient in the new structure of some
internal components, and it's a data model we don't want to support
anyway.
@jakelishman
Copy link
Member Author

Now rebased over main.

This is what the QSS stack expects (they don't like bitstring literals
in expressions yet), and is more in line with what we used to do anyway.
Comment on lines +589 to +592
# CX gets super duper special treatment because it's the base of Terra's definition
# tree, but isn't an OQ3 built-in. We use `isinstance` because we haven't fully
# fixed what the name/class distinction is (there's a test from the original OQ3
# exporter that tries a naming collision with 'cx').
Copy link
Contributor

Choose a reason for hiding this comment

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

The same comment appears above. We probably don't need it twice. The comments are a bit mysterious. I'm not sure if they should be clarified, or maybe I just have to read more code to understand.

Copy link
Member Author

Choose a reason for hiding this comment

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

Which bits of it would you like me to clarify? I had it in both places because I thought they were sufficiently far apart in the code that it warranted it, but I'm happy to reduce them if you think it'd be clearer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since putting the comments in both places was intentional, I'm ok with leaving them both in place.

@jakelishman jakelishman added on hold Can not fix yet and removed on hold Can not fix yet labels Jul 20, 2023
@jakelishman
Copy link
Member Author

This is just slightly on hold because I have a couple of extra tests that I want to write - the exporter will already pass them, but there's a couple of particular forms of output I want to enshrine in it first. The exporter code won't change as I write them, so this could be reviewed already.

Actually, I suppose it can even merge without me writing the tests - I can add them later if necessary.

@jakelishman
Copy link
Member Author

I've added the test case I was worried about now, so this PR is totally ready.

TAU = enum.auto()


class IntegerLiteral(Expression):
Copy link
Contributor

Choose a reason for hiding this comment

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

Renaming these classes with correct names is a great idea. There seem to be a number of these improvements that are unrelated to the PR. One might argue for doing this in a separate PR. But in the interest of development efficiency, I think it's ok to include in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

In some senses there's not a lot of renaming going on here - most of the classes I removed weren't actually used, and I was putting in new nodes for the actual Expr tree. All previous ParameterExpression values were being handled by the base ast.Expression node, which I effectively renamed to ast.StringifyAndPray to make it clearer what was actually going on, and so I had a more sane base ast.Expression node to work from.

I think IntegerLiteral might be the only one that logically existed in the old form that still exists now but with a different name.

Fwiw, I think this AST module will be completely gone by the end of the year; once we have our custom importer, we can use the AST from that, rather than needed to maintain a parallel AST and formatter in Terra.

Copy link
Contributor

@jlapeyre jlapeyre left a comment

Choose a reason for hiding this comment

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

The tests are clear and look through. LGTM

@jlapeyre jlapeyre enabled auto-merge July 20, 2023 17:41
@jlapeyre jlapeyre added this pull request to the merge queue Jul 20, 2023
Merged via the queue into Qiskit:main with commit d13f0bb Jul 20, 2023
13 checks passed
@jakelishman jakelishman deleted the expr/qasm3 branch July 26, 2023 12:44
to24toro pushed a commit to to24toro/qiskit-terra that referenced this pull request Aug 3, 2023
* Add `Expr` support to OpenQASM 3 exporter

This adds support for `Expr` nodes in `condition` fields and
`SwitchCaseOp.target`.  The general `c_if` condition checking is lifted
into an `Expr` node as part of the OQ3 export, so the form of them
automatically becomes like the new-style outputs, depending on how
`expr.lift_legacy_condition` chooses to raise them to `Expr` nodes.

As part of the refactor of the `Expression` subtree of the temporary
AST, I removed `SubroutineCall`.  Its definition was severely
out-of-date (a form from before even the initial arXiv draft), where it
was statement-like instead of expression-like.  There is currently no
support in Qiskit for representing these sorts of subroutines properly,
and the OQ3 exporter would produce invalid OQ3 if it encountered
something that it thought should be a a subroutine call.  This commit
fixes that bug, correctly raising an exception that non-unitary
instructions are not yet supported for export.

This commit also removes the special casing from the `CXGate` into the
exporter itself; it was more convenient in the new structure of some
internal components, and it's a data model we don't want to support
anyway.

* Use `IntegerLiteral` for `Uint` values

This is what the QSS stack expects (they don't like bitstring literals
in expressions yet), and is more in line with what we used to do anyway.

* Fix quotes

* Add test of no spurious casts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog Changelog: New Feature Include in the "Added" section of the changelog mod: qasm3 Related to OpenQASM 3 import or export
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for exporting Expr values to OpenQASM 3
5 participants