Quanlse/QHamiltonian.py
def subSystem(self, onSubSys: Union[int, List[int]]) -> 'QHamiltonian':
        """
        This method extracts a subsystem from the target Hamiltonian.

        :param onSubSys: a list of qubit indexes
        :return: subsystem's QHamiltonian object
        """
        subHam = copy.deepcopy(self)
        subHam.clearCache()
        # Information about the sub system
        if isinstance(onSubSys, int):
            subQubits = 1
            indexMapping = {onSubSys: 0}
            if onSubSys >= self._subSysNum:
                raise Error.ArgumentError("onSubSys should be less than the subSysNum of this QHamiltonian.")
        elif isinstance(onSubSys, list):
            subQubits = len(onSubSys)
            # qubit index mapping
            indexMapping = {}
            for newId, originalId in enumerate(onSubSys):
                indexMapping[originalId] = newId
                if originalId >= self._subSysNum:
                    raise Error.ArgumentError("all onSubSys items should be less than the subSysNum "
                                              "of this QHamiltonian.")
        else:
            raise Error.ArgumentError("Unsupported input of qubitNum, it should be an int value or a list.")

        # Update the sysLevel property
        if isinstance(self._sysLevel, list):
            if isinstance(onSubSys, int):
                subHam.sysLevel = self._sysLevel[onSubSys]
                subHam.job.sysLevel = self._sysLevel[onSubSys]
            else:
                subHam.sysLevel = [self._sysLevel[qIndex] for qIndex in onSubSys]
                subHam.job.sysLevel = [self._sysLevel[qIndex] for qIndex in onSubSys]

        # Set the mapping relationship
        subHam.subSystemIndexMapping = copy.deepcopy(indexMapping)

        def mapping(index: Union[int, List[int]]) -> Union[int, List[int]]:
            """
            Map the `onQubits` index(es) from the original Ham to the extracted Ham.

            :param index: 'onQubits' index(es) from the original Ham
            :return: mapped index(es) onto the extracted Ham
            """
            if isinstance(index, int):
                return indexMapping[index]
            else:
                mappedList = []
                for item in index:
                    mappedList.append(indexMapping[item])
                return mappedList

        def allIn(listA: Union[int, List[int]], listB: Union[int, List[int]]) -> bool:
            """
            Check whether all the items in listB are in listA.

            :param listA: listA
            :param listB: listB
            :return: a bool value
            """
            if isinstance(listA, int):
                listA = [listA]
            if isinstance(listB, int):
                return listB in listA
            else:
                for item in listB:
                    if item not in listA:
                        return False
                return True

        # Trim the drift terms
        del subHam.driftOperators
        for key in self._driftOperators:
            drifts = self._driftOperators[key]  # type: Union[QOperator, List[QOperator]]
            if isinstance(drifts, list):
                _coef = drifts[0].coef
                originalOnSubSys = [op.onSubSys for op in drifts]
            else:
                _coef = drifts.coef
                originalOnSubSys = copy.deepcopy(drifts.onSubSys)
            if allIn(onSubSys, originalOnSubSys):
                subHam.addDrift(copy.deepcopy(drifts), mapping(originalOnSubSys), coef=_coef)

        # Trim the control terms
        subHam.job.clearWaves()
        for key in self.job.ctrlOperators:
            ctrls = self.job.ctrlOperators[key]  # type: Union[QOperator, List[QOperator]]
            if isinstance(ctrls, list):
                originalOnSubSys = [op.onSubSys for op in ctrls]
            else:
                originalOnSubSys = ctrls.onSubSys
            if allIn(onSubSys, originalOnSubSys):
                wavesDict = self.job.searchWave(ctrls, originalOnSubSys)
                for waveKey in wavesDict.keys():
                    # Update the waveform setup
                    subHam.job.addWave(ctrls, mapping(originalOnSubSys), waves=wavesDict[waveKey])

        # Trim the DoNotClear terms
        subHam.doNotClearFlagWaves.clear()
        subHam.doNotClearFlagOperators.clear()

        for key in self.doNotClearFlagWaves:
            doNotClrCtrl = self.doNotClearFlagOperators[key]  # type: Union[QOperator, List[QOperator]]
            if isinstance(doNotClrCtrl, list):
                originalOnSubSys = [op.onSubSys for op in doNotClrCtrl]
            else:
                originalOnSubSys = doNotClrCtrl.onSubSys
            if allIn(onSubSys, originalOnSubSys):
                wavesDict = QJob.searchWaveTool(self.doNotClearFlagWaves, self.subSysNum, self.sysLevel,
                                                doNotClrCtrl, originalOnSubSys)
                for waveKey in wavesDict.keys():
                    # Update the waveform setup
                    subHam.addWave(doNotClrCtrl, mapping(originalOnSubSys), waves=wavesDict[waveKey])

        # Trim the collapse terms
        cList = copy.deepcopy(self._collapseList)
        subCList = None
        if cList is not None:
            subCList = {}
            for originalId, newId in indexMapping.items():
                if originalId not in cList.keys():
                    pass
                else:
                    subCList[newId] = cList[originalId]

        # Update the basic information
        subHam._collapseList = subCList
        subHam.subSysNum = subQubits
        subHam.job.subSysNum = subQubits

        return subHam

        This method extracts a subsystem from the target Hamiltonian.

        :param onSubSys: a list of qubit indexes
        :return: subsystem's QHamiltonian object
        
