@@ -23,7 +23,7 @@ class to define your observable, and a :class:`ExpectationMeasure` to perform
23
23
from mpqp .core .languages import Language
24
24
from mpqp .tools .display import one_lined_repr
25
25
from mpqp .tools .errors import NumberQubitsError
26
- from mpqp .tools .generics import Matrix , OneOrMany
26
+ from mpqp .tools .generics import Matrix
27
27
from mpqp .tools .maths import is_diagonal , is_hermitian , is_power_of_two
28
28
29
29
if TYPE_CHECKING :
@@ -155,7 +155,19 @@ def diagonal_elements(self) -> npt.NDArray[np.float32]:
155
155
156
156
@matrix .setter
157
157
def matrix (self , matrix : Matrix ):
158
- # TODO: add some checks on the matrix (square, power of two, hermitian)
158
+ shape = matrix .shape
159
+ if len (shape ) != 2 or shape [0 ] != shape [1 ]:
160
+ raise ValueError (f"The matrix must be square, but got shape { shape } ." )
161
+
162
+ size_1 = shape [0 ]
163
+ if not is_power_of_two (size_1 ):
164
+ raise ValueError (f"Matrix dimension { size_1 } must be a power of two." )
165
+
166
+ if not is_hermitian (matrix ):
167
+ raise ValueError (
168
+ "The matrix is not hermitian (cannot define an observable)."
169
+ )
170
+
159
171
self ._matrix = matrix
160
172
self ._pauli_string = None
161
173
self ._diag_elements = None
@@ -170,7 +182,10 @@ def pauli_string(self, pauli_string: PauliString):
170
182
171
183
@diagonal_elements .setter
172
184
def diagonal_elements (self , diag_elements : list [Real ] | npt .NDArray [np .float64 ]):
173
- # TODO: add some checks on the diagonal elements (size power of 2)
185
+ if not is_power_of_two (len (diag_elements )):
186
+ raise ValueError (
187
+ "The size of the diagonal elements of the matrix is not a power of two."
188
+ )
174
189
175
190
self ._diag_elements = diag_elements
176
191
self ._is_diagonal = True
@@ -312,9 +327,7 @@ class ExpectationMeasure(Measure):
312
327
313
328
def __init__ (
314
329
self ,
315
- observable : Union [
316
- Observable , list [Observable ]
317
- ], # TODO : handle the multi_observable case
330
+ observable : Union [Observable , list [Observable ]],
318
331
targets : Optional [list [int ]] = None ,
319
332
shots : int = 0 ,
320
333
label : Optional [str ] = None ,
0 commit comments