-
Notifications
You must be signed in to change notification settings - Fork 985
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
Approximately equal circuits with symbols not detected by cirq.approx_eq #3195
Conversation
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, thank you! I added two minor nits.
Can you also change in the description of the PR ISSUE-3192 to "Fixes #3192." ? That will automatically close the issue when closing the PR!
@@ -94,6 +95,11 @@ def approx_eq(val: Any, other: Any, *, atol: Union[int, float] = 1e-8) -> bool: | |||
if isinstance(val, str): | |||
return val == other | |||
|
|||
if isinstance(val, sympy.core.mul.Mul) or isinstance( | |||
other, sympy.core.mul.Mul): | |||
delta = sympy.simplify(sympy.Abs(other - val)) |
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.
Why do we need the simplify here?
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.
If I don't do that, the unit test fails. Upon inspection, if I print delta
, I see delta=Abs((s + 1.51)/pi - (s + 1.515)/pi)
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.
Got it - however, this is still not kosher. There might be a case where you can't evaluate to a number literal and then this question is undecidable. We should handle that and throw an error instead of silently returning False.
sympy.Abs(val - other).simplify().is_number
should give you a good indication whether the simplification worked.
Please add a test case covering that too.
Thanks. PTAL. |
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.
I dug a bit deeper, added some more comments! Thank you!
@@ -94,6 +95,11 @@ def approx_eq(val: Any, other: Any, *, atol: Union[int, float] = 1e-8) -> bool: | |||
if isinstance(val, str): | |||
return val == other | |||
|
|||
if isinstance(val, sympy.core.mul.Mul) or isinstance( | |||
other, sympy.core.mul.Mul): | |||
delta = sympy.simplify(sympy.Abs(other - val)) |
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.
Got it - however, this is still not kosher. There might be a case where you can't evaluate to a number literal and then this question is undecidable. We should handle that and throw an error instead of silently returning False.
sympy.Abs(val - other).simplify().is_number
should give you a good indication whether the simplification worked.
Please add a test case covering that too.
Fixes #2492 |
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.
One last nit, thank you for the changes!
…_eq (quantumlib#3195) Approximately equal circuits with symbols not detected by cirq.approx_eq (Fixes quantumlib#3192.) We should use sympy to make comparisons when available.
Approximately equal circuits with symbols not detected by cirq.approx_eq (Fixes #3192.)
We should use sympy to make comparisons when available.