Quanlse/QHamiltonian.py
def _generateOperator(self, operator: Union[QOperator, List[QOperator]]) -> ndarray:
        """
        Generate the operator of the system in the complete Hilbert space by taking tensor products.

        :param operator: a list of operator(s)
        :return: the operator after taking the tensor products of the input operator
        """
        sysLevel = self.sysLevel
        subSysNum = self.subSysNum
        # Generate matrices
        if isinstance(operator, list):
            matrices = []
            for op in operator:
                matrices.append(op.matrix)
        else:
            matrices = operator.matrix
        # Each subsystem of the system has the same energy level.
        if isinstance(sysLevel, int):
            # We first define the identity matrix to fill un-assigned subsystems
            idMat = identity(sysLevel, dtype=complex)
            if isinstance(operator, QOperator):
                if size(matrices) == (sysLevel, sysLevel):
                    raise Error.ArgumentError("Dimension of matrix does not match the system Level.")
                # The operator is on only one subsystem.
                if operator.onSubSys == 0:
                    # This operator is on the first subsystem.
                    finalOperator = matrices
                    for i in range(1, subSysNum):
                        finalOperator = kron(finalOperator, idMat)
                else:
                    # This operator is not on the first subsystem.
                    finalOperator = idMat
                    for i in range(1, operator.onSubSys):
                        finalOperator = kron(finalOperator, idMat)
                    finalOperator = kron(finalOperator, matrices)
                    for i in range(operator.onSubSys + 1, subSysNum):
                        finalOperator = kron(finalOperator, idMat)
                return finalOperator
            elif isinstance(operator, list):
                finalOperator = []
                onSubSys = []
                for op in operator:
                    onSubSys.append(op.onSubSys)
                for i in range(subSysNum):
                    if i == 0:
                        # On the first subsystem
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            finalOperator = matrices[matrixIndex]
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel, sysLevel)):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel}).")
                        else:
                            finalOperator = idMat
                    else:
                        # Not on the first subsystem
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel, sysLevel)):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel}).")
                            finalOperator = kron(finalOperator, matrices[matrixIndex])
                        else:
                            finalOperator = kron(finalOperator, idMat)
                return finalOperator

            else:
                raise Error.ArgumentError("Variable onSubSys should be a list or an int value.")
        # The sysLevel is a list of different energy levels for multiple subsystems
        elif isinstance(sysLevel, list):
            # Create a list of identities of different dimension for each subsystem of different energy level
            idMat = [identity(i, dtype=complex) for i in sysLevel]
            # The operator is acting on only one subsystem.
            if isinstance(operator, QOperator):
                if not (matrices.shape == (sysLevel[operator.onSubSys], sysLevel[operator.onSubSys])):
                    raise Error.ArgumentError("Dimension of matrix does not match the system Level.")
                # The operator is acting on the first subsystem.
                if operator.onSubSys == 0:
                    finalOperator = matrices
                    for i in range(1, subSysNum):
                        finalOperator = kron(finalOperator, idMat[i])
                else:
                    # This operator is not acting on the first subsystem.
                    finalOperator = idMat[0]
                    for i in range(1, operator.onSubSys):
                        finalOperator = kron(finalOperator, idMat[i])
                    finalOperator = kron(finalOperator, matrices)
                    for i in range(operator.onSubSys + 1, subSysNum):
                        finalOperator = kron(finalOperator, idMat[i])
                return finalOperator
            # The operator is acting on multiple subsystems.
            elif isinstance(operator, list):
                finalOperator = []
                onSubSys = []
                for op in operator:
                    onSubSys.append(op.onSubSys)
                for i in range(subSysNum):
                    if i == 0:
                        # Acting on the first subsystem
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            finalOperator = matrices[matrixIndex]
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel[i], sysLevel[i])):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel[i]}).")
                        else:
                            finalOperator = idMat[i]
                    else:
                        # Not acting on the first subsystem
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel[i], sysLevel[i])):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel[i]}).")
                            finalOperator = kron(finalOperator, matrices[matrixIndex])
                        else:
                            finalOperator = kron(finalOperator, idMat[i])
                return finalOperator

            else:
                raise Error.ArgumentError("Variable onSubSys should be a list or an int value.")

        Generate the operator of the system in the complete Hilbert space by taking tensor products.

        :param operator: a list of operator(s)
        :return: the operator after taking the tensor products of the input operator
        
