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

StatevectorSampler reuses the same seed for all PUBs #13730

Open
tnemoz opened this issue Jan 24, 2025 · 3 comments
Open

StatevectorSampler reuses the same seed for all PUBs #13730

tnemoz opened this issue Jan 24, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@tnemoz
Copy link
Contributor

tnemoz commented Jan 24, 2025

Environment

  • Qiskit version:
    1.3.0
  • Python version:
    3.10.10
  • Operating system:
    Ubuntu

What is happening?

When initialized with a seed, StatevectorSampler will reuse the same seed for all PUBs instead of generating a RNG once using this seed and then sticking to it. This causes undesired behaviors when running (close to) identical PUBs.

How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler

qc = QuantumCircuit(1)
qc.h(0)
qc.measure_all()

sampler = StatevectorSampler(seed=42)
res = sampler.run([(qc, None, 1) for _ in range(10)]).result()

for r in res:
    print(r.data.meas.get_int_counts())

What should happen?

The code should instantiate a RNG once using the provided seed and then generate subsequent random numbers using this generator for all PUBs without re-instantiate it with the provided seed, so that the experiment is reproducible, but results between PUBs are not correlated.

Any suggestions?

It's definitely a niche problem, that I noticed because I had to run identical QuantumCircuits among different PUBs, so I'd understand if this isn't fixed.

@tnemoz tnemoz added the bug Something isn't working label Jan 24, 2025
@BramDo
Copy link

BramDo commented Jan 29, 2025

The result of running quantum circuit qc 10 times using the StatevectorSampler. It shows some random alternated output, how to see that the results between the pubs are not correlated:
{1: 1}
{0: 1}
{1: 1}
{1: 1}
{0: 1}
{1: 1}
{1: 1}
{1: 1}
{0: 1}
{0: 1}

One could update the run method to use _rng for generating random numbers, but is it necessary?

@tnemoz
Copy link
Contributor Author

tnemoz commented Jan 30, 2025

@BramDo I'm not sure I understand where do the numbers you printed come from. Did you got them running the code I provided (in particular, setting the seed)?

@BramDo
Copy link

BramDo commented Jan 30, 2025

I used the provided code and the seed, however I modified the qiskit source code. Results between the versions are:

print(qiskit.version)
1.1.0

Result
{1: 1}
{1: 1}
{1: 1}
{1: 1}
{1: 1}
{1: 1}
{1: 1}
{1: 1}
{1: 1}
{1: 1}

Modified version
{1: 1}
{0: 1}
{1: 1}
{1: 1}
{0: 1}
{1: 1}
{1: 1}
{1: 1}
{0: 1}
{0: 1}

change the statevector_sampler.py init:
self._rng = np.random.default_rng(seed) if seed is not None else np.random.default_rng()

In run methode:
#final_state.seed(self._seed)
final_state.seed(self._rng)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants