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

Reject other QASM versions in QASM 2 parser #7456

Merged
merged 17 commits into from
Jan 18, 2022

Conversation

3yakuya
Copy link
Contributor

@3yakuya 3yakuya commented Jan 1, 2022

Summary

Fixing QASM-319, qasm parser will fail with appropriate error if invalid OPENQASM version is specified. Reno release note is added.

Details and comments

Small change in qasm lexer to only accept 2.0 as a version, rather than any digit.digit. The latter caused a bug where user could provide any version, like 3.0 and believe it works while in reality their code was still parsed as 2.0.

Should not break any workflows, unless they rely on broken openqasm strings. Added reno release note. Did not see any place in the documentation per se that would require updating.

Other numerical will now result in error from parser.
@3yakuya 3yakuya requested a review from a team as a code owner January 1, 2022 05:57
@3yakuya
Copy link
Contributor Author

3yakuya commented Jan 1, 2022

Its effectively blocked by #7457

@3yakuya 3yakuya force-pushed the 3yakuya/fix-openqasm-319 branch from c844ac5 to 950c148 Compare January 3, 2022 17:02
@jakelishman jakelishman linked an issue Jan 4, 2022 that may be closed by this pull request
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for looking at this!

If we're making more programmes fail, we should make sure that our error messages are particularly clear - in this case, it'd be good if we can ensure that the error tells us that only parsing of QASM 2 is currently supported, if we encounter a bad OPENQASM statement. At the moment, the error is:

QasmError: 'Unable to match any token rule, got -->O<-- Check your OPENQASM source and any include statements.'

which isn't super helpful.

qiskit/qasm/qasmlexer.py Outdated Show resolved Hide resolved
releasenotes/notes/qasm-lexer-bugfix-1779525b3738902c.yaml Outdated Show resolved Hide resolved
releasenotes/notes/qasm-lexer-bugfix-1779525b3738902c.yaml Outdated Show resolved Hide resolved
@jakelishman jakelishman changed the title 3yakuya/fix openqasm 319 Reject other QASM versions in QASM 2 parser Jan 4, 2022
@3yakuya
Copy link
Contributor Author

3yakuya commented Jan 4, 2022

Makes sense, thanks! Will try to look late today or early tomorrow

@3yakuya 3yakuya requested a review from jakelishman January 5, 2022 15:23
@3yakuya
Copy link
Contributor Author

3yakuya commented Jan 13, 2022

@jakelishman any chance for some quick eyes here?

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Sorry this took so long for me to look at again. The errors look much better now, in most of the situations, I just had one little tweak for a new situation that OpenQASM 3 allows, but OpenQASM 2 doesn't, that'll improve them a little more too.

We're good to go on this after that.

qiskit/qasm/qasmparser.py Outdated Show resolved Hide resolved
releasenotes/notes/qasm-lexer-bugfix-1779525b3738902c.yaml Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jan 13, 2022

Pull Request Test Coverage Report for Build 1714209317

  • 7 of 7 (100.0%) changed or added relevant lines in 2 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.0003%) to 83.141%

Files with Coverage Reduction New Missed Lines %
qiskit/extensions/quantum_initializer/uc.py 2 87.94%
Totals Coverage Status
Change from base Build 1714031495: 0.0003%
Covered Lines: 51983
Relevant Lines: 62524

💛 - Coveralls

3yakuya and others added 4 commits January 16, 2022 13:58
Co-authored-by: Jake Lishman <jake@binhbar.com>
Version 2; should succeed, as the lexer will accept single-digit versions.
Parser adds default version 0.
OPENQASM 2; or OPENQASM 2.0; both succeed, else fail.
@3yakuya 3yakuya requested a review from jakelishman January 17, 2022 14:56
qiskit/qasm/node/format.py Outdated Show resolved Hide resolved
test/python/test_qasm_parser.py Outdated Show resolved Hide resolved
@@ -0,0 +1,11 @@
OPENQASM 2;
Copy link
Member

Choose a reason for hiding this comment

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

The fact that OPENQASM 2; works now is kind of a side-effect - it'd be best for the good test file to use 2.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this one we have a few tests that run with 2.0 and I wanted to keep at least one that shows that indeed a single-digit version will work

