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

CircuitMPS.sample with seed returns the same bitstring for every shot #247

Closed
kevinsung opened this issue Jul 19, 2024 · 2 comments · Fixed by #248
Closed

CircuitMPS.sample with seed returns the same bitstring for every shot #247

kevinsung opened this issue Jul 19, 2024 · 2 comments · Fixed by #248
Labels

Comments

@kevinsung
Copy link
Contributor

kevinsung commented Jul 19, 2024

What happened?

  • Create a CircuitMPS that flips a coin.
  • Draw 50 samples from it.
    • Do this with seed=None. Behavior is as expected.
    • Do this with seed set to some random integers. In each instance, the same bit is measured every single time.

What did you expect to happen?

The same bit should not be returned every time. It's a coin flip.

Minimal Complete Verifiable Example

import quimb.tensor

circ = quimb.tensor.CircuitMPS(1)
circ.apply_gate("H", 0)

shots = 50
for seed in [None, 1613, 3024, 7943]:
    print(f"seed = {seed}")
    samples = "".join(circ.sample(shots, seed=seed))
    print(f"\t{samples}")

Relevant log output

seed = None
	11110111110001001110000100011101110001101110101001
seed = 1613
	11111111111111111111111111111111111111111111111111
seed = 3024
	00000000000000000000000000000000000000000000000000
seed = 7943
	00000000000000000000000000000000000000000000000000

Anything else we need to know?

It looks like the same Generator is being recreated before each shot:

rng = np.random.default_rng(seed)

Instead, the Generator should be created only once, before sampling, and reused for every shot.

This is probably unrelated but I noticed that here quimb sets the random number generator seed in a way that is discouraged:

np.random.seed(seed)

Instead, a Generator should be created with np.random.default_rng and used.

Environment

  • Arch Linux
  • Python 3.11.9
  • quimb 1960e05
@kevinsung kevinsung added the bug label Jul 19, 2024
@kevinsung kevinsung changed the title CircuitMPS.sample with seed gives incorrect results CircuitMPS.sample with seed returns the same bitstring for every shot Jul 19, 2024
@jcmgray
Copy link
Owner

jcmgray commented Jul 19, 2024

Thanks for catching this quickly and the PR! And yes moving to the generator style everywhere would definitely be preferable.

@kevinsung
Copy link
Contributor Author

Thanks for catching this quickly and the PR! And yes moving to the generator style everywhere would definitely be preferable.

Opened #249 to track moving to generator style.

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

Successfully merging a pull request may close this issue.

2 participants