How do I train a QNode with a nonconventional cost function?

Hello! I’m using Pennylane and JAX to implement a certain quantum circuit which outputs various expectation values of observables that allows me to create simple cost functions like the MSE. However, what if I want to compute non-conventional cost functions? For example, how would I go about creating a cost function that is just the QFI (where I want to optimize the parameters during training to maximize the QFI)? Any help would be appreciated!

Hey @Nikhil_Narayanan,

So long as your cost function outputs something that’s a scalar value, you won’t have to do anything different than what you’d do with MSE. But, it’s hard to say without seeing your code (or at least an example of what you’d like to do).

PennyLane does have a transform that calculates the quantum Fischer information matrix: qml.qinfo.transforms.quantum_fisher — PennyLane 0.34.0 documentation

Let me know if that helps!

Hi @isaacdevlugt, thank you for the prompt response! I will try it out and post it here later today. But what differentiates the scalar value vs. what these circuits output which are usually ExpectationMP objects?

I’ll check out the documentation!

Quantum functions that don’t get decorated with @qml.qnode but still return, say, qml.expval will return an ExpectationMP object. But, when they’re transformed into quantum circuits / a QNode with @qml.qnode, the result will be numpy arrays, scalars, etc., depending on what measurement you output. Let me know if that makes sense!

Ok, got it. :+1:

So just to be clear: If I want to just use my quantum circuit as a function (which outputs some scalar value or an array with multiple different scalar values corresponding to different measurements), I just need to decorate it with @qml.qnode?

That’s right! You can check out more information on our docs page. Let us know if we can help with anything else.