# My first quantum simulation

**URL:** <https://discuss.pennylane.ai/t/my-first-quantum-simulation/4403>\
**Category:** PennyLane Help\
**Created:** [April 6, 2024, 3:35pm UTC](https://discuss.pennylane.ai/t/my-first-quantum-simulation/4403 "2024-04-06T15:35:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mileva2319](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mileva2319](https://discuss.pennylane.ai/u/mileva2319)\
**Post date:** [April 6, 2024, 3:35pm UTC](https://discuss.pennylane.ai/t/my-first-quantum-simulation/4403/1 "2024-04-06T15:35:24Z")

</div>

Hi, I am trying to solve the My first quantum simulation Pennylane challenge. My code is attached below. However, I keep getting incorrect answers. I tried running the code in Jupyter notebook and it worked properly. I’ve also tried setting the seed to 0, as they say it is set when evaluating, and the results are exactly the same as the official ones. Could someone tell me where I went wrong?

```auto
import json
import numpy as np
def initialize_state():
    state = np.array([1.0, 0.0], dtype=float)
    return state
def apply_u(U, state):
    return np.dot(U, state)
def measure_state(state, num_meas):
    prob_0 = np.abs(state[0])**2
    prob_1 = np.abs(state[1])**2
    outcomes = np.zeros(num_meas, dtype = int)
    np.random.seed(0)
    for i in range(num_meas):
        random_number = np.random.rand()
        if random_number < prob_0:
            outcomes[i] = 0
        else:
            outcomes[i] = 1
    return outcomes
def quantum_algorithm(U):
    state = apply_u(U, initialize_state())
    measure_state(state, 20)

```

---

<div class="post-metadata">

**Author:** ![Alvaro\_Ballon](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/alvaro_ballon/32/874_2.png) [@Alvaro\_Ballon](https://discuss.pennylane.ai/u/Alvaro_Ballon)\
**Post date:** [April 8, 2024, 6:22pm UTC](https://discuss.pennylane.ai/t/my-first-quantum-simulation/4403/2 "2024-04-08T18:22:02Z")

</div>

Hello @mileva2319 !

In the code snippet that you posted, you’re missing a `return` for `quantum_algorithm`. So you should write `return measure_state(state, 20)`. Does this fix things?

Cheers,

Alvaro

---

<div class="post-metadata">

**Author:** ![mileva2319](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mileva2319](https://discuss.pennylane.ai/u/mileva2319)\
**Post date:** [April 9, 2024, 12:51pm UTC](https://discuss.pennylane.ai/t/my-first-quantum-simulation/4403/3 "2024-04-09T12:51:54Z")

</div>

It worked 🙂 I don’t know how I managed not to see that, thank you!
