Hi not sure if this has been addressed before but when I try to run qml.GradientDescentOptimizer on a cost function with intermediate costs, I noticed the output types of these (global) variables are ‘autograd.numpy.numpy_boxes.ArrayBox’.
Is there a way I can output these variables with the usual ‘tensor’ or ‘numpy.ndarray’ types instead? Thank you!
Thank you for your prompt reply. I was trying to export the ratio of the probability outputs from two circuits between cost function evaluations, but later found I could not work with the autograd arrayboxes, something like:
Thanks @leongfy, I see the issue now This arises because cost_n is computed via a side effect of your cost function. That is, your cost function is updating a global variable, rather than returning the cost.
When using Autograd with PennyLane, cost functions must be pure — they cannot perform side effects (such as updating external variables), otherwise:
You will see ArrayBox objects rather than NumPy arrays, and
The values stored via the side-effect will no longer be differentiable.
In this particular case, since you are not using computing the gradient of the side effect, it should be okay. You can use the qml.math.toarray() function to convert any ArrayBox to a NumPy array:
What you propose can have undesired side effects, especially when you’re calculating gradients. I’m not sure whether it will cause problems or not in this particular case but in general it’s important to be careful when doing this .