# Codercise V.1.4 - Training a model

**URL:** <https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495>\
**Category:** Codebook\
**Created:** [November 5, 2024, 8:10pm UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495 "2024-11-05T20:10:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![feynmannlu](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/feynmannlu/32/3305_2.png) [@feynmannlu](https://discuss.pennylane.ai/u/feynmannlu)\
**Post date:** [November 5, 2024, 8:10pm UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495/1 "2024-11-05T20:10:50Z")

</div>

![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/9/90afc9a188727f689ba966461ae7018edcfcf754.png)  
I don’t understand what problem.  
Have someone also encounter the same problem?

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [November 6, 2024, 8:39pm UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495/2 "2024-11-06T20:39:40Z")

</div>

Hi @feynmannlu,

I can see several things happening here. On one hand you’re not returning the optimized parameters, but instead the result after just one step. On the other hand, you’re telling your optimizer that it should train both the observable and the parameters. If we only want to train the parameters you may need to create an additional function within the optimizer function such that the only argument to that new function are the parameters.

I hope this helps you!

---

<div class="post-metadata">

**Author:** ![Mayu](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/mayu/32/3328_2.png) [@Mayu](https://discuss.pennylane.ai/u/Mayu)\
**Post date:** [November 10, 2024, 2:12am UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495/3 "2024-11-10T02:12:34Z")

</div>

This may be a bit difficult if you aren’t familiar with lambda, so I’ll give you a hint.

params = opt.step(lambda p: cost\_function(observable,p), params)

---

<div class="post-metadata">

**Author:** ![to.helen.babenko](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/to.helen.babenko/32/3452_2.png) [@to.helen.babenko](https://discuss.pennylane.ai/u/to.helen.babenko)\
**Post date:** [December 19, 2024, 4:04pm UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495/4 "2024-12-19T16:04:37Z")

</div>

I have noticed, that I got error “The cost function should be close to -1.” in 2 cases: when I made a mistake and when I used not optimal parameters. If anyone have the same issue, try at first [`qml.GradientDescentOptimizer`](https://docs.pennylane.ai/en/stable/code/api/pennylane.GradientDescentOptimizer.html) with parameters learning\_rate, max\_iterations from 1.3.a, to verify that everything is correct, and play around with different optimizers and parameters after.

---

<div class="post-metadata">

**Author:** ![kasterma](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kasterma/32/4235_2.png) [@kasterma](https://discuss.pennylane.ai/u/kasterma)\
**Post date:** [March 5, 2026, 10:52am UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495/5 "2026-03-05T10:52:38Z")

</div>

I have a correct solution to this problem (it is accepted, and when using the solution given by “compare with our solution” I see the same outputs), but now when trying to run it locally it doesn’t optimize anything. How do I call this optimize function?

I thought this would do it:

```auto
shape = qml.StronglyEntanglingLayers.shape(n_layers=1, n_wires=n_bits)
params = np.random.default_rng().random(shape)
print(params)
print("now we optimize")
print(optimizer(qml.PauliZ(0) @ qml.PauliZ(1) @ qml.PauliZ(2) @ qml.PauliZ(3), params))

```

but I always get

```auto
[[[0.34685277 0.10049847 0.60894508]
  [0.05617771 0.10079831 0.38770051]
  [0.41074692 0.97757178 0.30923342]
  [0.60472938 0.49871546 0.71779572]]]
now we optimize
[[[0.34685277 3.14159265 0.60894508]
  [0.05617771 0.10079831 0.38770051]
  [0.41074692 0.97757178 0.30923342]
  [0.60472938 0.49871546 0.71779572]]]

```

with different numbers obviously.

---

<div class="post-metadata">

**Author:** ![daniela.murcillo](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/daniela.murcillo/32/3612_2.png) [@daniela.murcillo](https://discuss.pennylane.ai/u/daniela.murcillo)\
**Post date:** [March 5, 2026, 8:09pm UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495/6 "2026-03-05T20:09:01Z")

</div>

Hi!  
I believe you are implementing it correctly and this is a particular case where the optimizer only changes one number of the parameters. Notice how these two matrices are not the same, almost. The first element of the second row changes from 0.10049847 to 3.14159265.  
Try changing the observable to `qml.PauliZ(0) @ qml.PauliY(1) @ qml.PauliZ(2) @ qml.PauliZ(3)`, for example and see that the final result differs even more.

Thanks for your question and I hope this was helpful.

---

<div class="post-metadata">

**Author:** ![kasterma](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kasterma/32/4235_2.png) [@kasterma](https://discuss.pennylane.ai/u/kasterma)\
**Post date:** [March 7, 2026, 7:58am UTC](https://discuss.pennylane.ai/t/codercise-v-1-4-training-a-model/7495/7 "2026-03-07T07:58:34Z")

</div>

Yes, very helpful. What added to my not seeing anything happen was that I had

```
print(optimizer(qml.PauliZ(0), params))
np.allclose(params_before, params)

```

and not

```
params = optimizer(qml.PauliZ(0), params)
print(params)
np.allclose(params_before, params)

```

So the slightly updated params were printed. But I didn’t see the difference, and np.allclose also didn’t see it b/c the params are not updated in place. With this changed observable the change is very clear. Thanks!
