In the sample code of Gaussian Bose sampling, what role does the random seed set play?
np.random.seed(42)
In the sample code of Gaussian Bose sampling, what role does the random seed set play?
np.random.seed(42)
Hi @hhx,
Welcome to the Xanadu community!
By setting the seed to 42, or really any other constant, we guarantee the results reproducibility.
If you don’t do it, and you don’t really need to do it, the results will be different every time.
Please let me know if you have any other questions.
Thank you for your timely reply. According to your answer, I tried to change this number or delete this line of code, but the result did not change. I don’t know where this line of code is reflected in the whole program, or can you answer why setting this line of code can ensure the reproducibility of the result? Thanks!
Hi @hhx,
The np.random.seed(42)
line of code reseeds the random number generator, this is common practice for keep the results reproducible , as I said. You can have more information about this function and alternatives in the numpy documentation.
You can more easily see the role this function is playing with this small example:
import numpy as np
np.random.seed(42)
np.random.rand(5)
If you run several times this piece of code you’ll see that the numbers will always be the same.
You can now try with:
import numpy as np
np.random.seed(101010)
np.random.rand(5)
and you will see that the numbers are different but always the same.
In the Quantum advantage with Gaussian Boson Sampling tutorial notebook you can see the difference between different seeds, or the absence of one, in the results printed by the 6th cell:
# Fock states to measure at output
measure_states = [(0,0,0,0), (1,1,0,0), (0,1,0,1), (1,1,1,1), (2,0,0,0)]
# extract the probabilities of calculating several
# different Fock states at the output, and print them out
for i in measure_states:
print(f"|{''.join(str(j) for j in i)}>: {probs[i]}")
Please let me know if you have any other questions.