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

Avoid lossing precision when scaling frequencies #12392

Merged
merged 2 commits into from May 16, 2024

Conversation

iyanmv
Copy link
Contributor

@iyanmv iyanmv commented May 12, 2024

Summary

Classes in pulse_instruction.py scale frequency values to GHz by multipliying ParameterExpression with float 1e9. This can lead to numerical errors on some systems due to symengine "rounding" errors. Instead, this scaling can be done multiplying by integer 10**9.

Details and comments

In this unit test

def test_parameterized_shift_frequency(self):

The frequency string "f / 1000" gets converted to ParameterExpression(1000000.0*f) after ParameterExpression(f/1000) is multiplied by 1e9. For some unknown reason, when the symbol f is later substituted with the value 3.14, and the RealDouble is converted to float, an error is introduced that can't be fixed by

# Remove truncation error and convert the result into Python builtin type.
# Value could originally contain a rounding error, e.g. 1.00000000001
# which may occur during the parameter expression evaluation.
evaluated = np.round(operand, decimals=decimal).item()

This fixes: #12359 (comment)

Upstream issue: symengine/symengine.py#476

Classes in pulse_instruction.py scale frequency values to GHz by
multipliying `ParameterExpression` with float 1e9. This can lead
to numerical errors on some systems using symengine. Instead, this
scaling can be done multiplying by integer 10**9.

See: Qiskit#12359 (comment)
@iyanmv iyanmv requested a review from a team as a code owner May 12, 2024 15:22
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 12, 2024
@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
  • @nkanazawa1989

@coveralls
Copy link

coveralls commented May 12, 2024

Pull Request Test Coverage Report for Build 9100081845

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 35 unchanged lines in 6 files lost coverage.
  • Overall coverage increased (+0.004%) to 89.635%

Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/expr.rs 2 94.26%
crates/qasm2/src/lex.rs 4 92.62%
crates/qasm2/src/parse.rs 6 97.61%
qiskit/dagcircuit/dagnode.py 8 88.12%
crates/circuit/src/circuit_instruction.rs 13 87.59%
Totals Coverage Status
Change from base Build 9037095581: 0.004%
Covered Lines: 62311
Relevant Lines: 69516

💛 - Coveralls

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 so much for all the work to track this down! This seems like an easy change to make - in 99% of cases, Python's going to immediately convert the int to a float, but in the case that the pulse is symbolic, it's good not to introduce the floating-point error into the calculation.

I pushed up a release note, but otherwise this is good to go.

@jakelishman jakelishman added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog labels May 15, 2024
@jakelishman jakelishman added this to the 1.1.0 milestone May 15, 2024
@jakelishman jakelishman added this pull request to the merge queue May 16, 2024
Merged via the queue into Qiskit:main with commit 96607f6 May 16, 2024
15 checks passed
mergify bot pushed a commit that referenced this pull request May 16, 2024
* Avoid lossing precision when scaling frequencies

Classes in pulse_instruction.py scale frequency values to GHz by
multipliying `ParameterExpression` with float 1e9. This can lead
to numerical errors on some systems using symengine. Instead, this
scaling can be done multiplying by integer 10**9.

See: #12359 (comment)

* Add release note

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 96607f6)
github-merge-queue bot pushed a commit that referenced this pull request May 16, 2024
* Avoid lossing precision when scaling frequencies

Classes in pulse_instruction.py scale frequency values to GHz by
multipliying `ParameterExpression` with float 1e9. This can lead
to numerical errors on some systems using symengine. Instead, this
scaling can be done multiplying by integer 10**9.

See: #12359 (comment)

* Add release note

---------

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

Co-authored-by: Iyán <me@iyanmv.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 Community PR PRs from contributors that are not 'members' of the Qiskit repo stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Some unit tests crash when using symengine
4 participants