N.2.2.b Compilation error even code is correct with correct probability

Here I am getting the error messages as

`Blockquote Your code isn't giving the correct probabilities!Your code isn'tgiving the correct probabilities!`

My code is as follows:

``
def outcome_probs(rho, B):

``````"""
Args:
rho (np.array([array[complex]])): The density matrix of the state before measurement
B (np.array([array[complex]])): Matrix representation of the measured observable
Returns:
(np.array(float)): List of measurement probabilities, in no particular order.
"""

eigen_projectors =  eigenprojectors(B)# Use a previously defined function to obtain the eigenprojectors

prob_list = np.real(np.trace(np.dot(rho,eigen_projectors)))# Use list comprehension and the relevant formula to find the probabilities

return prob_list
``````

rho = np.array([[3/4,0],[0,1/4]])
B = qml.matrix(qml.PauliY(0))

print(â€śState: [[3/4,0],[0,1/4]], Observable: {}â€ť.format(B))
print(â€śMeasurement probabilities {}â€ť.format(outcome_probs(rho,B).round(2)))``

The problem is with how youâ€™re calculating `prob_list`. You have to return the trace of `rho` times each value of the eigenprojectors:

``````    prob_list = [np.trace(np.dot(rho,elem)) for elem in eigen_projectors]
``````

Let me know if that helps!

Yes It helps. Thank you so much.

1 Like