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.39k stars 608 forks source link

[BUG] `qml.matrix()` augments observables in tensor product observable #2435

Closed Jaybsoni closed 2 years ago

Jaybsoni commented 2 years ago

Expected behavior

Running qml.matrix on a tensor product of observables does not change either observable.

>>> obs =  qml.PauliZ(wires=0) @ qml.PauliX(wires=1) 
>>> obs1 = qml.PauliZ(wires=0) @ qml.PauliX(wires=1) 
>>> obs2 = qml.PauliX(wires=0) @ qml.PauliZ(wires=1)
>>>
>>> qml.matrix(obs1 @ obs2)
array([[ 0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0]])

>>> obs == obs1 
True

Actual behavior

Base observable is updated during the tensor product.

image

Additional information

No response

Source code

No response

Tracebacks

No response

System information

Name: PennyLane
Version: 0.23.0.dev0
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: /Users/jay/Desktop/PL/pennylane
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, retworkx, scipy, semantic_version, toml
Required-by: amazon-braket-pennylane-plugin, PennyLane-Cirq, PennyLane-Forest, PennyLane-Lightning, PennyLane-Orquestra, PennyLane-Qchem, PennyLane-qiskit, PennyLane-qsharp
Platform info:           macOS-10.16-x86_64-i386-64bit
Python version:          3.9.7
Numpy version:           1.21.0
Scipy version:           1.7.1
Installed devices:
- lightning.qubit (PennyLane-Lightning-0.22.0)
- braket.aws.qubit (amazon-braket-pennylane-plugin-1.5.1)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.5.1)
- forest.numpy_wavefunction (PennyLane-Forest-0.18.0.dev0)
- forest.qvm (PennyLane-Forest-0.18.0.dev0)
- forest.wavefunction (PennyLane-Forest-0.18.0.dev0)
- cirq.mixedsimulator (PennyLane-Cirq-0.18.0.dev0)
- cirq.pasqal (PennyLane-Cirq-0.18.0.dev0)
- cirq.qsim (PennyLane-Cirq-0.18.0.dev0)
- cirq.qsimh (PennyLane-Cirq-0.18.0.dev0)
- cirq.simulator (PennyLane-Cirq-0.18.0.dev0)
- microsoft.QuantumSimulator (PennyLane-qsharp-0.9.0.dev0)
- qiskit.aer (PennyLane-qiskit-0.19.0.dev0)
- qiskit.basicaer (PennyLane-qiskit-0.19.0.dev0)
- qiskit.ibmq (PennyLane-qiskit-0.19.0.dev0)
- orquestra.forest (PennyLane-Orquestra-0.16.0.dev0)
- orquestra.ibmq (PennyLane-Orquestra-0.16.0.dev0)
- orquestra.qiskit (PennyLane-Orquestra-0.16.0.dev0)
- orquestra.qulacs (PennyLane-Orquestra-0.16.0.dev0)
- default.gaussian (PennyLane-0.23.0.dev0)
- default.mixed (PennyLane-0.23.0.dev0)
- default.qubit (PennyLane-0.23.0.dev0)
- default.qubit.autograd (PennyLane-0.23.0.dev0)
- default.qubit.jax (PennyLane-0.23.0.dev0)
- default.qubit.tf (PennyLane-0.23.0.dev0)
- default.qubit.torch (PennyLane-0.23.0.dev0)

Existing GitHub issues

antalszava commented 2 years ago

@Jaybsoni do I recall correctly that after our chat it turns out that this is intended behaviour?

Jaybsoni commented 2 years ago

@antalszava, yes this is correct! We can close this issue