Try to rewrite expr using :class:`OperatorPlusMinusCC`

    Example:

        >>> A = OperatorSymbol('A', hs=1)
        >>> sum = A + A.dag()
        >>> sum2 = rewrite_with_operator_pm_cc(sum)
        >>> print(ascii(sum2))
        A^(1) + c.c.

def rewrite_with_operator_pm_cc(expr):
    """Try to rewrite expr using :class:`OperatorPlusMinusCC`

    Example:

        >>> A = OperatorSymbol('A', hs=1)
        >>> sum = A + A.dag()
        >>> sum2 = rewrite_with_operator_pm_cc(sum)
        >>> print(ascii(sum2))
        A^(1) + c.c.
    """
    # TODO: move this to the toolbox
    from qnet.algebra.toolbox.core import temporary_rules

    def _combine_operator_p_cc(A, B):
        if B.adjoint() == A:
            return OperatorPlusMinusCC(A, sign=+1)
        else:
            raise CannotSimplify

    def _combine_operator_m_cc(A, B):
        if B.adjoint() == A:
            return OperatorPlusMinusCC(A, sign=-1)
        else:
            raise CannotSimplify

    def _scal_combine_operator_pm_cc(c, A, d, B):
        if B.adjoint() == A:
            if c == d:
                return c * OperatorPlusMinusCC(A, sign=+1)
            elif c == -d:
                return c * OperatorPlusMinusCC(A, sign=-1)
        raise CannotSimplify

    A = wc("A", head=Operator)
    B = wc("B", head=Operator)
    c = wc("c", head=Scalar)
    d = wc("d", head=Scalar)

    with temporary_rules(OperatorPlus, clear=True):
        OperatorPlus.add_rule(
            'PM1', pattern_head(A, B), _combine_operator_p_cc)
        OperatorPlus.add_rule(
            'PM2',
            pattern_head(pattern(ScalarTimesOperator, -1, B), A),
            _combine_operator_m_cc)
        OperatorPlus.add_rule(
            'PM3',
            pattern_head(
                pattern(ScalarTimesOperator, c, A),
                pattern(ScalarTimesOperator, d, B)),
            _scal_combine_operator_pm_cc)
        return expr.rebuild()