Codercise I.10.3: How to calculate the eigenvalues?

For this codercise, we are supposed to calculate the expectation value from the samples. The question states that the eigenvalues of the measurements are supposed to be either 1 or -1, so I assumed it was just the mean of the entire sample. 

def compute_expval_from_samples(samples):
"""Compute the expectation value of an observable given a set of
sample outputs. You can assume that there are two possible outcomes,
1 and -1.

Args:
samples (array[float]): 100000 samples representing the results of
running the above circuit.

Returns:
float: the expectation value computed based on samples.
"""

estimated_expval = 0

return np.mean(samples)


However, this comes up as incorrect. After some trial and error, I found that calculating the weighted average using the eigenvalues 1/sqrt(2) and 3/sqrt(2) was able to pass the codercise. But I’m completely clueless on how to arrive at these eigenvalues. I thought since we are measuring on the Z-axis, then the eigenvalues should be 1 or -1. So this has left me deeply confused… Is the codercise bugged or are the eigenvalues supposed to be 1/sqrt(2) and 3/sqrt(2)? If it’s the latter please let me know how to calculate that! Thank you for reading!

Hey, I was checking the exercise and what it is asking us is to calculate the expected value of the circuit in the image (the one given in 10.1 and 10.2) using samples instead of using the qml.expval function.

So first our circuit() function needs to return an array with the outcomes of measuring Y at the end of our circuit (not Z) n times. To do this we can use a function called qml.sample. The number of samples you obtain using this function ie. n depends on the definition of shots in the device.

After we have the samples in an array we can calculate the expected value using the formula given to us:

\hspace{3cm} <Y>=\frac{1*(\text{num of 1 in samples})-1*(\text{num. of -1in samples})}{\text{total number of samples}}

This is what the function compute_expval_from_samples(samples) is supposed to return. I hope this helps.

1 Like

Hi @austin_huang, welcome to the Forum!

You’re absolutely right @Jefferson_Pule! This is what the codercise is asking for. Thank you for providing this explanation here!

I tried implementing the formula that Jefferson gave me but it isn’t passing the tests. Perhaps I’m doing something wrong?

def compute_expval_from_samples(samples):
estimated_expval = 0
num_of_one = 0
num_of_negative_one = 0

for val in samples:
if val == 1:
num_of_one += 1
else:
num_of_negative_one += 1

estimated_expval = (num_of_one* 1 - num_of_negative_one* 1)/len(samples)
return estimated_expval


Thank you for all the help so far.

So I checked your code and it looks correct. I also ran it instead of the answer I used and I got a correct answer. This makes me think that maybe your implementation of circuit() is wrong and not compute_expval_from_samplese(samples) Are you sure you are calculating the samples of the Pauli Y gate in circuit() ?

1 Like

Ah yes I fixed it. I was calculating the samples of the Z gate instead by accident. Thanks for all the help, I really appreciate it.

1 Like

Awesome @austin_huang and @Jefferson_Pule!