Skip to content
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

Closed
kevinsung opened this issue Sep 20, 2024 · 7 comments · Fixed by #94
Closed

Use numpy random Generator for random numbers #52

kevinsung opened this issue Sep 20, 2024 · 7 comments · Fixed by #94
Milestone

Comments

@kevinsung
Copy link
Collaborator

What should we add?

Changing the global random state like this is discouraged:

Instead, a Generator object should be initialized using np.random.default_rng(seed) and used.

@caleb-johnson
Copy link
Collaborator

caleb-johnson commented Sep 20, 2024

What should we add?

Changing the global random state like this is discouraged:

Instead, a Generator object should be initialized using np.random.default_rng(seed) and used.

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

@kevinsung
Copy link
Collaborator Author

kevinsung commented Sep 20, 2024

Generator has been the recommended way of generating random numbers since Numpy 1.17: https://numpy.org/doc/1.17/reference/random/generator.html

@caleb-johnson caleb-johnson modified the milestones: 0.6.0, 0.7.0 Sep 20, 2024
@kevinsung
Copy link
Collaborator Author

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.

@caleb-johnson
Copy link
Collaborator

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?

@kevinsung
Copy link
Collaborator Author

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.

All it does is fix the randomness for each of these functions to make things deterministic.

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.

@caleb-johnson
Copy link
Collaborator

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.

All it does is fix the randomness for each of these functions to make things deterministic.

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

@kevinsung
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants