-
Notifications
You must be signed in to change notification settings - Fork 6
allow theta and epsilon to be float #9
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
shun0923
left a comment
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.
When changing the dps for theta or epsilon, converting via a string:
theta = mpmath.mpmathify(f"{theta}")can introduce unintended rounding errors if theta is already an mpf.
It might be better to convert without the intermediate string, for example:
theta = mpmath.mpmathify(theta)For example, with:
mpmath.mp.dps = 20
epsilon = 1e-20
theta = mpmath.mpmathify("2.1")the latter preserves the value of theta (repr(theta) remains unchanged),
while the former (current code) alters it.
|
Thanks for the quick review. Could you give me an example for the issue? |
|
You can see the rounding issue with the following code: import mpmath
mpmath.mp.dps = 20
theta = mpmath.mpmathify("2.1")
mpmath.mp.dps = 65
theta1 = mpmath.mpmathify(f"{theta}")
theta2 = mpmath.mpmathify(theta)
print(repr(theta))
print(theta == theta1, repr(theta1))
print(theta == theta2, repr(theta2))
# mpf('2.1000000000000000000013552527156068805425093160010874271392822265625')
# False mpf('2.1000000000000000000013552527156068805425093160010874271392822266005')
# True mpf('2.1000000000000000000013552527156068805425093160010874271392822265625')Here, when converting via the intermediate string ( |
|
Additional note for when If However, the conversion without going through a string actually preserves the value that more accurately represents the actual Therefore, if you want mpmath.mp.dps = <suitable precision>
theta = mpmath.mpmathify("0.3")In summary, extra care is needed when passing |
|
I see your point, thanks. # outside gridsynth
theta = np.pi/8
# inside gridsynth
theta = mpmath.mpmathify(theta)Shall we even have |
|
I think it’s fine to allow floats, since there are cases where only an error at the level of float precision is required, and in such situations it can be more convenient to pass a float directly. |
In
gridsynth_gates(theta, eps)we assumed thatthetaandepsare mpmath objects, with its precision tuned properly bympmath.mp.dps.I made a change reflecting PRs #5 and #6 so that the following both works.
@shun0923 can you check if these changes are ok, upon your convenience?