Exemplo n.º 1
0
    def __rmul__(self, other):
        # multiplication with scalar is assumed to be commutative, but any
        # other multiplication is not
        from qalgebra.core.scalar_algebra import is_scalar

        if is_scalar(other):
            return self.__mul__(other)
        else:
            return NotImplemented
Exemplo n.º 2
0
def scalars_to_op(cls, ops, kwargs):
    r"""Convert any scalar $\alpha$ in `ops` into an operator $\alpha
    \identity$"""
    from qalgebra.core.scalar_algebra import is_scalar

    op_ops = []
    for op in ops:
        if is_scalar(op):
            op_ops.append(op * cls._one)
        else:
            op_ops.append(op)
    return op_ops, kwargs
Exemplo n.º 3
0
    def __mul__(self, other):
        from qalgebra.core.scalar_algebra import ScalarValue, is_scalar

        if not isinstance(other, self._base_cls):
            if is_scalar(other):
                other = ScalarValue.create(other)
                # if other was an ScalarExpression, the conversion above leaves
                # it unchanged
                return self.__class__._scalar_times_expr_cls.create(
                    other, self
                )
        if isinstance(other, self.__class__._base_cls):
            return self.__class__._times_cls.create(self, other)
        else:
            return NotImplemented
Exemplo n.º 4
0
    def __rmul__(self, other):
        from qalgebra.core.scalar_algebra import is_scalar

        if isinstance(other, IndexedSum):
            self_new = self.make_disjunct_indices(other)
            new_ranges = other.ranges + self_new.ranges
            new_term = other.term * self_new.term
            # note that class may change, depending on type of new_term
            return new_term.__class__._indexed_sum_cls.create(
                new_term, ranges=new_ranges
            )
        elif is_scalar(other):
            return self.__class__._scalar_times_expr_cls.create(other, self)
        elif isinstance(other, ScalarTimesQuantumExpression):
            return self._class__._scalar_times_expr_cls(
                other.coeff, other.term * self
            )
        else:
            sum = self.make_disjunct_indices(*other.bound_symbols)
            new_term = other * sum.term
            return new_term.__class__._indexed_sum_cls.create(
                new_term, ranges=sum.ranges
            )