# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
ParameterExpression Class to enable creating simple expressions of Parameters.
"""
from typing import Callable, Dict, Set, Union

import numbers
import operator

import numpy

from spinqit.qiskit.circuit.exceptions import CircuitError

try:
    import symengine

    HAS_SYMENGINE = True
except ImportError:
    HAS_SYMENGINE = False


ParameterValueType = Union["ParameterExpression", float]


class ParameterExpression:
    """ParameterExpression class to enable creating expressions of Parameters."""

    __slots__ = ["_parameter_symbols", "_parameters", "_symbol_expr", "_names"]

    def __init__(self, symbol_map: Dict, expr):
        """Create a new :class:`ParameterExpression`.

        Not intended to be called directly, but to be instantiated via operations
        on other :class:`Parameter` or :class:`ParameterExpression` objects.

        Args:
            symbol_map (Dict[Parameter, [ParameterExpression, float, or int]]):
                Mapping of :class:`Parameter` instances to the :class:`sympy.Symbol`
                serving as their placeholder in expr.
            expr (sympy.Expr): Expression of :class:`sympy.Symbol` s.
        """
        self._parameter_symbols = symbol_map
        self._parameters = set(self._parameter_symbols)
        self._symbol_expr = expr
        self._names = None

    @property
    def parameters(self) -> Set:
        """Returns a set of the unbound Parameters in the expression."""
        return self._parameters

    def conjugate(self) -> "ParameterExpression":
        """Return the conjugate."""
        if HAS_SYMENGINE:
            conjugated = ParameterExpression(
                self._parameter_symbols, symengine.conjugate(self._symbol_expr)
            )
        else:
            conjugated = ParameterExpression(self._parameter_symbols, self._symbol_expr.conjugate())
        return conjugated

    def assign(self, parameter, value: ParameterValueType) -> "ParameterExpression":
        """
        Assign one parameter to a value, which can either be numeric or another parameter
        expression.

        Args:
            parameter (Parameter): A parameter in this expression whose value will be updated.
            value: The new value to bind to.

        Returns:
            A new expression parameterized by any parameters which were not bound by assignment.
        """
        if isinstance(value, ParameterExpression):
            return self.subs({parameter: value})
        return self.bind({parameter: value})

    def bind(self, parameter_values: Dict) -> "ParameterExpression":
        """Binds the provided set of parameters to their corresponding values.

        Args:
            parameter_values: Mapping of Parameter instances to the numeric value to which
                              they will be bound.

        Raises:
            CircuitError:
                - If parameter_values contains Parameters outside those in self.
                - If a non-numeric value is passed in parameter_values.
            ZeroDivisionError:
                - If binding the provided values requires division by zero.

        Returns:
            A new expression parameterized by any parameters which were not bound by
            parameter_values.
        """

        self._raise_if_passed_unknown_parameters(parameter_values.keys())
        self._raise_if_passed_nan(parameter_values)

        symbol_values = {}
        for parameter, value in parameter_values.items():
            param_expr = self._parameter_symbols[parameter]
            # TODO: Remove after symengine supports single precision floats
            # see symengine/symengine.py#351 for more details
            if isinstance(value, numpy.floating):
                symbol_values[param_expr] = float(value)
            else:
                symbol_values[param_expr] = value

        bound_symbol_expr = self._symbol_expr.subs(symbol_values)

        # Don't use sympy.free_symbols to count remaining parameters here.
        # sympy will in some cases reduce the expression and remove even
        # unbound symbols.
        # e.g. (sympy.Symbol('s') * 0).free_symbols == set()

        free_parameters = self.parameters - parameter_values.keys()
        free_parameter_symbols = {
            p: s for p, s in self._parameter_symbols.items() if p in free_parameters
        }

        if (
            hasattr(bound_symbol_expr, "is_infinite") and bound_symbol_expr.is_infinite
        ) or bound_symbol_expr == float("inf"):
            raise ZeroDivisionError(
                "Binding provided for expression "
                "results in division by zero "
                "(Expression: {}, Bindings: {}).".format(self, parameter_values)
            )

        return ParameterExpression(free_parameter_symbols, bound_symbol_expr)

    def subs(self, parameter_map: Dict) -> "ParameterExpression":
        """Returns a new Expression with replacement Parameters.

        Args:
            parameter_map: Mapping from Parameters in self to the ParameterExpression
                           instances with which they should be replaced.

        Raises:
            CircuitError:
                - If parameter_map contains Parameters outside those in self.
                - If the replacement Parameters in parameter_map would result in
                  a name conflict in the generated expression.

        Returns:
            A new expression with the specified parameters replaced.
        """

        inbound_parameters = {
            p for replacement_expr in parameter_map.values() for p in replacement_expr.parameters
        }

        self._raise_if_passed_unknown_parameters(parameter_map.keys())
        self._raise_if_parameter_names_conflict(inbound_parameters, parameter_map.keys())
        if HAS_SYMENGINE:
            new_parameter_symbols = {p: symengine.Symbol(p.name) for p in inbound_parameters}
        else:
            from sympy import Symbol

            new_parameter_symbols = {p: Symbol(p.name) for p in inbound_parameters}

        # Include existing parameters in self not set to be replaced.
        new_parameter_symbols.update(
            {p: s for p, s in self._parameter_symbols.items() if p not in parameter_map}
        )

        # If new_param is an expr, we'll need to construct a matching sympy expr
        # but with our sympy symbols instead of theirs.

        symbol_map = {
            self._parameter_symbols[old_param]: new_param._symbol_expr
            for old_param, new_param in parameter_map.items()
        }

        substituted_symbol_expr = self._symbol_expr.subs(symbol_map)

        return ParameterExpression(new_parameter_symbols, substituted_symbol_expr)

    def _raise_if_passed_unknown_parameters(self, parameters):
        unknown_parameters = parameters - self.parameters
        if unknown_parameters:
            raise CircuitError(
                "Cannot bind Parameters ({}) not present in "
                "expression.".format([str(p) for p in unknown_parameters])
            )

    def _raise_if_passed_nan(self, parameter_values):
        nan_parameter_values = {
            p: v for p, v in parameter_values.items() if not isinstance(v, numbers.Number)
        }
        if nan_parameter_values:
            raise CircuitError(
                "Expression cannot bind non-numeric values ({})".format(nan_parameter_values)
            )

    def _raise_if_parameter_names_conflict(self, inbound_parameters, outbound_parameters=None):
        if outbound_parameters is None:
            outbound_parameters = set()

        if self._names is None:
            self._names = {p.name: p for p in self._parameters}

        inbound_names = {p.name: p for p in inbound_parameters}
        outbound_names = {p.name: p for p in outbound_parameters}

        shared_names = (self._names.keys() - outbound_names.keys()) & inbound_names.keys()
        conflicting_names = {
            name for name in shared_names if self._names[name] != inbound_names[name]
        }
        if conflicting_names:
            raise CircuitError(
                "Name conflict applying operation for parameters: " "{}".format(conflicting_names)
            )

    def _apply_operation(
        self, operation: Callable, other: ParameterValueType, reflected: bool = False
    ) -> "ParameterExpression":
        """Base method implementing math operations between Parameters and
        either a constant or a second ParameterExpression.

        Args:
            operation: One of operator.{add,sub,mul,truediv}.
            other: The second argument to be used with self in operation.
            reflected: Optional - The default ordering is "self operator other".
                       If reflected is True, this is switched to "other operator self".
                       For use in e.g. __radd__, ...

        Raises:
            CircuitError:
                - If parameter_map contains Parameters outside those in self.
                - If the replacement Parameters in parameter_map would result in
                  a name conflict in the generated expression.

        Returns:
            A new expression describing the result of the operation.
        """
        self_expr = self._symbol_expr

        if isinstance(other, ParameterExpression):
            self._raise_if_parameter_names_conflict(other._parameter_symbols.keys())

            parameter_symbols = {**self._parameter_symbols, **other._parameter_symbols}
            other_expr = other._symbol_expr
        elif isinstance(other, numbers.Number) and numpy.isfinite(other):
            parameter_symbols = self._parameter_symbols.copy()
            other_expr = other
        else:
            return NotImplemented

        if reflected:
            expr = operation(other_expr, self_expr)
        else:
            expr = operation(self_expr, other_expr)

        return ParameterExpression(parameter_symbols, expr)

    def gradient(self, param) -> Union["ParameterExpression", float]:
        """Get the derivative of a parameter expression w.r.t. a specified parameter expression.

        Args:
            param (Parameter): Parameter w.r.t. which we want to take the derivative

        Returns:
            ParameterExpression representing the gradient of param_expr w.r.t. param
        """
        # Check if the parameter is contained in the parameter expression
        if param not in self._parameter_symbols.keys():
            # If it is not contained then return 0
            return 0.0

        # Compute the gradient of the parameter expression w.r.t. param
        key = self._parameter_symbols[param]
        if HAS_SYMENGINE:
            expr_grad = symengine.Derivative(self._symbol_expr, key)
        else:
            # TODO enable nth derivative
            from sympy import Derivative

            expr_grad = Derivative(self._symbol_expr, key).doit()

        # generate the new dictionary of symbols
        # this needs to be done since in the derivative some symbols might disappear (e.g.
        # when deriving linear expression)
        parameter_symbols = {}
        for parameter, symbol in self._parameter_symbols.items():
            if symbol in expr_grad.free_symbols:
                parameter_symbols[parameter] = symbol
        # If the gradient corresponds to a parameter expression then return the new expression.
        if len(parameter_symbols) > 0:
            return ParameterExpression(parameter_symbols, expr=expr_grad)
        # If no free symbols left, return a float corresponding to the gradient.
        return float(expr_grad)

    def __add__(self, other):
        return self._apply_operation(operator.add, other)

    def __radd__(self, other):
        return self._apply_operation(operator.add, other, reflected=True)

    def __sub__(self, other):
        return self._apply_operation(operator.sub, other)

    def __rsub__(self, other):
        return self._apply_operation(operator.sub, other, reflected=True)

    def __mul__(self, other):
        return self._apply_operation(operator.mul, other)

    def __neg__(self):
        return self._apply_operation(operator.mul, -1.0)

    def __rmul__(self, other):
        return self._apply_operation(operator.mul, other, reflected=True)

    def __truediv__(self, other):
        if other == 0:
            raise ZeroDivisionError("Division of a ParameterExpression by zero.")
        return self._apply_operation(operator.truediv, other)

    def __rtruediv__(self, other):
        return self._apply_operation(operator.truediv, other, reflected=True)

    def _call(self, ufunc):
        return ParameterExpression(self._parameter_symbols, ufunc(self._symbol_expr))

    def sin(self):
        """Sine of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.sin)
        else:
            from sympy import sin as _sin

            return self._call(_sin)

    def cos(self):
        """Cosine of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.cos)
        else:
            from sympy import cos as _cos

            return self._call(_cos)

    def tan(self):
        """Tangent of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.tan)
        else:
            from sympy import tan as _tan

            return self._call(_tan)

    def arcsin(self):
        """Arcsin of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.asin)
        else:
            from sympy import asin as _asin

            return self._call(_asin)

    def arccos(self):
        """Arccos of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.acos)
        else:
            from sympy import acos as _acos

            return self._call(_acos)

    def arctan(self):
        """Arctan of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.atan)
        else:
            from sympy import atan as _atan

            return self._call(_atan)

    def exp(self):
        """Exponential of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.exp)
        else:
            from sympy import exp as _exp

            return self._call(_exp)

    def log(self):
        """Logarithm of a ParameterExpression"""
        if HAS_SYMENGINE:
            return self._call(symengine.log)
        else:
            from sympy import log as _log

            return self._call(_log)

    def __repr__(self):
        return "{}({})".format(self.__class__.__name__, str(self))

    def __str__(self):
        from sympy import sympify

        return str(sympify(self._symbol_expr))

    def __float__(self):
        if self.parameters:
            raise TypeError(
                "ParameterExpression with unbound parameters ({}) "
                "cannot be cast to a float.".format(self.parameters)
            )
        return float(self._symbol_expr)

    def __complex__(self):
        if self.parameters:
            raise TypeError(
                "ParameterExpression with unbound parameters ({}) "
                "cannot be cast to a complex.".format(self.parameters)
            )
        return complex(self._symbol_expr)

    def __int__(self):
        if self.parameters:
            raise TypeError(
                "ParameterExpression with unbound parameters ({}) "
                "cannot be cast to an int.".format(self.parameters)
            )
        return int(self._symbol_expr)

    def __hash__(self):
        return hash((frozenset(self._parameter_symbols), self._symbol_expr))

    def __copy__(self):
        return self

    def __deepcopy__(self, memo=None):
        return self

    def __eq__(self, other):
        """Check if this parameter expression is equal to another parameter expression
           or a fixed value (only if this is a bound expression).
        Args:
            other (ParameterExpression or a number):
                Parameter expression or numeric constant used for comparison
        Returns:
            bool: result of the comparison
        """
        if isinstance(other, ParameterExpression):
            if self.parameters != other.parameters:
                return False
            if HAS_SYMENGINE:
                from sympy import sympify

                return sympify(self._symbol_expr).equals(sympify(other._symbol_expr))
            else:
                return self._symbol_expr.equals(other._symbol_expr)
        elif isinstance(other, numbers.Number):
            return len(self.parameters) == 0 and complex(self._symbol_expr) == other
        return False

    def __getstate__(self):
        if HAS_SYMENGINE:
            from sympy import sympify

            symbols = {k: sympify(v) for k, v in self._parameter_symbols.items()}
            expr = sympify(self._symbol_expr)
            return {"type": "symengine", "symbols": symbols, "expr": expr, "names": self._names}
        else:
            return {
                "type": "sympy",
                "symbols": self._parameter_symbols,
                "expr": self._symbol_expr,
                "names": self._names,
            }

    def __setstate__(self, state):
        if state["type"] == "symengine":
            self._symbol_expr = symengine.sympify(state["expr"])
            self._parameter_symbols = {k: symengine.sympify(v) for k, v in state["symbols"].items()}
            self._parameters = set(self._parameter_symbols)
        else:
            self._symbol_expr = state["expr"]
            self._parameter_symbols = state["symbols"]
            self._parameters = set(self._parameter_symbols)
        self._names = state["names"]

    def is_real(self):
        """Return whether the expression is real"""

        if not self._symbol_expr.is_real and self._symbol_expr.is_real is not None:
            # Symengine returns false for is_real on the expression if
            # there is a imaginary component (even if that component is 0),
            # but the parameter will evaluate as real. Check that if the
            # expression's is_real attribute returns false that we have a
            # non-zero imaginary
            if HAS_SYMENGINE:
                if self._symbol_expr.imag != 0.0:
                    return False
            else:
                return False
        return True
