In this codercise, I’m supposed to return the index wherever the vector is 1.
def find_the_one(syndrome):
"""Finds the entry in the error syndrome that is equal to 1.
Args:
syndrome (tensor): the output of the shor function
Returns:
(int): the index of the syndrome vector that is equal to 1. For example,
if syndrome = [0, 0, 1, 0], then this function would return 2.
"""
index = np.where(syndrome[1] == 1)[0]
if len(index) > 0:
return index[0]
else:
return None
It’s always giving the error
Try again. Make sure you’re returning an integer!
I don’t see where I’m getting it wrong. I mean syndrome is the output of the short function, and the error syndrome is in the second element, that’s why syndrome[1], still it’s giving the error.
What may be I’m doing wrong?
Hey @Amit_Kulshreshta! Thanks for checking our new content out and welcome to the forum! 
It’s worth printing out what syndrome
is just to make sure you see and understand what the data structure is
. I.e., what is syndrome[1]
?
Also, the return in the else
statement is problematic — returning None
is probably at the heart of the Try again. Make sure you’re returning an integer!
message.
Let me know if this helps!
I tried printing syndrome[1], it’s not printing it, it’s always saying syndrome
is not defined, and if I do this:
def find_the_one(syndrome):
"""Finds the entry in the error syndrome that is equal to 1.
Args:
syndrome (tensor): the output of the shor function
Returns:
(int): the index of the syndrome vector that is equal to 1. For example,
if syndrome = [0, 0, 1, 0], then this function would return 2.
"""
index = np.where(syndrome[1] == 1)[0] # Get the first index where the syndrome is equal to 1
return index
It’s still giving the same error.
Also regarding your other suggestion, I thought the data type of syndrome
was tuple(tensor,tensor) , but the code says it’s only tensor, so even after doing this:
def find_the_one(syndrome):
"""Finds the entry in the error syndrome that is equal to 1.
Args:
syndrome (tensor): the output of the shor function
Returns:
(int): the index of the syndrome vector that is equal to 1. For example,
if syndrome = [0, 0, 1, 0], then this function would return 2.
"""
index = np.where(syndrome == 1)[0] # Find the indices where the error syndrome is equal to 1
if len(index) > 0:
return index[0] # Return the first index where the error syndrome is equal to 1
Doesn’t give me right.
Is syndrome
output of the shor
function, because that would be tuple(tensor,tensor)
, or is it just tensor
as mentioned in the code
Is syndrome
output of the shor
function, because that would be tuple(tensor,tensor)
, or is it just tensor
as mentioned in the code
I see where there might be a bit of confusion. The output of the shor
function is indeed a tuple, but the error syndrome is the second entry of the output (i.e., qml.probs(wires=range(1, 9))
) — a tensor
:
return qml.probs(wires=[0]), qml.probs(wires=range(1, 9))
syndrome
will look something like [0, 0, 0, ..., 1, ..., 0, 0]
. np.where
is a good thing to use, but there are a couple tricks that may or may not be obvious to everyone, so I will update the hint.
Another thing you can try is np.nonzero
: numpy.nonzero — NumPy v1.25 Manual. I will update the hint to say that nonzero
is helpful here rather than where
.
1 Like
Thanks , it worked, but why? I have both the implementation of np.where
and np.nonzero
, they both effectively do the same thing, right? Why is it that one method works while the other don’t?
Hey @Amit_Kulshreshta , this seems like more of a Python question than a Codebook/PennyLane question, so I can recommend that you look at the NumPy documentation for where
(also, see the note at the top of the page). 
But here’s a tip: the syndrome you’re dealing with is a list, not an np.array
, so there is a bit of nuance to use with np.where
to get it to spit out exactly what you want, like Isaac mentioned. Using np.nonzero
is definitely more straightforward.
On the other hand, we always try to include some helpful error messages in the Codebook codercise interface and you might be able to get a hint like that. If you have another suggestion for us that would help make some of the problems easier to understand, definitely let us know. 
Thanks for learning and playing around with the Codebook, that’s always great to see! 
1 Like