Qiskit / qiskit-aer

Aer is a high performance simulator for quantum circuits that includes noise models
https://qiskit.github.io/qiskit-aer/
Apache License 2.0
464 stars 354 forks source link

AerSimulator seed should accept Numpy random Generator #2149

Open kevinsung opened 1 month ago

kevinsung commented 1 month ago

What is the expected behavior?

This should work:

import numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit_aer import AerSimulator

rng = np.random.default_rng()
sim = AerSimulator(seed_simulator=rng)
sim.run(QuantumCircuit()).result()
TypeError: invalid option type: name=seed_simulator, type=Generator, expected=int
hhorii commented 1 month ago

@kevinsung Could you tell the situation that application specifies a random number generator of numpy as a seed instead of int value? I know Qiskit takes a random number generator of numpy as a seed. Do you need to reuse the same instance?

kevinsung commented 1 month ago

Yes, exactly, if you already have a random Generator instance, it's best to use it everywhere to ensure that the results are as "random" as possible.

chriseclectic commented 1 month ago

@kevinsung The issue with this request is the seed option is only used in the C++ code which has no idea about numpy objects and can only handle int values to initialize the C++ rng generator. You would need to convert the Python object into an int for the C++ code to be able to use.

kevinsung commented 1 month ago

To work around this issue I've been passing seed_simulator=rng.integers(2**32). I'm not sure if this is totally valid, but if something like this is valid, then the AerSimulator can do it itself.