Generate the operator of the system in the complete Hilbert space by taking tensor products.
Generate the operator of the system in the complete Hilbert space by taking tensor products .
def _generateOperator(self, operator: Union[QOperator, List[QOperator]]) -> ndarray:
        sysLevel = self.sysLevel
        subSysNum = self.subSysNum
        if isinstance(operator, list):
            matrices = []
            for op in operator:
                matrices.append(op.matrix)
        else:
            matrices = operator.matrix
        if isinstance(sysLevel, int):
            idMat = identity(sysLevel, dtype=complex)
            if isinstance(operator, QOperator):
                if size(matrices) == (sysLevel, sysLevel):
                    raise Error.ArgumentError("Dimension of matrix does not match the system Level.")
                if operator.onSubSys == 0:
                    finalOperator = matrices
                    for i in range(1, subSysNum):
                        finalOperator = kron(finalOperator, idMat)
                else:
                    finalOperator = idMat
                    for i in range(1, operator.onSubSys):
                        finalOperator = kron(finalOperator, idMat)
                    finalOperator = kron(finalOperator, matrices)
                    for i in range(operator.onSubSys + 1, subSysNum):
                        finalOperator = kron(finalOperator, idMat)
                return finalOperator
            elif isinstance(operator, list):
                finalOperator = []
                onSubSys = []
                for op in operator:
                    onSubSys.append(op.onSubSys)
                for i in range(subSysNum):
                    if i == 0:
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            finalOperator = matrices[matrixIndex]
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel, sysLevel)):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel}).")
                        else:
                            finalOperator = idMat
                    else:
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel, sysLevel)):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel}).")
                            finalOperator = kron(finalOperator, matrices[matrixIndex])
                        else:
                            finalOperator = kron(finalOperator, idMat)
                return finalOperator
            else:
                raise Error.ArgumentError("Variable onSubSys should be a list or an int value.")
        elif isinstance(sysLevel, list):
            idMat = [identity(i, dtype=complex) for i in sysLevel]
            if isinstance(operator, QOperator):
                if not (matrices.shape == (sysLevel[operator.onSubSys], sysLevel[operator.onSubSys])):
                    raise Error.ArgumentError("Dimension of matrix does not match the system Level.")
                if operator.onSubSys == 0:
                    finalOperator = matrices
                    for i in range(1, subSysNum):
                        finalOperator = kron(finalOperator, idMat[i])
                else:
                    finalOperator = idMat[0]
                    for i in range(1, operator.onSubSys):
                        finalOperator = kron(finalOperator, idMat[i])
                    finalOperator = kron(finalOperator, matrices)
                    for i in range(operator.onSubSys + 1, subSysNum):
                        finalOperator = kron(finalOperator, idMat[i])
                return finalOperator
            elif isinstance(operator, list):
                finalOperator = []
                onSubSys = []
                for op in operator:
                    onSubSys.append(op.onSubSys)
                for i in range(subSysNum):
                    if i == 0:
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            finalOperator = matrices[matrixIndex]
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel[i], sysLevel[i])):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel[i]}).")
                        else:
                            finalOperator = idMat[i]
                    else:
                        if i in onSubSys:
                            matrixIndex = onSubSys.index(i)
                            operatorSize = shape(matrices[matrixIndex])
                            if not (operatorSize == (sysLevel[i], sysLevel[i])):
                                raise Error.ArgumentError(f"Dim of input matrix {operatorSize} does not match"
                                                          f" with the system level ({sysLevel[i]}).")
                            finalOperator = kron(finalOperator, matrices[matrixIndex])
                        else:
                            finalOperator = kron(finalOperator, idMat[i])
                return finalOperator
            else:
                raise Error.ArgumentError("Variable onSubSys should be a list or an int value.")
