Skip to content

Commit 7a22a0a

Browse files
Strilancdamiansteiger
authored andcommitted
Add FermionOperator.zero/identity (#35)
* Add multiplicative/additive identity methods to FermionOperator
1 parent c958f1b commit 7a22a0a

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/fermilib/ops/_fermion_operator.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,26 @@ def __init__(self, term=None, coefficient=1.):
263263
'Invalid action in FermionOperator: '
264264
'Must be 0 (lowering) or 1 (raising).')
265265

266+
@staticmethod
267+
def zero():
268+
"""
269+
Returns:
270+
additive_identity (FermionOperator):
271+
A fermion operator o with the property that o+x = x+o = x for
272+
all fermion operators x.
273+
"""
274+
return FermionOperator(term=None)
275+
276+
@staticmethod
277+
def identity():
278+
"""
279+
Returns:
280+
multiplicative_identity (FermionOperator):
281+
A fermion operator u with the property that u*x = x*u = x for
282+
all fermion operators x.
283+
"""
284+
return FermionOperator(term=())
285+
266286
def compress(self, abs_tol=EQ_TOLERANCE):
267287
"""
268288
Eliminates all terms with coefficients close to zero and removes

src/fermilib/ops/_fermion_operator_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,54 @@ def test_init_tuple_npcomplex128_coefficient(self):
5656
self.assertEqual(len(fermion_op.terms), 1)
5757
self.assertEqual(fermion_op.terms[loc_op], coefficient)
5858

59+
def test_identity_is_multiplicative_identity(self):
60+
u = FermionOperator.identity()
61+
f = FermionOperator(((0, 1), (5, 0), (6, 1)), 0.6j)
62+
g = FermionOperator(((0, 0), (5, 0), (6, 1)), 0.3j)
63+
h = f + g
64+
self.assertTrue(f.isclose(u * f))
65+
self.assertTrue(f.isclose(f * u))
66+
self.assertTrue(g.isclose(u * g))
67+
self.assertTrue(g.isclose(g * u))
68+
self.assertTrue(h.isclose(u * h))
69+
self.assertTrue(h.isclose(h * u))
70+
71+
u *= h
72+
self.assertTrue(h.isclose(u))
73+
self.assertFalse(f.isclose(u))
74+
75+
# Method always returns new instances.
76+
self.assertFalse(FermionOperator.identity().isclose(u))
77+
78+
def test_zero_is_additive_identity(self):
79+
o = FermionOperator.zero()
80+
f = FermionOperator(((0, 1), (5, 0), (6, 1)), 0.6j)
81+
g = FermionOperator(((0, 0), (5, 0), (6, 1)), 0.3j)
82+
h = f + g
83+
self.assertTrue(f.isclose(o + f))
84+
self.assertTrue(f.isclose(f + o))
85+
self.assertTrue(g.isclose(o + g))
86+
self.assertTrue(g.isclose(g + o))
87+
self.assertTrue(h.isclose(o + h))
88+
self.assertTrue(h.isclose(h + o))
89+
90+
o += h
91+
self.assertTrue(h.isclose(o))
92+
self.assertFalse(f.isclose(o))
93+
94+
# Method always returns new instances.
95+
self.assertFalse(FermionOperator.zero().isclose(o))
96+
97+
def test_zero_is_multiplicative_nil(self):
98+
o = FermionOperator.zero()
99+
u = FermionOperator.identity()
100+
f = FermionOperator(((0, 1), (5, 0), (6, 1)), 0.6j)
101+
g = FermionOperator(((0, 0), (5, 0), (6, 1)), 0.3j)
102+
self.assertTrue(o.isclose(o * u))
103+
self.assertTrue(o.isclose(o * f))
104+
self.assertTrue(o.isclose(o * g))
105+
self.assertTrue(o.isclose(o * (f + g)))
106+
59107
def test_init_str(self):
60108
fermion_op = FermionOperator('0^ 5 12^', -1.)
61109
correct = ((0, 1), (5, 0), (12, 1))

0 commit comments

Comments
 (0)