Skip to content

Commit f557008

Browse files
committed
Add tests for the 0.0 → 0 changes
1 parent 93055b7 commit f557008

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/openfermion/ops/operators/fermion_operator_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
12-
"""Tests fermion_operator.py."""
12+
13+
"""Tests fermion_operator.py."""
14+
1315
import unittest
16+
import sympy
1417

1518
from openfermion.ops.operators.fermion_operator import FermionOperator
1619
from openfermion.hamiltonians import number_operator
@@ -78,3 +81,10 @@ def test_is_two_body_number_conserving_three(self):
7881
def test_is_two_body_number_conserving_out_of_order(self):
7982
op = FermionOperator(((0, 1), (2, 0), (1, 1), (3, 0)))
8083
self.assertTrue(op.is_two_body_number_conserving())
84+
85+
def test_add_sympy_rational(self):
86+
"""Test adding operators with sympy.Rational coefficients."""
87+
a = FermionOperator('0^ 0', sympy.Rational(1, 2))
88+
b = FermionOperator('1^ 1', sympy.Rational(1, 2))
89+
c = a + b
90+
self.assertIsInstance(c.terms[((0, 1), (0, 0))], sympy.Rational)

src/openfermion/ops/operators/symbolic_operator_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from openfermion.testing.testing_utils import EqualsTester
2121

2222
from openfermion.ops.operators.symbolic_operator import SymbolicOperator
23+
from openfermion.ops.operators.fermion_operator import FermionOperator
2324

2425

2526
class DummyOperator1(SymbolicOperator):
@@ -687,6 +688,14 @@ def test_add_sympy(self):
687688
self.assertTrue(a.terms[term_a] - coeff_a == 0)
688689
self.assertTrue(a.terms[term_b] - coeff_b - 0.5 == 0)
689690

691+
def test_add_sympy_new_term(self):
692+
"""Test adding a new term with a sympy coefficient."""
693+
x = sympy.Symbol('x')
694+
op = FermionOperator('1^', x)
695+
op += FermionOperator('2', 2 * x)
696+
self.assertEqual(op.terms[((1, 1),)], x)
697+
self.assertEqual(op.terms[((2, 0),)], 2 * x)
698+
690699
def test_radd(self):
691700
term_a = ((1, 1), (3, 0), (8, 1))
692701
coeff_a = 1

0 commit comments

Comments
 (0)