-
Notifications
You must be signed in to change notification settings - Fork 4
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
Use numpy random Generator for random numbers #52
Comments
When I looked at this before, rustowrkx, qiskit, matplotlib, and scipy all had numpy<2.0 pinned. We cant use Generators until they all support it. Will take another look and see if all dep softwsre has bumped to supporting 2.0 |
Generator has been the recommended way of generating random numbers since Numpy 1.17: https://numpy.org/doc/1.17/reference/random/generator.html |
The way the same integer seed is passed around in the chemistry tutorial might be statistically invalid. Again, a single Generator instance should be created and passed instead. |
@kevinsung , I don't quite understand why a single seed, used in different contexts, would cause some degeneracy. All it does is fix the randomness for each of these functions to make things deterministic. If the seed is causing some anomalous behavior, you can just change it and make sure that behavior stops. Could you please explain what you mean here? |
An integer seed is used to set the internal state of a pseudo-random number generator (PRNG). If the same seed is used at two different points in time, then the PRNG will have the same internal state at those different points in time, and its results won't be statistically independent.
The performance of SQD relies on the random numbers drawn at different points of time being statistically independent. While seeding is needed to make results deterministic, care needs to be taken when seeding to ensure that the results are statistically valid. |
Cool, that makes sense. Avoiding global variables is almost always a good thing |
It's not that the variable is global, it's that it's an integer, which resets the PRNG to the same state every time. Instead of an integer, it should be a Generator object, which will be used directly without having its state reset. So it's more like, you should have a single Generator object that's initialized once globally and used everywhere. |
What should we add?
Changing the global random state like this is discouraged:
qiskit-addon-sqd/qiskit_addon_sqd/configuration_recovery.py
Line 213 in 7ddfb21
Instead, a Generator object should be initialized using
np.random.default_rng(seed)
and used.The text was updated successfully, but these errors were encountered: