def from_boolean_expression(
        cls, boolean_expr: sympy.Expr, qubit_map: dict[str, cirq.Qid]
    ) -> PauliSum:
        """Builds the Hamiltonian representation of a Boolean expression.

        This is based on "On the representation of Boolean and real functions as Hamiltonians for
        quantum computing" by Stuart Hadfield, https://arxiv.org/abs/1804.09130

        Args:
            boolean_expr: A Sympy expression containing symbols and Boolean operations
            qubit_map: map of string (boolean variable name) to qubit.

        Return:
            The PauliSum that represents the Boolean expression.

        Raises:
            ValueError: If `boolean_expr` is of an unsupported type.
        """
        if isinstance(boolean_expr, sympy.Symbol):
            # In table 1, the entry for 'x' is '1/2.I - 1/2.Z'
            return cls.from_pauli_strings(
                [
                    PauliString({}, 0.5),
                    PauliString({qubit_map[boolean_expr.name]: pauli_gates.Z}, -0.5),
                ]
            )

        if isinstance(boolean_expr, (And, Not, Or, Xor)):
            sub_pauli_sums = [
                cls.from_boolean_expression(sub_boolean_expr, qubit_map)
                for sub_boolean_expr in boolean_expr.args
            ]
            # We apply the equalities of theorem 1.
            if isinstance(boolean_expr, And):
                pauli_sum = cls.from_pauli_strings(PauliString({}, 1.0))
                for sub_pauli_sum in sub_pauli_sums:
                    pauli_sum = pauli_sum * sub_pauli_sum
            elif isinstance(boolean_expr, Not):
                assert len(sub_pauli_sums) == 1
                pauli_sum = cls.from_pauli_strings(PauliString({}, 1.0)) - sub_pauli_sums[0]
            elif isinstance(boolean_expr, Or):
                pauli_sum = cls.from_pauli_strings(PauliString({}, 0.0))
                for sub_pauli_sum in sub_pauli_sums:
                    pauli_sum = pauli_sum + sub_pauli_sum - pauli_sum * sub_pauli_sum
            elif isinstance(boolean_expr, Xor):
                pauli_sum = cls.from_pauli_strings(PauliString({}, 0.0))
                for sub_pauli_sum in sub_pauli_sums:
                    pauli_sum = pauli_sum + sub_pauli_sum - 2.0 * pauli_sum * sub_pauli_sum
            return pauli_sum

        raise ValueError(f'Unsupported type: {type(boolean_expr)}')
Builds the Hamiltonian representation of a Boolean expression.

        This is based on "On the representation of Boolean and real functions as Hamiltonians for
        quantum computing" by Stuart Hadfield, https://arxiv.org/abs/1804.09130

        Args:
            boolean_expr: A Sympy expression containing symbols and Boolean operations
            qubit_map: map of string (boolean variable name) to qubit.

        Return:
            The PauliSum that represents the Boolean expression.

        Raises:
            ValueError: If `boolean_expr` is of an unsupported type.
        
cirq-core/cirq/ops/linear_combinations.py