# Random accuracy using VQC and Iris dataset

Using the same seed (seed_simulator and seed_transpiler) I get different accuracy when running it several times. In Aqua library (from qiskit.aqua.algorithms import VQC) I solved it adding aqua_globals.random_seed. In machine learning (from qiskit_machine_learning.algorithms import VQC) we have the equivalent: algorithm_globals.random_seed. But the problem continues.

Someone else has found this issue?

• Usually, it's preferable to provide a whole MWE with code snippets included accordingly, not in the form of screenshots ;) And also, welcome to SE! Aug 1 at 11:39

QSVC is really the sklearn SVC passing the quantum kernel to the SVC. Now I see SVC has a 'random_state' argument on its constructor to control any randomness of the SVC within sklearn - see https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html and https://scikit-learn.org/stable/common_pitfalls.html#controlling-randomness

Since QSVC accepts kwargs, so any of these SVC params can be set via the QSVC constructor, you should be able to add a random_state=someseed parameter when you create the QSVC. Try that and see if it helps. I note the unit tests we have in qiskit optimization do not set this and pass each time - i.e. are reproducible despite that, maybe since the data it uses is way simpler.

Now perhaps internally QSVC ought to automatically pass on up the algorithm_globals as the random_state. If that works out for you perhaps you would like to raise an issue on qiskit-optimization repo in this regard.

Update: I think I mistakenly answered in respect to the wrong algorithm! You were asking about VQC not QSVC.

For VQC an initial_point was setting has been added recently to allow the starting point for the optiimzation it to be set. In what is released it uses a random initial point which may explain the variability you are seeing. Also I see (even in the updated code when it uses a random initial point) it uses the numpy random directly rather than algorithm global random generator. So seeding the numpy random generator could help avoid the randomness.

• Thanks Steve for your answer. I've tried adding this random_state to quantum_instance , but the random result continues happen. Jul 11 at 22:06
• random_state was for the QSVC constructor. But I realized, when I looked back over my answer earlier, that you were asking about QVC, For QVC it starts at a random initial point that is determined directly from numpy,random - try setting numpy.random.seed which should make the outcome predictable. Arguably QVC should use the common algorithms random generator, but presently it seems it does not. Jul 11 at 22:19

Thank Steve! Finally, it works initializing the qubits and save_statevector and probabilities

ans.initialize([1/np.sqrt(2), -1/np.sqrt(2)], 0)
ans.initialize([1/np.sqrt(2), -1/np.sqrt(2)], 1)
vqc.circuit.save_state()
vqc.circuit.save_probabilities()
vqc.circuit.save_statevector()

Regards!