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.27k stars 585 forks source link

[BUG] Incorrect expval of a tensor product containing `qml.Hermitian` #2394

Open antalszava opened 2 years ago

antalszava commented 2 years ago

Expected behavior

The expectation value obtained for the tensor product of an observable containing terms using qml.Hermitian is correct.

Actual behavior

Incorrect result.

Additional information

The example compares two equivalent cases:

  1. Getting the expectation value of a tensor observable that contains qml.Hermitian terms;
  2. Querying the matrix of the same observable and using qml.Hermitian.

The results are different with 2. being the expected result.

Note: the qml.operation.Tensor class is to be changed such that qml.Hermitian terms are not accepted. The reason for that is that under the hood, qml.Hermitian uses np.linalg.eigh to get the eigenvectors and eigenvalues of the matrix. NumPy seems to return eigenvalues always in a sorted order, which can cause issues when used with the Tensor class.

Source code

import pennylane as qml
import numpy as np

dev = qml.device('default.qubit', wires=num_wires, shots=None)

A = np.array([[6+0j, 1-2j],[1+2j, -1]])

obs = qml.Hermitian(A, wires=[0]) @ qml.PauliZ(2) @ qml.Hermitian(A, wires=[1])

def prepare_psi():
    qml.RY(1.234, wires=0)
    qml.RY(np.pi/2, wires=1)
    qml.RY(2.3214, wires=2)
    qml.CNOT(wires=[0,1])
    qml.CNOT(wires=[1,2])
    qml.CNOT(wires=[2,0])

@qml.qnode(dev)
def circuit1():
    prepare_psi()
    return qml.expval(obs)

@qml.qnode(dev)
def circuit2():
    prepare_psi()
    return qml.expval(qml.Hermitian(qml.matrix(obs), wires=[0,1,2]))

res1 = circuit1()
res2 = circuit2()

print(res1, res2)

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: /pennylane
Requires: numpy, scipy, networkx, retworkx, autograd, toml, appdirs, semantic_version, autoray, cachetools, pennylane-lightning
Required-by: PennyLane-Orquestra, pennylane-qulacs, PennyLane-Honeywell, PennyLane-AQT, PennyLane-PQ, PennyLane-Forest, PennyLane-qsharp, PennyLane-Qchem, PennyLane-IonQ, PennyLane-Cirq, PennyLane-qiskit, amazon-braket-pennylane-plugin, PennyLane-SF, PennyLane-Lightning
Platform info:           Linux-5.13.0-35-generic-x86_64-with-glibc2.10
Python version:          3.8.5
Numpy version:           1.19.5
Scipy version:           1.7.3
Installed devices:
- orquestra.forest (PennyLane-Orquestra-0.15.0)
- orquestra.ibmq (PennyLane-Orquestra-0.15.0)
- orquestra.qiskit (PennyLane-Orquestra-0.15.0)
- orquestra.qulacs (PennyLane-Orquestra-0.15.0)
- qulacs.simulator (pennylane-qulacs-0.17.0.dev0)
- honeywell.hqs (PennyLane-Honeywell-0.16.0.dev0)
- aqt.noisy_sim (PennyLane-AQT-0.18.0)
- aqt.sim (PennyLane-AQT-0.18.0)
- projectq.classical (PennyLane-PQ-0.18.0.dev0)
- projectq.ibm (PennyLane-PQ-0.18.0.dev0)
- projectq.simulator (PennyLane-PQ-0.18.0.dev0)
- 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)
- microsoft.QuantumSimulator (PennyLane-qsharp-0.19.0)
- ionq.qpu (PennyLane-IonQ-0.17.0.dev0)
- ionq.simulator (PennyLane-IonQ-0.17.0.dev0)
- cirq.mixedsimulator (PennyLane-Cirq-0.22.0)
- cirq.pasqal (PennyLane-Cirq-0.22.0)
- cirq.qsim (PennyLane-Cirq-0.22.0)
- cirq.qsimh (PennyLane-Cirq-0.22.0)
- cirq.simulator (PennyLane-Cirq-0.22.0)
- qiskit.aer (PennyLane-qiskit-0.22.0.dev0)
- qiskit.basicaer (PennyLane-qiskit-0.22.0.dev0)
- qiskit.ibmq (PennyLane-qiskit-0.22.0.dev0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.22.0.dev0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.22.0.dev0)
- braket.aws.qubit (amazon-braket-pennylane-plugin-1.5.7.dev0)
- braket.local.qubit (amazon-braket-pennylane-plugin-1.5.7.dev0)
- strawberryfields.fock (PennyLane-SF-0.21.0.dev0)
- strawberryfields.gaussian (PennyLane-SF-0.21.0.dev0)
- strawberryfields.gbs (PennyLane-SF-0.21.0.dev0)
- strawberryfields.remote (PennyLane-SF-0.21.0.dev0)
- strawberryfields.tf (PennyLane-SF-0.21.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.23.0.dev2)
- 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

Jaybsoni commented 2 years ago

This won't be addressed with a direct fix. we will instead mark it as won't fix and wait for the operator arithmetic PRs to address it.