Yield factors from expr, mixing sympy and QNET

    Auxiliary routine for :func:`_expand_delta`.
489571
def _factors_for_expand_delta(expr):
    
    from qnet.algebra.core.scalar_algebra import ScalarValue
    from qnet.algebra.core.abstract_quantum_algebra import (
        ScalarTimesQuantumExpression)
    if isinstance(expr, ScalarTimesQuantumExpression):
        yield from _factors_for_expand_delta(expr.coeff)
        yield expr.term
    elif isinstance(expr, ScalarValue):
        yield from _factors_for_expand_delta(expr.val)
    elif isinstance(expr, sympy.Basic) and expr.is_Mul:
        yield from expr.args
    else:
        yield expr
226241
def qn_to_qubo(expr):
    
    try:
        import sympy
    except ImportError:
        raise ImportError("This function requires sympy. Please install it.")
    assert type(expr) == sympy.Add
    to_i = lambda s: int(str(s)[1:])
    max_i = max(map(to_i, expr.free_symbols)) + 1
    qubo = [[0.] * max_i for _ in range(max_i)]
    for arg in expr.args:
        syms = arg.free_symbols
        assert len(syms) <= 2
        if len(syms) == 2:
            assert type(arg) == sympy.Mul
            i, j = list(map(to_i, syms))
            if i > j:
                i, j = j, i
            if i == j:
                if len(arg.args) == 2:
                    qubo[i][i] = float(arg.args[0])
                elif len(arg.args) == 1:
                    qubo[i][i] = 1.0
                else:
                    raise ValueError(f"Too many args! arg.args = {arg.args}")
                continue
            if len(arg.args) == 3:
                qubo[i][j] = float(arg.args[0])
            elif len(arg.args) == 2:
                qubo[i][j]
        if len(syms) == 1:
            if len(arg.args) == 2:
                assert type(arg) == sympy.Mul
                i = to_i(next(iter(syms)))
                qubo[i][i] = float(arg.args[0])
            elif len(arg.args) == 1:
                qubo[i][i] = 1.0
            else:
                raise ValueError(f"Too many args! arg.args = {arg.args}")
    return qubo
832942
def gen_mu(K, delta, c):
    

    S = c * log(K/delta) * sqrt(K) 
    tau = gen_tau(S, K, delta)
    rho = gen_rho(K)
    normalizer = sum(rho) + sum(tau)
    return [(rho[d] + tau[d])/normalizer for d in range(K)]
1163565
def _estimate_delta(ntriangles, area):
    
    return np.sqrt(4./np.sqrt(3) * float(area) / float(ntriangles))
781962
def _create_exprs_using_func(self, f, columns):
        """"""
        expressions = map(lambda c: f(c).alias(c),
                          self._columns)
        return expressions
1020537
def p_call_expr_nobf(self, p):
        
        if len(p) == 3:
            p[0] = ast.FunctionCall(p[1], p[2])
        elif len(p) == 4:
            p[0] = ast.DotAccessor(p[1], p[3])
        else:
            p[0] = ast.BracketAccessor(p[1], p[3])
250017
def deltas(self, start, height):
        
        for y in range(start, min(start + height, self._height)):
            for x in range(self._width):
                old_cell = self._screen_buffer[y][x]
                new_cell = self._double_buffer[y][x]
                if old_cell != new_cell:
                    yield y, x
350454
def _transform_delta(f:Formula, formula2AtomicFormula):
    """"""
    t = type(f)
    if t == PLNot:
        return PLNot(_transform_delta(f, formula2AtomicFormula))
    # elif isinstance(f, PLBinaryOperator): #PLAnd, PLOr, PLImplies, PLEquivalence
    elif t == PLAnd or t == PLOr or t == PLImplies or t == PLEquivalence:
        return t([_transform_delta(subf, formula2AtomicFormula) for subf in f.formulas])
    elif t == PLTrue or t == PLFalse:
        return f
    else:
        return formula2AtomicFormula[f]
