Computing objective function for QAOA from sampled bitstrings

Hi @Abhishek_Awasthi, welcome to the forum!

The issue here is that your parameters are being turned into an ArrayBox object. The way you can fix this is to add the following line in your objective function: params = qml.math.toarray(params)

This is how your function should look:

def objective(params):
        params = qml.math.toarray(params)
        strings = circuit(params[0], params[1], n_layers=n_layers)
        cost = 0.0
        for x_i in strings:
            for x_j in strings:
                x1 = qml.math.cast(x_i, np.float64)
                x2 = qml.math.cast(x_j, np.float64)
                cost += -x1 - x2 + 2.0 * x1 * x2
        return cost

That being said, I expect that you might have some optimization issues because if the value is being turned into ArrayBox, it might mean that you have some side effect within your cost function as explained here.

If you do get training issues my recommendation would be to go back to the original demo and change it little by little until you can pinpoint the line that is causing this behaviour.

Please let me know if this helps or if you’re still having the error!