-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Improve casting and error message for ParameterExpression
#10244
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
Conversation
Previously, we assumed that the only reason a cast of `ParameterExpression` to `complex`/`float`/`int` could fail was because of unbound parameters, and emitted an error accordingly. This is not the case; a fully bound complex value will still fail to convert to `float`. This PR improves the error messages in these cases, and works around a difference of Sympy and Symengine, where the latter will fail to convert real-valued expressions that were symbollically complex at some point in their binding history to `float`. Sympy more reliably reduces values down to real-only values when the imaginary part is exactly cancelled, which is a use-case our users tend to expect.
One or more of the the following people are requested to review this:
|
Pull Request Test Coverage Report for Build 5213303912
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once we get validation from wshanks.
Co-authored-by: Kevin Hartman <kevin@hart.mn>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. In working on #10183, I did not personally have a case where a complex expression needed to evaluate back to a real value. Where I ran into that problem was that my initial change caused this test to fail until I put in a workaround:
Since that test is still passing here, I think the change here is good. I checked that the example in #9187 also ran without error.
Thanks Will. I'm personally torn on things like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* Improve casting and error message for `ParameterExpression` Previously, we assumed that the only reason a cast of `ParameterExpression` to `complex`/`float`/`int` could fail was because of unbound parameters, and emitted an error accordingly. This is not the case; a fully bound complex value will still fail to convert to `float`. This PR improves the error messages in these cases, and works around a difference of Sympy and Symengine, where the latter will fail to convert real-valued expressions that were symbollically complex at some point in their binding history to `float`. Sympy more reliably reduces values down to real-only values when the imaginary part is exactly cancelled, which is a use-case our users tend to expect. * Fix typo in test * Update releasenotes/notes/parameter-float-cast-48f3731fec5e47cd.yaml Co-authored-by: Kevin Hartman <kevin@hart.mn> --------- Co-authored-by: Kevin Hartman <kevin@hart.mn> (cherry picked from commit 86df15d) # Conflicts: # test/python/circuit/test_parameters.py
* Improve casting and error message for `ParameterExpression` Previously, we assumed that the only reason a cast of `ParameterExpression` to `complex`/`float`/`int` could fail was because of unbound parameters, and emitted an error accordingly. This is not the case; a fully bound complex value will still fail to convert to `float`. This PR improves the error messages in these cases, and works around a difference of Sympy and Symengine, where the latter will fail to convert real-valued expressions that were symbollically complex at some point in their binding history to `float`. Sympy more reliably reduces values down to real-only values when the imaginary part is exactly cancelled, which is a use-case our users tend to expect. * Fix typo in test * Update releasenotes/notes/parameter-float-cast-48f3731fec5e47cd.yaml Co-authored-by: Kevin Hartman <kevin@hart.mn> --------- Co-authored-by: Kevin Hartman <kevin@hart.mn>
…10268) * Improve casting and error message for `ParameterExpression` Previously, we assumed that the only reason a cast of `ParameterExpression` to `complex`/`float`/`int` could fail was because of unbound parameters, and emitted an error accordingly. This is not the case; a fully bound complex value will still fail to convert to `float`. This PR improves the error messages in these cases, and works around a difference of Sympy and Symengine, where the latter will fail to convert real-valued expressions that were symbollically complex at some point in their binding history to `float`. Sympy more reliably reduces values down to real-only values when the imaginary part is exactly cancelled, which is a use-case our users tend to expect. * Fix typo in test * Update releasenotes/notes/parameter-float-cast-48f3731fec5e47cd.yaml --------- Co-authored-by: Jake Lishman <jake@binhbar.com> Co-authored-by: Kevin Hartman <kevin@hart.mn>
…0244) * Improve casting and error message for `ParameterExpression` Previously, we assumed that the only reason a cast of `ParameterExpression` to `complex`/`float`/`int` could fail was because of unbound parameters, and emitted an error accordingly. This is not the case; a fully bound complex value will still fail to convert to `float`. This PR improves the error messages in these cases, and works around a difference of Sympy and Symengine, where the latter will fail to convert real-valued expressions that were symbollically complex at some point in their binding history to `float`. Sympy more reliably reduces values down to real-only values when the imaginary part is exactly cancelled, which is a use-case our users tend to expect. * Fix typo in test * Update releasenotes/notes/parameter-float-cast-48f3731fec5e47cd.yaml Co-authored-by: Kevin Hartman <kevin@hart.mn> --------- Co-authored-by: Kevin Hartman <kevin@hart.mn>
Summary
Previously, we assumed that the only reason a cast of
ParameterExpression
tocomplex
/float
/int
could fail was because of unbound parameters, and emitted an error accordingly. This is not the case; a fully bound complex value will still fail to convert tofloat
.This PR improves the error messages in these cases, and works around a difference of Sympy and Symengine, where the latter will fail to convert real-valued expressions that were symbollically complex at some point in their binding history to
float
. Sympy more reliably reduces values down to real-only values when the imaginary part is exactly cancelled, which is a use-case our users tend to expect.Details and comments
Fix #9187
Fix #10191
@wshanks: could I ask you to test this with one of your previous failing examples, just to check any regression? I think we included all the relevant parts in our test suite, but I don't want to accidentally regress for you.