My first quantum simulation

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?

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)

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

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

2 Likes