Variational classifier — PennyLane error

Hi.

While running: https://pennylane.ai/qml/demos/tutorial_variational_classifier.html I get:
ValueError: BasisState parameter must consist of 0 or 1 integers.

Hi @_risto.

The error you’re describing has to do with the BasisState operation only accepting an array of integers (you can read more about it here) representing a specific state. E.g., np.array([0, 1, 0]) prepares the state |010⟩.

Could you provide me with the exact code that you’re trying to run? Running the specific tutorial you linked to works fine for me locally, so I’m not sure exactly why it’s not working for you.

Hi @theodor

I am running the code as it is provided in the demo: https://pennylane.ai/qml/demos/tutorial_variational_classifier.html I have made no changes to it.

Could you make sure that you’re running the latest version of PennyLane (run pip install pennylane --upgrade to update PennyLane), and also provide me with the output of qml.about() when using import pennylane as qml.

You could also attempt to create a clean environment and run things from there.

Hi @theodor

Hi @_risto. Did you attempt to update PennyLane? You could also try to run the tutorial as a Python file, downloadable from the bottom or the right side of the tutorial page, to see if that works better (which could mean that there’s something going on with the Jupyter environment).

I also see that you’re using Anaconda. I would recommend you to try and create a new environment (conda create -n "qml_test" python=3.8), activate the environment (conda activate qml_test), install PennyLane (pip install pennylane) and then run the tutorial from there. When you’re done you can remove the environment with conda env remove -n "qml_test", as long as it’s not the active environment.

Preferably you could run the tutorial as a Python file that time as well.

Let me know how that goes!

Hi @theodor

Yes, I have done as you said, but no success.
For future references, how should I install packages:

  • powershell
  • powershell administrator
  • anaconda
  • anaconda adniminstrator
    ?

If you look at Problem using the Forest plugin it seems I am also having problems installing packages to proper environments…?

Hi @_risto,

It’s up to you how you want to install packages, although the recommended ways are either to stick with using Anaconda (conda install package) or PyPI (pip install package). It’s also always a good idea to have separate environments for separate projects/tasks, and only install the necessary packages in each environment. This avoids the risk of mismatching versions and other dependency issues that might arise between packages. You generally shouldn’t have to run as admin for this to work.

Whether you should use Powershell or not is a completely different question. It’s a shell, such as Bash and cmd, which all can run Anaconda commands and install packages via both conda and pip. It’s more a case of personal preference than anything else.

I hope this helps!

Hi @theodor

Than you for clarification.
I have followed the guidance in a post before, but still I get the ValueError: BasisState parameter must consist of 0 or 1 integers.

Hi @_risto,

I’m sorry, but I can’t really help you much more without knowing exactly what’s not working. If you can provide a minimal non-working example where this error shows up I could have a look at it (that is, remove as much code as possible from the tutorial and attempt to locate exactly where it fails).

Hi @theodor

Here is where I get it:

Hi @_risto,

Just tried running the tutorial as a Python script in a clean Windows 10 environment and it executed well. Just to make sure, did the error also arise when running the tutorial as a Python script (i.e., running python .\tutorial_variational_classifier.py)?

Also, is the parity.txt and the iris_classes1and2_scaled.txt file the correct one specified in the tutorial?

My output of qml.about() is as follows:

Name: PennyLane
Version: 0.16.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: None
Author-email: None
License: Apache License 2.0
Location: c:\anaconda3\envs\qml_test\lib\site-packages
Requires: toml, semantic-version, autograd, networkx, appdirs, scipy, autoray, numpy
Required-by:
Platform info:           Windows-10-10.0.19041-SP0
Python version:          3.8.10
Numpy version:           1.21.0
Scipy version:           1.7.0
Installed devices:
- default.gaussian (PennyLane-0.16.0)
- default.mixed (PennyLane-0.16.0)
- default.qubit (PennyLane-0.16.0)
- default.qubit.autograd (PennyLane-0.16.0)
- default.qubit.jax (PennyLane-0.16.0)
- default.qubit.tf (PennyLane-0.16.0)
- default.tensor (PennyLane-0.16.0)
- default.tensor.tf (PennyLane-0.16.0)

In your previous output of qml.about(), it seems that the PennyLane devices were from PennyLane-0.15.1, so an update would be required to get the latest released version of the devices. This might also explain why in the traceback the line numbers seem to reflect a previous version of the device.