Codercise P.2.4 probabilities does not match

I have the following code

for index, prob in enumerate(probs):
    if np.isclose(prob,1,rtol=1e-05):
        bit_list = [int(x) for x in np.binary_repr(1, width=len(estimation_wires))]
        print(f"{bit_list= }")

phase= 0.0
for num in bit_list:
    phase+= float(num)/2**t
return phase

U = qml.T.compute_matrix()

probs = qpe(U)

estimated_phase = estimate_phase(probs)

Despite I have the correct phase state output |theta> = |001> and the estimated phase of 0.125 the autograder pop up: “Incorrect: the phase calculated is not correct.”
What did I still missing?

I found that replacing my code with

phase_estimated = np.argmax(probs) / 2 ** len(estimation_wires)

I do not know why but it do the job! I hope someone can explain it.

Taken from


Hi @Adin ,

np.argmax returns the indices of the maximum values in probs.
len(estimation_wires) (n) is the number of estimation wires and it determines the precision to which we learn the phase.
So what we’re doing is finding the index of the largest value in the probability distribution and dividing that number by 2^n. This number will be an estimate of θ with an error that decreases exponentially with the number of estimation wires n.

The right hand side of node P.2 in the Codebook may be helpful, especially the first 2-3 exercises.

Please let me know if this helps to clarify your question.

1 Like

Hi @CatalinaAlbornoz,

Reading Node P.2 helped me, thank you!

I’m glad this helped you @Adin!