コード例 #1
0
ファイル: test_ghz.py プロジェクト: NunoEdgarGFlowHub/toqito
    def test_ghz_2_3(self):
        """Produces the 3-qubit GHZ state: `1/sqrt(2) * (|000> + |111>)`."""
        e_0, e_1 = ket(2, 0), ket(2, 1)
        expected_res = (
            1 / np.sqrt(2) *
            (tensor_list([e_0, e_0, e_0]) + tensor_list([e_1, e_1, e_1])))

        res = ghz(2, 3).toarray()

        bool_mat = np.isclose(res, expected_res)
        self.assertEqual(np.all(bool_mat), True)
コード例 #2
0
    def test_w_state_3(self):
        """The 3-qubit W-state."""
        e_0, e_1 = ket(2, 0), ket(2, 1)
        expected_res = (
            1
            / np.sqrt(3)
            * (
                tensor_list([e_1, e_0, e_0])
                + tensor_list([e_0, e_1, e_0])
                + tensor_list([e_0, e_0, e_1])
            )
        )

        res = w_state(3)

        bool_mat = np.isclose(res, expected_res, atol=0.2)
        self.assertEqual(np.all(bool_mat), True)
コード例 #3
0
    def test_tensor_list_3(self):
        """Test tensor list with three items."""
        e_0, e_1 = ket(2, 0), ket(2, 1)
        expected_res = np.kron(np.kron(e_0, e_1), e_0)

        res = tensor_list([e_0, e_1, e_0])

        bool_mat = np.isclose(res, expected_res)
        self.assertEqual(np.all(bool_mat), True)
コード例 #4
0
    def test_tensor_list_1(self):
        """Test tensor list with one item."""
        e_0 = ket(2, 0)
        expected_res = e_0

        res = tensor_list([e_0])

        bool_mat = np.isclose(res, expected_res)
        self.assertEqual(np.all(bool_mat), True)
コード例 #5
0
    def test_generalized_w_state(self):
        """Generalized 4-qubit W-state."""
        e_0, e_1 = ket(2, 0), ket(2, 1)
        expected_res = (
            1
            / np.sqrt(30)
            * (
                tensor_list([e_1, e_0, e_0, e_0])
                + 2 * tensor_list([e_0, e_1, e_0, e_0])
                + 3 * tensor_list([e_0, e_0, e_1, e_0])
                + 4 * tensor_list([e_0, e_0, e_0, e_1])
            )
        )

        coeffs = np.array([1, 2, 3, 4]) / np.sqrt(30)
        res = w_state(4, coeffs)

        bool_mat = np.isclose(res, expected_res, atol=0.2)
        self.assertEqual(np.all(bool_mat), True)
コード例 #6
0
ファイル: test_ghz.py プロジェクト: NunoEdgarGFlowHub/toqito
    def test_ghz_4_7(self):
        r"""
        The following generates the following GHZ state in `(C^4)^{\otimes 7}`.

        `1/sqrt(30) * (|0000000> + 2|1111111> + 3|2222222> + 4|3333333>)`.
        """
        e0_4 = np.array([[1], [0], [0], [0]])
        e1_4 = np.array([[0], [1], [0], [0]])
        e2_4 = np.array([[0], [0], [1], [0]])
        e3_4 = np.array([[0], [0], [0], [1]])

        expected_res = (
            1 / np.sqrt(30) *
            (tensor_list([e0_4, e0_4, e0_4, e0_4, e0_4, e0_4, e0_4]) +
             2 * tensor_list([e1_4, e1_4, e1_4, e1_4, e1_4, e1_4, e1_4]) +
             3 * tensor_list([e2_4, e2_4, e2_4, e2_4, e2_4, e2_4, e2_4]) +
             4 * tensor_list([e3_4, e3_4, e3_4, e3_4, e3_4, e3_4, e3_4])))

        res = ghz(4, 7, np.array([1, 2, 3, 4]) / np.sqrt(30)).toarray()

        bool_mat = np.isclose(res, expected_res)
        self.assertEqual(np.all(bool_mat), True)
コード例 #7
0
    def test_tensor_list_0(self):
        """Test tensor empty list."""
        expected_res = None

        res = tensor_list([])
        self.assertEqual(res, expected_res)
コード例 #8
0
def pauli(ind: Union[int, str, List[int], List[str]],
          is_sparse: bool = False) -> Union[np.ndarray, sparse.csr_matrix]:
    r"""
    Produce a Pauli operator.

    Provides the 2-by-2 Pauli matrix indicated by the value of `ind`. The
    variable `ind = 1` gives the Pauli-X operator, `ind = 2` gives the Pauli-Y
    operator, `ind =3` gives the Pauli-Z operator, and `ind = 0`gives the
    identity operator. Alternatively, `ind` can be set to "I", "X", "Y", or "Z"
    (case insensitive) to indicate the Pauli identity, X, Y, or Z operator.

    The 2-by-2 Pauli matrices are defined as the following matrices:

    .. math::

        \begin{equation}
            \begin{aligned}
                X = \begin{pmatrix}
                        0 & 1 \\
                        1 & 0
                    \end{pmatrix}, \quad
                Y = \begin{pmatrix}
                        0 & -i \\
                        i & 0
                    \end{pmatrix}, \quad
                Z = \begin{pmatrix}
                        1 & 0 \\
                        0 & -1
                    \end{pmatrix}, \quad
                I = \begin{pmatrix}
                        1 & 0 \\
                        0 & 1
                    \end{pmatrix}.
                \end{aligned}
            \end{equation}

    References:
        [1] Wikipedia: Pauli matrices
        https://en.wikipedia.org/wiki/Pauli_matrices

    :param ind: The index to indicate which Pauli operator to generate.
    :param is_sparse: Returns a sparse matrix if set to True and a non-sparse
                      matrix if set to False.
    """
    if isinstance(ind, (int, str)):
        if ind in ("x", "X", 1):
            pauli_mat = np.array([[0, 1], [1, 0]])
        elif ind in ("y", "Y", 2):
            pauli_mat = np.array([[0, -1j], [1j, 0]])
        elif ind in ("z", "Z", 3):
            pauli_mat = np.array([[1, 0], [0, -1]])
        else:
            pauli_mat = np.identity(2)

        if is_sparse:
            pauli_mat = sparse.csr_matrix(pauli_mat)

        return pauli_mat

    num_qubits = len(ind)
    pauli_mats = []
    for i in range(num_qubits - 1, -1, -1):
        pauli_mats.append(pauli(ind[i], is_sparse))
    return tensor_list(pauli_mats)