Closed peendebak closed 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!
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.
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.
Expected behavior
I create a Hamiltonian in this way:
I would expect the
H.compute_matrix()
andH.compute_eigvals()
to give me a representation of the Hamiltonian and the eigenvalues, respectively.Actual behavior
Running:
results in
Running
gives an exception
EigvalsUndefinedError
Additional information
No response
Source code
Tracebacks
No response
System information
Existing GitHub issues