You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. """ifisinstance(key, str):
try:
key=int(key, 2)
exceptValueError:
raiseQiskitError(f"Key '{key}' is not a valid binary string.") fromNoneifisinstance(key, int):
ifkey>=self.dim:
raiseQiskitError(f"Key {key} is greater than Statevector dimension {self.dim}.")
ifkey<0:
raiseQiskitError(f"Key {key} is not a valid positive value.")
returnself._data[key]
else:
raiseQiskitError("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. """ifisinstance(key, str):
try:
key=int(key, 2)
exceptValueError:
raiseQiskitError(f"Key '{key}' is not a valid binary string.") fromNoneifisinstance(key, int):
ifkey>=self.dim:
raiseQiskitError(f"Key {key} is greater than Statevector dimension {self.dim}.")
ifkey<0:
raiseQiskitError(f"Key {key} is not a valid positive value.")
returnself._data[key]
""" From here """ifisinstance(key, slice):
returnself._data[key]
""" Until here """else:
raiseQiskitError("Key must be int or a valid binary string.")
The text was updated successfully, but these errors were encountered:
taketakeyyy
changed the title
Excercises no working, and suggestions to Statevector class.
Excercises not working, and suggestions to Statevector class.
May 9, 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 🙂
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.
Environment
Qiskit Terra version: 0.20.1
Python version: 3.10.4
Operating system: Windows 10
Others
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.
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
.vec
isStatevector
class. TheStatevector
class does not accept slice and raises QiskitError("Key must be int or a valid binary string.").I think the following change would solve the problem.
The text was updated successfully, but these errors were encountered: