def __rmul__(self, other): """ MULTIPLICATION with Qobj on RIGHT [ ex. 4*Qobj ] """ if isinstance(other, Qobj): #if both are quantum objects if self.shape[1] == other.shape[0] and self.dims[1] == other.dims[ 0]: out = Qobj() out.data = other.data * self.data out.dims = self.dims out.shape = [self.shape[0], other.shape[1]] out.type = ischeck(out) out.isherm = hermcheck(out) if qset.auto_tidyup: return out.tidyup() else: return out else: raise TypeError("Incompatible Qobj shapes") if isinstance( other, (list, np.ndarray )): # if other is a list, do element-wise multiplication return np.array([item * self for item in other]) if _checkeseries(other) == 'eseries': return other.__mul__(self) if isinstance( other, (int, float, complex, np.int64)): #if other is int,float,complex out = Qobj(type=self.type) out.data = other * self.data out.dims = self.dims out.shape = self.shape isherm = None if isinstance(other, (int, float)): isherm = self.isherm if qset.auto_tidyup: return Qobj(out, type=self.type, isherm=isherm).tidyup() else: return Qobj(out, type=self.type, isherm=isherm) else: raise TypeError("Incompatible object for multiplication")
def __rmul__(self,other): """ MULTIPLICATION with Qobj on RIGHT [ ex. 4*Qobj ] """ if isinstance(other,Qobj): #if both are quantum objects if self.shape[1]==other.shape[0] and self.dims[1]==other.dims[0]: out=Qobj() out.data=other.data * self.data out.dims=self.dims out.shape=[self.shape[0],other.shape[1]] out.type=ischeck(out) out.isherm=hermcheck(out) if qset.auto_tidyup: return out.tidyup() else: return out else: raise TypeError("Incompatible Qobj shapes") if isinstance(other, (list,np.ndarray)): # if other is a list, do element-wise multiplication return np.array([item*self for item in other]) if _checkeseries(other)=='eseries': return other.__mul__(self) if isinstance(other,(int,float,complex,np.int64)): #if other is int,float,complex out=Qobj(type=self.type) out.data=other*self.data out.dims=self.dims out.shape=self.shape isherm=None if isinstance(other,(int,float)): isherm=self.isherm if qset.auto_tidyup: return Qobj(out,type=self.type,isherm=isherm).tidyup() else: return Qobj(out,type=self.type,isherm=isherm) else: raise TypeError("Incompatible object for multiplication")
def __init__(self,inpt=np.array([[0]]),dims=[[],[]],shape=[],type=None,isherm=None,fast=False): """ Qobj constructor. """ if fast=='mc':#fast Qobj construction for use in mcsolve with ket output self.data=sp.csr_matrix(inpt,dtype=complex) self.dims=dims self.shape=shape self.isherm=False self.type='ket' return if fast=='mc-dm':#fast Qobj construction for use in mcsolve with dm output self.data=sp.csr_matrix(inpt,dtype=complex) self.dims=dims self.shape=shape self.isherm=True self.type='oper' return elif isinstance(inpt,Qobj):#if input is already Qobj then return identical copy ##Quantum object data self.data=sp.csr_matrix(inpt.data,dtype=complex) #make sure matrix is sparse (safety check) if not any(dims): ## Dimensions of quantum object used for keeping track of tensor components self.dims=inpt.dims else: self.dims=dims if not any(shape): ##Shape of undelying quantum obejct data matrix self.shape=inpt.shape else: self.shape=shape else:#if input is NOT Qobj #if input is int, float, or complex then convert to array if isinstance(inpt,(int,float,complex,np.int64)): inpt=np.array([[inpt]]) #case where input is array or sparse if (isinstance(inpt,np.ndarray)) or sp.issparse(inpt): self.data=sp.csr_matrix(inpt,dtype=complex) #data stored as space array if not any(dims): self.dims=[[int(inpt.shape[0])],[int(inpt.shape[1])]] #list of object dimensions else: self.dims=dims if not any(shape): self.shape=[int(inpt.shape[0]),int(inpt.shape[1])] # list of matrix dimensions else: self.shape=shape elif isinstance(inpt,list):# case where input is not array or sparse, i.e. a list if len(np.array(inpt).shape)==1:#if list has only one dimension (i.e [5,4]) inpt=np.array([inpt]) else:#if list has two dimensions (i.e [[5,4]]) inpt=np.array(inpt) self.data=sp.csr_matrix(inpt,dtype=complex) if not any(dims): self.dims=[[int(inpt.shape[0])],[int(inpt.shape[1])]] else: self.dims=dims if not any(shape): self.shape=[int(inpt.shape[0]),int(inpt.shape[1])] else: self.shape=shape else: print("Warning: Initializing Qobj from unsupported type") inpt=np.array([[0]]) self.data=sp.csr_matrix(inpt,dtype=complex) self.dims=[[int(inpt.shape[0])],[int(inpt.shape[1])]] self.shape=[int(inpt.shape[0]),int(inpt.shape[1])] ##Signifies if quantum object corresponds to Hermitian operator if isherm == None: if qset.auto_herm: self.isherm=hermcheck(self) else: self.isherm=None else: self.isherm=isherm ##Signifies if quantum object corresponds to a ket, bra, operator, or super-operator if type == None: self.type=ischeck(self) else: self.type=type
def __init__(self, inpt=np.array([[0]]), dims=[[], []], shape=[], type=None, isherm=None, fast=False): """ Qobj constructor. """ if fast == 'mc': #fast Qobj construction for use in mcsolve with ket output self.data = sp.csr_matrix(inpt, dtype=complex) self.dims = dims self.shape = shape self.isherm = False self.type = 'ket' return if fast == 'mc-dm': #fast Qobj construction for use in mcsolve with dm output self.data = sp.csr_matrix(inpt, dtype=complex) self.dims = dims self.shape = shape self.isherm = True self.type = 'oper' return elif isinstance( inpt, Qobj): #if input is already Qobj then return identical copy ##Quantum object data self.data = sp.csr_matrix( inpt.data, dtype=complex) #make sure matrix is sparse (safety check) if not any(dims): ## Dimensions of quantum object used for keeping track of tensor components self.dims = inpt.dims else: self.dims = dims if not any(shape): ##Shape of undelying quantum obejct data matrix self.shape = inpt.shape else: self.shape = shape else: #if input is NOT Qobj #if input is int, float, or complex then convert to array if isinstance(inpt, (int, float, complex, np.int64)): inpt = np.array([[inpt]]) #case where input is array or sparse if (isinstance(inpt, np.ndarray)) or sp.issparse(inpt): self.data = sp.csr_matrix( inpt, dtype=complex) #data stored as space array if not any(dims): self.dims = [[int(inpt.shape[0])], [int(inpt.shape[1])] ] #list of object dimensions else: self.dims = dims if not any(shape): self.shape = [int(inpt.shape[0]), int(inpt.shape[1]) ] # list of matrix dimensions else: self.shape = shape elif isinstance( inpt, list ): # case where input is not array or sparse, i.e. a list if len(np.array(inpt).shape ) == 1: #if list has only one dimension (i.e [5,4]) inpt = np.array([inpt]) else: #if list has two dimensions (i.e [[5,4]]) inpt = np.array(inpt) self.data = sp.csr_matrix(inpt, dtype=complex) if not any(dims): self.dims = [[int(inpt.shape[0])], [int(inpt.shape[1])]] else: self.dims = dims if not any(shape): self.shape = [int(inpt.shape[0]), int(inpt.shape[1])] else: self.shape = shape else: print("Warning: Initializing Qobj from unsupported type") inpt = np.array([[0]]) self.data = sp.csr_matrix(inpt, dtype=complex) self.dims = [[int(inpt.shape[0])], [int(inpt.shape[1])]] self.shape = [int(inpt.shape[0]), int(inpt.shape[1])] ##Signifies if quantum object corresponds to Hermitian operator if isherm == None: if qset.auto_herm: self.isherm = hermcheck(self) else: self.isherm = None else: self.isherm = isherm ##Signifies if quantum object corresponds to a ket, bra, operator, or super-operator if type == None: self.type = ischeck(self) else: self.type = type