def test_has_any(): x,y,z,t,u = symbols('x y z t u') f = Function("f") g = Function("g") p = Wild('p') assert sin(x).has(x) assert sin(x).has(sin) assert not sin(x).has(y) assert not sin(x).has(cos) assert f(x).has(x) assert f(x).has(f) assert not f(x).has(y) assert not f(x).has(g) assert f(x).diff(x).has(x) assert f(x).diff(x).has(f) assert f(x).diff(x).has(Derivative) assert not f(x).diff(x).has(y) assert not f(x).diff(x).has(g) assert not f(x).diff(x).has(sin) assert (x**2).has(Symbol) assert not (x**2).has(Wild) assert (2*p).has(Wild) i = Integer(4400) assert i.has(x) is False assert (i*x**i).has(x) assert (i*y**i).has(x) is False assert (i*y**i).has(x, y) expr = x**2*y + sin(2**t + log(z)) assert expr.has(u) is False assert expr.has(x) assert expr.has(y) assert expr.has(z) assert expr.has(t) assert expr.has(x, y, z, t) assert expr.has(x, y, z, t, u) from sympy.physics.units import m, s assert (x*m/s).has(x) assert (x*m/s).has(y, z) is False poly = Poly(x**2 + x*y*sin(z), x, y, t) assert poly.has(x) assert poly.has(x, y, z) assert poly.has(x, y, z, t) assert FockState((x, y)).has(x)
def _big_delta_coeff(aa, bb, cc, prec=None): r""" Calculates the Delta coefficient of the 3 angular momenta for Racah symbols. Also checks that the differences are of integer value. INPUT: - ``aa`` - first angular momentum, integer or half integer - ``bb`` - second angular momentum, integer or half integer - ``cc`` - third angular momentum, integer or half integer - ``prec`` - precision of the ``sqrt()`` calculation OUTPUT: double - Value of the Delta coefficient EXAMPLES:: sage: from sage.functions.wigner import _big_delta_coeff sage: _big_delta_coeff(1,1,1) 1/2*sqrt(1/6) """ if int(aa + bb - cc) != (aa + bb - cc): raise ValueError("j values must be integer or half integer and fulfill the triangle relation") if int(aa + cc - bb) != (aa + cc - bb): raise ValueError("j values must be integer or half integer and fulfill the triangle relation") if int(bb + cc - aa) != (bb + cc - aa): raise ValueError("j values must be integer or half integer and fulfill the triangle relation") if (aa + bb - cc) < 0: return 0 if (aa + cc - bb) < 0: return 0 if (bb + cc - aa) < 0: return 0 maxfact = max(aa + bb - cc, aa + cc - bb, bb + cc - aa, aa + bb + cc + 1) _calc_factlist(maxfact) argsqrt = Integer(_Factlist[int(aa + bb - cc)] * \ _Factlist[int(aa + cc - bb)] * \ _Factlist[int(bb + cc - aa)]) / \ Integer(_Factlist[int(aa + bb + cc + 1)]) ressqrt = argsqrt.sqrt(prec) if type(ressqrt) is ComplexNumber: res = ressqrt.real() else: res = ressqrt return res
def test_power(): x,y,a,b,c = map(Symbol, 'xyabc') p,q,r = map(Wild, 'pqr') e = (x+y)**a assert e.match(p**q) == {p: x+y, q: a} assert e.match(p**p) == None e = (x+y)**(x+y) assert e.match(p**p) == {p: x+y} assert e.match(p**q) == {p: x+y, q: x+y} e = (2*x)**2 assert e.match(p*q**r) == {p: 4, q: x, r: 2} e = Integer(1) assert e.match(x**p) == {p: 0}
def test_has_multiple(): f = x ** 2 * y + sin(2 ** t + log(z)) assert f.has(x) assert f.has(y) assert f.has(z) assert f.has(t) assert not f.has(u) assert f.has(x, y, z, t) assert f.has(x, y, z, t, u) i = Integer(4400) assert not i.has(x) assert (i * x ** i).has(x) assert not (i * y ** i).has(x) assert (i * y ** i).has(x, y) assert not (i * y ** i).has(x, z)
def test_has_any_symbols(): x,y,z,t,u = symbols('xyztu') i = Integer(4400) assert i.has_any_symbols(x) == False assert (i*x**i).has_any_symbols(x) == True assert (i*y**i).has_any_symbols(x) == False assert (i*y**i).has_any_symbols(x, y) == True expr = x**2*y + sin(2**t + log(z)) assert expr.has_any_symbols(u) == False assert expr.has_any_symbols(x) == True assert expr.has_any_symbols(y) == True assert expr.has_any_symbols(z) == True assert expr.has_any_symbols(t) == True assert expr.has_any_symbols(x, y, z, t) == True assert expr.has_any_symbols(x, y, z, t, u) == True from sympy.physics.units import m, s assert (x*m/s).has_any_symbols(x) == True assert (x*m/s).has_all_symbols(x) == True assert (x*m/s).has_any_symbols(y, z) == False assert (x*m/s).has_all_symbols(x, y) == False poly = Poly(x**2 + x*y*sin(z), x, y, t) assert poly.has_any_symbols(x) == True assert poly.has_any_symbols(x, y, z) == True assert poly.has_any_symbols(x, y, z, t) == True assert poly.has_all_symbols(x, y, z) == True assert poly.has_all_symbols(x, y, z, t) == False
def test_has_all(): x,y,z,t,u = symbols('xyztu') u = symbols('u') i = Integer(4400) assert i.has(x, all=True) is False assert (i*x**i).has(x, all=True) assert (i*y**i).has(x, all=True) is False expr = x**2*y + sin(2**t + log(z)) assert expr.has(y, z, t, all=True) assert expr.has(x, z, t, all=True) assert expr.has(x, y, t, all=True) assert expr.has(x, y, z, all=True) assert expr.has(y, u, t, all=True) is False assert expr.has(x, z, u, all=True) is False assert expr.has(u, y, z, all=True) is False assert expr.has(x, y, z, t, all=True) assert expr.has(x, y, z, t, u, all=True) is False from sympy.physics.units import m, s assert (x*m/s).has(x, all=True) assert (x*m/s).has(x, y, all=True) is False poly = Poly(x**2 + x*y*sin(z), x, y, t) assert poly.has(x, y, z, all=True) assert poly.has(x, y, z, t, all=True) is False f = FockState((x, y)) assert f.has(x, y, all=True) assert f.has(x, y, z, all=True) is False
def test_has_all_symbols(): x,y,z,t,u = symbols('xyztu') i = Integer(4400) assert i.has_all_symbols(x) == False assert (i*x**i).has_all_symbols(x) == True assert (i*y**i).has_all_symbols(x) == False expr = x**2*y + sin(2**t + log(z)) assert expr.has_all_symbols(y, z, t) == True assert expr.has_all_symbols(x, z, t) == True assert expr.has_all_symbols(x, y, t) == True assert expr.has_all_symbols(x, y, z) == True assert expr.has_all_symbols(y, u, t) == False assert expr.has_all_symbols(x, z, u) == False assert expr.has_all_symbols(u, y, z) == False assert expr.has_all_symbols(x, y, z, t) == True assert expr.has_all_symbols(x, y, z, t, u) == False
def test_dict_from_expr(): assert dict_from_expr(Eq(x, 1)) == \ ({(0,): -Integer(1), (1,): Integer(1)}, (x,)) raises(PolynomialError, lambda: dict_from_expr(A*B - B*A))
def generate_equivalent_ids(gate_seq, return_as_muls=False): """Returns a set of equivalent gate identities. A gate identity is a quantum circuit such that the product of the gates in the circuit is equal to a scalar value. For example, XYZ = i, where X, Y, Z are the Pauli gates and i is the imaginary value, is considered a gate identity. This function uses the four operations (LL, LR, RL, RR) to generate the gate rules and, subsequently, to locate equivalent gate identities. Note that all equivalent identities are reachable in n operations from the starting gate identity, where n is the number of gates in the sequence. The max number of gate identities is 2n, where n is the number of gates in the sequence (unproven). Parameters ========== gate_seq : Gate tuple, Mul, or Number A variable length tuple or Mul of Gates whose product is equal to a scalar matrix. return_as_muls: bool True to return as Muls; False to return as tuples Examples ======== Find equivalent gate identities from the current circuit with tuples: >>> from sympy.physics.quantum.identitysearch import generate_equivalent_ids >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> generate_equivalent_ids((x, x)) {(X(0), X(0))} >>> generate_equivalent_ids((x, y, z)) {(X(0), Y(0), Z(0)), (X(0), Z(0), Y(0)), (Y(0), X(0), Z(0)), (Y(0), Z(0), X(0)), (Z(0), X(0), Y(0)), (Z(0), Y(0), X(0))} Find equivalent gate identities from the current circuit with Muls: >>> generate_equivalent_ids(x*x, return_as_muls=True) {1} >>> generate_equivalent_ids(x*y*z, return_as_muls=True) {X(0)*Y(0)*Z(0), X(0)*Z(0)*Y(0), Y(0)*X(0)*Z(0), Y(0)*Z(0)*X(0), Z(0)*X(0)*Y(0), Z(0)*Y(0)*X(0)} """ if isinstance(gate_seq, Number): return {Integer(1)} elif isinstance(gate_seq, Mul): gate_seq = gate_seq.args # Filter through the gate rules and keep the rules # with an empty tuple either on the left or right side # A set of equivalent gate identities eq_ids = set() gate_rules = generate_gate_rules(gate_seq) for rule in gate_rules: l, r = rule if l == (): eq_ids.add(r) elif r == (): eq_ids.add(l) if return_as_muls: convert_to_mul = lambda id_seq: Mul(*id_seq) eq_ids = set(map(convert_to_mul, eq_ids)) return eq_ids
def test_Integer_as_index(): if hasattr(int, '__index__'): # Python 2.5+ (PEP 357) assert 'hello'[Integer(2):] == 'llo'
def test_mod(): x = Rational(1, 2) y = Rational(3, 4) z = Rational(5, 18043) assert x % x == 0 assert x % y == 1/S(2) assert x % z == 3/S(36086) assert y % x == 1/S(4) assert y % y == 0 assert y % z == 9/S(72172) assert z % x == 5/S(18043) assert z % y == 5/S(18043) assert z % z == 0 a = Float(2.6) assert (a % .2) == 0 assert (a % 2).round(15) == 0.6 assert (a % 0.5).round(15) == 0.1 p = Symbol('p', infinite=True) assert zoo % 0 == nan assert oo % oo == nan assert zoo % oo == nan assert 5 % oo == nan assert p % 5 == nan # In these two tests, if the precision of m does # not match the precision of the ans, then it is # likely that the change made now gives an answer # with degraded accuracy. r = Rational(500, 41) f = Float('.36', 3) m = r % f ans = Float(r % Rational(f), 3) assert m == ans and m._prec == ans._prec f = Float('8.36', 3) m = f % r ans = Float(Rational(f) % r, 3) assert m == ans and m._prec == ans._prec s = S.Zero assert s % float(1) == S.Zero # No rounding required since these numbers can be represented # exactly. assert Rational(3, 4) % Float(1.1) == 0.75 assert Float(1.5) % Rational(5, 4) == 0.25 assert Rational(5, 4).__rmod__(Float('1.5')) == 0.25 assert Float('1.5').__rmod__(Float('2.75')) == Float('1.25') assert 2.75 % Float('1.5') == Float('1.25') a = Integer(7) b = Integer(4) assert type(a % b) == Integer assert a % b == Integer(3) assert Integer(1) % Rational(2, 3) == Rational(1, 3) assert Rational(7, 5) % Integer(1) == Rational(2, 5) assert Integer(2) % 1.5 == 0.5 assert Integer(3).__rmod__(Integer(10)) == Integer(1) assert Integer(10) % 4 == Integer(2) assert 15 % Integer(4) == Integer(3)
def test_hashing_sympy_integers(): # Test for issue 5072 assert set([Integer(3)]) == set([int(3)]) assert hash(Integer(4)) == hash(int(4))
def test_conversion_to_mpmath(): assert mpmath.mpmathify(Integer(1)) == mpmath.mpf(1) assert mpmath.mpmathify(Rational(1, 2)) == mpmath.mpf(0.5) assert mpmath.mpmathify(Float('1.23', 15)) == mpmath.mpf('1.23')
def test_IntegerInteger(): a = Integer(4) b = Integer(a) assert a == b
def _sympy_(self): return Integer(5)
def test_sympify_function(): assert sympify('factor(x**2-1, x)') == -(1 - x) * (x + 1) assert sympify('sin(pi/2)*cos(pi)') == -Integer(1)
def test_check_arguments(): x, y = symbols("x, y") ### Test arguments for plot() # single expressions args = _plot_sympify((x + 1, )) r = _check_arguments(args, 1, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, (x, -10, 10), "x + 1") # single expressions with range args = _plot_sympify((x + 1, (x, -2, 2))) r = _check_arguments(args, 1, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, (x, -2, 2), "x + 1") # single expressions with range and label args = _plot_sympify((x + 1, (x, -2, 2), "test")) r = _check_arguments(args, 1, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, (x, -2, 2), "test") # multiple expressions args = _plot_sympify((x + 1, x**2)) r = _check_arguments(args, 1, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, (x, -10, 10), "x + 1") assert r[1] == (x**2, (x, -10, 10), "x**2") # multiple expressions over the same range args = _plot_sympify((x + 1, x**2, (x, 0, 5))) r = _check_arguments(args, 1, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, (x, 0, 5), "x + 1") assert r[1] == (x**2, (x, 0, 5), "x**2") # multiple expressions with different ranges and labels args = _plot_sympify([(x + 1, (x, 0, 5)), (x**2, (x, -2, 2), "test")]) r = _check_arguments(args, 1, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, (x, 0, 5), "x + 1") assert r[1] == (x**2, (x, -2, 2), "test") ### Test arguments for plot_parametric() # single parametric expression args = _plot_sympify((x + 1, x)) r = _check_arguments(args, 2, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, x, (x, -10, 10), "(x + 1, x)") # single parametric expression with custom range and label args = _plot_sympify((x + 1, x, (x, -2, 2), "test")) r = _check_arguments(args, 2, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, x, (x, -2, 2), "test") args = _plot_sympify(((x + 1, x), (x, -2, 2), "test")) r = _check_arguments(args, 2, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, x, (x, -2, 2), "test") # multiple parametric expressions args = _plot_sympify([(x + 1, x), (x**2, x + 1)]) r = _check_arguments(args, 2, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, x, (x, -10, 10), "(x + 1, x)") assert r[1] == (x**2, x + 1, (x, -10, 10), "(x**2, x + 1)") # multiple parametric expressions same range args = _plot_sympify([(x + 1, x), (x**2, x + 1), (x, -2, 2)]) r = _check_arguments(args, 2, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, x, (x, -2, 2), "(x + 1, x)") assert r[1] == (x**2, x + 1, (x, -2, 2), "(x**2, x + 1)") # multiple parametric expressions, custom ranges and labels args = _plot_sympify([(x + 1, x, (x, -2, 2)), (x**2, x + 1, (x, -3, 3), "test")]) r = _check_arguments(args, 2, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, x, (x, -2, 2), "(x + 1, x)") assert r[1] == (x**2, x + 1, (x, -3, 3), "test") ### Test arguments for plot3d_parametric_line() # single parametric expression args = _plot_sympify((x + 1, x, sin(x))) r = _check_arguments(args, 3, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, x, sin(x), (x, -10, 10), "(x + 1, x, sin(x))") # single parametric expression with custom range and label args = _plot_sympify((x + 1, x, sin(x), (x, -2, 2), "test")) r = _check_arguments(args, 3, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, x, sin(x), (x, -2, 2), "test") args = _plot_sympify(((x + 1, x, sin(x)), (x, -2, 2), "test")) r = _check_arguments(args, 3, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + 1, x, sin(x), (x, -2, 2), "test") # multiple parametric expression args = _plot_sympify([(x + 1, x, sin(x)), (x**2, 1, cos(x))]) r = _check_arguments(args, 3, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, x, sin(x), (x, -10, 10), "(x + 1, x, sin(x))") assert r[1] == (x**2, Integer(1), cos(x), (x, -10, 10), "(x**2, 1, cos(x))") # multiple parametric expression, custom ranges and labels args = _plot_sympify([(x + 1, x, sin(x)), (x**2, 1, cos(x), (x, -2, 2), "test")]) r = _check_arguments(args, 3, 1) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + 1, x, sin(x), (x, -10, 10), "(x + 1, x, sin(x))") assert r[1] == (x**2, Integer(1), cos(x), (x, -2, 2), "test") ### Test arguments for plot3d() and plot_contour() # single expression args = _plot_sympify((x + y, )) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert len(r[0]) == 4 assert r[0][0] == x + y assert r[0][1] == (x, -10, 10) or (y, -10, 10) assert r[0][2] == (y, -10, 10) or (x, -10, 10) assert r[0][1] != r[0][2] assert r[0][3] == "x + y" # single expression, custom range and label args = _plot_sympify((x + y, (x, -2, 2), "test")) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert len(r[0]) == 4 assert r[0][0] == x + y assert r[0][1] == (x, -2, 2) or (y, -10, 10) assert r[0][2] == (y, -10, 10) or (x, -2, 2) assert r[0][1] != r[0][2] assert r[0][3] == "test" args = _plot_sympify((x + y, (x, -2, 2), (y, -4, 4), "test")) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + y, (x, -2, 2), (y, -4, 4), "test") # multiple expressions args = _plot_sympify((x + y, x * y)) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert len(r[0]) == 4 assert len(r[1]) == 4 assert r[0][0] == x + y assert r[0][1] == (x, -10, 10) or (y, -10, 10) assert r[0][2] == (y, -10, 10) or (x, -10, 10) assert r[0][1] != r[0][2] assert r[0][3] == "x + y" assert r[1][0] == x * y assert r[1][1] == (x, -10, 10) or (y, -10, 10) assert r[1][2] == (y, -10, 10) or (x, -10, 10) assert r[1][1] != r[0][2] assert r[1][3] == "x*y" # multiple expressions, same custom ranges args = _plot_sympify((x + y, x * y, (x, -2, 2), (y, -4, 4))) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + y, (x, -2, 2), (y, -4, 4), "x + y") assert r[1] == (x * y, (x, -2, 2), (y, -4, 4), "x*y") # multiple expressions, custom ranges and labels args = _plot_sympify([(x + y, (x, -2, 2), (y, -4, 4)), (x * y, (x, -3, 3), (y, -6, 6), "test")]) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert r[0] == (x + y, (x, -2, 2), (y, -4, 4), "x + y") assert r[1] == (x * y, (x, -3, 3), (y, -6, 6), "test") ### Test arguments for plot3d_parametric_surface() # single parametric expression args = _plot_sympify((x + y, cos(x + y), sin(x + y))) r = _check_arguments(args, 3, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert len(r[0]) == 6 assert r[0][0] == x + y assert r[0][1] == cos(x + y) assert r[0][2] == sin(x + y) assert r[0][3] == (x, -10, 10) or (y, -10, 10) assert r[0][4] == (y, -10, 10) or (x, -10, 10) assert r[0][3] != r[0][4] assert r[0][5] == "(x + y, cos(x + y), sin(x + y))" # single parametric expression, custom ranges and labels args = _plot_sympify( (x + y, cos(x + y), sin(x + y), (x, -2, 2), (y, -4, 4), "test")) r = _check_arguments(args, 3, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert r[0] == (x + y, cos(x + y), sin(x + y), (x, -2, 2), (y, -4, 4), "test") # multiple parametric expressions args = _plot_sympify([(x + y, cos(x + y), sin(x + y)), (x - y, cos(x - y), sin(x - y))]) r = _check_arguments(args, 3, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert len(r[0]) == 6 assert len(r[1]) == 6 assert r[0][0] == x + y assert r[0][1] == cos(x + y) assert r[0][2] == sin(x + y) assert r[0][3] == (x, -10, 10) or (y, -10, 10) assert r[0][4] == (y, -10, 10) or (x, -10, 10) assert r[0][3] != r[0][4] assert r[0][5] == "(x + y, cos(x + y), sin(x + y))" assert r[1][0] == x - y assert r[1][1] == cos(x - y) assert r[1][2] == sin(x - y) assert r[1][3] == (x, -10, 10) or (y, -10, 10) assert r[1][4] == (y, -10, 10) or (x, -10, 10) assert r[1][3] != r[0][4] assert r[1][5] == "(x - y, cos(x - y), sin(x - y))" # multiple parametric expressions, custom ranges and labels args = _plot_sympify([ (x + y, cos(x + y), sin(x + y), (x, -2, 2), "test"), (x - y, cos(x - y), sin(x - y), (x, -3, 3), (y, -4, 4), "test2"), ]) r = _check_arguments(args, 3, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert len(r[0]) == 6 assert r[0][0] == x + y assert r[0][1] == cos(x + y) assert r[0][2] == sin(x + y) assert r[0][3] == (x, -2, 2) or (y, -10, 10) assert r[0][4] == (y, -10, 10) or (x, -2, 2) assert r[0][3] != r[0][4] assert r[0][5] == "test" assert r[1] == (x - y, cos(x - y), sin(x - y), (x, -3, 3), (y, -4, 4), "test2") ### Test arguments for plot_implicit # single expression with both ranges args = _plot_sympify((x > 0, (x, -2, 2), (y, -3, 3))) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert len(r[0]) == 4 assert r[0] == (x > 0, (x, -2, 2), (y, -3, 3), "x > 0") # single expression with one missing range args = _plot_sympify((x > 0, (x, -2, 2), "test")) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 1 assert len(r[0]) == 4 assert r[0][:2] == (x > 0, (x, -2, 2)) assert r[0][-1] == "test" assert (r[0][2][1] == Integer(-10)) and (r[0][2][2] == Integer(10)) # multiple expressions args = _plot_sympify([(x > 0, (x, -2, 2), (y, -3, 3)), ((x > 0) & (y < 0), "test")]) r = _check_arguments(args, 1, 2) assert isinstance(r, (list, tuple, Tuple)) and len(r) == 2 assert len(r[0]) == 4 assert r[0] == (x > 0, (x, -2, 2), (y, -3, 3), "x > 0") assert len(r[1]) == 4 assert r[1][0] == ((x > 0) & (y < 0)) assert (r[1][1] == Tuple(x, -10, 10)) or (r[1][1] == Tuple(y, -10, 10)) assert (r[1][2] == Tuple(x, -10, 10)) or (r[1][2] == Tuple(y, -10, 10)) assert r[1][-1] == "test" # incompatible free symbols between expression and ranges z = symbols("z") args = _plot_sympify((x * y > 0, (x, -2, 2), (z, -3, 3))) raises(ValueError, lambda: _check_arguments(args, 1, 2))
def test_powers_Integer(): """Test Integer._eval_power""" # check infinity assert S(1)**S.Infinity == 1 assert S(-1)**S.Infinity == S.NaN assert S(2)**S.Infinity == S.Infinity assert S(-2)**S.Infinity == S.Infinity + S.Infinity * S.ImaginaryUnit assert S(0)**S.Infinity == 0 # check Nan assert S(1)**S.NaN == S.One assert S(-1)**S.NaN == S.NaN # check for exact roots assert S(-1)**Rational(6, 5) == -(-1)**(S(1) / 5) assert sqrt(S(4)) == 2 assert sqrt(S(-4)) == I * 2 assert S(16)**Rational(1, 4) == 2 assert S(-16)**Rational(1, 4) == 2 * (-1)**Rational(1, 4) assert S(9)**Rational(3, 2) == 27 assert S(-9)**Rational(3, 2) == -27 * I assert S(27)**Rational(2, 3) == 9 assert S(-27)**Rational(2, 3) == 9 * (S(-1)**Rational(2, 3)) assert (-2)**Rational(-2, 1) == Rational(1, 4) # not exact roots assert sqrt(-3) == I * sqrt(3) assert (3)**(S(3) / 2) == 3 * sqrt(3) assert (-3)**(S(3) / 2) == -3 * sqrt(-3) assert (-3)**(S(5) / 2) == 9 * I * sqrt(3) assert (-3)**(S(7) / 2) == -I * 27 * sqrt(3) assert (2)**(S(3) / 2) == 2 * sqrt(2) assert (2)**(S(-3) / 2) == sqrt(2) / 4 assert (81)**(S(2) / 3) == 9 * (S(3)**(S(2) / 3)) assert (-81)**(S(2) / 3) == 9 * (S(-3)**(S(2) / 3)) assert (-3) ** Rational(-7, 3) == \ -(-1)**Rational(2, 3)*3**Rational(2, 3)/27 assert (-3) ** Rational(-2, 3) == \ -(-1)**Rational(1, 3)*3**Rational(1, 3)/3 # join roots assert sqrt(6) + sqrt(24) == 3 * sqrt(6) assert sqrt(2) * sqrt(3) == sqrt(6) # separate symbols & constansts x = Symbol("x") assert sqrt(49 * x) == 7 * sqrt(x) assert sqrt((3 - sqrt(pi))**2) == 3 - sqrt(pi) # check that it is fast for big numbers assert (2**64 + 1)**Rational(4, 3) assert (2**64 + 1)**Rational(17, 25) # negative rational power and negative base assert (-3) ** Rational(-7, 3) == \ -(-1)**Rational(2, 3)*3**Rational(2, 3)/27 assert (-3) ** Rational(-2, 3) == \ -(-1)**Rational(1, 3)*3**Rational(1, 3)/3 assert S(1234).factors() == {617: 1, 2: 1} assert Rational(2 * 3, 3 * 5 * 7).factors() == {2: 1, 5: -1, 7: -1} # test that eval_power factors numbers bigger than # the current limit in factor_trial_division (2**15) from sympy import nextprime n = nextprime(2**15) assert sqrt(n**2) == n assert sqrt(n**3) == n * sqrt(n) assert sqrt(4 * n) == 2 * sqrt(n) # check that factors of base with powers sharing gcd with power are removed assert (2**4 * 3)**Rational(1, 6) == 2**Rational(2, 3) * 3**Rational(1, 6) assert (2**4 * 3)**Rational( 5, 6) == 8 * 2**Rational(1, 3) * 3**Rational(5, 6) # check that bases sharing a gcd are exptracted assert 2**Rational(1, 3)*3**Rational(1, 4)*6**Rational(1, 5) == \ 2**Rational(8, 15)*3**Rational(9, 20) assert sqrt(8)*24**Rational(1, 3)*6**Rational(1, 5) == \ 4*2**Rational(7, 10)*3**Rational(8, 15) assert sqrt(8)*(-24)**Rational(1, 3)*(-6)**Rational(1, 5) == \ 4*(-3)**Rational(8, 15)*2**Rational(7, 10) assert 2**Rational(1, 3) * 2**Rational(8, 9) == 2 * 2**Rational(2, 9) assert 2**Rational(2, 3) * 6**Rational(1, 3) == 2 * 3**Rational(1, 3) assert 2**Rational(2, 3)*6**Rational(8, 9) == \ 2*2**Rational(5, 9)*3**Rational(8, 9) assert (-2)**Rational(2, S(3)) * (-4)**Rational( 1, S(3)) == -2 * 2**Rational(1, 3) assert 3 * Pow(3, 2, evaluate=False) == 3**3 assert 3 * Pow(3, -1 / S(3), evaluate=False) == 3**(2 / S(3)) assert (-2)**(1/S(3))*(-3)**(1/S(4))*(-5)**(5/S(6)) == \ -(-1)**Rational(5, 12)*2**Rational(1, 3)*3**Rational(1, 4) * \ 5**Rational(5, 6) assert Integer(-2)**Symbol('', even=True) == \ Integer(2)**Symbol('', even=True) assert (-1)**Float(.5) == 1.0 * I
def test_mod(): x = Rational(1, 2) y = Rational(3, 4) z = Rational(5, 18043) assert x % x == 0 assert x % y == 1 / S(2) assert x % z == 3 / S(36086) assert y % x == 1 / S(4) assert y % y == 0 assert y % z == 9 / S(72172) assert z % x == 5 / S(18043) assert z % y == 5 / S(18043) assert z % z == 0 a = Float(2.6) assert (a % .2).round(15) == 0.2 assert (a % 2).round(15) == 0.6 assert (a % 0.5).round(15) == 0.1 # No rounding required since these numbers can be represented # exactly. assert Rational(3, 4) % Float(1.1) == 0.75 assert Float(1.5) % Rational(5, 4) == 0.25 assert Rational(5, 4).__rmod__(Float('1.5')) == 0.25 assert Float('1.5').__rmod__(Float('2.75')) == Float('1.25') assert 2.75 % Float('1.5') == Float('1.25') a = Integer(7) b = Integer(4) assert type(a % b) == Integer assert a % b == Integer(3) assert Integer(1) % Rational(2, 3) == Rational(1, 3) assert Rational(7, 5) % Integer(1) == Rational(2, 5) assert Integer(2) % 1.5 == 0.5 assert Integer(3).__rmod__(Integer(10)) == Integer(1) assert Integer(10) % 4 == Integer(2) assert 15 % Integer(4) == Integer(3)
def test_Float(): def eq(a, b): t = Float("1.0E-15") return (-t < a - b < t) a = Float(2)**Float(3) assert eq(a.evalf(), Float(8)) assert eq((pi**-1).evalf(), Float("0.31830988618379067")) a = Float(2)**Float(4) assert eq(a.evalf(), Float(16)) assert (S(.3) == S(.5)) is False x_str = Float((0, '13333333333333', -52, 53)) x2_str = Float((0, '26666666666666', -53, 53)) x_hex = Float((0, 0x13333333333333L, -52, 53)) x_dec = Float((0, 5404319552844595L, -52, 53)) x2_hex = Float((0, 0x13333333333333L * 2, -53, 53)) assert x_str == x_hex == x_dec == x2_hex == Float(1.2) # x2_str and 1.2 are superficially the same assert str(x2_str) == str(Float(1.2)) # but are different at the mpf level assert Float(1.2)._mpf_ == (0, 5404319552844595L, -52, 53) assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53) assert Float((0, 0L, -123, -1)) == Float('nan') assert Float((0, 0L, -456, -2)) == Float('inf') == Float('+inf') assert Float((1, 0L, -789, -3)) == Float('-inf') # do not automatically evalf def teq(a): assert (a.evalf() == a) is False assert (a.evalf() != a) is True assert (a == a.evalf()) is False assert (a != a.evalf()) is True teq(pi) teq(2 * pi) teq(cos(0.1, evaluate=False)) i = 12345678901234567890 assert _aresame(Float(12, ''), Float('12', '')) assert _aresame(Float(Integer(i), ''), Float(i, '')) assert _aresame(Float(i, ''), Float(str(i), 20)) assert not _aresame(Float(str(i)), Float(i, '')) # inexact floats (repeating binary = denom not multiple of 2) # cannot have precision greater than 15 assert Float(.125, 22) == .125 assert Float(2.0, 22) == 2 assert float(Float('.12500000000000001', '')) == .125 raises(ValueError, lambda: Float(.12500000000000001, '')) # allow spaces Float('123 456.123 456') == Float('123456.123456') Integer('123 456') == Integer('123456') Rational('123 456.123 456') == Rational('123456.123456') assert Float(' .3e2') == Float('0.3e2') # allow auto precision detection assert Float('.1', '') == Float(.1, 1) assert Float('.125', '') == Float(.125, 3) assert Float('.100', '') == Float(.1, 3) assert Float('2.0', '') == Float('2', 2) raises(ValueError, lambda: Float("12.3d-4", "")) raises(ValueError, lambda: Float(12.3, "")) raises(ValueError, lambda: Float('.')) raises(ValueError, lambda: Float('-.')) zero = Float('0.0') assert Float('-0') == zero assert Float('.0') == zero assert Float('-.0') == zero assert Float('-0.0') == zero assert Float(0.0) == zero assert Float(0) == zero assert Float(0, '') == Float('0', '') assert Float(1) == Float(1.0) assert Float(S.Zero) == zero assert Float(S.One) == Float(1.0)
def test_no_len(): # there should be no len for numbers raises(TypeError, lambda: len(Rational(2))) raises(TypeError, lambda: len(Rational(2, 3))) raises(TypeError, lambda: len(Integer(2)))
def F(i): return Integer(i).factors()
def test_issue1034(): a = sympify('Integer(4)') assert a == Integer(4) assert a.is_Integer
def test_Rational_gcd_lcm_cofactors(): assert Integer(4).gcd(2) == Integer(2) assert Integer(4).lcm(2) == Integer(4) assert Integer(4).gcd(Integer(2)) == Integer(2) assert Integer(4).lcm(Integer(2)) == Integer(4) assert Integer(4).gcd(3) == Integer(1) assert Integer(4).lcm(3) == Integer(12) assert Integer(4).gcd(Integer(3)) == Integer(1) assert Integer(4).lcm(Integer(3)) == Integer(12) assert Rational(4, 3).gcd(2) == Rational(2, 3) assert Rational(4, 3).lcm(2) == Integer(4) assert Rational(4, 3).gcd(Integer(2)) == Rational(2, 3) assert Rational(4, 3).lcm(Integer(2)) == Integer(4) assert Integer(4).gcd(Rational(2, 9)) == Rational(2, 9) assert Integer(4).lcm(Rational(2, 9)) == Integer(4) assert Rational(4, 3).gcd(Rational(2, 9)) == Rational(2, 9) assert Rational(4, 3).lcm(Rational(2, 9)) == Rational(4, 3) assert Rational(4, 5).gcd(Rational(2, 9)) == Rational(2, 45) assert Rational(4, 5).lcm(Rational(2, 9)) == Integer(4) assert Integer(4).cofactors(2) == (Integer(2), Integer(2), Integer(1)) assert Integer(4).cofactors(Integer(2)) == \ (Integer(2), Integer(2), Integer(1)) assert Integer(4).gcd(Float(2.0)) == S.One assert Integer(4).lcm(Float(2.0)) == Float(8.0) assert Integer(4).cofactors(Float(2.0)) == (S.One, Integer(4), Float(2.0)) assert Rational(1, 2).gcd(Float(2.0)) == S.One assert Rational(1, 2).lcm(Float(2.0)) == Float(1.0) assert Rational(1, 2).cofactors(Float(2.0)) == \ (S.One, Rational(1, 2), Float(2.0))
def test_issue883(): a = [3, 2.0] assert sympify(a) == [Integer(3), Real(2.0)] assert sympify(tuple(a)) == (Integer(3), Real(2.0)) assert sympify(set(a)) == set([Integer(3), Real(2.0)])
def test_Integer_as_index(): assert 'hello'[Integer(2):] == 'llo'
def test__dict_from_expr_if_gens(): assert dict_from_expr( Integer(17), gens=(x,)) == ({(0,): Integer(17)}, (x,)) assert dict_from_expr( Integer(17), gens=(x, y)) == ({(0, 0): Integer(17)}, (x, y)) assert dict_from_expr( Integer(17), gens=(x, y, z)) == ({(0, 0, 0): Integer(17)}, (x, y, z)) assert dict_from_expr( Integer(-17), gens=(x,)) == ({(0,): Integer(-17)}, (x,)) assert dict_from_expr( Integer(-17), gens=(x, y)) == ({(0, 0): Integer(-17)}, (x, y)) assert dict_from_expr(Integer( -17), gens=(x, y, z)) == ({(0, 0, 0): Integer(-17)}, (x, y, z)) assert dict_from_expr( Integer(17)*x, gens=(x,)) == ({(1,): Integer(17)}, (x,)) assert dict_from_expr( Integer(17)*x, gens=(x, y)) == ({(1, 0): Integer(17)}, (x, y)) assert dict_from_expr(Integer( 17)*x, gens=(x, y, z)) == ({(1, 0, 0): Integer(17)}, (x, y, z)) assert dict_from_expr( Integer(17)*x**7, gens=(x,)) == ({(7,): Integer(17)}, (x,)) assert dict_from_expr( Integer(17)*x**7*y, gens=(x, y)) == ({(7, 1): Integer(17)}, (x, y)) assert dict_from_expr(Integer(17)*x**7*y*z**12, gens=( x, y, z)) == ({(7, 1, 12): Integer(17)}, (x, y, z)) assert dict_from_expr(x + 2*y + 3*z, gens=(x,)) == \ ({(1,): Integer(1), (0,): 2*y + 3*z}, (x,)) assert dict_from_expr(x + 2*y + 3*z, gens=(x, y)) == \ ({(1, 0): Integer(1), (0, 1): Integer(2), (0, 0): 3*z}, (x, y)) assert dict_from_expr(x + 2*y + 3*z, gens=(x, y, z)) == \ ({(1, 0, 0): Integer( 1), (0, 1, 0): Integer(2), (0, 0, 1): Integer(3)}, (x, y, z)) assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x,)) == \ ({(1,): y + 2*z, (0,): 3*y*z}, (x,)) assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y)) == \ ({(1, 1): Integer(1), (1, 0): 2*z, (0, 1): 3*z}, (x, y)) assert dict_from_expr(x*y + 2*x*z + 3*y*z, gens=(x, y, z)) == \ ({(1, 1, 0): Integer( 1), (1, 0, 1): Integer(2), (0, 1, 1): Integer(3)}, (x, y, z)) assert dict_from_expr(2**y*x, gens=(x,)) == ({(1,): 2**y}, (x,)) assert dict_from_expr(Integral(x, (x, 1, 2)) + x) == ( {(0, 1): 1, (1, 0): 1}, (x, Integral(x, (x, 1, 2)))) raises(PolynomialError, lambda: dict_from_expr(2**y*x, gens=(x, y)))
def test_Float(): def eq(a, b): t = Float("1.0E-15") return (-t < a - b < t) a = Float(2) ** Float(3) assert eq(a.evalf(), Float(8)) assert eq((pi ** -1).evalf(), Float("0.31830988618379067")) a = Float(2) ** Float(4) assert eq(a.evalf(), Float(16)) assert (S(.3) == S(.5)) is False x_str = Float((0, '13333333333333', -52, 53)) x2_str = Float((0, '26666666666666', -53, 53)) x_hex = Float((0, long(0x13333333333333), -52, 53)) x_dec = Float((0, 5404319552844595, -52, 53)) x2_hex = Float((0, long(0x13333333333333)*2, -53, 53)) assert x_str == x_hex == x_dec == x2_hex == Float(1.2) # x2_str and 1.2 are superficially the same assert str(x2_str) == str(Float(1.2)) # but are different at the mpf level assert Float(1.2)._mpf_ == (0, long(5404319552844595), -52, 53) assert x2_str._mpf_ == (0, long(10808639105689190), -53, 53) assert Float((0, long(0), -123, -1)) == Float('nan') assert Float((0, long(0), -456, -2)) == Float('inf') == Float('+inf') assert Float((1, long(0), -789, -3)) == Float('-inf') raises(ValueError, lambda: Float((0, 7, 1, 3), '')) assert Float('+inf').is_finite is False assert Float('+inf').is_negative is False assert Float('+inf').is_positive is True assert Float('+inf').is_infinite is True assert Float('+inf').is_zero is False assert Float('-inf').is_finite is False assert Float('-inf').is_negative is True assert Float('-inf').is_positive is False assert Float('-inf').is_infinite is True assert Float('-inf').is_zero is False assert Float('0.0').is_finite is True assert Float('0.0').is_negative is False assert Float('0.0').is_positive is False assert Float('0.0').is_infinite is False assert Float('0.0').is_zero is True # rationality properties assert Float(1).is_rational is None assert Float(1).is_irrational is None assert sqrt(2).n(15).is_rational is None assert sqrt(2).n(15).is_irrational is None # do not automatically evalf def teq(a): assert (a.evalf() == a) is False assert (a.evalf() != a) is True assert (a == a.evalf()) is False assert (a != a.evalf()) is True teq(pi) teq(2*pi) teq(cos(0.1, evaluate=False)) # long integer i = 12345678901234567890 assert same_and_same_prec(Float(12, ''), Float('12', '')) assert same_and_same_prec(Float(Integer(i), ''), Float(i, '')) assert same_and_same_prec(Float(i, ''), Float(str(i), 20)) assert same_and_same_prec(Float(str(i)), Float(i, '')) assert same_and_same_prec(Float(i), Float(i, '')) # inexact floats (repeating binary = denom not multiple of 2) # cannot have precision greater than 15 assert Float(.125, 22) == .125 assert Float(2.0, 22) == 2 assert float(Float('.12500000000000001', '')) == .125 raises(ValueError, lambda: Float(.12500000000000001, '')) # allow spaces Float('123 456.123 456') == Float('123456.123456') Integer('123 456') == Integer('123456') Rational('123 456.123 456') == Rational('123456.123456') assert Float(' .3e2') == Float('0.3e2') # allow auto precision detection assert Float('.1', '') == Float(.1, 1) assert Float('.125', '') == Float(.125, 3) assert Float('.100', '') == Float(.1, 3) assert Float('2.0', '') == Float('2', 2) raises(ValueError, lambda: Float("12.3d-4", "")) raises(ValueError, lambda: Float(12.3, "")) raises(ValueError, lambda: Float('.')) raises(ValueError, lambda: Float('-.')) zero = Float('0.0') assert Float('-0') == zero assert Float('.0') == zero assert Float('-.0') == zero assert Float('-0.0') == zero assert Float(0.0) == zero assert Float(0) == zero assert Float(0, '') == Float('0', '') assert Float(1) == Float(1.0) assert Float(S.Zero) == zero assert Float(S.One) == Float(1.0) assert Float(decimal.Decimal('0.1'), 3) == Float('.1', 3) assert Float(decimal.Decimal('nan')) == S.NaN assert Float(decimal.Decimal('Infinity')) == S.Infinity assert Float(decimal.Decimal('-Infinity')) == S.NegativeInfinity assert '{0:.3f}'.format(Float(4.236622)) == '4.237' assert '{0:.35f}'.format(Float(pi.n(40), 40)) == '3.14159265358979323846264338327950288'
def test_complex_e(): assert_equal("e^{I\\pi}", Integer(-1))
def test_scalar_sympy(): assert represent(Integer(1)) == Integer(1) assert represent(Float(1.0)) == Float(1.0) assert represent(1.0 + I) == 1.0 + I
def _eval_innerproduct_QubitBra(self, bra, **hints): if self.label == bra.label: return Integer(1) else: return Integer(0)
def test_equality_subs1(): f = Function('f') x = abc.x eq = Eq(f(x)**2, x) res = Eq(Integer(16), x) assert eq.subs(f(x), 4) == res
def test_parallel_dict_from_expr(): assert parallel_dict_from_expr([Eq(x, 1), Eq( x**2, 2)]) == ([{(0,): -Integer(1), (1,): Integer(1)}, {(0,): -Integer(2), (2,): Integer(1)}], (x,)) raises(PolynomialError, lambda: parallel_dict_from_expr([A*B - B*A]))
def test__parallel_dict_from_expr_if_gens(): assert parallel_dict_from_expr([x + 2*y + 3*z, Integer(7)], gens=(x,)) == \ ([{(1,): Integer(1), (0,): 2*y + 3*z}, {(0,): Integer(7)}], (x,))
def generate_gate_rules(gate_seq, return_as_muls=False): """Returns a set of gate rules. Each gate rules is represented as a 2-tuple of tuples or Muls. An empty tuple represents an arbitrary scalar value. This function uses the four operations (LL, LR, RL, RR) to generate the gate rules. A gate rule is an expression such as ABC = D or AB = CD, where A, B, C, and D are gates. Each value on either side of the equal sign represents a circuit. The four operations allow one to find a set of equivalent circuits from a gate identity. The letters denoting the operation tell the user what activities to perform on each expression. The first letter indicates which side of the equal sign to focus on. The second letter indicates which gate to focus on given the side. Once this information is determined, the inverse of the gate is multiplied on both circuits to create a new gate rule. For example, given the identity, ABCD = 1, a LL operation means look at the left value and multiply both left sides by the inverse of the leftmost gate A. If A is Hermitian, the inverse of A is still A. The resulting new rule is BCD = A. The following is a summary of the four operations. Assume that in the examples, all gates are Hermitian. LL : left circuit, left multiply ABCD = E -> AABCD = AE -> BCD = AE LR : left circuit, right multiply ABCD = E -> ABCDD = ED -> ABC = ED RL : right circuit, left multiply ABC = ED -> EABC = EED -> EABC = D RR : right circuit, right multiply AB = CD -> ABD = CDD -> ABD = C The number of gate rules generated is n*(n+1), where n is the number of gates in the sequence (unproven). Parameters ========== gate_seq : Gate tuple, Mul, or Number A variable length tuple or Mul of Gates whose product is equal to a scalar matrix return_as_muls : bool True to return a set of Muls; False to return a set of tuples Examples ======== Find the gate rules of the current circuit using tuples: >>> from sympy.physics.quantum.identitysearch import generate_gate_rules >>> from sympy.physics.quantum.gate import X, Y, Z >>> x = X(0); y = Y(0); z = Z(0) >>> generate_gate_rules((x, x)) {((X(0),), (X(0),)), ((X(0), X(0)), ())} >>> generate_gate_rules((x, y, z)) {((), (X(0), Z(0), Y(0))), ((), (Y(0), X(0), Z(0))), ((), (Z(0), Y(0), X(0))), ((X(0),), (Z(0), Y(0))), ((Y(0),), (X(0), Z(0))), ((Z(0),), (Y(0), X(0))), ((X(0), Y(0)), (Z(0),)), ((Y(0), Z(0)), (X(0),)), ((Z(0), X(0)), (Y(0),)), ((X(0), Y(0), Z(0)), ()), ((Y(0), Z(0), X(0)), ()), ((Z(0), X(0), Y(0)), ())} Find the gate rules of the current circuit using Muls: >>> generate_gate_rules(x*x, return_as_muls=True) {(1, 1)} >>> generate_gate_rules(x*y*z, return_as_muls=True) {(1, X(0)*Z(0)*Y(0)), (1, Y(0)*X(0)*Z(0)), (1, Z(0)*Y(0)*X(0)), (X(0)*Y(0), Z(0)), (Y(0)*Z(0), X(0)), (Z(0)*X(0), Y(0)), (X(0)*Y(0)*Z(0), 1), (Y(0)*Z(0)*X(0), 1), (Z(0)*X(0)*Y(0), 1), (X(0), Z(0)*Y(0)), (Y(0), X(0)*Z(0)), (Z(0), Y(0)*X(0))} """ if isinstance(gate_seq, Number): if return_as_muls: return {(Integer(1), Integer(1))} else: return {((), ())} elif isinstance(gate_seq, Mul): gate_seq = gate_seq.args # Each item in queue is a 3-tuple: # i) first item is the left side of an equality # ii) second item is the right side of an equality # iii) third item is the number of operations performed # The argument, gate_seq, will start on the left side, and # the right side will be empty, implying the presence of an # identity. queue = deque() # A set of gate rules rules = set() # Maximum number of operations to perform max_ops = len(gate_seq) def process_new_rule(new_rule, ops): if new_rule is not None: new_left, new_right = new_rule if new_rule not in rules and (new_right, new_left) not in rules: rules.add(new_rule) # If haven't reached the max limit on operations if ops + 1 < max_ops: queue.append(new_rule + (ops + 1,)) queue.append((gate_seq, (), 0)) rules.add((gate_seq, ())) while len(queue) > 0: left, right, ops = queue.popleft() # Do a LL new_rule = ll_op(left, right) process_new_rule(new_rule, ops) # Do a LR new_rule = lr_op(left, right) process_new_rule(new_rule, ops) # Do a RL new_rule = rl_op(left, right) process_new_rule(new_rule, ops) # Do a RR new_rule = rr_op(left, right) process_new_rule(new_rule, ops) if return_as_muls: # Convert each rule as tuples into a rule as muls mul_rules = set() for rule in rules: left, right = rule mul_rules.add((Mul(*left), Mul(*right))) rules = mul_rules return rules
def test__parallel_dict_from_expr_no_gens(): assert parallel_dict_from_expr([x*y, Integer(3)]) == \ ([{(1, 1): Integer(1)}, {(0, 0): Integer(3)}], (x, y)) assert parallel_dict_from_expr([x*y, 2*z, Integer(3)]) == \ ([{(1, 1, 0): Integer( 1)}, {(0, 0, 1): Integer(2)}, {(0, 0, 0): Integer(3)}], (x, y, z))
def test_fcode_Integer(): assert fcode(Integer(67)) == "67" assert fcode(Integer(-1)) == "-1"