(self, qubits, **options)
return result
Apply this gate to a Qubit.
Apply this gate to a Qubit.
Apply this gate to a Qubit .
def _apply_operator_Qubit(self, qubits, **options):
        """Apply this gate to a Qubit."""

        # Check number of qubits this gate acts on.
        if qubits.nqubits < self.min_qubits:
            raise QuantumError(
                'Gate needs a minimum of %r qubits to act on, got: %r' %
                (self.min_qubits, qubits.nqubits)
            )

        # If the controls are not met, just return
        if isinstance(self, CGate):
            if not self.eval_controls(qubits):
                return qubits

        targets = self.targets
        target_matrix = self.get_target_matrix(format='sympy')

        # Find which column of the target matrix this applies to.
        column_index = 0
        n = 1
        for target in targets:
            column_index += n*qubits[target]
            n = n << 1
        column = target_matrix[:, int(column_index)]

        # Now apply each column element to the qubit.
        result = 0
        for index in range(column.rows):
            # TODO: This can be optimized to reduce the number of Qubit
            # creations. We should simply manipulate the raw list of qubit
            # values and then build the new Qubit object once.
            # Make a copy of the incoming qubits.
            new_qubit = qubits.__class__(*qubits.args)
            # Flip the bits that need to be flipped.
            for bit in range(len(targets)):
                if new_qubit[targets[bit]] != (index >> bit) & 1:
                    new_qubit = new_qubit.flip(targets[bit])
            # The value in that row and column times the flipped-bit qubit
            # is the result for that part.
            result += column[index]*new_qubit
        return result
def _apply_operator_Qubit ( self , qubits , * * options ) : # Check number of qubits this gate acts on. if qubits . nqubits < self . min_qubits : raise QuantumError ( 'Gate needs a minimum of %r qubits to act on, got: %r' % ( self . min_qubits , qubits . nqubits ) ) # If the controls are not met, just return if isinstance ( self , CGate ) : if not self . eval_controls ( qubits ) : return qubits targets = self . targets target_matrix = self . get_target_matrix ( format = 'sympy' ) # Find which column of the target matrix this applies to. column_index = 0 n = 1 for target in targets : column_index += n * qubits [ target ] n = n << 1 column = target_matrix [ : , int ( column_index ) ] # Now apply each column element to the qubit. result = 0 for index in range ( column . rows ) : # TODO: This can be optimized to reduce the number of Qubit # creations. We should simply manipulate the raw list of qubit # values and then build the new Qubit object once. # Make a copy of the incoming qubits. new_qubit = qubits . __class__ ( * qubits . args ) # Flip the bits that need to be flipped. for bit in range ( len ( targets ) ) : if new_qubit [ targets [ bit ] ] != ( index >> bit ) & 1 : new_qubit = new_qubit . flip ( targets [ bit ] ) # The value in that row and column times the flipped-bit qubit # is the result for that part. result += column [ index ] * new_qubit return result