Exemplo n.º 1
0
    def test_qwc_complement_adj_matrix_exception(self):
        """Tests that the ``qwc_complement_adj_matrix`` function raises an exception if
        the matrix is not binary."""
        not_binary_observables = np.array([[1.1, 0.5, 1., 0., 0., 1.],
                                           [0., 1.3, 1., 1., 0., 1.],
                                           [2.2, 0., 0., 1., 0., 0.]])

        with pytest.raises(ValueError,
                           match="Expected a binary array, instead got"):
            qwc_complement_adj_matrix(not_binary_observables)
Exemplo n.º 2
0
    def test_qwc_complement_adj_matrix(self):
        """Tests that the ``qwc_complement_adj_matrix`` function returns the correct
        adjacency matrix."""
        binary_observables = np.array([[1., 0., 1., 0., 0., 1.],
                                       [0., 1., 1., 1., 0., 1.],
                                       [0., 0., 0., 1., 0., 0.]])
        adj = qwc_complement_adj_matrix(binary_observables)

        expected = np.array([[0., 1., 1.], [1., 0., 0.], [1., 0., 0.]])

        assert np.all(adj == expected)

        binary_obs_list = list(binary_observables)
        adj = qwc_complement_adj_matrix(binary_obs_list)
        assert np.all(adj == expected)

        binary_obs_tuple = tuple(binary_observables)
        adj = qwc_complement_adj_matrix(binary_obs_tuple)
        assert np.all(adj == expected)
Exemplo n.º 3
0
    def complement_adj_matrix_for_operator(self):
        """Constructs the adjacency matrix for the complement of the Pauli graph.

        The adjacency matrix for an undirected graph of N vertices is an N by N symmetric binary
        matrix, where matrix elements of 1 denote an edge, and matrix elements of 0 denote no edge.

        Returns:
            array[int]: the square and symmetric adjacency matrix
        """

        if self.binary_observables is None:
            self.binary_observables = self.binary_repr()

        n_qubits = int(np.shape(self.binary_observables)[1] / 2)

        if self.grouping_type == "qwc":
            adj = qwc_complement_adj_matrix(self.binary_observables)

        elif self.grouping_type in frozenset(["commuting", "anticommuting"]):
            symplectic_form = np.block(
                [
                    [np.zeros((n_qubits, n_qubits)), np.eye(n_qubits)],
                    [np.eye(n_qubits), np.zeros((n_qubits, n_qubits))],
                ]
            )
            mat_prod = (
                self.binary_observables @ symplectic_form @ np.transpose(self.binary_observables)
            )

            if self.grouping_type == "commuting":

                adj = mat_prod % 2

            elif self.grouping_type == "anticommuting":

                adj = (mat_prod + 1) % 2
                np.fill_diagonal(adj, 0)

        return adj