-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 pow as supported operation for ParameterExpression #11235
Add pow as supported operation for ParameterExpression #11235
Conversation
Adds powering for qiskit.circuit.ParameterExpression allowing to use a**n, n**a, a**b, pow(a,b) etc. for ParameterExpressions a, b and numbers n. Minimal change using default support of pow by Sympy/Symengine. Added pow to list of operators in TestParameterExpressions test case for unit testing. fixes Qiskit#8959 Changelog: New Feature
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:
|
Pull Request Test Coverage Report for Build 6914056656
💛 - Coveralls |
It seems that the Windows version (of Symengine I assume) introduces a small numerical error in pow that leads to some of the tests to fail. Not sure how to deal with this? Just loosen the test to only assert almost equal? |
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.
Hmm, this is a slightly tricksy question. On the one hand, I'm not keen to relax the tolerances of the test, because the expressions are a simple binary op, and the floating-point evaluation should be bit-for-bit identical between the two cases. On the other, complex-number powers are a mathematically tricky operation to perform, so they're not really akin to the simple +
, -
, *
and /
operations otherwise tested.
The failing test case is pow(-1, 2.3)
, which in pure real-number arithmetic should be equal to (0.5877852522924731+0.8090169943749475j)
, which notably isn't the returned result from either component, so there's already a few-ULP difference in both pow(-1, 2.3)
and the symbolic form. That's quite to be expected, since even one of the basic simplifications I did ([2, 4)
interval being different to the [0.25, 0.5)
interval: 2.3 - 2 != 0.3
. That gets even more tricky when introducing pi
, and that the power is eventually going to be evaluated by summing power series in some form or another.
I think preferentially I'd like to keep the exact bit-for-bit equality assertion for +
, -
, *
and /
, because those should be bit-for-bit equal. For power, perhaps it would be cleaner to introduce a new test for power. I don't mind too much if we want to make that use floating-point numbers that should produce bit-for-bit identical outputs, or if we want to relax the assertions to fuzzy matches with ~5 ULP tolerance (or whatever's appropriate).
The actual code and release note of this PR looks totally fine to me, thanks.
@jakelishman Sounds good. I will add new tests for pow then (probably this weekend). Will opt for the fuzzy matches (mainly because the issue doesn't appear on Linux and I have no Windows environment for testing, so this seems easier). PS: it looks like there are no tests for exp, log, sin, ...? or am I missing those? |
In |
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 great, thanks for the new tests!
Summary
Adds powering for
qiskit.circuit.ParameterExpression
allowing to usea**n
,n**a
,a**b
,pow(a,b)
etc. forParameterExpression
sa
,b
and numbersn
.Details and comments
Minimal change using default support of
pow
by Sympy/Symengine.Also added
pow
to list of operators inTestParameterExpressions
test case for unit testing.Fixes #8959
Changelog: New Feature