PennyLaneAI / pennylane

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.
https://pennylane.ai
Apache License 2.0
2.29k stars 590 forks source link

[BUG] Tensor product alters operands #5823

Closed poojithumeshrao closed 3 months ago

poojithumeshrao commented 3 months ago

Expected behavior

When a tensor product between two 'qml.operation.Tensor' operands (c = a @ b) is performed, the result must only be in variable 'c'. The value of operands 'a' and 'b' must not be altered.

Actual behavior

Upon computing c = a @ b, the value of operand 'a' is changed to 'c'. This causes problems as the value of 'a' is lost.

Additional information

No response

Source code

import pennylane as qml

a = qml.Identity(0) @ qml.Identity(1)
b = qml.Identity(2) @ qml.Identity(3)
c = a @ b
print('a = ',a)
print('b = ',b)
print('c = ',c)

Tracebacks

The code snippet doesn't throw an error but does not follow the expected behavior. The output of the above code snippet is as follows:

a =  Identity(wires=[0]) @ Identity(wires=[1]) @ Identity(wires=[2]) @ Identity(wires=[3])
b =  Identity(wires=[2]) @ Identity(wires=[3])
c =  Identity(wires=[0]) @ Identity(wires=[1]) @ Identity(wires=[2]) @ Identity(wires=[3])

System information

Name: PennyLane
Version: 0.32.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/poojith/miniconda3/envs/pennylane/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-Lightning

Platform info:           Linux-4.19.0-23-amd64-x86_64-with-glibc2.28
Python version:          3.9.17
Numpy version:           1.23.5
Scipy version:           1.11.2
Installed devices:
- default.gaussian (PennyLane-0.32.0)
- default.mixed (PennyLane-0.32.0)
- default.qubit (PennyLane-0.32.0)
- default.qubit.autograd (PennyLane-0.32.0)
- default.qubit.jax (PennyLane-0.32.0)
- default.qubit.tf (PennyLane-0.32.0)
- default.qubit.torch (PennyLane-0.32.0)
- default.qutrit (PennyLane-0.32.0)
- null.qubit (PennyLane-0.32.0)
- lightning.qubit (PennyLane-Lightning-0.32.0)

Existing GitHub issues