1
$\begingroup$

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.

First execution Second execution

Someone else has found this issue?

$\endgroup$
1
  • 1
    $\begingroup$ 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! $\endgroup$
    – Eenoku
    Aug 1, 2021 at 11:39

2 Answers 2

0
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Thanks Steve for your answer. I've tried adding this random_state to quantum_instance , but the random result continues happen. $\endgroup$
    – Eva Andres
    Jul 11, 2021 at 22:06
  • $\begingroup$ 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. $\endgroup$
    – Steve Wood
    Jul 11, 2021 at 22:19
0
$\begingroup$

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!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.