Qiskit / rustworkx

A high performance Python graph library implemented in Rust.
https://www.rustworkx.org
Apache License 2.0
1.09k stars 150 forks source link

[joss] How to set a seed to get deterministic results? #508

Open szhorvat opened 2 years ago

szhorvat commented 2 years ago

Some functions use random numbers. How can I set a seed for retworkx's random number generator, in order to make an entire analysis pipelines reproducible? It does not appear to be using Python's own random number generator, as it does not react to random.seed().

I see that some individual functions have a seed argument. But suppose I wrote a script with several commands in it. How I can I set a seed for the entire calculation, at the beginning of the script?

mtreinish commented 2 years ago

There currently isn't support for a global seed in retworkx. Only a few functions, mainly the random graph generators and some of the layout functions, use a rng and each of them instantiates a new rng object in rust when the function is called (https://github.com/Qiskit/retworkx/blob/main/src/random_graph.rs#L70-L73 ).

But having a pattern for doing this is a good idea for an enhancement to make though or at least having a way to manage seeds in a shared way even if not truly global would be useful.

IvanIsCoding commented 2 years ago

We could suggest a pattern of using a deterministic stream to generate seeds, and then passing those seeds to our functions. Something along those lines:

import retworkx as rx
import numpy as np

rng = np.random.default_rng(seed=42)

graph = rx.undirected_gnp_random_graph(100, 0.5, seed=rng.integers(2**64))
other_graph = rx.undirected_gnp_random_graph(100, 0.5, seed=rng.integers(2**64))

This code should always be reproducible, because the NumPy generator is reproducible. I will also say that overall avoiding hidden global states is a good thing, I think Numpy is trying to move away from it.