@@ -63,7 +63,7 @@ def nb_qubits(self) -> int:
63
63
return 0 if len (self ._monomials ) == 0 else self ._monomials [0 ].nb_qubits
64
64
65
65
def __str__ (self ):
66
- return " + " .join (map (str , self ._monomials ))
66
+ return " + " .join (map (str , self .round (). monomials ))
67
67
68
68
def __repr__ (self ):
69
69
return str (self )
@@ -165,6 +165,32 @@ def simplify(self) -> PauliString:
165
165
PauliStringMonomial (0 , [I for _ in range (self .nb_qubits )])
166
166
)
167
167
return res
168
+
169
+ def round (self , round_off_till : int = 5 ) -> PauliString :
170
+ """Round the coefficients of the PauliString to a specified number of decimal places.
171
+
172
+ Example:
173
+ >>> ps = 0.6875*I@I + 0.415*I@X + 0.1275*I@Z + 1.0*X@I + 1.0*X@X + 0.0375*Z@I + 0.085*Z@X + -0.2225*Z@Z
174
+ >>> rounded_ps = ps.round(1)
175
+ >>> print(rounded_ps)
176
+ -0.2*Z@Z + 1.0*X@X + 0.1*I@Z + 1.0*X@I + 0.1*Z@X + 0.7*I@I + 0.4*I@X
177
+
178
+ Args:
179
+ round_off_till : Number of decimal places to round the coefficients to. Defaults to 5.
180
+
181
+ Returns:
182
+ PauliString: A PauliString with coefficients rounded to the specified number of decimal places.
183
+ """
184
+ res = PauliString ()
185
+ for mono in self .monomials :
186
+ coef = float (round (mono .coef , round_off_till ))
187
+ if coef != 0 :
188
+ res .monomials .append (PauliStringMonomial (coef , mono .atoms ))
189
+ if len (res .monomials ) == 0 :
190
+ res .monomials .append (
191
+ PauliStringMonomial (0 , [I for _ in range (self .nb_qubits )])
192
+ )
193
+ return res
168
194
169
195
def to_matrix (self ) -> Matrix :
170
196
"""Convert the PauliString to a matrix representation.
0 commit comments