Copy link
Member

Choose a reason for hiding this comment

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

That's a good idea, let's leave it as it is.

@@ -0,0 +1,11 @@
OPENQASM 5.0;
Copy link
Member

Choose a reason for hiding this comment

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

You already test OPENQASM 3; so this isn't testing anything new; a good other test would be that minor versions of 2 are also detected, such as OPENQASM 2.1; - this would have caught the error with has_minor_version above, because the file would have parsed successfully when it should have thrown an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eh you got more work than help with my contribution here, sorry for rookie mistakes! Working on it

Copy link
Member

Choose a reason for hiding this comment

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

Don't worry about it - it's not really true anyway. Before you made the PR, I didn't know how hard or easy it would be to fix. After you'd made it, all I had to do was check that what you'd done was sensible, not actually go through and do it myself, and you did this well.

Finding extra special test cases, catching weirdnesses with re and things like that is just more experience, and it's why we have review anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well I appreciate the encouragement and would continue to work on this anyways :D

Tests now fail with incorrect minor version.
Tests of qasmparser cleaned up a bit.
@3yakuya 3yakuya force-pushed the 3yakuya/fix-openqasm-319 branch from f1da822 to 6696124 Compare January 18, 2022 03:20
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

I pushed up a couple of commits just to simplify the creation of some strings, and fix a typo in the test case (which was probably my fault anyway). This is good to merge now, and it's stable for backport, so we'll put it out in Terra 0.19.2 whenever we get around to releasing that 🎉

@jakelishman jakelishman added automerge Changelog: Bugfix Include in the "Fixed" section of the changelog stable backport potential The bug might be minimal and/or import enough to be port to stable labels Jan 18, 2022
@jakelishman jakelishman added this to the 0.19.2 milestone Jan 18, 2022
@mergify mergify bot merged commit 2d8bb5c into Qiskit:main Jan 18, 2022
mergify bot pushed a commit that referenced this pull request Jan 18, 2022
* Explicitly specifying QASM version 2.0 in lexer.

Other numerical will now result in error from parser.

* adding release note.

* Moving QASM version check to parser.

Lexer accepts any structurally proper version string, parser fails if it is other than 2.0.
Added tests and updated docs.

* Fixing qasm version checking.

* Fixing tab in docs

* Update releasenotes/notes/qasm-lexer-bugfix-1779525b3738902c.yaml

Co-authored-by: Jake Lishman <jake@binhbar.com>

* Improved error message for invalid QASM version.

* Adding ability to parse OPENQASM single-digit ver

Version 2; should succeed, as the lexer will accept single-digit versions.
Parser adds default version 0.
OPENQASM 2; or OPENQASM 2.0; both succeed, else fail.

* Linting and adding docstrings

* Fixing QASM minor-version parsing & testing

Tests now fail with incorrect minor version.
Tests of qasmparser cleaned up a bit.

* Convert string expressions to format strings

* Remove unused test assertion

* Fix typo in test case

Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 2d8bb5c)
mergify bot added a commit that referenced this pull request Jan 18, 2022
* Explicitly specifying QASM version 2.0 in lexer.

Other numerical will now result in error from parser.

* adding release note.

* Moving QASM version check to parser.

Lexer accepts any structurally proper version string, parser fails if it is other than 2.0.
Added tests and updated docs.

* Fixing qasm version checking.

* Fixing tab in docs

* Update releasenotes/notes/qasm-lexer-bugfix-1779525b3738902c.yaml

Co-authored-by: Jake Lishman <jake@binhbar.com>

* Improved error message for invalid QASM version.

* Adding ability to parse OPENQASM single-digit ver

Version 2; should succeed, as the lexer will accept single-digit versions.
Parser adds default version 0.
OPENQASM 2; or OPENQASM 2.0; both succeed, else fail.

* Linting and adding docstrings

* Fixing QASM minor-version parsing & testing

Tests now fail with incorrect minor version.
Tests of qasmparser cleaned up a bit.

* Convert string expressions to format strings

* Remove unused test assertion

* Fix typo in test case

Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 2d8bb5c)

Co-authored-by: Kuba Pilch <6464505+3yakuya@users.noreply.github.com>
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 stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QASM does not error when unrecognized version is provided
3 participants