Python sympy.I Examples
The following are 30
code examples of sympy.I().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sympy
, or try the search function
.
Example #1
Source File: sympy_interface.py From fungrim with MIT License | 6 votes |
def sympy_to_grim(expr, **kwargs): import sympy assert isinstance(expr, sympy.Expr) if expr.is_Integer: return Expr(int(expr)) if expr.is_Symbol: return Expr(symbol_name=expr.name) if expr is sympy.pi: return Pi if expr is sympy.E: return ConstE if expr is sympy.I: return ConstI if expr.is_Add: args = [sympy_to_grim(x, **kwargs) for x in expr.args] return Add(*args) if expr.is_Mul: args = [sympy_to_grim(x, **kwargs) for x in expr.args] return Mul(*args) if expr.is_Pow: args = [sympy_to_grim(x, **kwargs) for x in expr.args] b, e = args return Pow(b, e) raise NotImplementedError("converting %s to Grim", type(expr))
Example #2
Source File: test_sympy_conv.py From symengine.py with MIT License | 6 votes |
def test_conv10(): A = DenseMatrix(1, 4, [Integer(1), Integer(2), Integer(3), Integer(4)]) assert (A._sympy_() == sympy.Matrix(1, 4, [sympy.Integer(1), sympy.Integer(2), sympy.Integer(3), sympy.Integer(4)])) B = DenseMatrix(4, 1, [Symbol("x"), Symbol("y"), Symbol("z"), Symbol("t")]) assert (B._sympy_() == sympy.Matrix(4, 1, [sympy.Symbol("x"), sympy.Symbol("y"), sympy.Symbol("z"), sympy.Symbol("t")]) ) C = DenseMatrix(2, 2, [Integer(5), Symbol("x"), function_symbol("f", Symbol("x")), 1 + I]) assert (C._sympy_() == sympy.Matrix([[5, sympy.Symbol("x")], [sympy.Function("f")(sympy.Symbol("x")), 1 + sympy.I]]))
Example #3
Source File: test_sympy_conv.py From symengine.py with MIT License | 6 votes |
def test_conv10b(): A = sympy.Matrix([[sympy.Symbol("x"), sympy.Symbol("y")], [sympy.Symbol("z"), sympy.Symbol("t")]]) assert sympify(A) == DenseMatrix(2, 2, [Symbol("x"), Symbol("y"), Symbol("z"), Symbol("t")]) B = sympy.Matrix([[1, 2], [3, 4]]) assert sympify(B) == DenseMatrix(2, 2, [Integer(1), Integer(2), Integer(3), Integer(4)]) C = sympy.Matrix([[7, sympy.Symbol("y")], [sympy.Function("g")(sympy.Symbol("z")), 3 + 2*sympy.I]]) assert sympify(C) == DenseMatrix(2, 2, [Integer(7), Symbol("y"), function_symbol("g", Symbol("z")), 3 + 2*I])
Example #4
Source File: resolver_test.py From Cirq with Apache License 2.0 | 6 votes |
def test_value_of(): assert not bool(cirq.ParamResolver()) r = cirq.ParamResolver({'a': 0.5, 'b': 0.1, 'c': 1 + 1j}) assert bool(r) assert r.value_of('x') == sympy.Symbol('x') assert r.value_of('a') == 0.5 assert r.value_of(sympy.Symbol('a')) == 0.5 assert r.value_of(0.5) == 0.5 assert r.value_of(sympy.Symbol('b')) == 0.1 assert r.value_of(0.3) == 0.3 assert r.value_of(sympy.Symbol('a') * 3) == 1.5 assert r.value_of(sympy.Symbol('b') / 0.1 - sympy.Symbol('a')) == 0.5 assert r.value_of(sympy.pi) == np.pi assert r.value_of(2 * sympy.pi) == 2 * np.pi assert r.value_of('c') == 1 + 1j assert r.value_of(sympy.I * sympy.pi) == np.pi * 1j
Example #5
Source File: pauli.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _apply_operator_SigmaY(self, op, **options): return I * SigmaZKet(1) if self.n == 0 else (-I) * SigmaZKet(0)
Example #6
Source File: sympy_backend.py From Blueqat with Apache License 2.0 | 5 votes |
def __init__(self): try: lazy_import() except ImportError: raise ImportError('sympy_unitary requires sympy. Please install before call this option.') theta, phi, lambd = symbols('theta phi lambd') self.theta = theta self.phi = phi self.lambd = lambd self.SYMPY_GATE = { '_C0': Matrix([[1, 0], [0, 0]]), '_C1': Matrix([[0, 0], [0, 1]]), 'X': Matrix([[0, 1], [1, 0]]), 'Y': Matrix([[0, -I], [I, 0]]), 'Z': Matrix([[1, 0], [0, -1]]), 'H': Matrix([[1, 1], [1, -1]]) / sqrt(2), 'RX': Matrix([[cos(theta / 2), -I * sin(theta / 2)], [-I * sin(theta / 2), cos(theta / 2)]]), 'RY': Matrix([[cos(theta / 2), -sin(theta / 2)], [sin(theta / 2), cos(theta / 2)]]), 'RZ': Matrix([[exp(-I * theta / 2), 0], [0, exp(I * theta / 2)]]), 'PHASE': Matrix([[1, 0], [0, exp(I * theta)]]), 'U1': Matrix([[exp(-I * lambd / 2), 0], [0, exp(I * lambd / 2)]]), 'U2': Matrix([ [exp(-I * (phi + lambd) / 2) / sqrt(2), -exp(-I * (phi - lambd) / 2) / sqrt(2)], [exp(I * (phi - lambd) / 2) / sqrt(2), exp(I * (phi + lambd) / 2) / sqrt(2)]]), 'U3': Matrix([ [exp(-I * (phi + lambd) / 2) * cos(theta / 2), -exp(-I * (phi - lambd) / 2) * sin(theta / 2)], [exp(I * (phi - lambd) / 2) * sin(theta / 2), exp(I * (phi + lambd) / 2) * cos(theta / 2)]]), }
Example #7
Source File: represent.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _sympy_to_scalar(e): """Convert from a sympy scalar to a Python scalar.""" if isinstance(e, Expr): if e.is_Integer: return int(e) elif e.is_Float: return float(e) elif e.is_Rational: return float(e) elif e.is_Number or e.is_NumberSymbol or e == I: return complex(e) raise TypeError('Expected number, got: %r' % e)
Example #8
Source File: test_pauli.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_pauli_operators_commutator(): assert Commutator(sx, sy).doit() == 2 * I * sz assert Commutator(sy, sz).doit() == 2 * I * sx assert Commutator(sz, sx).doit() == 2 * I * sy
Example #9
Source File: test_pauli.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_pauli_operators_commutator_with_labels(): assert Commutator(sx1, sy1).doit() == 2 * I * sz1 assert Commutator(sy1, sz1).doit() == 2 * I * sx1 assert Commutator(sz1, sx1).doit() == 2 * I * sy1 assert Commutator(sx2, sy2).doit() == 2 * I * sz2 assert Commutator(sy2, sz2).doit() == 2 * I * sx2 assert Commutator(sz2, sx2).doit() == 2 * I * sy2 assert Commutator(sx1, sy2).doit() == 0 assert Commutator(sy1, sz2).doit() == 0 assert Commutator(sz1, sx2).doit() == 0
Example #10
Source File: test_pauli.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_pauli_operators_multiplication(): assert sx * sx == 1 assert sy * sy == 1 assert sz * sz == 1 assert sx * sy == I * sz assert sy * sz == I * sx assert sz * sx == I * sy assert sy * sx == - I * sz assert sz * sy == - I * sx assert sx * sz == - I * sy
Example #11
Source File: test_dagger.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_matrix(): x = symbols('x') m = Matrix([[I, x*I], [2, 4]]) assert Dagger(m) == m.H
Example #12
Source File: test_qapply.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_commutator(): assert qapply(Commutator(Jx, Jy)*Jz*po) == I*hbar**3*po assert qapply(Commutator(J2, Jz)*Jz*po) == 0 assert qapply(Commutator(Jz, Foo('F'))*po) == 0 assert qapply(Commutator(Foo('F'), Jz)*po) == 0
Example #13
Source File: test_represent.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #14
Source File: test_represent.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_scalar_numpy(): if not np: skip("numpy not installed.") assert represent(Integer(1), format='numpy') == 1 assert represent(Float(1.0), format='numpy') == 1.0 assert represent(1.0 + I, format='numpy') == 1.0 + 1.0j
Example #15
Source File: test_represent.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_scalar_scipy_sparse(): if not np: skip("numpy not installed.") if not scipy: skip("scipy not installed.") assert represent(Integer(1), format='scipy.sparse') == 1 assert represent(Float(1.0), format='scipy.sparse') == 1.0 assert represent(1.0 + I, format='scipy.sparse') == 1.0 + 1.0j
Example #16
Source File: test_sympy.py From Blueqat with Apache License 2.0 | 5 votes |
def test_sympy_backend_for_one_qubit_gate(): E = eye(2) X = Matrix([[0, 1], [1, 0]]) Y = Matrix([[0, -I], [I, 0]]) Z = Matrix([[1, 0], [0, -1]]) H = Matrix([[1, 1], [1, -1]]) / sqrt(2) T = Matrix([[1, 0], [0, exp(I*pi/4)]]) S = Matrix([[1, 0], [0, exp(I*pi/2)]]) x, y, z = symbols('x, y, z') RX = Matrix([[cos(x / 2), -I * sin(x / 2)], [-I * sin(x / 2), cos(x / 2)]]) RY = Matrix([[cos(y / 2), -sin(y / 2)], [sin(y / 2), cos(y / 2)]]) RZ = Matrix([[exp(-I * z / 2), 0], [0, exp(I * z / 2)]]) actual_1 = Circuit().x[0, 1].y[1].z[2].run(backend="sympy_unitary") expected_1 = reduce(TensorProduct, [Z, Y * X, X]) assert actual_1 == expected_1 actual_2 = Circuit().y[0].z[3].run(backend="sympy_unitary") expected_2 = reduce(TensorProduct, [Z, E, E, Y]) assert actual_2 == expected_2 actual_3 = Circuit().x[0].z[3].h[:].t[1].s[2].run(backend="sympy_unitary") expected_3 = reduce(TensorProduct, [H * Z, S * H, T * H, H * X]) assert actual_3 == expected_3 actual_4 = Circuit().rx(-pi / 2)[0].rz(pi / 2)[1].ry(pi)[2].run(backend="sympy_unitary") expected_4 = reduce(TensorProduct, [RY, RZ, RX]).subs([[x, -pi / 2], [y, pi], [z, pi / 2]]) assert actual_4 == expected_4
Example #17
Source File: test_sympy.py From Blueqat with Apache License 2.0 | 5 votes |
def test_cygate(): u1 = Circuit().cy[1, 0].to_unitary() u2 = Matrix([ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, -I], [0, 0, I, 0]]) assert simplify(u1 - u2) == zeros(4)
Example #18
Source File: test_sympy.py From Blueqat with Apache License 2.0 | 5 votes |
def test_rxgate(): t = symbols('t') u = Circuit().rx(t)[0].to_unitary() expected = exp(-I * t / 2 * Circuit().x[0].to_unitary()) assert simplify(u - expected) == zeros(2)
Example #19
Source File: test_sympy.py From Blueqat with Apache License 2.0 | 5 votes |
def test_rygate(): t = symbols('t') u = Circuit().ry(t)[0].to_unitary() expected = simplify(exp(-I * t / 2 * Circuit().y[0].to_unitary())) assert simplify(u - expected) == zeros(2)
Example #20
Source File: test_sympy.py From Blueqat with Apache License 2.0 | 5 votes |
def test_rxxgate(): t = symbols('t') u = Circuit().rxx(t)[1, 0].to_unitary() expected = simplify(exp(-I * t / 2 * Circuit().x[0, 1].to_unitary())) assert simplify(u - expected) == zeros(4)
Example #21
Source File: test_sympy.py From Blueqat with Apache License 2.0 | 5 votes |
def test_ryygate(): t = symbols('t') u = Circuit().ryy(t)[1, 0].to_unitary() expected = simplify(exp(-I * t / 2 * Circuit().y[0, 1].to_unitary())) assert simplify(u - expected) == zeros(4)
Example #22
Source File: test_sympy.py From Blueqat with Apache License 2.0 | 5 votes |
def test_rzzgate(): t = symbols('t') u = Circuit().rzz(t)[1, 0].to_unitary() expected = simplify(exp(-I * t / 2 * Circuit().z[0, 1].to_unitary())) assert simplify(u - expected) == zeros(4)
Example #23
Source File: sympy_backend.py From Blueqat with Apache License 2.0 | 5 votes |
def lazy_import(): global eye, symbols, sin, cos, exp, sqrt, pi, I, Matrix, sympy_gate, TensorProduct, sympy from sympy import eye, symbols, sin, cos, exp, sqrt, pi, I, Matrix from sympy.physics.quantum import TensorProduct import sympy
Example #24
Source File: sympy_interface.py From fungrim with MIT License | 5 votes |
def grim_to_sympy(expr, **kwargs): import sympy assert isinstance(expr, Expr) if expr.is_integer(): return sympy.sympify(int(expr)) if expr.is_symbol(): if expr == Pi: return sympy.pi if expr == ConstI: return sympy.I if expr == ConstE: return sympy.E return sympy.Symbol(str(expr)) head = expr.head() args = [grim_to_sympy(x, **kwargs) for x in expr.args()] if head in (Pos, Parentheses, Brackets, Braces, AngleBrackets, Logic): x, = args return x if expr.head() == Add: return sympy.Add(*args) if expr.head() == Mul: return sympy.Mul(*args) if expr.head() == Neg: x, = args return -x if expr.head() == Sub: a, b = args return a - b if expr.head() == Div: p, q = args return p / q if expr.head() == Pow: b, e = args return b ** e if expr.head() == Sqrt: x, = args return sympy.sqrt(x) raise NotImplementedError("converting %s to SymPy", expr.head())
Example #25
Source File: pauli.py From sympsi with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _eval_commutator_SigmaZ(self, other, **hints): if self.name != other.name: return Integer(0) else: return 2 * I * SigmaX(self.name)
Example #26
Source File: test_fcode.py From Computable with MIT License | 5 votes |
def test_fcode_complex(): assert fcode(I) == " cmplx(0,1)" x = symbols('x') assert fcode(4*I) == " cmplx(0,4)" assert fcode(3 + 4*I) == " cmplx(3,4)" assert fcode(3 + 4*I + x) == " cmplx(3,4) + x" assert fcode(I*x) == " cmplx(0,1)*x" assert fcode(3 + 4*I - x) == " cmplx(3,4) - x" x = symbols('x', imaginary=True) assert fcode(5*x) == " 5*x" assert fcode(I*x) == " cmplx(0,1)*x" assert fcode(3 + x) == " x + 3"
Example #27
Source File: linear_EM_waves.py From galgebra with BSD 3-Clause "New" or "Revised" License | 5 votes |
def EM_Waves_in_Geom_Calculus_Complex(): #Print_Function() X = (t,x,y,z) = symbols('t x y z',real=True) g = '1 # # 0,# 1 # 0,# # 1 0,0 0 0 -1' coords = (xE,xB,xk,t) = symbols('x_E x_B x_k t',real=True) (EBkst,eE,eB,ek,et) = Ga.build('e_E e_B e_k e_t',g=g,coords=coords) i = EBkst.i E,B,k,w = symbols('E B k omega',real=True) F = E*eE*et+i*B*eB*et K = k*ek+w*et X = xE*eE+xB*eB+xk*ek+t*et KX = (K|X).scalar() F = F*exp(I*KX) g = EBkst.g print('g =', g) print('X =', X) print('K =', K) print('K|X =', KX) print('F =', F) gradF = EBkst.grad*F gradF = gradF.simplify() (gradF).Fmt(3,'grad*F = 0') gradF = gradF.subs({g[0,1]:0,g[0,2]:0,g[1,2]:0}) KX = KX.subs({g[0,1]:0,g[0,2]:0,g[1,2]:0}) print(r'%\mbox{Substituting }e_{E}\cdot e_{B} = e_{E}\cdot e_{k} = e_{B}\cdot e_{k} = 0') (gradF / (I*exp(I*KX))).Fmt(3,r'%\lp\bm{\nabla}F\rp/\lp ie^{iK\cdot X}\rp = 0') return
Example #28
Source File: test_sympy_conv.py From symengine.py with MIT License | 5 votes |
def test_conv9(): x = Symbol("x") y = Symbol("y") assert (I)._sympy_() == sympy.I assert (2*I+3)._sympy_() == 2*sympy.I+3 assert (2*I/5+Integer(3)/5)._sympy_() == 2*sympy.I/5+sympy.S(3)/5 assert (x*I+3)._sympy_() == sympy.Symbol("x")*sympy.I + 3 assert (x+I*y)._sympy_() == sympy.Symbol("x") + sympy.I*sympy.Symbol("y")
Example #29
Source File: test_sympy_conv.py From symengine.py with MIT License | 5 votes |
def test_conv9b(): x = Symbol("x") y = Symbol("y") assert sympify(sympy.I) == I assert sympify(2*sympy.I+3) == 2*I+3 assert sympify(2*sympy.I/5+sympy.S(3)/5) == 2*I/5+Integer(3)/5 assert sympify(sympy.Symbol("x")*sympy.I + 3) == x*I+3 assert sympify(sympy.Symbol("x") + sympy.I*sympy.Symbol("y")) == x+I*y
Example #30
Source File: test_sympy_conv.py From symengine.py with MIT License | 5 votes |
def test_mpc(): if have_mpc: a = ComplexMPC('1', '2', 100) b = sympy.Float(1, 29) + sympy.Float(2, 29) * sympy.I assert sympify(b) == a assert b == a._sympy_()