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

Increase Accuracy of OpenQASM 2.0 Dump #8250

Merged
merged 9 commits into from
Nov 18, 2022

Conversation

burgholzer
Copy link
Contributor

@burgholzer burgholzer commented Jun 28, 2022

Summary

This PR improves the accuracy of the OpenQASM 2.0 export by not truncating parameters of gates to single precision and using a tighter bound for checking whether a given parameter is close to a fraction of pi.
Resolves #7166.

Details and comments

For details, see the discussion in #7166. The additional precision is accomplished by relying on Python's built-in float formatting as a default in the pi_check function.

@burgholzer burgholzer requested a review from a team as a code owner June 28, 2022 11:52
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@burgholzer burgholzer changed the title Fix float truncation Increase Accuracy of OpenQASM 2.0 Dump Jun 28, 2022
@CLAassistant
Copy link

CLAassistant commented Jun 28, 2022

CLA assistant check
All committers have signed the CLA.

@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 28, 2022
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.

Given the discussion in #7166, I'm fine with this. On the ndigits front: probably the ideal would be for us to have the default be None, and to use Python's default float formatting if ndigits is None (or the current implementation if not). In that mode, Python will output the shortest string which evaluates to the same target float - that's why str(0.3) is "0.3", str(0.3 * 3) == "0.8999999999999999" and str(0.9) == "0.9", for example.

If you'd like to do that in this PR, let me know, otherwise we can just move on to merge. Either way, please could you add an "upgrade" release note for this change (reno new --edit change-qasm-float-output) that explains that the number of digits output by QuantumCircuit.qasm() has changed to more accurately represent the parameters. If you want to make the above change, it'll be a "feature" note, rather than an "upgrade".

edit: the lint CI run is also failing, probably because there's a docstring line that's a bit too long in the new test you added. Nothing major - you can just split the line in two.

@burgholzer
Copy link
Contributor Author

burgholzer commented Jun 29, 2022

Thanks for your feedback and the detailed information.
I'll happily work out your suggestion in this PR. Just to double check that we are on the same page: what you are proposing is to change the default for the ndigits parameter in the pi_check method to None and rely on Python's built-in number printing in case no particular value is provided, right?

Of course I'll fix the linter warnings and create a corresponding release note in the process 👍🏼

@jakelishman
Copy link
Member

Ok cool! Yeah, that's exactly it - if ndigits=None then we don't specify a format specifier (as opposed to :{ndigits}f).

@burgholzer burgholzer requested a review from ikkoham as a code owner June 30, 2022 11:18
@burgholzer
Copy link
Contributor Author

burgholzer commented Jun 30, 2022

I refactored the pi_check function accordingly and added corresponding release notes (which I hope are sufficiently accurate).

Some of the pi_check tests had to be adapted. The alternative there would have been to explicitly set ndigits=8 in the corresponding tests and leave everything there as is. Let me know if that should be changed.

One of the (potentially easy to fix) things in the pi_check function is the hard-coded check for zero in
https://github.com/Qiskit/qiskit-terra/blob/fe8ab43f1218d6997458fb3bf4011045f40932b0/qiskit/circuit/tools/pi_check.py#L69-L70
All other comparisons are conducted with respect to eps.
In my opinion, it would make the function more consistent, if this zero check was also conducted with respect to eps.

@jakelishman
Copy link
Member

Yeah, your new form looks better to me, thanks, and all your changes look like they make a lot of sense. I really dislike the whole pi_check function as a whole, and I'm not surprised there's more hard-coded weirdness in it, to be honest... I agree that that line you've highlighted should also be a comparison to eps.

(I can't do a proper review on GitHub right now because it seems to have bugged out a little, and it's showing commits that aren't actually on the branch. Don't worry about it - your branch is actually fine, it's just GitHub being weird, and it'll fix itself in a bit, I'm sure.)

@nonhermitian
Copy link
Contributor

I really dislike the whole pi_check function as a whole

That is a shame. It is actually quite useful for seeing when a given angle is near some commonly used fraction of pi. I agree it has been a bit mangled over time, but that is not its fault.

@burgholzer
Copy link
Contributor Author

Yeah, your new form looks better to me, thanks, and all your changes look like they make a lot of sense. I really dislike the whole pi_check function as a whole, and I'm not surprised there's more hard-coded weirdness in it, to be honest... I agree that that line you've highlighted should also be a comparison to eps.

I updated the respective line to properly compare against eps and adjusted the tests to reflect the change.
I believe that the function itself has it's purpose but it also has the potential of introducing unnecessary approximations without people actively being aware of them.

(I can't do a proper review on GitHub right now because it seems to have bugged out a little, and it's showing commits that aren't actually on the branch. Don't worry about it - your branch is actually fine, it's just GitHub being weird, and it'll fix itself in a bit, I'm sure.)

Something seems to have gotten messed up in the git history. I rebased the commits and changes now show up properly again.
This should be ready for a proper review now.

@burgholzer
Copy link
Contributor Author

@jakelishman gentle ping so this doesn't go stale again.

jakelishman
jakelishman previously approved these changes Jul 22, 2022
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 read it quickly, and all the code looks sensible to me, thank you! I'm actually on paternity leave for the next few weeks, so it's difficult for me to check completely properly. @1ucian0, please could you take over and merge if you're happy?

@burgholzer
Copy link
Contributor Author

@1ucian0 @jakelishman gentle ping. It's been another month 🙃

@jakelishman jakelishman assigned jakelishman and unassigned 1ucian0 Aug 20, 2022
@1ucian0 1ucian0 self-requested a review November 15, 2022 19:50
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 once more for this falling through the cracks. I've fixed the merge conflict that had arisen, and we can merge this for the 0.23 series.

@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog automerge mod: qasm2 Relating to OpenQASM 2 import or export labels Nov 18, 2022
@burgholzer
Copy link
Contributor Author

No worries, thanks for getting back!

@coveralls
Copy link

Pull Request Test Coverage Report for Build 3499080974

  • 10 of 11 (90.91%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 84.583%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/tools/pi_check.py 10 11 90.91%
Totals Coverage Status
Change from base Build 3498826182: 0.001%
Covered Lines: 62524
Relevant Lines: 73920

💛 - Coveralls

@mergify mergify bot merged commit 80d12e0 into Qiskit:main Nov 18, 2022
Cryoris pushed a commit to Cryoris/qiskit-terra that referenced this pull request Jan 12, 2023
* use tighter `eps` of `1e-12` in `qiskit.circuit.instruction.Instruction.qasm`

* change to `double` precision (`ndigits=16`) in `qiskit.circuit.instruction.Instruction.qasm`

* add tests

* refactor `pi_check` to use default float formatting when `ndigits=None`

* add release notes

* fix tests

* use parameter `eps` in check for zero in `pi_check`

* fix tests by changing default `eps` to `1e-9`

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: qasm2 Relating to OpenQASM 2 import or export
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Truncating floats in QASM dump alters circuit's functionality
8 participants