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.26k stars 583 forks source link

[BUG] `qml.eigvals` sometimes fails when using `LinearCombination` #5811

Closed KetpuntoG closed 2 months ago

KetpuntoG commented 3 months ago

Expected behavior

qml.eigvals(H) should return the eigenvalues of H

Actual behavior

It returns incorrect values. The correct ones can be calculated with:

qml.eigvals(qml.SparseHamiltonian(H.sparse_matrix(), wires=H.wires))

Additional information

I tried to simplify the Hamiltonian but in simple cases it worked correctly. The error was found with this script:

h2 = qml.data.load("qchem", molname="H2", bondlength=0.742, basis="STO-3G")[0]
H, qubits = h2.hamiltonian, len(h2.hamiltonian.wires)

print(qml.eigvals(H))

The min eigenvalue should be -1.13

Source code

No response

Tracebacks

No response

System information

Name: PennyLane
Version: 0.37.0.dev0
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: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           Linux-6.1.85+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.25.2
Scipy version:           1.11.4
Installed devices:
- default.clifford (PennyLane-0.37.0.dev0)
- default.gaussian (PennyLane-0.37.0.dev0)
- default.mixed (PennyLane-0.37.0.dev0)
- default.qubit (PennyLane-0.37.0.dev0)
- default.qubit.autograd (PennyLane-0.37.0.dev0)
- default.qubit.jax (PennyLane-0.37.0.dev0)
- default.qubit.legacy (PennyLane-0.37.0.dev0)
- default.qubit.tf (PennyLane-0.37.0.dev0)
- default.qubit.torch (PennyLane-0.37.0.dev0)
- default.qutrit (PennyLane-0.37.0.dev0)
- default.qutrit.mixed (PennyLane-0.37.0.dev0)
- default.tensor (PennyLane-0.37.0.dev0)
- null.qubit (PennyLane-0.37.0.dev0)
- lightning.qubit (PennyLane_Lightning-0.36.0)

Existing GitHub issues

KetpuntoG commented 3 months ago

Hi @trbromley @isaacdevlugt , here is an issue that I consider important. It doesn't throw any error, it just returns incorrect values.