I asked this question in the Pennylane forum, but there was no reply for a long time, the link is: https://discuss.pennylane.ai/t/why-does-the-embedding-metric-learning-case-not-work/2211?u=rx1

The corresponding demo is https://github.com/PennyLaneAI/qml/blob/66eadc288823bdd4dabea66efc9d1ad341db999a/demonstrations/tutorial_embeddings_metric_learning.py

Main Ques. is enter image description here How can I set up this demo so that the parameters that need to be trained can be trained properly, otherwise it is not feasible to train according to the current demo. That is, how to set 'requires_grad' attribute or 'argnum' keyword for pars.

  • $\begingroup$ It looks like the tutorial file you linked is from May 2020 and in the latest version of that Pennylane/qml repo, does not exist. There's a strong possibility the tutorial isn't compatible with the latest version of Pennylane. What version of Pennylane are you using to run that code? $\endgroup$
    – ryanhill1
    Oct 10, 2022 at 14:37
  • $\begingroup$ pennylane ver. 0.26 $\endgroup$ Oct 11, 2022 at 9:14
  • 1
    $\begingroup$ Just posting here as well for consistency, but this demo was taken down a while ago. It's just outdated code, so use at your own risk! $\endgroup$
    – isaac
    Oct 11, 2022 at 13:16
  • $\begingroup$ There is a new demo, that let's me run into the same error. github.com/Rlag1998/Embedding_Generalization $\endgroup$
    – jo87casi
    Dec 9, 2023 at 19:20

1 Answer 1


Since you're using a nested list, you might need to flatten the parameters before passing them to the optimizer and then reshape them back after the optimization step.

Here's an example of how you can flatten and then reshape the parameters:

# Flatten the parameters before passing them to the optimizer
flat_pars = np.concatenate([init_pars_classical.flatten(), init_pars_quantum.flatten()])

# Inside the optimization loop, after the optimizer step
new_flat_pars = optimizer.step(lambda w: cost(reshape_pars(w), A=A_batch, B=B_batch), flat_pars)

# Reshape the parameters back to their original structure
pars = reshape_pars(new_flat_pars)

# Define a helper function to reshape the flat parameters back to the nested structure
def reshape_pars(flat_pars):
    split_index = init_pars_classical.size  # Index at which to split the flat parameters
    new_pars_classical = flat_pars[:split_index].reshape(init_pars_classical.shape)
    new_pars_quantum = flat_pars[split_index:].reshape(init_pars_quantum.shape)
    return [new_pars_classical, new_pars_quantum]

By flattening and reshaping the parameters, you ensure that the optimizer can handle them correctly, and you avoid potential issues with nested structures. I had the same problem with a similar notebook and flattening and reshaping the parameters solved my problem.

If you've made these changes and the warning persists, please check the following:

  • Ensure that requires_grad=True is set for all trainable parameters.
  • Verify that the cost function is indeed a function of the trainable parameters.
  • Check that the QNode is being called with the correct parameters within the cost function.

Your Answer

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

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