1""" 2Defines a class representing a Hilbert space. 3""" 4 5importnumpyasnp 6fromtypingimportOptional,Any,Union 7
[docs] 8classHilbertSpace(): 9""" 10 Represents a Hilbert space with a prefered basis indexed by non-negative 11 integers. 12 13 Note 14 ---- 15 The class also can be treated as an iterator and will return :attr:`basis` 16 as the iterator. 17 18 19 --- 20 """ 21 22_basis:np.ndarray[int] 23"An array of positive integers labeling the Hilbert space basis" 24_inverse:np.ndarray[int] 25"An array satisfying the property ``_inverse[basis[i]]=i``" 26
[docs] 27def__init__(self,basis:np.ndarray[int]): 28""" 29 Initialises a new :class:`HilbertSpace`. 30 31 Parameters 32 ---------- 33 basis : NDArray[Shape[:attr:`dim`], int] 34 The labels for the basis vectors. 35 """ 36self._basis=np.array(basis) 37self._basis.flags.writeable=False 38iflen(self._basis)==0: 39self._inverse=np.empty(0,dtype=np.int64) 40else: 41self._inverse=np.zeros(self._basis.max()+1,dtype=np.int64) 42fori,xinenumerate(self._basis): 43self._inverse[x]=i 44self._inverse.flags.writeable=False
45def__iter__(self):returniter(self._basis) 46def__getitem__(self,key):returnself._basis[key] 47def__len__(self):returnlen(self._basis) 48def__eq__(self,other:"HilbertSpace")->bool: 49ifisinstance(other,self.__class__): 50returnnp.array_equal(self._basis,other._basis) 51returnFalse 52@property 53defdim(self): 54""" 55 The Hilbert space dimension 56 """ 57returnlen(self) 58@property 59defbasis(self)->np.ndarray[int]: 60""" 61 An array of positive integers labeling the Hilbert space basis 62 """ 63returnself._basis 64@property 65definverse(self)->np.ndarray[int]: 66"An array satisfying the property ``self.inverse[self.basis[i]]=i``" 67returnself._inverse
[docs] 68defbasis_vector(self,basis_state_label:int)->np.ndarray[np.complex128]: 69"""Returns a column vector represnetation corresponding to the input 70 basis state label. 71 72 Parameters 73 ---------- 74 basis_state_label : int 75 A positive integer denoting the label of the basis state to generate 76 the basis vector for. 77 78 Returns 79 ------- 80 NDArray[Shape[:attr:`dim`], np.complex128] 81 The basis vector corresponding to the input basis state label. 82 """ 83returnnp.eye(1, 84len(self._basis), 85self._inverse[basis_state_label] 86).flatten().astype(np.complex128)
[docs] 87defget_subspace(self,filter:np.ndarray[bool])->"HilbertSpace": 88"""Generates a new subspace by filtering the basis state labels. 89 90 Parameters 91 ---------- 92 filter : NDArray[Shape[:attr:`dim`], bool] 93 ``True`` entries are retained in the new subspace. 94 95 Returns 96 ------- 97 HilbertSpace 98 The filtered subspace. 99 """100returnHilbertSpace(self._basis[filter])
101@staticmethod102def_labels(state:Any)->str:103"""104 Generates a strings that represent the state.105106 Parameters107 ----------108 state : Any109 The state to label.110111 Returns112 -------113 str114 The label for the specified state.115 """116returnf"|{repr(state)}⟩"
[docs]117deflabels(self,118states:Optional[Union[int,list[int]]]=None119)->Union[str,list[str]]:120"""121 Generates a string (list of strings) that represent the state(s).122123 Parameters124 ----------125 states : int | list[int], optional126 The state(s) to label. If ``None`` then the labels for all states in127 :attr:`basis` are returned. By default ``None``.128129 Returns130 -------131 str | list[str]132 The label(s) for the specified states.133 """134ifisinstance(states,int):135returnself._labels(states)136elifstatesisNone:states=self.basis137return[self._labels(int(s))forsinstates]