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

[BUG] Hamiltonion is unable to compute matrix and eigenvalues #3603

Closed peendebak closed 1 year ago

peendebak commented 1 year ago

Expected behavior

I create a Hamiltonian in this way:

import pennylane as qml
from pennylane import numpy as np

symbols = ["H", "H"]
coordinates = np.array([0.0, 0.0, -0.6614, 0.0, 0.0, 0.6614])
H, qubits = qml.qchem.molecular_hamiltonian(symbols, coordinates)
print(H)

I would expect the H.compute_matrix() and H.compute_eigvals() to give me a representation of the Hamiltonian and the eigenvalues, respectively.

Actual behavior

Running:

H.compute_matrix() # fails, although qml.matrix(H) seems to work

results in

---------------------------------------------------------------------------
MatrixUndefinedError                      Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3772\915052534.py in <cell line: 1>()
----> 1 H.compute_matrix() # fails, although qml.matrix(H) seems to work

C:\projects\misc\pennylane\pennylane\operation.py in compute_matrix(*params, **hyperparams)
    444             tensor_like: matrix representation
    445         """
--> 446         raise MatrixUndefinedError
    447 
    448     # pylint: disable=no-self-argument, comparison-with-callable

MatrixUndefinedError: 

Running

H.compute_eigvals() 

gives an exception EigvalsUndefinedError

Additional information

No response

Source code

See the comments above

Tracebacks

No response

System information

Name: PennyLane
Version: 0.28.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: c:\develop\env310\lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning

Platform info:           Windows-10-10.0.19045-SP0
Python version:          3.10.8
Numpy version:           1.22.4
Scipy version:           1.9.2
Installed devices:
- default.gaussian (PennyLane-0.28.0.dev0)
- default.mixed (PennyLane-0.28.0.dev0)
- default.qubit (PennyLane-0.28.0.dev0)
- default.qubit.autograd (PennyLane-0.28.0.dev0)
- default.qubit.jax (PennyLane-0.28.0.dev0)
- default.qubit.tf (PennyLane-0.28.0.dev0)
- default.qubit.torch (PennyLane-0.28.0.dev0)
- default.qutrit (PennyLane-0.28.0.dev0)
- null.qubit (PennyLane-0.28.0.dev0)
- lightning.qubit (PennyLane-Lightning-0.26.1)

Existing GitHub issues

timmysilv commented 1 year ago

Hi @peendebak, thanks for bringing this up! Today, the way to get the matrix of a Hamiltonian is by using qml.matrix(H) (which uses qml.utils.sparse_hamiltonian under the hood). It would definitely be nice if the error message told you this, so I'll look into that.

Side note: op.compute_matrix is usually more of an internal implementation detail, and you should instead use op.matrix when possible (again, not for Hamiltonians because they are a special case).

Hope that helps!

albi3ro commented 1 year ago

This is a bit of a historical artifact.

We didn't want to rely on the matrices for Hamiltonians, since for large Hamiltonian's these can be expensive to compute. Eigenvalues can be calculated by diagonalizing the matrix, so eigenvalues are thus even more expensive for large Hamiltonians.

We are moving away from that way of thinking about things. We are the process of replacing Hamiltonian's by a combination of Sum, SProd, and Prod classes. These three all define matrices, eigenvalues, and diagonalizing gates whenever possible.

As an additional note: qml.matrix is the user-facing way to request matrices.

albi3ro commented 1 year ago

I'm going to go ahead and close this bug report. This is more a lack of features than one that isn't behaving properly, and we are already working to add that feature coverage.