This method extracts a subsystem from the target Hamiltonian.
This method extracts a subsystem from the target Hamiltonian .
def subSystem(self, onSubSys: Union[int, List[int]]) -> 'QHamiltonian':
        subHam = copy.deepcopy(self)
        subHam.clearCache()
        if isinstance(onSubSys, int):
            subQubits = 1
            indexMapping = {onSubSys: 0}
            if onSubSys >= self._subSysNum:
                raise Error.ArgumentError("onSubSys should be less than the subSysNum of this QHamiltonian.")
        elif isinstance(onSubSys, list):
            subQubits = len(onSubSys)
            indexMapping = {}
            for newId, originalId in enumerate(onSubSys):
                indexMapping[originalId] = newId
                if originalId >= self._subSysNum:
                    raise Error.ArgumentError("all onSubSys items should be less than the subSysNum "
                                              "of this QHamiltonian.")
        else:
            raise Error.ArgumentError("Unsupported input of qubitNum, it should be an int value or a list.")
        if isinstance(self._sysLevel, list):
            if isinstance(onSubSys, int):
                subHam.sysLevel = self._sysLevel[onSubSys]
                subHam.job.sysLevel = self._sysLevel[onSubSys]
            else:
                subHam.sysLevel = [self._sysLevel[qIndex] for qIndex in onSubSys]
                subHam.job.sysLevel = [self._sysLevel[qIndex] for qIndex in onSubSys]
        subHam.subSystemIndexMapping = copy.deepcopy(indexMapping)
        def mapping(index: Union[int, List[int]]) -> Union[int, List[int]]:
            if isinstance(index, int):
                return indexMapping[index]
            else:
                mappedList = []
                for item in index:
                    mappedList.append(indexMapping[item])
                return mappedList
        def allIn(listA: Union[int, List[int]], listB: Union[int, List[int]]) -> bool:
            if isinstance(listA, int):
                listA = [listA]
            if isinstance(listB, int):
                return listB in listA
            else:
                for item in listB:
                    if item not in listA:
                        return False
                return True
        del subHam.driftOperators
        for key in self._driftOperators:
            drifts = self._driftOperators[key]  
            if isinstance(drifts, list):
                _coef = drifts[0].coef
                originalOnSubSys = [op.onSubSys for op in drifts]
            else:
                _coef = drifts.coef
                originalOnSubSys = copy.deepcopy(drifts.onSubSys)
            if allIn(onSubSys, originalOnSubSys):
                subHam.addDrift(copy.deepcopy(drifts), mapping(originalOnSubSys), coef=_coef)
        subHam.job.clearWaves()
        for key in self.job.ctrlOperators:
            ctrls = self.job.ctrlOperators[key]  
            if isinstance(ctrls, list):
                originalOnSubSys = [op.onSubSys for op in ctrls]
            else:
                originalOnSubSys = ctrls.onSubSys
            if allIn(onSubSys, originalOnSubSys):
                wavesDict = self.job.searchWave(ctrls, originalOnSubSys)
                for waveKey in wavesDict.keys():
                    subHam.job.addWave(ctrls, mapping(originalOnSubSys), waves=wavesDict[waveKey])
        subHam.doNotClearFlagWaves.clear()
        subHam.doNotClearFlagOperators.clear()
        for key in self.doNotClearFlagWaves:
            doNotClrCtrl = self.doNotClearFlagOperators[key]  
            if isinstance(doNotClrCtrl, list):
                originalOnSubSys = [op.onSubSys for op in doNotClrCtrl]
            else:
                originalOnSubSys = doNotClrCtrl.onSubSys
            if allIn(onSubSys, originalOnSubSys):
                wavesDict = QJob.searchWaveTool(self.doNotClearFlagWaves, self.subSysNum, self.sysLevel,
                                                doNotClrCtrl, originalOnSubSys)
                for waveKey in wavesDict.keys():
                    subHam.addWave(doNotClrCtrl, mapping(originalOnSubSys), waves=wavesDict[waveKey])
        cList = copy.deepcopy(self._collapseList)
        subCList = None
        if cList is not None:
            subCList = {}
            for originalId, newId in indexMapping.items():
                if originalId not in cList.keys():
                    pass
                else:
                    subCList[newId] = cList[originalId]
        subHam._collapseList = subCList
        subHam.subSysNum = subQubits
        subHam.job.subSysNum = subQubits
        return subHam
