1
$\begingroup$

The callback for the VQC class is defined as:

callback (Optional[Callable[[ndarray, float], None]]) – a reference to a user’s callback function that has two parameters and returns None. The callback can access intermediate data during training. On each iteration, an optimizer invokes the callback and passes current weights as an array and a computed value as a float of the objective function being optimized. This allows to track how well the optimization / training process is going on.

The function that I am passing to that argument is:

weights = []
loss = []
def store_intermediate_result(current_weights, current_loss):
    print(current_loss)
    weights.append(np.copy(current_weights))
    loss.append(current_loss)

However, the loss value passed back is always the same. I had this issue before with the weights, however, when I printed the weights they were different. So it was a problem of just copying the array as you can see in the code sample. But with the loss values, they are always the same no matter what I do.

The vqc code is as follows:

vqc = VQC(feature_map=feature_map,
          ansatz=ansatz,
          loss='cross_entropy', 
          optimizer=AQGD(maxiter=5),
          quantum_instance=QuantumInstance(Aer.get_backend('aer_simulator_statevector')),
         callback=store_intermediate_result)

EDIT: The problem seemed to be the optimiser used. COBYLA works but AQGD does not. The issue has been raised on GitHub.

$\endgroup$
4
  • $\begingroup$ Maybe this following tutorial notebook will help. It has been updated recently to add plots via the callback, including one for VQC github.com/Qiskit/qiskit-machine-learning/blob/main/docs/… (This updated version will be published on the main Qiskit Machine Learning tutorial documentation page, for now its still the prior version that does not have the plots otherwise I would have linked that rather than the repository) $\endgroup$
    – Steve Wood
    Sep 3 at 18:48
  • $\begingroup$ Hello Steve, thank you so much for your reply. I tried the example notebook you pointed me too but it still didn't work sadly. However, I noticed that I am on the newest official release of 0.2.1 for qiskit-machine-learning. In the notebook however, the qiskit-machine-learning version is 0.3.0. How can I update to the latest version? Should I just install from source? Thank you. $\endgroup$ Sep 3 at 21:58
  • $\begingroup$ The notebook may say 0.3.0 since it was run against main when checked in. However it works in 0.2.1. I just created a clean environment., installed the latest qiskit and qiskit-machine-learning 0.2.1 and the notebook runs to completion successfully for me. If you still have some specific issue that does not work then might I suggest raising an Issue on qiskit-machine-learning repo and include a small working code example that demonstrates the error. $\endgroup$
    – Steve Wood
    Sep 9 at 20:55
  • $\begingroup$ Thank you. I found out that the optimiser was the problem, not the software version. The issue has been raised on GitHub. $\endgroup$ Sep 10 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.