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] Recursion error when accessing operator built using qml.prod #5948

Closed
1 task done
asymptoticgains opened this issue Jul 4, 2024 · 0 comments · Fixed by #6375
Closed
1 task done

[BUG] Recursion error when accessing operator built using qml.prod #5948

asymptoticgains opened this issue Jul 4, 2024 · 0 comments · Fixed by #6375
Labels
bug 🐛 Something isn't working

Comments

@asymptoticgains
Copy link

Expected behavior

Should be able to build time evolution operator that is a product of many Pauli rotations

Actual behavior

RecursionError: maximum recursion depth exceeded while calling a Python object

Additional information

It works fine if lazy=False is set in qml.prod. Seems like if there are too many nested operators in the result from taking qml.prod too many times, it will cause a recursion error. The limit seems to be about 800 terms; for example, setting range() to 370 in the for loop in attached source code runs fine.

Source code

import numpy as np 
import pennylane as qml
import random

time_ev = qml.PauliRot(random.uniform(0, 2 * np.pi), 'X', wires=1)
for i in range(500):
    time_ev = qml.prod(time_ev, qml.PauliRot(random.uniform(0, 2 * np.pi), 'Z', wires=1))
    time_ev = qml.prod(time_ev, qml.PauliRot(random.uniform(0, 2 * np.pi), 'Y', wires=1))

print(time_ev)

Tracebacks

---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
Cell In[10], line 10
      7     time_ev = qml.prod(time_ev, qml.PauliRot(random.uniform(0, 2 * np.pi), 'Z', wires=1))
      8     time_ev = qml.prod(time_ev, qml.PauliRot(random.uniform(0, 2 * np.pi), 'Y', wires=1))
---> 10 print(time_ev)

File ~/.conda/envs/xanadu/lib/python3.9/site-packages/pennylane/ops/op_math/composite.py:80, in CompositeOp.__repr__(self)
     78 def __repr__(self):
     79     return f" {self._op_symbol} ".join(
---> 80         [f"({op})" if op.arithmetic_depth > 0 else f"{op}" for op in self]
     81     )

File ~/.conda/envs/xanadu/lib/python3.9/site-packages/pennylane/ops/op_math/composite.py:80, in <listcomp>(.0)
     78 def __repr__(self):
     79     return f" {self._op_symbol} ".join(
---> 80         [f"({op})" if op.arithmetic_depth > 0 else f"{op}" for op in self]
     81     )

File ~/.conda/envs/xanadu/lib/python3.9/site-packages/pennylane/ops/op_math/prod.py:384, in Prod.arithmetic_depth(self)
    382 @property
    383 def arithmetic_depth(self) -> int:
--> 384     return 1 + max(factor.arithmetic_depth for factor in self)

File ~/.conda/envs/xanadu/lib/python3.9/site-packages/pennylane/ops/op_math/prod.py:384, in <genexpr>(.0)
...
     94 def __iter__(self):
     95     """Return the iterator over the underlying operands."""
---> 96     return iter(self.operands)

System information

Name: PennyLane
Version: 0.36.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/serene.shum/.conda/envs/xanadu/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-qiskit, PennyLane_Lightning, vibrant

Platform info:           Linux-6.5.0-41-generic-x86_64-with-glibc2.35
Python version:          3.9.19
Numpy version:           1.26.4
Scipy version:           1.13.0
Installed devices:
- default.clifford (PennyLane-0.36.0)
- default.gaussian (PennyLane-0.36.0)
- default.mixed (PennyLane-0.36.0)
- default.qubit (PennyLane-0.36.0)
- default.qubit.autograd (PennyLane-0.36.0)
- default.qubit.jax (PennyLane-0.36.0)
- default.qubit.legacy (PennyLane-0.36.0)
- default.qubit.tf (PennyLane-0.36.0)
- default.qubit.torch (PennyLane-0.36.0)
- default.qutrit (PennyLane-0.36.0)
- default.qutrit.mixed (PennyLane-0.36.0)
- null.qubit (PennyLane-0.36.0)
- lightning.qubit (PennyLane-Lightning-0.36.0)
- qiskit.aer (PennyLane-qiskit-0.36.0)
- qiskit.basicaer (PennyLane-qiskit-0.36.0)
- qiskit.basicsim (PennyLane-qiskit-0.36.0)
- qiskit.ibmq (PennyLane-qiskit-0.36.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.36.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.36.0)
- qiskit.remote (PennyLane-qiskit-0.36.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@asymptoticgains asymptoticgains added the bug 🐛 Something isn't working label Jul 4, 2024
@Alex-Preciado Alex-Preciado changed the title Recursion error when accessing operator built using qml.prod [BUG] Recursion error when accessing operator built using qml.prod Aug 12, 2024
austingmhuang pushed a commit that referenced this issue Oct 23, 2024
By default, `qml.sum` and `qml.prod` set `lazy=True`, which keeps its
operands nested. Given the recursive nature of such structures, if there
are too many levels of nesting, a `RecursionError` would occur when
accessing many of the properties and methods. Catching these errors and
re-raising a more sensible error message suggesting that the user could
either set `lazy=False` or use the `+` and `@` operators instead, which
sets `lazy=False`.

**Update**
Extending the behaviour to `SProd`

Fixes #5948
[sc-67745]
mudit2812 pushed a commit that referenced this issue Nov 11, 2024
By default, `qml.sum` and `qml.prod` set `lazy=True`, which keeps its
operands nested. Given the recursive nature of such structures, if there
are too many levels of nesting, a `RecursionError` would occur when
accessing many of the properties and methods. Catching these errors and
re-raising a more sensible error message suggesting that the user could
either set `lazy=False` or use the `+` and `@` operators instead, which
sets `lazy=False`.

**Update**
Extending the behaviour to `SProd`

Fixes #5948
[sc-67745]
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

Successfully merging a pull request may close this issue.

1 participant