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

[BUG] AttributeError: 'float' object has no attribute 'ndim' when using qml.RX/qml.RY #3867

Closed
1 task done
salcc opened this issue Mar 4, 2023 · 5 comments
Closed
1 task done
Labels
bug 🐛 Something isn't working

Comments

@salcc
Copy link

salcc commented Mar 4, 2023

Expected behavior

It works without error, like in the documentation: https://pennylane.ai/qml/demos/tutorial_qubit_rotation.html.

Actual behavior

It produces an error (AttributeError: 'float' object has no attribute 'ndim').

Additional information

To reproduce the error, I used a minimal clean Conda environment created with the following commands:

conda create --name bugreport python=3.10
conda activate bugreport
pip install pennylane

Source code

import pennylane as qml
from pennylane import numpy as np

dev1 = qml.device("default.qubit", wires=1)

@qml.qnode(dev1, interface="autograd")
def circuit(params):
    qml.RX(params[0], wires=0)
    qml.RY(params[1], wires=0)
    return qml.expval(qml.PauliZ(0))

print(circuit([0.54, 0.12]))

Tracebacks

Traceback (most recent call last):
  File "/home/salcc/Documents/bugreport/main.py", line 12, in <module>
    print(circuit([0.54, 0.12]))
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/qnode.py", line 842, in __call__
    self.construct(args, kwargs)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/qnode.py", line 751, in construct
    self._tape = make_qscript(self.func)(*args, **kwargs)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/tape/qscript.py", line 1371, in wrapper
    result = fn(*args, **kwargs)
  File "/home/salcc/Documents/bugreport/main.py", line 9, in circuit
    qml.RY(params[1], wires=0)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/ops/qubit/parametric_ops.py", line 182, in __init__
    super().__init__(phi, wires=wires, do_queue=do_queue, id=id)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/operation.py", line 1627, in __init__
    super().__init__(*params, wires=wires, do_queue=do_queue, id=id)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/operation.py", line 1005, in __init__
    self._check_batching(params)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/operation.py", line 1025, in _check_batching
    ndims = tuple(qml.math.ndim(p) for p in params)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/pennylane/operation.py", line 1025, in <genexpr>
    ndims = tuple(qml.math.ndim(p) for p in params)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/autoray/autoray.py", line 79, in do
    return get_lib_fn(backend, fn)(*args, **kwargs)
  File "/home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages/autoray/autoray.py", line 886, in ndim
    return x.ndim
AttributeError: 'float' object has no attribute 'ndim'

System information

Name: PennyLane
Version: 0.29.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/salcc/miniconda3/envs/bugreport/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning

Platform info:           Linux-5.19.0-32-generic-x86_64-with-glibc2.35
Python version:          3.10.9
Numpy version:           1.23.5
Scipy version:           1.10.1
Installed devices:
- lightning.qubit (PennyLane-Lightning-0.29.0)
- default.gaussian (PennyLane-0.29.0)
- default.mixed (PennyLane-0.29.0)
- default.qubit (PennyLane-0.29.0)
- default.qubit.autograd (PennyLane-0.29.0)
- default.qubit.jax (PennyLane-0.29.0)
- default.qubit.tf (PennyLane-0.29.0)
- default.qubit.torch (PennyLane-0.29.0)
- default.qutrit (PennyLane-0.29.0)
- null.qubit (PennyLane-0.29.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@salcc salcc added the bug 🐛 Something isn't working label Mar 4, 2023
@salcc
Copy link
Author

salcc commented Mar 4, 2023

I tried installing previous versions of PennyLane, and it gives me the same error for all the versions down to 0.24. PennyLane 0.23 does not produce the error and works as expected.

@josh146
Copy link
Member

josh146 commented Mar 5, 2023

Hi @salcc! Unfortunately, this is due to a new version of autoray, which was released this week, which is incompatible with PennyLane 0.29.

We're currently working on a bugfix - in the meantime, if you downgrade autoray,

pip install autoray==0.6.0

this should allow PennyLane v0.29 to work.

@a-o-can
Copy link

a-o-can commented Mar 5, 2023

@salcc @josh146
It also works if you pass in a numpy array as such:

print(circuit(np.array([0.54, 0.12])))

ajberdy added a commit to amazon-braket/amazon-braket-pennylane-plugin-python that referenced this issue Mar 6, 2023
autoray update broke PennyLane (PennyLaneAI/pennylane#3867)
@trbromley
Copy link
Contributor

We have now released a bugfix 0.29.1 version of PennyLane that fixes this issue. Please run:

pip install -U pennylane

@jcmgray
Copy link

jcmgray commented Mar 21, 2023

Hello I'm the main developer of autoray - just stumbled across this issue by chance and wasn't aware of the regression, sorry about that:

  1. it should be fixed in https://github.com/jcmgray/autoray/releases/tag/v0.6.3
  2. thought I'd also encourage any developers here to upstream any issues and also extra functionality to autoray, if you want!

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

5 participants