Codercise V.1.4 - Training a model


I don’t understand what problem.
Have someone also encounter the same problem?

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!

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)

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 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.

1 Like

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:

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

[[[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.

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.

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!

1 Like