def _generateOperator ( self , operator : Union [ QOperator , List [ QOperator ] ] ) -> ndarray : sysLevel = self . sysLevel subSysNum = self . subSysNum if isinstance ( operator , list ) : matrices = [ ] for op in operator : matrices . append ( op . matrix ) else : matrices = operator . matrix if isinstance ( sysLevel , int ) : idMat = identity ( sysLevel , dtype = complex ) if isinstance ( operator , QOperator ) : if size ( matrices ) == ( sysLevel , sysLevel ) : raise Error . ArgumentError ( "Dimension of matrix does not match the system Level." ) if operator . onSubSys == 0 : finalOperator = matrices for i in range ( 1 , subSysNum ) : finalOperator = kron ( finalOperator , idMat ) else : finalOperator = idMat for i in range ( 1 , operator . onSubSys ) : finalOperator = kron ( finalOperator , idMat ) finalOperator = kron ( finalOperator , matrices ) for i in range ( operator . onSubSys + 1 , subSysNum ) : finalOperator = kron ( finalOperator , idMat ) return finalOperator elif isinstance ( operator , list ) : finalOperator = [ ] onSubSys = [ ] for op in operator : onSubSys . append ( op . onSubSys ) for i in range ( subSysNum ) : if i == 0 : if i in onSubSys : matrixIndex = onSubSys . index ( i ) finalOperator = matrices [ matrixIndex ] operatorSize = shape ( matrices [ matrixIndex ] ) if not ( operatorSize == ( sysLevel , sysLevel ) ) : raise Error . ArgumentError ( f"Dim of input matrix {operatorSize} does not match" f" with the system level ({sysLevel})." ) else : finalOperator = idMat else : if i in onSubSys : matrixIndex = onSubSys . index ( i ) operatorSize = shape ( matrices [ matrixIndex ] ) if not ( operatorSize == ( sysLevel , sysLevel ) ) : raise Error . ArgumentError ( f"Dim of input matrix {operatorSize} does not match" f" with the system level ({sysLevel})." ) finalOperator = kron ( finalOperator , matrices [ matrixIndex ] ) else : finalOperator = kron ( finalOperator , idMat ) return finalOperator else : raise Error . ArgumentError ( "Variable onSubSys should be a list or an int value." ) elif isinstance ( sysLevel , list ) : idMat = [ identity ( i , dtype = complex ) for i in sysLevel ] if isinstance ( operator , QOperator ) : if not ( matrices . shape == ( sysLevel [ operator . onSubSys ] , sysLevel [ operator . onSubSys ] ) ) : raise Error . ArgumentError ( "Dimension of matrix does not match the system Level." ) if operator . onSubSys == 0 : finalOperator = matrices for i in range ( 1 , subSysNum ) : finalOperator = kron ( finalOperator , idMat [ i ] ) else : finalOperator = idMat [ 0 ] for i in range ( 1 , operator . onSubSys ) : finalOperator = kron ( finalOperator , idMat [ i ] ) finalOperator = kron ( finalOperator , matrices ) for i in range ( operator . onSubSys + 1 , subSysNum ) : finalOperator = kron ( finalOperator , idMat [ i ] ) return finalOperator elif isinstance ( operator , list ) : finalOperator = [ ] onSubSys = [ ] for op in operator : onSubSys . append ( op . onSubSys ) for i in range ( subSysNum ) : if i == 0 : if i in onSubSys : matrixIndex = onSubSys . index ( i ) finalOperator = matrices [ matrixIndex ] operatorSize = shape ( matrices [ matrixIndex ] ) if not ( operatorSize == ( sysLevel [ i ] , sysLevel [ i ] ) ) : raise Error . ArgumentError ( f"Dim of input matrix {operatorSize} does not match" f" with the system level ({sysLevel[i]})." ) else : finalOperator = idMat [ i ] else : if i in onSubSys : matrixIndex = onSubSys . index ( i ) operatorSize = shape ( matrices [ matrixIndex ] ) if not ( operatorSize == ( sysLevel [ i ] , sysLevel [ i ] ) ) : raise Error . ArgumentError ( f"Dim of input matrix {operatorSize} does not match" f" with the system level ({sysLevel[i]})." ) finalOperator = kron ( finalOperator , matrices [ matrixIndex ] ) else : finalOperator = kron ( finalOperator , idMat [ i ] ) return finalOperator else : raise Error . ArgumentError ( "Variable onSubSys should be a list or an int value." )
Generate the operator of the system in the complete Hilbert space by taking tensor products.
Generate the operator of the system in the complete Hilbert space by taking tensor products .
"""
        Generate the operator of the system in the complete Hilbert space by taking tensor products.

        :param operator: a list of operator(s)
        :return: the operator after taking the tensor products of the input operator
        """ # Generate matrices # Each subsystem of the system has the same energy level. # We first define the identity matrix to fill un-assigned subsystems # The operator is on only one subsystem. # This operator is on the first subsystem. # This operator is not on the first subsystem. # On the first subsystem # Not on the first subsystem # The sysLevel is a list of different energy levels for multiple subsystems # Create a list of identities of different dimension for each subsystem of different energy level # The operator is acting on only one subsystem. # The operator is acting on the first subsystem. # This operator is not acting on the first subsystem. # The operator is acting on multiple subsystems. # Acting on the first subsystem # Not acting on the first subsystem
the operator after taking the tensor products of the input operator
the operator after taking the tensor products of the input operator
self
operator
Union[QOperator, List[QOperator]]
a list of operator(s)
a list of operator ( s )