Gradients in strawberry fields using Tensorflow

Hello! I’ve been trying to implement QAOA using Strawberry fields to optimize the function f(x) = (x-3)**2 and the code shows an error while computing the gradients using TensorFlow. The following is the main snippet of the code.!


with U_B and U_C and loss function is given by
and the error is
As far as I understand, this error is due to the fact that there is no connection between the variables and the loss function but I can’t find a way to get over it. Any help is much appreciated! Thanks in advance!

Hi @Pranav_Chandarana,

Could you please copy and paste your full code here? It’s easier to understand what may be going wrong if we can use the full code on our end too.

Hello @CatalinaAlbornoz. I don’t know why when I copy the code there are some formatting issues so maybe you can’t see it properly. Hence I have made a google collab. Here is the link

UPDATE: I think I figured out the error. Now the code is working but still, the optimization results are not as expected. Please let me know if there are any errors in the code or if it is just the gradient descent.

Hi @Pranav_Chandarana. I’m glad you found the error! What was the error? And what are the results you’re expecting?

Hello @CatalinaAlbornoz. There were some errors in the args of sf.eng() function. The answer I am expecting isx=a but I am not getting it after the optimization.

Hi @Pranav_Chandarana, I can see that the optimization is moving towards a probability of 100 always. Let me dig deeper into this.

1 Like

Hello @CatalinaAlbornoz, any updates on this?

Hi @Pranav_Chandarana,

I took a look at it but I’m not sure what’s going wrong.
I have the feeling that it’s something related to the optimization procedure but I may be wrong.

Maybe you can start by executing an example that works (such as this one) and then modify it piece by piece to achieve the functionality that you’re looking for.