Open timmintam opened 1 month ago
Good catch @timmintam, I think you are right and the generator should be set in the init, not every time users call run
. And this issue extends to the StatevectorEstimator
too.
As for StatevectorEstimator
, it is an intentional behavior to fix #12519. See #12862 for details.
I recalled that I first implemented StatevectorSampler
https://github.com/Qiskit/qiskit/pull/11566#event-11509453112 in the same way as proposed in this issue.
https://github.com/Qiskit/qiskit/blob/e5f8cdbd9ebeaad2769144faf617d4fabcd52488/qiskit/primitives/statevector_sampler.py#L58-L67
But, I modified it in the current way to apply @chriseclectic's suggestion https://github.com/Qiskit/qiskit/pull/11566#pullrequestreview-1827508583
So, we need to discuss the int seed behavior with @chriseclectic.
Environment
What is happening?
Suppose you have a
StatevectorSampler
with theseed
set to an integer. Then if you run the same circuit multiple times, it will always sample the same outcomes.The same issue probably affects
StatevectorEstimator
.How can we reproduce the issue?
Output:
What should happen?
It should give different outcomes for each
pub
and for each call to therun
method, even if the circuits are the same. If you set theseed
to anumpy.random.Generator
it has the expected behavior. For instance keeping the code snippet from above but only changing the seed asthen it will output:
Any suggestions?
Currently the
StatevectorSampler
class stores the private attribute_seed
as it is (eitherGenerator
,int
orNone
). Therun
method iterates over the circuits. Inside thisfor
loop, it fixes the seed of thestatevector
, which transforms the integerseed
into aGenerator
when needed. It means the exact sameGenerator
is instantiated each single time. Instead we could transform an integer seed into aGenerator
at the initiliazation ofStatevectorSampler
objects, such that the attribute_seed
is either aGenerator
orNone
. For instance: