# Codercise E.3.4.a : Complete the find\_the\_one function such that it outputs the index corresponding to where the error syndrome is equal to 1

**URL:** <https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281>\
**Category:** Codebook\
**Created:** [August 3, 2023, 2:50pm UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281 "2023-08-03T14:50:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Kulshreshta](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amit_kulshreshta/32/1849_2.png) [@Amit\_Kulshreshta](https://discuss.pennylane.ai/u/Amit_Kulshreshta)\
**Post date:** [August 3, 2023, 2:50pm UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281/1 "2023-08-03T14:50:59Z")

</div>

In this codercise, I’m supposed to return the index wherever the vector is 1.

```auto
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?

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [August 3, 2023, 3:59pm UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281/2 "2023-08-03T15:59:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Amit\_Kulshreshta](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amit_kulshreshta/32/1849_2.png) [@Amit\_Kulshreshta](https://discuss.pennylane.ai/u/Amit_Kulshreshta)\
**Post date:** [August 3, 2023, 5:23pm UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281/3 "2023-08-03T17:23:26Z")

</div>

I tried printing syndrome[1], it’s not printing it, it’s always saying `syndrome` is not defined, and if I do this:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [August 3, 2023, 8:26pm UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281/4 "2023-08-03T20:26:30Z")

</div>

> 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`:

```auto
    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.26 Manual](https://numpy.org/doc/stable/reference/generated/numpy.nonzero.html#numpy.nonzero). I will update the hint to say that `nonzero` is helpful here rather than `where` 😁.

---

<div class="post-metadata">

**Author:** ![Amit\_Kulshreshta](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amit_kulshreshta/32/1849_2.png) [@Amit\_Kulshreshta](https://discuss.pennylane.ai/u/Amit_Kulshreshta)\
**Post date:** [August 5, 2023, 2:16am UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281/5 "2023-08-05T02:16:54Z")

</div>

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?

 ![Screenshot 2023-08-05 101455](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/8/86f71e1974783af515372449c8c3e20a9dfac452.png)

---

<div class="post-metadata">

**Author:** ![Amit\_Kulshreshta](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/amit_kulshreshta/32/1849_2.png) [@Amit\_Kulshreshta](https://discuss.pennylane.ai/u/Amit_Kulshreshta)\
**Post date:** [August 5, 2023, 2:17am UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281/6 "2023-08-05T02:17:56Z")

</div>

![Screenshot 2023-08-05 101509](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/8/855908f7481910ab513a627a711a6cd9adf23366.png)

---

<div class="post-metadata">

**Author:** ![Ivana\_at\_Xanadu](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ivana_at_xanadu/32/1852_2.png) [@Ivana\_at\_Xanadu](https://discuss.pennylane.ai/u/Ivana_at_Xanadu)\
**Post date:** [August 7, 2023, 10:15am UTC](https://discuss.pennylane.ai/t/codercise-e-3-4-a-complete-the-find-the-one-function-such-that-it-outputs-the-index-corresponding-to-where-the-error-syndrome-is-equal-to-1/3281/7 "2023-08-07T10:15:36Z")

</div>

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`](https://numpy.org/doc/stable/reference/generated/numpy.where.html) (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! 🙂
