Skip to content

Commit

Permalink
Add constant property to SymbolicOperator (#423)
Browse files Browse the repository at this point in the history
* add constant property to SymbolicOperator

* add test
  • Loading branch information
kevinsung authored and babbush committed Aug 6, 2018
1 parent 9cbe5d4 commit c1c3c5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/openfermion/ops/_polynomial_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def __init__(self, n_body_tensors):

@property
def constant(self):
"""Get the value of the constant term."""
return self.n_body_tensors.get((), 0.)
"""The value of the constant term."""
return self.n_body_tensors.get((), 0.0)

@constant.setter
def constant(self, value):
Expand Down
5 changes: 5 additions & 0 deletions src/openfermion/ops/_symbolic_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ def _parse_string(self, term):
# Return a tuple
return tuple(processed_term)

@property
def constant(self):
"""The value of the constant term."""
return self.terms.get((), 0.0)

@classmethod
def zero(cls):
"""
Expand Down
7 changes: 7 additions & 0 deletions src/openfermion/ops/_symbolic_operator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def test_symbolic_operator_is_abstract_cant_instantiate(self):
with self.assertRaises(TypeError):
_ = SymbolicOperator()

def test_symbolic_operator_constant(self):
op = DummyOperator1((), 1.723)
self.assertEqual(op.constant, 1.723)

op = DummyOperator1('1^ 4', 0.182)
self.assertEqual(op.constant, 0.0)

def test_init_single_factor(self):
"""Test initialization of the form DummyOperator((index, action))."""
equals_tester = EqualsTester(self)
Expand Down

0 comments on commit c1c3c5b

Please sign in to comment.