Skip to content

Commit

Permalink
Checking the dimension of the Hamiltonian - some associated changes a…
Browse files Browse the repository at this point in the history
…re included

Note that General Hamiltonian is considered now as spin-unrestricted. Related to #99
  • Loading branch information
shiozaki committed Jun 29, 2021
1 parent 9f1e59c commit e985332
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/fqe/fqe_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def build_hamiltonian(ops: Union[FermionOperator, hamiltonian.Hamiltonian],

if isinstance(ops, tuple):
validate_tuple(ops)

return general_hamiltonian.General(ops, e_0=e_0)
if norb != 0 and ops[0].shape[0] == norb:
return restricted_hamiltonian.RestrictedHamiltonian(ops, e_0=e_0)
else:
return general_hamiltonian.General(ops, e_0=e_0)

if not isinstance(ops, FermionOperator):
raise TypeError('Expected FermionOperator' \
Expand Down Expand Up @@ -373,7 +375,9 @@ def convert(self, ops: Union['FermionOperator', 'hamiltonian.Hamiltonian']):
Args:
ops (FermionOperator or Hamiltonian) - input operator
"""
hamil = build_hamiltonian(ops, conserve_number=self.conserve_number())
hamil = build_hamiltonian(ops,
norb=self.norb(),
conserve_number=self.conserve_number())
return apply(self, hamil)

return convert
Expand All @@ -395,7 +399,9 @@ def convert(self,
ops (FermionOperator or Hamiltonian) - input operator
"""
hamil = build_hamiltonian(ops, conserve_number=self.conserve_number())
hamil = build_hamiltonian(ops,
norb=self.norb(),
conserve_number=self.conserve_number())
return time_evolve(self, time, hamil, inplace)

return convert
Expand Down Expand Up @@ -433,7 +439,9 @@ def convert(self,
Returns:
newwfn (Wavefunction) - a new intialized wavefunction object
"""
hamil = build_hamiltonian(ops, conserve_number=self.conserve_number())
hamil = build_hamiltonian(ops,
norb=self.norb(),
conserve_number=self.conserve_number())
return apply_generated_unitary(self,
time,
algo,
Expand Down
4 changes: 2 additions & 2 deletions src/fqe/property_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import fqe
from fqe.wavefunction import Wavefunction
from fqe.hamiltonians import general_hamiltonian
from fqe.hamiltonians import restricted_hamiltonian
from fqe.fqe_ops.fqe_ops import (
NumberOperator,
S2Operator,
Expand All @@ -47,7 +47,7 @@ def test_lih_energy(self):
nele = nalpha + nbeta
h1e, h2e, lih_ground = build_lih_data.build_lih_data('energy')

elec_hamil = general_hamiltonian.General((h1e, h2e))
elec_hamil = restricted_hamiltonian.RestrictedHamiltonian((h1e, h2e))
wfn = Wavefunction([[nele, nalpha - nbeta, norb]])
wfn.set_wfn(strategy='from_data',
raw_data={(nele, nalpha - nbeta): lih_ground})
Expand Down
21 changes: 17 additions & 4 deletions src/fqe/wavefunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from fqe.util import sort_configuration_keys
from fqe.util import vdot
from fqe.hamiltonians import hamiltonian, sparse_hamiltonian
from fqe.hamiltonians import diagonal_hamiltonian
from fqe.hamiltonians import diagonal_hamiltonian, restricted_hamiltonian
from fqe.bitstring import count_bits
from fqe.fqe_ops import fqe_operator, fqe_ops_utils
from fqe.wick import wick
Expand Down Expand Up @@ -369,6 +369,16 @@ def apply(self, hamil: 'hamiltonian.Hamiltonian') -> 'Wavefunction':
if isinstance(hamil, diagonal_hamiltonian.Diagonal):
transformed = out._apply_diagonal(hamil)
else:
if isinstance(hamil,
restricted_hamiltonian.RestrictedHamiltonian):
expected = self._norb
else:
expected = self._norb * 2
if hamil.dim() != expected:
raise ValueError('Hamiltonian has incorrect size:' \
+ ' expected {}'.format(expected) \
+ ' provided {}'.format(hamil.dim()))

transformed = out._apply_array(hamil.tensors(), hamil.e_0())

if self._conserve_spin and not self._conserve_number:
Expand All @@ -388,6 +398,8 @@ def _apply_array(self, array: Tuple[numpy.ndarray, ...],
Arg:
array (numpy.array) - numpy array
e_0 (complex) - constant part of the Hamiltonian
Returns:
newwfn (Wavvefunction) - a new intialized wavefunction object
Expand Down Expand Up @@ -942,10 +954,12 @@ def time_evolve(self, time: float, hamil,
either as raw operations or wrapped up in a Hamiltonian.
Args:
ops (FermionOperators) - FermionOperators which are to be time evolved.
time (float) - the duration by which to evolve the operators
hamil - Hamiltoninans or FermionOperators which are to be time evolved.
inplace (bool) - whether the result will be stored in place
Returns:
Wavefunction - a wavefunction object that has been time evolved.
"""
Expand Down Expand Up @@ -1291,7 +1305,6 @@ def rdm(self, string: str, brawfn: Optional['Wavefunction'] = None
# TODO: Delete or make unit test?
if __name__ == "__main__":
from openfermion import FermionOperator
import numpy
import fqe
from fqe.unittest_data import build_lih_data, build_hamiltonian

Expand Down
14 changes: 12 additions & 2 deletions src/fqe/wavefunction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from fqe.hamiltonians import general_hamiltonian
from fqe.hamiltonians import sparse_hamiltonian
from fqe.hamiltonians import diagonal_hamiltonian
from fqe.hamiltonians import restricted_hamiltonian
from fqe import get_restricted_hamiltonian

from fqe.unittest_data import build_wfn, build_hamiltonian
Expand Down Expand Up @@ -123,9 +124,18 @@ def test_apply_type_error(self):
self.assertRaises(TypeError, wfn.apply, hamil)
self.assertRaises(TypeError, wfn.time_evolve, 0.1, hamil)

def test_apply_value_error(self):
data = numpy.zeros((2, 2), dtype=numpy.complex128)
wfn = Wavefunction([[2, 0, 2]])
hamil = get_restricted_hamiltonian((data,))
self.assertRaises(ValueError, wfn.time_evolve, 0.1, hamil, True)
hamil = general_hamiltonian.General((data,))
self.assertRaises(ValueError, wfn.apply, hamil)

data2 = numpy.zeros((3, 3), dtype=numpy.complex128)
hamil2 = restricted_hamiltonian.RestrictedHamiltonian((data2,))
self.assertRaises(ValueError, wfn.apply, hamil2)

hamil3 = restricted_hamiltonian.RestrictedHamiltonian((data,))
self.assertRaises(ValueError, wfn.time_evolve, 0.1, hamil3, True)

def test_apply_individual_nbody_error(self):
fop = FermionOperator('1^ 0')
Expand Down

0 comments on commit e985332

Please sign in to comment.