# QAOA, sample measured bitstrings 100 times, Error

**URL:** <https://discuss.pennylane.ai/t/qaoa-sample-measured-bitstrings-100-times-error/1071>\
**Category:** PennyLane Help\
**Created:** [May 29, 2021, 4:22am UTC](https://discuss.pennylane.ai/t/qaoa-sample-measured-bitstrings-100-times-error/1071 "2021-05-29T04:22:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amandeep](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amandeep/32/385_2.png) [@Amandeep](https://discuss.pennylane.ai/u/Amandeep)\
**Post date:** [May 29, 2021, 4:22am UTC](https://discuss.pennylane.ai/t/qaoa-sample-measured-bitstrings-100-times-error/1071/1 "2021-05-29T04:22:20Z")

</div>

```python
def qaoa(n_layers=1):
    
    
    print("\np={:d}".format(n_layers))

    # initialize the parameters near zero
    init_params = 0.01 * np.random.rand(2, n_layers)

    # minimize the negative of the objective function
    def objective(params):
        
        gammas = params[0]
        betas = params[1]
        neg_obj = 0
        for idx in range(zahl_qubits):
            obj = circuit(gammas, betas, edge=graph1[idx], n_layers=n_layers)
        return obj

    # initialize optimizer: Adagrad works well empirically
    opt = qml.GradientDescentOptimizer(stepsize=0.01)

    # optimize parameters in objective
    params = init_params
    steps = 30
    for i in range(steps):
        params = opt.step(objective, params)
        if (i + 1) % 5 == 0:
            print("Objective after step {:5d}: {: .7f}".format(i + 1, objective(params)))

    # sample measured bitstrings 100 times
    n_samples = 10
    bit_strings = []

    for i in range(n_samples):
        gamma = params[0]
        beta = params[1]
        bit_string = int(circuit(gamma, beta, edge = None, n_layers = n_layers))
        print(bit_string)
        bit_strings.append(bit_string)

    # print optimal parameters and most frequently sampled bitstring
    counts = np.bincount(np.array(bit_strings))
    most_freq_bit_string = np.argmax(counts)
    print("Optimized (gamma, beta) vectors:\n{}".format(params[:, :n_layers]))
    print("Most frequently sampled bit string is: {:05b}".format(most_freq_bit_string))

    return objective(params), bit_strings

b1=qaoa(n_layers=1)

```

Error comes when I give shots more than 1 in `dev = qml.device("default.qubit", wires=n_wires, shots=5)`

```auto
p=1
Objective after step 5: 0.2000000
Objective after step 10: -0.2000000
Objective after step 15: -0.2000000
Objective after step 20: -0.2000000
Objective after step 25: 0.2000000
Objective after step 30: -0.2000000
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-309-01b03994f51e> in <module>
      1 #perform qaoa on our graph with p=1,2 and # keep the bitstring sample lists
----> 2 b1=qaoa(n_layers=1)

<ipython-input-308-61abd27f982d> in qaoa(n_layers)
     35 gamma = params[0]
     36 beta = params[1]
---> 37 bit_string = int(circuit(gamma, beta, edge = None, n_layers = n_layers))
     38 print(bit_string)
     39 bit_strings.append(bit_string)

TypeError: only size-1 arrays can be converted to Python scalars
```

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [May 31, 2021, 7:29am UTC](https://discuss.pennylane.ai/t/qaoa-sample-measured-bitstrings-100-times-error/1071/2 "2021-05-31T07:29:15Z")

</div>

Hi @Amandeep!

Would you be able to post a fully contained, minimal non-working version to highlight this issue? That is,

- The code posted should execute by itself, without needing any extra information

- As much of the boilerplate has been removed, so that the posted code is as small as possible while still showing the error.

This will be a great help in allowing me to assist you in debugging this issue 🙂
