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

**URL:** <https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986>\
**Category:** PennyLane Help\
**Created:** [January 30, 2024, 4:15pm UTC](https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986 "2024-01-30T16:15:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickGut0711](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nickgut0711/32/2137_2.png) [@NickGut0711](https://discuss.pennylane.ai/u/NickGut0711)\
**Post date:** [January 30, 2024, 4:15pm UTC](https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986/1 "2024-01-30T16:15:00Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [January 30, 2024, 10:50pm UTC](https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986/2 "2024-01-30T22:50:53Z")

</div>

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](https://docs.pennylane.ai/en/stable/code/api/pennylane.qinfo.transforms.quantum_fisher.html)

Let me know if that helps!

---

<div class="post-metadata">

**Author:** ![NickGut0711](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nickgut0711/32/2137_2.png) [@NickGut0711](https://discuss.pennylane.ai/u/NickGut0711)\
**Post date:** [January 31, 2024, 6:09pm UTC](https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986/3 "2024-01-31T18:09:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [January 31, 2024, 11:32pm UTC](https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986/4 "2024-01-31T23:32:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![NickGut0711](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nickgut0711/32/2137_2.png) [@NickGut0711](https://discuss.pennylane.ai/u/NickGut0711)\
**Post date:** [February 8, 2024, 8:48pm UTC](https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986/5 "2024-02-08T20:48:46Z")

</div>

Ok, got it. 👍

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`?

---

<div class="post-metadata">

**Author:** ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)\
**Post date:** [February 9, 2024, 2:10pm UTC](https://discuss.pennylane.ai/t/how-do-i-train-a-qnode-with-a-nonconventional-cost-function/3986/6 "2024-02-09T14:10:12Z")

</div>

That’s right! You can check out more information on our [docs page](https://docs.pennylane.ai/en/stable/introduction/circuits.html#creating-a-quantum-node). Let us know if we can help with anything else.
