qiskit/quantum_info/operators/channel/superop.py
def _append_instruction(self, obj, qargs=None):
        """Update the current Operator by apply an instruction."""
        if isinstance(obj, Instruction):
            chan = None
            if obj.name == 'reset':
                # For superoperator evolution we can simulate a reset as
                # a non-unitary supeorperator matrix
                chan = SuperOp(
                    np.array([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
                              [0, 0, 0, 0]]))
            if obj.name == 'kraus':
                kraus = obj.params
                dim = len(kraus[0])
                chan = SuperOp(_to_superop('Kraus', (kraus, None), dim, dim))
            elif hasattr(obj, 'to_matrix'):
                # If instruction is a gate first we see if it has a
                # `to_matrix` definition and if so use that.
                try:
                    kraus = [obj.to_matrix()]
                    dim = len(kraus[0])
                    chan = SuperOp(
                        _to_superop('Kraus', (kraus, None), dim, dim))
                except QiskitError:
                    pass
            if chan is not None:
                # Perform the composition and inplace update the current state
                # of the operator
                op = self.compose(chan, qargs=qargs)
                self._data = op.data
            else:
                # If the instruction doesn't have a matrix defined we use its
                # circuit decomposition definition if it exists, otherwise we
                # cannot compose this gate and raise an error.
                if obj.definition is None:
                    raise QiskitError('Cannot apply Instruction: {}'.format(
                        obj.name))
                for instr, qregs, cregs in obj.definition:
                    if cregs:
                        raise QiskitError(
                            'Cannot apply instruction with classical registers: {}'
                            .format(instr.name))
                    # Get the integer position of the flat register
                    new_qargs = [tup[1] for tup in qregs]
                    self._append_instruction(instr, qargs=new_qargs)
        else:
            raise QiskitError('Input is not an instruction.')
def _append_instruction(self, obj, qargs=None):
        """Update the current Operator by apply an instruction."""
        if isinstance(obj, Instruction):
            chan = None
            if obj.name == 'reset':
                # For superoperator evolution we can simulate a reset as
                # a non-unitary supeorperator matrix
                chan = SuperOp(
                    np.array([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
                              [0, 0, 0, 0]]))
            if obj.name == 'kraus':
                kraus = obj.params
                dim = len(kraus[0])
                chan = SuperOp(_to_superop('Kraus', (kraus, None), dim, dim))
            elif hasattr(obj, 'to_matrix'):
                # If instruction is a gate first we see if it has a
                # `to_matrix` definition and if so use that.
                try:
                    kraus = [obj.to_matrix()]
                    dim = len(kraus[0])
                    chan = SuperOp(
                        _to_superop('Kraus', (kraus, None), dim, dim))
                except QiskitError:
                    pass
            if chan is not None:
                # Perform the composition and inplace update the current state
                # of the operator
                op = self.compose(chan, qargs=qargs)
                self._data = op.data
            else:
                # If the instruction doesn't have a matrix defined we use its
                # circuit decomposition definition if it exists, otherwise we
                # cannot compose this gate and raise an error.
                if obj.definition is None:
                    raise QiskitError('Cannot apply Instruction: {}'.format(
                        obj.name))
                for instr, qregs, cregs in obj.definition:
                    if cregs:
                        raise QiskitError(
                            'Cannot apply instruction with classical registers: {}'
                            .format(instr.name))
                    # Get the integer position of the flat register
                    new_qargs = [tup[1] for tup in qregs]
                    self._append_instruction(instr, qargs=new_qargs)
        else:
            raise QiskitError('Input is not an instruction.')
def _append_instruction ( self , obj , qargs = None ) : if isinstance ( obj , Instruction ) : chan = None if obj . name == 'reset' : # For superoperator evolution we can simulate a reset as # a non-unitary supeorperator matrix chan = SuperOp ( np . array ( [ [ 1 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] ) ) if obj . name == 'kraus' : kraus = obj . params dim = len ( kraus [ 0 ] ) chan = SuperOp ( _to_superop ( 'Kraus' , ( kraus , None ) , dim , dim ) ) elif hasattr ( obj , 'to_matrix' ) : # If instruction is a gate first we see if it has a # `to_matrix` definition and if so use that. try : kraus = [ obj . to_matrix ( ) ] dim = len ( kraus [ 0 ] ) chan = SuperOp ( _to_superop ( 'Kraus' , ( kraus , None ) , dim , dim ) ) except QiskitError : pass if chan is not None : # Perform the composition and inplace update the current state # of the operator op = self . compose ( chan , qargs = qargs ) self . _data = op . data else : # If the instruction doesn't have a matrix defined we use its # circuit decomposition definition if it exists, otherwise we # cannot compose this gate and raise an error. if obj . definition is None : raise QiskitError ( 'Cannot apply Instruction: {}' . format ( obj . name ) ) for instr , qregs , cregs in obj . definition : if cregs : raise QiskitError ( 'Cannot apply instruction with classical registers: {}' . format ( instr . name ) ) # Get the integer position of the flat register new_qargs = [ tup [ 1 ] for tup in qregs ] self . _append_instruction ( instr , qargs = new_qargs ) else : raise QiskitError ( 'Input is not an instruction.' )
Update the current Operator by apply an instruction.
Update the current Operator by apply an instruction .