Skip to content

Commit

Permalink
Additional tests for Hamiltonians and VQE
Browse files Browse the repository at this point in the history
  • Loading branch information
renezander90 committed Oct 31, 2024
1 parent 9167f8a commit 0a981f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
21 changes: 20 additions & 1 deletion tests/hamiltonian_tests/test_fermionic_to_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from qrisp.operators.fermionic import a, c
from qrisp.operators.pauli import X,Y,Z
from qrisp.vqe.problems.electronic_structure import *
from pyscf import gto

def test_fermionic_to_pauli():

Expand All @@ -38,4 +40,21 @@ def test_fermionic_to_pauli():
+ Y(0)*X(1)*X(2)*Y(3) + Y(0)*X(1)*Y(2)*X(3) \
- Y(0)*Y(1)*X(2)*X(3) + Y(0)*Y(1)*Y(2)*Y(3))

assert str(G1-K)=='0'
assert str(G1-K)=='0'


def test_hamiltonian_H2():

K = -0.812170607248714 -0.0453026155037992*X(0)*X(1)*Y(2)*Y(3) +0.0453026155037992*X(0)*Y(1)*Y(2)*X(3) +0.0453026155037992*Y(0)*X(1)*X(2)*Y(3) -0.0453026155037992*Y(0)*Y(1)*X(2)*X(3) \
+0.171412826447769*Z(0) +0.168688981703612*Z(0)*Z(1) +0.120625234833904*Z(0)*Z(2) +0.165927850337703*Z(0)*Z(3) +0.171412826447769*Z(1) \
+0.165927850337703*Z(1)*Z(2) +0.120625234833904*Z(1)*Z(3) -0.223431536908133*Z(2) +0.174412876122615*Z(2)*Z(3) -0.223431536908133*Z(3)

mol = gto.M(
atom = '''H 0 0 0; H 0 0 0.74''',
basis = 'sto-3g')

H = create_electronic_hamiltonian(mol).to_pauli_hamiltonian()

G = K-H
G.apply_threshold(1e-4)
assert str(G)=='0'
24 changes: 9 additions & 15 deletions tests/test_VQE_electronic_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
********************************************************************************/
"""

#from qrisp.vqe.problems.electronic_structure import *
from qrisp.vqe.problems.electronic_structure import *
from pyscf import gto
from qrisp import QuantumVariable
import numpy as np

#
# H2 molecule
#
"""

def test_vqe_electronic_structure_H2():
from pyscf import gto
from qrisp import QuantumVariable

mol = gto.M(
atom = '''H 0 0 0; H 0 0 0.74''',
basis = 'sto-3g')

H = create_electronic_hamiltonian(mol)
H = create_electronic_hamiltonian(mol).to_pauli_hamiltonian()
assert np.abs(H.ground_state_energy()-(-1.85238817356958))

vqe = electronic_structure_problem(mol)
Expand All @@ -40,27 +40,22 @@ def test_vqe_electronic_structure_H2():
for i in range(5):
res = vqe.run(QuantumVariable(4),
depth=1,
max_iter=50,
mes_kwargs={'method':'QWC'})
max_iter=50)
results.append(res)

assert np.abs(min(results)-(-1.85238817356958)) < 1e-1
"""

#
# BeH2 molecule, active space
#
"""
def test_vqe_electronic_structure_BeH2():
from pyscf import gto
from qrisp import QuantumVariable
mol = gto.M(
atom = f'''Be 0 0 0; H 0 0 3.0; H 0 0 -3.0''',
basis = 'sto-3g')
H = create_electronic_hamiltonian(mol,active_orb=6,active_elec=4)
H = create_electronic_hamiltonian(mol,active_orb=6,active_elec=4).to_pauli_hamiltonian()
assert np.abs(H.ground_state_energy()-(-16.73195995959339))
# runs for >1 minute
Expand All @@ -70,8 +65,7 @@ def test_vqe_electronic_structure_BeH2():
for i in range(5):
res = vqe.run(QuantumVariable(6),
depth=1,
max_iter=50,
mes_kwargs={'method':'QWC'})
max_iter=50)
results.append(res)
print(min(results))
Expand Down

0 comments on commit 0a981f1

Please sign in to comment.