Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excercises not working, and suggestions to Statevector class. #8032

Open
taketakeyyy opened this issue May 9, 2022 · 2 comments
Open

Excercises not working, and suggestions to Statevector class. #8032

taketakeyyy opened this issue May 9, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@taketakeyyy
Copy link

Environment

  • Qiskit Terra version: 0.20.1

  • Python version: 3.10.4

  • Operating system: Windows 10

  • Others

    • qiskit-aer: 0.10.4
    • qiskit-ignis: 0.7.0
    • qiskit-ibmq-provider: 0.19.1
    • qiskit: 0.36.1

What is happening?

Exercises not working due to Statevector class not accepting slices.

How can we reproduce the issue?

Go to https://qiskit.org/textbook/ch-algorithms/bernstein-vazirani.html, and try exercises.

from qiskit_textbook.widgets import bv_widget
bv_widget(3, "011", hide_oracle=False)

It doesn't work no matter which button I click.

What should happen?

Exercises works.

Any suggestions?

There is an error in the following part of bv_widget.

vfirst = vec[:2**nqubits//2]

vec is Statevector class. The Statevector class does not accept slice and raises QiskitError("Key must be int or a valid binary string.").

    def __getitem__(self, key):
        """Return Statevector item either by index or binary label
        Args:
            key (int or str): index or corresponding binary label, e.g. '01' = 1.

        Returns:
            numpy.complex128: Statevector item.

        Raises:
            QiskitError: if key is not valid.
        """
        if isinstance(key, str):
            try:
                key = int(key, 2)
            except ValueError:
                raise QiskitError(f"Key '{key}' is not a valid binary string.") from None
        if isinstance(key, int):
            if key >= self.dim:
                raise QiskitError(f"Key {key} is greater than Statevector dimension {self.dim}.")
            if key < 0:
                raise QiskitError(f"Key {key} is not a valid positive value.")
            return self._data[key]
        else:
            raise QiskitError("Key must be int or a valid binary string.")

I think the following change would solve the problem.

    def __getitem__(self, key):
        """Return Statevector item either by index or binary label
        Args:
            key (int or str): index or corresponding binary label, e.g. '01' = 1.

        Returns:
            numpy.complex128: Statevector item.

        Raises:
            QiskitError: if key is not valid.
        """
        if isinstance(key, str):
            try:
                key = int(key, 2)
            except ValueError:
                raise QiskitError(f"Key '{key}' is not a valid binary string.") from None
        if isinstance(key, int):
            if key >= self.dim:
                raise QiskitError(f"Key {key} is greater than Statevector dimension {self.dim}.")
            if key < 0:
                raise QiskitError(f"Key {key} is not a valid positive value.")
            return self._data[key]

        """ From here """
        if isinstance(key, slice):
            return self._data[key]
        """ Until here """

        else:
            raise QiskitError("Key must be int or a valid binary string.")
@taketakeyyy taketakeyyy added the bug Something isn't working label May 9, 2022
@taketakeyyy taketakeyyy changed the title Excercises no working, and suggestions to Statevector class. Excercises not working, and suggestions to Statevector class. May 9, 2022
@Cryoris
Copy link
Contributor

Cryoris commented May 10, 2022

We'd have to adjust the docs too but I think supporting slices is a nice feature! However @chriseclectic or @ikkoham might have an opinion as they know quantum info well 🙂

@ikkoham
Copy link
Contributor

ikkoham commented May 13, 2022

Thank you for your opening issue.
I wonder why this was broken in the first place. Maybe it was the introduction of __getitem__ that made it impossible to use __array__. I think __getitem__ is a very complicated interface and probably unnecessary, but I need to look into it a bit more.

(Workaround) If you want a slice of numpy.ndarray, you can use Statevector.data on the user side to explicitly take out the ndarray and use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants