4747import numpy as np
4848import opt_einsum as oe
4949from numpy import linalg as nla
50- from numpy .core import ndarray
5150from scipy import linalg as sla
5251from sparse import COO
5352
5655__all__ = ['Basis' , 'expand' , 'ggm_expand' , 'normalize' ]
5756
5857
59- class Basis (ndarray ):
58+ class Basis (np . ndarray ):
6059 r"""
6160 Class for operator bases. There are several ways to instantiate a
6261 Basis object:
@@ -217,12 +216,12 @@ def __eq__(self, other: object) -> bool:
217216 # Not ndarray
218217 return np .equal (self , other )
219218
220- return np .allclose (self .view (ndarray ), other .view (ndarray ),
219+ return np .allclose (self .view (np . ndarray ), other .view (np . ndarray ),
221220 atol = self ._atol , rtol = self ._rtol )
222221
223- def __contains__ (self , item : ndarray ) -> bool :
222+ def __contains__ (self , item : np . ndarray ) -> bool :
224223 """Implement 'in' operator."""
225- return any (np .isclose (item .view (ndarray ), self .view (ndarray ),
224+ return any (np .isclose (item .view (np . ndarray ), self .view (np . ndarray ),
226225 rtol = self ._rtol , atol = self ._atol ).all (axis = (1 , 2 )))
227226
228227 def __array_wrap__ (self , out_arr , context = None ):
@@ -232,7 +231,7 @@ def __array_wrap__(self, out_arr, context=None):
232231 https://github.com/numpy/numpy/issues/5819#issue-72454838
233232 """
234233 if out_arr .ndim :
235- return ndarray .__array_wrap__ (self , out_arr , context )
234+ return np . ndarray .__array_wrap__ (self , out_arr , context )
236235
237236 def _print_checks (self ) -> None :
238237 """Print checks for debug purposes."""
@@ -265,7 +264,7 @@ def isorthonorm(self) -> bool:
265264 actual = U .conj () @ U .T
266265 target = np .identity (dim )
267266 atol = self ._eps * (self .d ** 2 )** 3
268- self ._isorthonorm = np .allclose (actual .view (ndarray ), target ,
267+ self ._isorthonorm = np .allclose (actual .view (np . ndarray ), target ,
269268 atol = atol , rtol = self ._rtol )
270269
271270 return self ._isorthonorm
@@ -284,7 +283,10 @@ def istraceless(self) -> bool:
284283 elif nonzero [0 ].size == 1 :
285284 # Single element has nonzero trace, check if (proportional to)
286285 # identity
287- elem = self [nonzero ][0 ].view (ndarray ) if self .ndim == 3 else self .view (ndarray )
286+ if self .ndim == 3 :
287+ elem = self [nonzero ][0 ].view (np .ndarray )
288+ else :
289+ elem = self .view (np .ndarray )
288290 offdiag_nonzero = elem [~ np .eye (self .d , dtype = bool )].nonzero ()
289291 diag_equal = np .diag (elem ) == elem [0 , 0 ]
290292 if diag_equal .all () and not offdiag_nonzero [0 ].any ():
@@ -597,7 +599,7 @@ def _full_from_partial(elems: Sequence, traceless: bool, labels: Sequence[str])
597599 # sort Identity label to the front, default to first if not found
598600 # (should not happen since traceless checks that it is present)
599601 id_idx = next ((i for i , elem in enumerate (elems )
600- if np .allclose (Id .view (ndarray ), elem .view (ndarray ),
602+ if np .allclose (Id .view (np . ndarray ), elem .view (np . ndarray ),
601603 rtol = elems ._rtol , atol = elems ._atol )), 0 )
602604 labels .insert (0 , labels .pop (id_idx ))
603605
@@ -606,7 +608,7 @@ def _full_from_partial(elems: Sequence, traceless: bool, labels: Sequence[str])
606608 return basis , labels
607609
608610
609- def _norm (b : Sequence ) -> ndarray :
611+ def _norm (b : Sequence ) -> np . ndarray :
610612 """Frobenius norm with two singleton dimensions inserted at the end."""
611613 b = np .asanyarray (b )
612614 norm = nla .norm (b , axis = (- 1 , - 2 ))
@@ -633,8 +635,8 @@ def normalize(b: Basis) -> Basis:
633635 return (b / _norm (b )).squeeze ().view (Basis )
634636
635637
636- def expand (M : Union [ndarray , Basis ], basis : Union [ndarray , Basis ],
637- normalized : bool = True , hermitian : bool = False , tidyup : bool = False ) -> ndarray :
638+ def expand (M : Union [np . ndarray , Basis ], basis : Union [np . ndarray , Basis ],
639+ normalized : bool = True , hermitian : bool = False , tidyup : bool = False ) -> np . ndarray :
638640 r"""
639641 Expand the array *M* in the basis given by *basis*.
640642
@@ -684,8 +686,8 @@ def cast(arr):
684686 return util .remove_float_errors (coefficients ) if tidyup else coefficients
685687
686688
687- def ggm_expand (M : Union [ndarray , Basis ], traceless : bool = False ,
688- hermitian : bool = False ) -> ndarray :
689+ def ggm_expand (M : Union [np . ndarray , Basis ], traceless : bool = False ,
690+ hermitian : bool = False ) -> np . ndarray :
689691 r"""
690692 Expand the matrix *M* in a Generalized Gell-Mann basis [Bert08]_.
691693 This function makes use of the explicit construction prescription of
@@ -767,7 +769,7 @@ def cast(arr):
767769 return coeffs .squeeze () if square else coeffs
768770
769771
770- def equivalent_pauli_basis_elements (idx : Union [Sequence [int ], int ], N : int ) -> ndarray :
772+ def equivalent_pauli_basis_elements (idx : Union [Sequence [int ], int ], N : int ) -> np . ndarray :
771773 """
772774 Get the indices of the equivalent (up to identities tensored to it)
773775 basis elements of Pauli bases of qubits at position idx in the total
@@ -780,7 +782,7 @@ def equivalent_pauli_basis_elements(idx: Union[Sequence[int], int], N: int) -> n
780782 return elem_idx
781783
782784
783- def remap_pauli_basis_elements (order : Sequence [int ], N : int ) -> ndarray :
785+ def remap_pauli_basis_elements (order : Sequence [int ], N : int ) -> np . ndarray :
784786 """
785787 For a N-qubit Pauli basis, transpose the order of the subsystems and
786788 return the indices that permute the old basis to the new.
0 commit comments