-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 ParameterExpression.numeric
to cast to number
#11109
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
releasenotes/notes/parameterexpression.numeric-958d365dadabfb81.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
features: | ||
- | | ||
A new method :meth:`.ParameterExpression.numeric` is added, which converts a fully bound | ||
parameter expression into the most restrictive builtin Python numeric type that accurately | ||
describes the result of the symbolic evaluation. For example, a symbolic integer will become an | ||
:class:`int`, while a symbolic real number will become a :class:`float` and a complex number | ||
will become a :class:`complex`. This method includes several workarounds for peculiarities of | ||
the evaluation contexts of ``symengine``, which can sometimes lead to spurious results when | ||
calling :class:`complex` or :class:`float` on an expression directly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2005,6 +2005,32 @@ def test_bound_expression_is_real(self): | |
bound = x.bind({x: 1 + 1j}) | ||
self.assertEqual(bound.is_real(), False) | ||
|
||
def test_numeric(self): | ||
"""Tests of the 'numeric' method.""" | ||
a, b = Parameter("a"), Parameter("b") | ||
one_int = (1 + a).assign(a, 0) | ||
self.assertIsInstance(one_int.numeric(), int) | ||
self.assertEqual(one_int.numeric(), 1) | ||
one_float = (1.0 + a).assign(a, 0.0) | ||
self.assertIsInstance(one_float.numeric(), float) | ||
self.assertEqual(one_float.numeric(), 1.0) | ||
one_imaginary = (1j + a).assign(a, 0.0) | ||
self.assertIsInstance(one_imaginary.numeric(), complex) | ||
self.assertEqual(one_imaginary.numeric(), 1j) | ||
|
||
# This is one particular case where symengine 0.9.2 (and probably others) struggles when | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This corner case can be removed with #11315 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd leave it in regardless - it still works as a regression test for us. |
||
# evaluating in the complex domain, but gets the right answer if forced to the real domain. | ||
# It appears more commonly because `symengine.Basic.subs` does not simplify the expression | ||
# tree eagerly, so the `_symbol_expr` is `0.5 * (0.5)**2`. Older symengines then introduce | ||
# a spurious small imaginary component when evaluating this `Mul(x, Pow(y, z))` pattern in | ||
# the complex domain. | ||
problem = (0.5 * a * b).assign(b, 0.5).assign(a, 0.5) | ||
self.assertIsInstance(problem.numeric(), float) | ||
self.assertEqual(problem.numeric(), 0.125) | ||
|
||
with self.assertRaisesRegex(TypeError, "unbound parameters"): | ||
(a + b).numeric() | ||
|
||
|
||
class TestParameterEquality(QiskitTestCase): | ||
"""Test equality of Parameters and ParameterExpressions.""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should this be a math.isclose? Zero-checking floats is always hard to me...
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.
Personally I'd say no - the only times we've seen uses where bad imaginary components enter is from bugs within Symengine itself (which this function works around), where the coefficient scales with the operation being done, so could be arbitrarily large. Auto clipping to zero also makes it tricky for people who were expecting to deal with very small numbers (which floating point is good at).
As an interface thing, a user can always clip the output themselves if needed, so I think not clipping is the safer choice.