-
Notifications
You must be signed in to change notification settings - Fork 19
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
VQE supports initialization by computer #263
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import numpy as np | ||
from pytest import approx | ||
from qforte import ADAPTVQE, UCCNVQE | ||
from qforte import Circuit, Computer, gate, system_factory | ||
|
||
import os | ||
|
||
THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
data_path = os.path.join(THIS_DIR, "H4-sto6g-075a.json") | ||
|
||
|
||
class TestComputerInit: | ||
# @mark.skip(reason="long") | ||
def test_H4_VQE(self): | ||
mol = system_factory( | ||
system_type="molecule", | ||
build_type="external", | ||
basis="sto-6g", | ||
filename=data_path, | ||
) | ||
nqubits = len(mol.hf_reference) | ||
fci_energy = -2.162897881184882 | ||
|
||
computer = Computer(nqubits) | ||
coeff_vec = np.zeros(2**nqubits) | ||
coeff_vec[int("00001111", 2)] = 1 | ||
coeff_vec[int("00110011", 2)] = 0.2 | ||
coeff_vec[int("00111100", 2)] = 0.1 | ||
coeff_vec[int("11001100", 2)] = 0.04 | ||
coeff_vec /= np.linalg.norm(coeff_vec) | ||
computer.set_coeff_vec(coeff_vec) | ||
Comment on lines
+25
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As an additional test, would it be possible to check that the initial guess energy agrees with the expectation value of the Hamiltonian with respect to the state generated by the computer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initial guess energy isn't stored on the wavefunction. I could do it, but I'd need to hardcode that value of that initial guess energy. |
||
|
||
# Analytic and fin dif gradients agree | ||
analytic = UCCNVQE(mol, reference=computer, state_prep_type="computer") | ||
analytic.run(use_analytic_grad=False, pool_type="SD") | ||
findif = UCCNVQE(mol, reference=computer, state_prep_type="computer") | ||
findif.run(use_analytic_grad=True, pool_type="SD") | ||
assert analytic.get_gs_energy() == approx(findif.get_gs_energy(), abs=1.0e-8) | ||
|
||
# Computer-based and non-compute based agree | ||
hf = ADAPTVQE(mol) | ||
hf.run(use_analytic_grad=True, pool_type="GSD", avqe_thresh=1e-5) | ||
comp = ADAPTVQE(mol, reference=computer, state_prep_type="computer") | ||
comp.run(use_analytic_grad=True, pool_type="GSD", avqe_thresh=1e-5) | ||
assert hf.get_gs_energy() == approx(comp.get_gs_energy(), abs=1.0e-8) | ||
assert hf.get_gs_energy() == approx(fci_energy, abs=1.0e-8) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
_refprep
still needed in this case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe - it depends on what "this case" is.
If you want to use an excitation-based pool, it's needed. If you want to do PQE, it's needed. If you want to do moment corrections, it's needed. Otherwise, it isn't needed. Giving QForte the infrastructure to skip this step if not needed seemed beyond the scope of the PR.