def subSystem ( self , onSubSys : Union [ int , List [ int ] ] ) -> 'QHamiltonian' : subHam = copy . deepcopy ( self ) subHam . clearCache ( ) if isinstance ( onSubSys , int ) : subQubits = 1 indexMapping = { onSubSys : 0 } if onSubSys >= self . _subSysNum : raise Error . ArgumentError ( "onSubSys should be less than the subSysNum of this QHamiltonian." ) elif isinstance ( onSubSys , list ) : subQubits = len ( onSubSys ) indexMapping = { } for newId , originalId in enumerate ( onSubSys ) : indexMapping [ originalId ] = newId if originalId >= self . _subSysNum : raise Error . ArgumentError ( "all onSubSys items should be less than the subSysNum " "of this QHamiltonian." ) else : raise Error . ArgumentError ( "Unsupported input of qubitNum, it should be an int value or a list." ) if isinstance ( self . _sysLevel , list ) : if isinstance ( onSubSys , int ) : subHam . sysLevel = self . _sysLevel [ onSubSys ] subHam . job . sysLevel = self . _sysLevel [ onSubSys ] else : subHam . sysLevel = [ self . _sysLevel [ qIndex ] for qIndex in onSubSys ] subHam . job . sysLevel = [ self . _sysLevel [ qIndex ] for qIndex in onSubSys ] subHam . subSystemIndexMapping = copy . deepcopy ( indexMapping ) def mapping ( index : Union [ int , List [ int ] ] ) -> Union [ int , List [ int ] ] : """
            Map the `onQubits` index(es) from the original Ham to the extracted Ham.

            :param index: 'onQubits' index(es) from the original Ham
            :return: mapped index(es) onto the extracted Ham
            """ if isinstance ( index , int ) : return indexMapping [ index ] else : mappedList = [ ] for item in index : mappedList . append ( indexMapping [ item ] ) return mappedList def allIn ( listA : Union [ int , List [ int ] ] , listB : Union [ int , List [ int ] ] ) -> bool : """
            Check whether all the items in listB are in listA.

            :param listA: listA
            :param listB: listB
            :return: a bool value
            """ if isinstance ( listA , int ) : listA = [ listA ] if isinstance ( listB , int ) : return listB in listA else : for item in listB : if item not in listA : return False return True del subHam . driftOperators for key in self . _driftOperators : drifts = self . _driftOperators [ key ] if isinstance ( drifts , list ) : _coef = drifts [ 0 ] . coef originalOnSubSys = [ op . onSubSys for op in drifts ] else : _coef = drifts . coef originalOnSubSys = copy . deepcopy ( drifts . onSubSys ) if allIn ( onSubSys , originalOnSubSys ) : subHam . addDrift ( copy . deepcopy ( drifts ) , mapping ( originalOnSubSys ) , coef = _coef ) subHam . job . clearWaves ( ) for key in self . job . ctrlOperators : ctrls = self . job . ctrlOperators [ key ] if isinstance ( ctrls , list ) : originalOnSubSys = [ op . onSubSys for op in ctrls ] else : originalOnSubSys = ctrls . onSubSys if allIn ( onSubSys , originalOnSubSys ) : wavesDict = self . job . searchWave ( ctrls , originalOnSubSys ) for waveKey in wavesDict . keys ( ) : subHam . job . addWave ( ctrls , mapping ( originalOnSubSys ) , waves = wavesDict [ waveKey ] ) subHam . doNotClearFlagWaves . clear ( ) subHam . doNotClearFlagOperators . clear ( ) for key in self . doNotClearFlagWaves : doNotClrCtrl = self . doNotClearFlagOperators [ key ] if isinstance ( doNotClrCtrl , list ) : originalOnSubSys = [ op . onSubSys for op in doNotClrCtrl ] else : originalOnSubSys = doNotClrCtrl . onSubSys if allIn ( onSubSys , originalOnSubSys ) : wavesDict = QJob . searchWaveTool ( self . doNotClearFlagWaves , self . subSysNum , self . sysLevel , doNotClrCtrl , originalOnSubSys ) for waveKey in wavesDict . keys ( ) : subHam . addWave ( doNotClrCtrl , mapping ( originalOnSubSys ) , waves = wavesDict [ waveKey ] ) cList = copy . deepcopy ( self . _collapseList ) subCList = None if cList is not None : subCList = { } for originalId , newId in indexMapping . items ( ) : if originalId not in cList . keys ( ) : pass else : subCList [ newId ] = cList [ originalId ] subHam . _collapseList = subCList subHam . subSysNum = subQubits subHam . job . subSysNum = subQubits return subHam
This method extracts a subsystem from the target Hamiltonian.
This method extracts a subsystem from the target Hamiltonian .
"""
        This method extracts a subsystem from the target Hamiltonian.

        :param onSubSys: a list of qubit indexes
        :return: subsystem's QHamiltonian object
        """ # Information about the sub system # qubit index mapping # Update the sysLevel property # Set the mapping relationship """
            Map the `onQubits` index(es) from the original Ham to the extracted Ham.

            :param index: 'onQubits' index(es) from the original Ham
            :return: mapped index(es) onto the extracted Ham
            """ """
            Check whether all the items in listB are in listA.

            :param listA: listA
            :param listB: listB
            :return: a bool value
            """ # Trim the drift terms # type: Union[QOperator, List[QOperator]] # Trim the control terms # type: Union[QOperator, List[QOperator]] # Update the waveform setup # Trim the DoNotClear terms # type: Union[QOperator, List[QOperator]] # Update the waveform setup # Trim the collapse terms # Update the basic information
subsystem's QHamiltonian object
subsystem ' s QHamiltonian object
self
onSubSys
Union[int, List[int]]
a list of qubit indexes
a list of qubit indexes