Repository: Qiskit/qiskit

Issue:
QNSPSA produces irreproducible results
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: qiskit-terra                  0.17.0 
- **Python version**:                                             Python 3.7.3
- **Operating system**: MacOS Big Sur             11.6

### What is the current behavior?                     
Executing a `QNSPSA` optimization with the same random seed gives different results.
We checked that the same optimization gives reproducible results with other optimizers such as `ADAM`.

### Steps to reproduce the problem
Run an arbitrary optimization, e.g., VQE with QNSPSA, and compare the results.


### What is the expected behavior?
The same results for the same seed.


### Suggested solutions





Discussion:
Could you post a minimal reproducer?

The [documentation for `SPSA`](https://qiskit.org/documentation/stubs/qiskit.algorithms.optimizers.SPSA.html) (from which `QNSPSA` derives) says

> This component has some function that is normally random. If you want to reproduce behavior then you should set the random number generator seed in the algorithm_globals (``qiskit.utils.algorithm_globals.random_seed = seed``).

Could that have anything to do with it?
When I run the following VQE with QNSPSA I get consistent results, as @jakelishman said if you're not setting your seed using the `algorithm_globals.random_seed` that could be the cause.

```
seed = 50
algorithm_globals.random_seed = seed
qi = QuantumInstance(Aer.get_backend('statevector_simulator'), seed_transpiler=seed, seed_simulator=seed)

ansatz = PauliTwoDesign(2, reps=1, seed=seed)

fidelity = QNSPSA.get_fidelity(ansatz)
my_opt = QNSPSA(fidelity, maxiter=1000)

vqe = VQE(ansatz, optimizer=my_opt, quantum_instance=qi)
result = vqe.compute_minimum_eigenvalue(operator=H2_op)
print(result)
optimizer_evals = result.optimizer_evals
```

2 example results:
<img width="789" alt="Screenshot 2021-09-28 at 10 55 29" src="https://user-images.githubusercontent.com/23662430/135069857-88a1d171-3e39-492a-83eb-c62ef9a4484e.png">
![s1](https://user-images.githubusercontent.com/23662430/135069865-5533ccc3-caba-46a7-9077-30aa8a484a4e.jpg)

We should make sure that the documentation for the `QNSPSA` class also includes the same note about setting the global seed, since users probably won't see it with it just being on a parent class.  Hopefully this is the issue here.
Great! That resolved the problem. I was not aware of the global setting. Thanks for pointing this out.
Great to hear!  I'll just leave this open for now as a reminder to ourselves to update the documentation in the class.

Resolving patch:
diff --git a/qiskit/algorithms/optimizers/qnspsa.py b/qiskit/algorithms/optimizers/qnspsa.py
--- a/qiskit/algorithms/optimizers/qnspsa.py
+++ b/qiskit/algorithms/optimizers/qnspsa.py
@@ -44,6 +44,12 @@ class QNSPSA(SPSA):
     increasing the number of ``resamplings``. This leads to a Monte Carlo-style convergence to
     the exact, analytic value.
 
+    .. note::
+
+        This component has some function that is normally random. If you want to reproduce behavior
+        then you should set the random number generator seed in the algorithm_globals
+        (``qiskit.utils.algorithm_globals.random_seed = seed``).
+
     Examples:
 
         This short example runs QN-SPSA for the ground state calculation of the ``Z ^ Z``


Test patch: