コード例 #1
0
    def test___ne__(self):
        aux_g1a_eq = QuantumGate(self.matrix2x2)
        aux_g1a_ne = QuantumGate(np.matrix([[0, 1], [1, 0]],
                                           dtype=np.complex_))

        self.assertFalse(self.g1a != aux_g1a_eq)
        self.assertTrue(self.g1a != aux_g1a_ne)
コード例 #2
0
    def setUp(self):
        s = Sequence(QuantumGate(np.matrix(np.identity(2), dtype=np.complex_)))

        self.parent = Member(0, QuantumState(1))
        self.child = Member(1,
                            QuantumState(1),
                            parent=0,
                            gate='G',
                            sequence=s,
                            complexity=1)
コード例 #3
0
class EnumGates(Enum):
    def __new__(cls, *args, **kwds):
        value = len(cls.__members__) + 1
        obj = object.__new__(cls)
        obj._value_ = value
        return obj

    def __init__(self, gate):
        self.gate = gate

    """ -------------------------------------------------------------------------------------------------------------
        Implements the Hadamard Quantum Gate.
        This gate must be applied without controls, so the sequence will only be taken into
        consideration to locate the affected qubit, ignoring the state of the other qubits.
        Otherwise, resulting qubits may escape the model.

        Source: https://en.wikipedia.org/wiki/Quantum_gate#Hadamard_gate
    """
    H = QuantumGate(np.matrix([[1, 1], [1, -1]], dtype=np.complex), 'Hadamard')
    # ---------------------------------------------------------------------------------------------------------------
    """ -------------------------------------------------------------------------------------------------------------
    Implements the V Quantum Gate.
    """
    V = QuantumGate(np.matrix([[1, 0], [0, 1j]], dtype=np.complex), 'V')
    # ---------------------------------------------------------------------------------------------------------------
    """ -------------------------------------------------------------------------------------------------------------
        Implements the Pauli-Z Quantum Gate. It equates to a rotation around the Z-axis of the Bloch
        sphere by π radians. Z equals V^2, as it negates the real part of the last quantum state.
    """
    Z = QuantumGate(np.matrix([[1, 0], [0, -1]], dtype=np.complex), 'Z')
    # ---------------------------------------------------------------------------------------------------------------
    """ -------------------------------------------------------------------------------------------------------------
        Implements the Pauli-X Quantum Gate. It equates to a rotation of the Bloch sphere around the X-axis by π
        radians. It maps |0> to |1> and |1> to |0>. Due to this nature, it is sometimes called bit-flip.

        Source: https://en.wikipedia.org/wiki/Quantum_gate#Pauli-X_gate_(=_NOT_gate)
    """
    X = QuantumGate(np.matrix([[0, 1], [1, 0]], dtype=np.complex), 'X')
    # ---------------------------------------------------------------------------------------------------------------
    """ -------------------------------------------------------------------------------------------------------------
        Implements a modified version of the Hadamard Quantum Gate.
    """
    H_sym = QuantumGate(np.matrix([[-1, 1], [1, 1]], dtype=np.complex),
                        'H_sym')
    # ---------------------------------------------------------------------------------------------------------------
    """ -------------------------------------------------------------------------------------------------------------
        Implements the V Quantum Gate, altered to modify the state |0> instead of state |1>.
    """
    V_sym = QuantumGate(np.matrix([[1j, 0], [0, 1]], dtype=np.complex),
                        'V_sym')
    # ---------------------------------------------------------------------------------------------------------------
    """ -------------------------------------------------------------------------------------------------------------
    Implements a modified version of the Pauli-Z Quantum Gate, that affects state |0> instead of |1>.
    """
    Z_sym = QuantumGate(np.matrix([[-1, 0], [0, 1]], dtype=np.complex),
                        'Z_sym')
コード例 #4
0
    def apply_gate(self, sequence):
        """
        Applies the specified operation to the n-qubit, if possible.

        :param sequence : Configuration of the operation that is to be applied.
        """

        if not isinstance(sequence, Sequence):
            raise TypeError('The parameter must be a Sequence instance.')
        elif sequence.length != self.length:
            raise ValueError(
                'The length of the sequence does not match the number of qubits given.'
            )
        else:
            base_matrix = sequence.get_gate().matrix

            if not QuantumGate.can_use_controls(base_matrix):
                all_sequences = sequence.alter_controls()
            else:
                all_sequences = [sequence]

            new_vector = self.vector.copy()

            # Apply gate.
            for s in all_sequences:
                targets = s.get_decimal_states()

                for i in range(2):
                    new_vector[0, targets[i]] = base_matrix[i, 0] * self.vector[0, targets[0]]\
                                                + base_matrix[i, 1] * self.vector[0, targets[1]]
            self.vector = new_vector

            # Try to divide all coefficients by two.
            self._simplify()

            # Update normalization factor.
            sum_squares = 0
            for i in range(int(pow(2, self.length))):
                sum_squares += self.vector[0, i].real**2
                sum_squares += self.vector[0, i].imag**2
            self.level = int(log(sum_squares, 2))
コード例 #5
0
    def setUp(self):
        self.matrix1x1 = np.matrix(1, dtype=np.complex_)
        self.matrix2x2 = np.matrix([[1, 1], [1, 1]], dtype=np.complex)
        self.matrix2x3 = np.matrix([[1, 1, 1], [1, 1, 1]], dtype=np.complex_)
        self.matrix3x2 = np.matrix([[1, 1], [1, 1], [1, 1]], dtype=np.complex_)
        self.matrix3x3 = np.matrix([[1, 1, 1], [1, 1, 1], [1, 1, 1]],
                                   dtype=np.complex_)
        self.matrix4x4 = np.matrix(
            [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]],
            dtype=np.complex_)
        self.matrix5x5 = np.matrix(
            [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1],
             [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]],
            dtype=np.complex_)
        self.matrix6x6 = np.matrix(
            [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1],
             [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]],
            dtype=np.complex_)
        self.matrix8x8 = np.matrix(
            [[1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1],
             [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1],
             [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1],
             [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1]],
            dtype=np.complex_)

        self.g1a = QuantumGate(self.matrix2x2)
        self.g2a = QuantumGate(self.matrix2x2, 'Alpha')
        self.g3a = QuantumGate(self.matrix2x2, 'Beta')
        self.g4a = QuantumGate(self.matrix2x2, 'Gamma')
        self.g5a = QuantumGate(self.matrix2x2, 'Delta')
        self.g6a = QuantumGate(self.matrix2x2, 'Eta')
        self.g7a = QuantumGate(self.matrix2x2, 'Theta')
        self.g1b = QuantumGate(self.matrix4x4, )
        self.g2b = QuantumGate(self.matrix4x4, 'Alpha')
        self.g3b = QuantumGate(self.matrix4x4, 'Beta')
        self.g4b = QuantumGate(self.matrix4x4, 'Gamma')
        self.g5b = QuantumGate(self.matrix4x4, 'Delta')
        self.g6b = QuantumGate(self.matrix4x4, 'Eta')
        self.g7b = QuantumGate(self.matrix4x4, 'Theta')
        self.g1c = QuantumGate(self.matrix8x8)
        self.g2c = QuantumGate(self.matrix8x8, 'Alpha')
        self.g3c = QuantumGate(self.matrix8x8, 'Beta')
        self.g4c = QuantumGate(self.matrix8x8, 'Gamma')
        self.g5c = QuantumGate(self.matrix8x8, 'Delta')
        self.g6c = QuantumGate(self.matrix8x8, 'Eta')
        self.g7c = QuantumGate(self.matrix8x8, 'Theta')

        self.gates1 = [
            self.g1a, self.g2a, self.g3a, self.g4a, self.g5a, self.g6a,
            self.g7a
        ]
        self.gates2 = [
            self.g1b, self.g2b, self.g3b, self.g4b, self.g5b, self.g6b,
            self.g7b
        ]
        self.gates3 = [
            self.g1c, self.g2c, self.g3c, self.g4c, self.g5c, self.g6c,
            self.g7c
        ]
コード例 #6
0
    def test___eq__(self):
        aux_g1a = QuantumGate(self.matrix2x2)
        aux_g2a = QuantumGate(self.matrix2x2, 'Alpha')
        aux_g3a = QuantumGate(self.matrix2x2, 'Beta')
        aux_g4a = QuantumGate(self.matrix2x2, 'Gamma')
        aux_g5a = QuantumGate(self.matrix2x2, 'Delta')
        aux_g6a = QuantumGate(self.matrix2x2, 'Eta')
        aux_g7a = QuantumGate(self.matrix2x2, 'Theta')
        aux_g1b = QuantumGate(self.matrix4x4)
        aux_g2b = QuantumGate(self.matrix4x4, 'Alpha')
        aux_g3b = QuantumGate(self.matrix4x4, 'Beta')
        aux_g4b = QuantumGate(self.matrix4x4, 'Gamma')
        aux_g5b = QuantumGate(self.matrix4x4, 'Delta')
        aux_g6b = QuantumGate(self.matrix4x4, 'Eta')
        aux_g7b = QuantumGate(self.matrix4x4, 'Theta')
        aux_g1c = QuantumGate(self.matrix8x8)
        aux_g2c = QuantumGate(self.matrix8x8, 'Alpha')
        aux_g3c = QuantumGate(self.matrix8x8, 'Beta')
        aux_g4c = QuantumGate(self.matrix8x8, 'Gamma')
        aux_g5c = QuantumGate(self.matrix8x8, 'Delta')
        aux_g6c = QuantumGate(self.matrix8x8, 'Eta')
        aux_g7c = QuantumGate(self.matrix8x8, 'Theta')

        aux_gates1 = [
            aux_g1a, aux_g2a, aux_g3a, aux_g4a, aux_g5a, aux_g6a, aux_g7a
        ]
        aux_gates2 = [
            aux_g1b, aux_g2b, aux_g3b, aux_g4b, aux_g5b, aux_g6b, aux_g7b
        ]
        aux_gates3 = [
            aux_g1c, aux_g2c, aux_g3c, aux_g4c, aux_g5c, aux_g6c, aux_g7c
        ]

        # Cases that are equal.
        for i in range(len(aux_gates1)):
            self.assertTrue(aux_gates1[i] == self.gates1[i])
        for i in range(len(aux_gates2)):
            self.assertTrue(aux_gates2[i] == self.gates2[i])
        for i in range(len(aux_gates3)):
            self.assertTrue(aux_gates3[i] == self.gates3[i])

        # Cases that differ on their matrix size.
        for i in range(len(aux_gates1)):
            self.assertFalse(aux_gates1[i] == self.gates2[i])
            self.assertFalse(aux_gates1[i] == self.gates3[i])
        for i in range(len(aux_gates2)):
            self.assertFalse(aux_gates2[i] == self.gates1[i])
            self.assertFalse(aux_gates2[i] == self.gates3[i])
        for i in range(len(aux_gates3)):
            self.assertFalse(aux_gates3[i] == self.gates1[i])
            self.assertFalse(aux_gates3[i] == self.gates2[i])

        # Cases that differ on their identifier.
        for i in range(len(aux_gates1)):
            for j in range(len(aux_gates1)):
                if i != j:
                    self.assertFalse(self.gates1[i] == self.gates1[j])
                    self.assertFalse(self.gates2[i] == self.gates2[j])
                    self.assertFalse(self.gates3[i] == self.gates3[j])

        # Cases that only differ on the matrix contents.
        aux_g1a_2 = QuantumGate(np.matrix([[0, 1], [1, 0]], dtype=np.complex_))
        self.assertFalse(aux_g1a == aux_g1a_2)
コード例 #7
0
    def test_sequence(self):
        self.assertEquals(self.parent.sequence, None)

        s = Sequence(QuantumGate(np.matrix(np.identity(2), dtype=np.complex_)))
        self.assertEquals(self.child.sequence.length, s.length)
        self.assertEquals(self.child.sequence.array[0], s.array[0])