pennylane
pennylane copied to clipboard
[BUG] Recursion error when accessing operator built using qml.prod
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
- [X] I have searched existing GitHub issues to make sure the issue does not already exist.