qiskit/quantum_info/operators/operator.py
def _append_instruction(self, obj, qargs=None):
        """Update the current Operator by apply an instruction."""
        if isinstance(obj, Instruction):
            mat = None
            if 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:
                    mat = obj.to_matrix()
                except QiskitError:
                    pass
            if mat is not None:
                # Perform the composition and inplace update the current state
                # of the operator
                op = self.compose(mat, 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):
            mat = None
            if 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:
                    mat = obj.to_matrix()
                except QiskitError:
                    pass
            if mat is not None:
                # Perform the composition and inplace update the current state
                # of the operator
                op = self.compose(mat, 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 ) : mat = None if 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 : mat = obj . to_matrix ( ) except QiskitError : pass if mat is not None : # Perform the composition and inplace update the current state # of the operator op = self . compose ( mat , 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 .