1220674
def fswap(p, q):
    

    yield cirq.ISWAP(q, p), cirq.Z(p) ** 1.5
    yield cirq.Z(q) ** 1.5
997056
def p_expr_BXOR_expr(p):
    
    p[0] = make_binary(p.lineno(2), 'BXOR', p[1], p[3], lambda x, y: x ^ y)
703985
def _fillna(expr, value):
    

    if isinstance(expr, SequenceExpr):
        return FillNa(_input=expr, _fill_value=value, _data_type=expr.dtype)
    elif isinstance(expr, Scalar):
        return FillNa(_input=expr, _fill_value=value, _value_type=expr.dtype)
1011939
def _calcOffAxisFactor(self, aLocation, deltaLocation, limits):
        
        relative = []
        for dim in limits.keys():
            f = aLocation[dim]
            v = deltaLocation[dim]
            mB, M, mA = limits[dim]
            r = 0
            if mA is not None and v > mA:
                relative.append(0)
                continue
            elif mB is not None and v < mB:
                relative.append(0)
                continue
            if f < v-_EPSILON:
                if mB is None:
                    if M is not None and mA is not None:
                        if v == M:
                            r = (float(max(f,mA)-min(f, mA))/float(max(M,mA)-min(M, mA)))
                        else:
                            r = -(float(max(f,mA)-min(f, mA))/float(max(M,mA)-min(M, mA)) -1)
                    else: r = 0
                elif mA is None: r = 0
                else: r = float(f-mB)/(mA-mB)
            elif f > v+_EPSILON:
                if mB is None: r = 0
                elif mA is None:
                    if M is not None and mB is not None:
                        if v == M:
                            r = (float(max(f,mB)-min(f, mB))/(max(mB, M)-min(mB, M)))
                        else:
                            r = -(float(max(f,mB)-min(f, mB))/(max(mB, M)-min(mB, M)) - 1)
                    else: r = 0
                else: r = float(mA-f)/(mA-mB)
            else: r = 1
            relative.append(r)
        f = 1
        for i in relative:
            f *= i
        return f
894576
def getFactors(self, aLocation, axisOnly=False, allFactors=False):
        
        deltas = []
        aLocation.expand(self.getAxisNames())
        limits = getLimits(self._allLocations(), aLocation)
        for deltaLocationTuple, (mathItem, deltaName) in sorted(self.items()):
            deltaLocation = Location(deltaLocationTuple)
            deltaLocation.expand( self.getAxisNames())
            factor = self._accumulateFactors(aLocation, deltaLocation, limits, axisOnly)
            if not (factor-_EPSILON < 0 < factor+_EPSILON) or allFactors:
                # only add non-zero deltas.
                deltas.append((factor, mathItem, deltaName))
        deltas = sorted(deltas, key=itemgetter(0), reverse=True)
        return deltas
185074
def p_multiplicative_expr(self, p):
        
        if len(p) == 2:
            p[0] = p[1]
        else:
            p[0] = ast.BinOp(op=p[2], left=p[1], right=p[3])
1084595
def _transfers(reaction, delta, elements, result, epsilon):
    """"""
    left = set(c for c, _ in reaction.left)
    right = set(c for c, _ in reaction.right)
    for c1, c2 in product(left, right):
        items = {}
        for e in elements:
            v = result.get_value(delta[c1, c2, e])
            nearest_int = round(v)
            if abs(v - nearest_int) < epsilon:
                v = int(nearest_int)
            if v >= epsilon:
                items[e] = v

        if len(items) > 0:
            yield (c1, c2), Formula(items)
625975
def _get_deltas(event):
        """"""
        delta_x = round(event.deltaX())
        delta_y = round(event.deltaY())
        delta_z = round(event.deltaZ())
        return delta_x, delta_y, delta_z
775158
def generator(self, t):
        """"""
        self.check_fit()

        return (1.0 / self.theta) * (np.power(t, -self.theta) - 1)