@@ -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