# Computing objective function for QAOA from sampled bitstrings

**URL:** <https://discuss.pennylane.ai/t/computing-objective-function-for-qaoa-from-sampled-bitstrings/1820>\
**Category:** PennyLane Help\
**Created:** [April 12, 2022, 8:13am UTC](https://discuss.pennylane.ai/t/computing-objective-function-for-qaoa-from-sampled-bitstrings/1820 "2022-04-12T08:13:17Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [April 12, 2022, 5:59pm UTC](https://discuss.pennylane.ai/t/computing-objective-function-for-qaoa-from-sampled-bitstrings/1820/2 "2022-04-12T17:59:41Z")

</div>

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](http://discuss.pennylane.ai/t/autograd-arraybox-type-intermediate-output-from-optimizer/1465/3).

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!

---

_[View the full topic](https://discuss.pennylane.ai/t/computing-objective-function-for-qaoa-from-sampled-bitstrings/1820)._
