Issues with the problem I.1.3

Hi there, I am going through the codebook, and I am having an issue with exercise I.1.3… I tried my solution using jupyter notebook on my computer and it seems to be working fine, but when I run on the codebook platform it returns with the following error:

"Error: can't convert complex to float"

Here is the code I am using

prob_array = state.conj()*state
experiment = [   ]
for i in range(num_meas):
    experiment.append(np.random.choice([0,1], p=[prob_array[0], prob_array[1]] ))

Hi @carloskuhn, welcome to the forum!

If you do print(prob_array[0]) and print(prob_array[1]) on a Jupyter notebook, you will notice that these values are complex numbers, even if the imaginary part is zero.

The np.random.choice function takes only floats as the probabilities so you will need to turn your complex numbers into floats. In order to do this you can simply take the real part of each probability, since you know that the imaginary part is zero. You can do this by using the real function: np.real(<your_complex_number>)

This should solve your problem.

I hope you keep enjoying the codebook!

Thank you, interesting it works fine when I run the code that way on my computer. I will get the real part as suggested to move forwards.

Yes @carloskuhn, you may find slight differences from running it on your computer but it shouldn’t be anything major. Feel free to let us know if you find a similar issue on another codercise.

Keep enjoying PennyLane!