Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Expectation Value of Hamiltonian with respect to CISD Wavefunction gives Different Energy than the CISD Energy #5219

Closed
1 task done
simonwa7 opened this issue Feb 15, 2024 · 3 comments
Labels
bug 🐛 Something isn't working

Comments

@simonwa7
Copy link

Expected behavior

My expectation (though feel free to challenge this) is that the expectation value of the Hamiltonian with respect to the quantum state produced by CISD or UCISD $<\psi_{CISD}|H|\psi_{CISD}>$ matches the energy achieved by the CISD method up to machine precision.

Actual behavior

For some simple systems, the energy calculated as the Hamiltonian expectation value is typically higher than the energy reported by CISD.

Additional information

No response

Source code

import pennylane as qml
from pennylane import numpy as np
from pyscf import gto, scf, cc, ci
from pennylane.qchem.convert import _pennylane_to_openfermion
from openfermion import get_sparse_operator

symbols = ["H", "H"]
geometry = np.array([[0, 0, 0],
                     [0, 0, 0.7414]], requires_grad = False)

hamiltonian, qubits = qml.qchem.molecular_hamiltonian(symbols, geometry, method="pyscf")

mol = gto.M(atom=[("H", [0, 0, 0]), ("H", [0, 0, 0.7414])],
            basis="sto-3g",
            charge=0, 
            spin=0)

hf = scf.RHF(mol).run()
ucisd = ci.UCISD(hf).run()

ucisd_wavefuction = qml.qchem.import_state(ucisd).numpy().reshape(1 << 4, 1)

coeffs, ops = hamiltonian.terms()
hamiltonian_matrix = get_sparse_operator(_pennylane_to_openfermion(coeffs, ops)).todense()

print(np.dot(ucisd_wavefuction.T.conj(), hamiltonian_matrix @ ucisd_wavefuction).numpy()[0][0].real)

Tracebacks

converged SCF energy = -1.11668438708534
E(UCISD) = -1.137270174660903  E_corr = -0.0205857875755621
-0.8897192878249813

System information

Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/williamsimon/Desktop/Research/QUAC/quac-env/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, QUAC

Platform info:           macOS-14.1-arm64-arm-64bit
Python version:          3.9.6
Numpy version:           1.26.4
Scipy version:           1.12.0
Installed devices:
- default.gaussian (PennyLane-0.33.1)
- default.mixed (PennyLane-0.33.1)
- default.qubit (PennyLane-0.33.1)
- default.qubit.autograd (PennyLane-0.33.1)
- default.qubit.jax (PennyLane-0.33.1)
- default.qubit.legacy (PennyLane-0.33.1)
- default.qubit.tf (PennyLane-0.33.1)
- default.qubit.torch (PennyLane-0.33.1)
- default.qutrit (PennyLane-0.33.1)
- null.qubit (PennyLane-0.33.1)
- lightning.qubit (PennyLane-Lightning-0.34.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@simonwa7 simonwa7 added the bug 🐛 Something isn't working label Feb 15, 2024
@soranjh
Copy link
Contributor

soranjh commented Feb 16, 2024

Hi @simonwa7, it is great that you are using qml.qchem.import_state!

The code example uses default units for the coordinates: by default, we use the atomic unit in PennyLane while pyscf uses Å. The results should match if you use a bond length of 1.4010 or (0.7414 / 0.5291772109) when you construct the Hamiltonian. You can also use hamiltonian.sparse_matrix().toarray() to compute the sparse matrix representation of the Hamiltonian. I also encourage you to use the master branch of PennyLane which has an upgrade.

Could you please run the code with this correction and let us know if there is any issue? Thanks!

@simonwa7
Copy link
Author

Thanks for the quick reply and appreciate the help @soranjh! And thanks for pointing out the unit conversion - that definitely solved some of my problems.

I did notice that when I ran similar code with a hydrogen chain with 4 atoms, I was still seeing similar inconsistencies with the energies. But when I installed pennylane from source and used the master branch as you recommended, all those inconsistencies went away!! So kudos on the upgrade :D

Thanks again, I'll go ahead and close this!

@soranjh
Copy link
Contributor

soranjh commented Feb 20, 2024

Glad to hear that @simonwa7 and thanks again for using PennyLane!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants