def matrix_element(self, bra, ket): """Calculates a matrix element. Gives matrix for the Qobj sandwiched between a `bra` and `ket` vector. Parameters ----------- bra : qobj Quantum object of type 'bra'. ket : qobj Quantum object of type 'ket'. Returns ------- elem : complex Complex valued matrix element. Raises ------ TypeError Can only calculate matrix elements between a bra and ket quantum object. """ if isoper(self): if isbra(bra) and isket(ket): return (bra.data * self.data * ket.data)[0,0] if isket(bra) and isket(ket): return (bra.data.T * self.data * ket.data)[0,0] raise TypeError("Can only calculate matrix elements for operators and between ket and bra Qobj")
def matrix_element(self, bra, ket): """Calculates a matrix element. Gives matrix for the Qobj sandwiched between a `bra` and `ket` vector. Parameters ----------- bra : qobj Quantum object of type 'bra'. ket : qobj Quantum object of type 'ket'. Returns ------- elem : complex Complex valued matrix element. Raises ------ TypeError Can only calculate matrix elements between a bra and ket quantum object. """ if isoper(self): if isbra(bra) and isket(ket): return (bra.data * self.data * ket.data)[0, 0] if isket(bra) and isket(ket): return (bra.data.T * self.data * ket.data)[0, 0] raise TypeError( "Can only calculate matrix elements for operators and between ket and bra Qobj" )
def transform(self, inpt, inverse=False): """Performs a basis transformation defined by an input array. Input array can be a ``matrix`` defining the transformation, or a ``list`` of kets that defines the new basis. Parameters ---------- inpt : array_like A ``matrix`` or ``list`` of kets defining the transformation. inverse : bool Whether to return inverse transformation. Returns ------- oper : qobj Operator in new basis. Notes ----- This function is still in development. """ if isinstance(inpt, list) or isinstance(inpt, np.ndarray): if len(inpt) != self.shape[0] or len(inpt) != self.shape[1]: raise TypeError('Invalid size of ket list for basis transformation') S = np.matrix([inpt[n].full()[:,0] for n in range(len(inpt))]).H elif isinstance(inpt,np.ndarray): S = np.matrix(inpt) else: raise TypeError('Invalid operand for basis transformation') # normalize S just in case the supplied basis states aren't normalized #S = S/la.norm(S) out=Qobj(type=self.type) out.dims=[self.dims[1],self.dims[0]] out.shape=[self.shape[1],self.shape[0]] out.isherm=self.isherm out.type=self.type # transform data if inverse: if isket(self): out.data = S.H * self.data elif isbra(self): out.data = self.data * S else: out.data = S * self.data * S.H else: if isket(self): out.data = S * self.data elif isbra(self): out.data = self.data * S.H else: out.data = S.H * self.data * S # force sparse out.data = sp.csr_matrix(out.data,dtype=complex) return out
def transform(self, inpt, inverse=False): """Performs a basis transformation defined by an input array. Input array can be a ``matrix`` defining the transformation, or a ``list`` of kets that defines the new basis. Parameters ---------- inpt : array_like A ``matrix`` or ``list`` of kets defining the transformation. inverse : bool Whether to return inverse transformation. Returns ------- oper : qobj Operator in new basis. Notes ----- This function is still in development. """ if isinstance(inpt, list) or isinstance(inpt, np.ndarray): if len(inpt) != self.shape[0] or len(inpt) != self.shape[1]: raise TypeError( 'Invalid size of ket list for basis transformation') S = np.matrix([inpt[n].full()[:, 0] for n in range(len(inpt))]).H elif isinstance(inpt, np.ndarray): S = np.matrix(inpt) else: raise TypeError('Invalid operand for basis transformation') # normalize S just in case the supplied basis states aren't normalized #S = S/la.norm(S) out = Qobj(type=self.type) out.dims = [self.dims[1], self.dims[0]] out.shape = [self.shape[1], self.shape[0]] out.isherm = self.isherm out.type = self.type # transform data if inverse: if isket(self): out.data = S.H * self.data elif isbra(self): out.data = self.data * S else: out.data = S * self.data * S.H else: if isket(self): out.data = S * self.data elif isbra(self): out.data = self.data * S.H else: out.data = S.H * self.data * S # force sparse out.data = sp.csr_matrix(out.data, dtype=complex) return out