Ejemplo n.º 1
0
    def __init__(self, gate, num_controls, num_ancillas_zero,
                 num_ancillas_dirty):
        """Initialize a multi controlled gate.

        Args:
            gate (ndarray): 2*2 unitary (given as a (complex) ndarray)
            num_controls (int): number of control qubits
            num_ancillas_zero (int): number of ancilla qubits that start in the state zero
            num_ancillas_dirty (int): number of ancilla qubits that are allowed to start in an
                arbitrary state
        Raises:
            QiskitError: if the input format is wrong; if the array gate is not unitary
        """

        self.num_controls = num_controls
        self.num_ancillas_zero = num_ancillas_zero
        self.num_ancillas_dirty = num_ancillas_dirty
        # Check if the gate has the right dimension
        if not gate.shape == (2, 2):
            raise QiskitError(
                "The dimension of the controlled gate is not equal to (2,2).")
        # Check if the single-qubit gate is unitary
        if not is_isometry(gate, _EPS):
            raise QiskitError("The controlled gate is not unitary.")
        # Create new gate.
        num_qubits = 1 + num_controls + num_ancillas_zero + num_ancillas_dirty
        super().__init__("MCGupDiag", num_qubits, [gate])
Ejemplo n.º 2
0
    def __init__(self, isometry, num_ancillas_zero, num_ancillas_dirty):
        # Convert to numpy array in case not already an array
        isometry = np.array(isometry, dtype=complex)

        # change a row vector to a column vector (in the case of state preparation)
        if len(isometry.shape) == 1:
            isometry = isometry.reshape(isometry.shape[0], 1)

        self.num_ancillas_zero = num_ancillas_zero
        self.num_ancillas_dirty = num_ancillas_dirty

        # Check if the isometry has the right dimension and if the columns are orthonormal
        n = np.log2(isometry.shape[0])
        m = np.log2(isometry.shape[1])
        if not n.is_integer() or n < 0:
            raise QiskitError(
                "The number of rows of the isometry is not a non negative"
                " power of 2.")
        if not m.is_integer() or m < 0:
            raise QiskitError(
                "The number of columns of the isometry is not a non negative"
                " power of 2.")
        if m > n:
            raise QiskitError(
                "The input matrix has more columns than rows and hence "
                "it can't be an isometry.")
        if not is_isometry(isometry, _EPS):
            raise QiskitError(
                "The input matrix has non orthonormal columns and hence "
                "it is not an isometry.")

        num_qubits = int(n) + num_ancillas_zero + num_ancillas_dirty

        super().__init__("isometry", num_qubits, 0, [isometry])