def collapse(c): tmp = Dagger(c) * c / 2 return spre(c) * spost(Dagger(c)) - spre(tmp) - spost(tmp)
from sympy.core.rules import Transform fold_conjugates = Transform( lambda f: 2 * re(f.args[0]), lambda f: isinstance(f, Add) and len(f.args) == 2 and f.args[1] == f.args[0].conjugate()) fold_conjugates_2 = Transform( lambda f: f.args[0] + 2 * re(f.args[1]), lambda f: isinstance(f, Add) and len(f.args) == 3 and f.args[2] == f.args[1].conjugate()) I, Q, U, V, l, m, n = symbols('I Q U V l m n', real=True) Jxxi, Jxyi, Jyxi, Jyyi = symbols("Jxxi Jxyi Jyxi Jyyi") Ji = Matrix([[Jxxi, Jxyi], [Jyxi, Jyyi]]) Jxxj, Jxyj, Jyxj, Jyyj = symbols("Jxxj Jxyj Jyxj Jyyj") Jj = Matrix([[Jxxj, Jxyj], [Jyxj, Jyyj]]) Jxx, Jxy, Jyx, Jyy = symbols("Jxx Jxy Jyx Jyy") J = Matrix([[Jxx, Jxy], [Jyx, Jyy]]) Vxxij, Vxyij, Vyxij, Vyyij = symbols("Vxxij Vxyij Vyxij Vyyij") Vij = Matrix([[Vxxij, Vxyij], [Vyxij, Vyyij]]) Cij = J * Vij * Dagger(J) pprint(Cij) pprint(collect(Cij, Vxxij)) #print(Cij.det())
def test_unitary_XGate(): x = XGate(1, 2) x_dagger = Dagger(x) assert (x * x_dagger == 1)
def test_unitary_ZGate(): z = ZGate(1, 2) z_dagger = Dagger(z) assert (z * z_dagger == 1)
def test_normal_ordered_form(): a = BosonOp('a') b = BosonOp('b') c = FermionOp('c') d = FermionOp('d') assert normal_ordered_form(Dagger(a) * a) == Dagger(a) * a assert normal_ordered_form(a * Dagger(a)) == 1 + Dagger(a) * a assert normal_ordered_form(a ** 2 * Dagger(a)) == \ 2 * a + Dagger(a) * a ** 2 assert normal_ordered_form(a ** 3 * Dagger(a)) == \ 3 * a ** 2 + Dagger(a) * a ** 3 assert normal_ordered_form(Dagger(c) * c) == Dagger(c) * c assert normal_ordered_form(c * Dagger(c)) == 1 - Dagger(c) * c assert normal_ordered_form(c**2 * Dagger(c)) == Dagger(c) * c**2 assert normal_ordered_form(c ** 3 * Dagger(c)) == \ c ** 2 - Dagger(c) * c ** 3
def test_hermitian_YGate(): y = YGate(1, 2) y_dagger = Dagger(y) assert (y == y_dagger)
def test_normal_order(): a = BosonOp("a") c = FermionOp("c") assert normal_order(a * Dagger(a)) == Dagger(a) * a assert normal_order(Dagger(a) * a) == Dagger(a) * a assert normal_order(a * Dagger(a)**2) == Dagger(a)**2 * a assert normal_order(c * Dagger(c)) == -Dagger(c) * c assert normal_order(Dagger(c) * c) == Dagger(c) * c assert normal_order(c * Dagger(c)**2) == Dagger(c)**2 * c
def test_pauli_operators_adjoint(): assert Dagger(sx) == sx assert Dagger(sy) == sy assert Dagger(sz) == sz
def eval_state(self, state): return qapply(Dagger(state) * self.args[0] * state, dagger=True).doit()
def test_normal_ordered_form(): a = BosonOp("a") c = FermionOp("c") assert normal_ordered_form(Dagger(a) * a) == Dagger(a) * a assert normal_ordered_form(a * Dagger(a)) == 1 + Dagger(a) * a assert normal_ordered_form(a**2 * Dagger(a)) == 2 * a + Dagger(a) * a**2 assert normal_ordered_form(a**3 * Dagger(a)) == 3 * a**2 + Dagger(a) * a**3 assert normal_ordered_form(Dagger(c) * c) == Dagger(c) * c assert normal_ordered_form(c * Dagger(c)) == 1 - Dagger(c) * c assert normal_ordered_form(c**2 * Dagger(c)) == Dagger(c) * c**2 assert normal_ordered_form(c**3 * Dagger(c)) == c**2 - Dagger(c) * c**3
from sympy import symbols from sympy.physics.quantum import AntiCommutator from sympy.physics.quantum import Operator, Dagger x, y = symbols('x,y') A = Operator('A') B = Operator('B') ac = AntiCommutator(A, B) ac print(ac) #doit multiple the anti A*B + B*A ac.doit() #define un autre anti ab = AntiCommutator(3 * x * A, x * y * B) #Adjoint operations applied to the anticommutator are properly applied to the arguments: aa = Dagger(AntiCommutator(A, B))
def density_matrix(state): return state @ Dagger(state)
# Construction of U # Basis states s0 = sp.Matrix([[1], [0]]) s1 = sp.Matrix([[0], [1]]) # s0 = Ket(0) # s1 = Ket(1) s00 = TensorProduct(s0, s0) s01 = TensorProduct(s0, s1) s10 = TensorProduct(s1, s0) s11 = TensorProduct(s1, s1) # Bell states _sp = (s01 + s10) / sp.sqrt(2) # (|01> + |10>)/√2 _sm = (s01 - s10) / sp.sqrt(2) # (|01> - |10>)/√2 o0000 = TensorProduct(s00, Dagger(s00)) o1111 = TensorProduct(s11, Dagger(s11)) opp = TensorProduct(_sp, Dagger(_sp)) omm = TensorProduct(_sm, Dagger(_sm)) I = sp.I def U00(t): return sp.Matrix( [[1, 0, 0, 0], [0, (sp.exp(-I * t) + 1) / 2, (sp.exp(-I * t) - 1) / 2, 0], [0, (sp.exp(-I * t) - 1) / 2, (sp.exp(-I * t) + 1) / 2, 0], [0, 0, 0, sp.exp(-I * t)]]) def U11(t):
eigenvectors_tp = np.transpose(np.array(eigenvectors)) N = int(2 * J + 1) N_sq = N * N #To calculate the reduced density matrix of the system (C_Dagger * C) for vector in eigenvectors_tp: C = np.zeros((N, N), dtype=np.complex64) dummy_index = 0 for i in range(N_sq): C[dummy_index][i % N] = vector[i] if i % N == N - 1: dummy_index = dummy_index + 1 C = np.matrix(C) DensityMatrix1 = C * Dagger(C) Lambda = eigvalsh(DensityMatrix1) Sv = 0.0 for x in Lambda: if x > 0: Sv = Sv - x * np.log(x) Sv_array.append(Sv) #plt.title('J=20, epsilon=5.0') plt.axis([0, N_sq, -0.5, 4]) plt.xlabel('Eigenvalues') plt.ylabel('Entanglement') plt.plot(Sv_array, 'bo') plt.show()
s11 = s12*s21 s22 = s21*s12 s33 = s31*s13 delo,delm=sym.symbols('delta_o delta_mu', real=True) #delao, delam =sym.symbols('delta_a_o delta_a_mu') #detunings between atom and cavity gamma13,gamma23,gamma2d,gamma3d,nbath,gammamu=sym.symbols('gamma_13 gamma_23 gamma_2d gamma_3d n_b gamma_mu', real=True, negative=False) #energy decay for atom levels Omega=sym.symbols('Omega', real=False, negative=False) #pump Rabi frequency rho11, rho12, rho13, rho21, rho22, rho23, rho31, rho32, rho33=sym.symbols('rho_11 rho_12 rho_13 rho_21 rho_22 rho_23 rho_31 rho_32 rho_33') #Density matrix elements a, b = sym.symbols('a b') #classical amplitudes of the optical and microwave fields #ar,ai=sym.symbols('a_r a_i', real=True) go, gm=sym.symbols('g_o, g_mu',real=False, negative=False) #coupling strengths for optical and microwave fields lam=sym.symbols('lambda') H=Omega*s32+gm*s21*b+go*s31*a H=H+Dagger(H) H=H+(delo)*s33+(delm)*s22 LH=-I*spre(H)+I*spost(H) L21 = gammamu*(nbath+1)*collapse(s12) L12 = gammamu*nbath*collapse(s21) L32 = gamma23*collapse(s23) L31 = gamma13*collapse(s13) L22 = gamma2d*collapse(s22) L33 = gamma3d*collapse(s33) L=LH + L21 + L12 + L32 + L31 + L22 + L33 #define the density matrix in square and row form #the row form is so the Liovillian in matrix form can be acted on it
from sympy import * from sympy.physics.quantum import Dagger x, y, z, t = symbols('x y z t') k, m, n = symbols('k m n', integer=True) f, g, h = symbols('f g h', cls=Function) from sympy import I, Matrix, symbols from sympy.physics.quantum import TensorProduct m1 = Matrix([[1, 2], [3, 4]]) m2 = Matrix([[1, 0], [0, 1]]) TensorProduct(m1, m2) A = Symbol('A', commutative=False) B = Symbol('B', commutative=False) C = Symbol('C', commutative=False) D = Symbol('D', commutative=False) tp = TensorProduct(A, B) #A⊗B Dagger(tp) #adjoint #Dagger(A)xDagger(B) l = TensorProduct(A + B, C) #(A + B)xC l.expand(tensorproduct=True) #AxC + BxC e = TensorProduct(A, B) * TensorProduct(C, D) #AxB*CxD tensor_product_simp(e) #(A*C)x(B*D)
def _eval_rewrite_as_gg(self,a,t): return (1+I*hh_plus(a,t)-Dagger(gg_plus(a,a,t)))
from sympy import * from sympy.physics.quantum import TensorProduct from sympy.physics.quantum import Dagger subsystem_base = [Matrix([1, 0, 0]), Matrix([0, 1, 0]), Matrix([0, 0, 1])] base = [None] * 9 for i in range(9): base[i] = [None] * 9 for i in range(3): for j in range(3): base[i][j] = TensorProduct(subsystem_base[i], subsystem_base[j]) sigma = 1 / 3 * (TensorProduct(base[0][1], Dagger(base[0][1])) + TensorProduct(base[1][2], Dagger(base[1][2])) + TensorProduct(base[2][0], Dagger(base[2][0]))) v = Matrix([[1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1]]) psi = 1 / sqrt(3) * (base[0][0] + base[1][1] + base[2][2]) alpha = Symbol("a") rho = 2 / 7 * TensorProduct( psi, Dagger(psi)) + alpha / 7 * sigma + (5 - alpha) / 7 * v * sigma * v
def _eval_rewrite_as_gg(self,a,t): return (1-I*hh_minus(a,t)-Dagger(gg_minus(a,a,t)))
def test_normal_order(): a = BosonOp('a') b = BosonOp('b') c = FermionOp('c') d = FermionOp('d') assert normal_order(a * Dagger(a)) == Dagger(a) * a assert normal_order(Dagger(a) * a) == Dagger(a) * a assert normal_order(a * Dagger(a)**2) == Dagger(a)**2 * a assert normal_order(c * Dagger(c)) == -Dagger(c) * c assert normal_order(Dagger(c) * c) == Dagger(c) * c assert normal_order(c * Dagger(c)**2) == Dagger(c)**2 * c
def _evaluate_second_order_rules(self): """ Evaluates second order terms in terms of the line shape function """ a = Wild('a') b = Wild('b') w = Wild('w') t1 = Wild('t1') t2 = Wild('t2') """ Combinations of hh hh_plus * hh_plus ---> hh_minus * hh_minus ---> hh_minus**2 ---> hh_plus * hh_minus ---> hh_minus * hh_plus ---> """ A = self.replace(w*hh_plus(a,t1)*hh_plus(b,t2), \ w*(gg(a,b,t1)-gg(a,b,t1-t2)+conjugate(gg(a,b,t2)))) A = A.replace(w*hh_plus(a,t1)**2,w*(gg(a,a,t1)+conjugate(gg(a,a,t1)))) A = A.replace(w*hh_minus(a,t1)*hh_minus(b,t2), \ w*(conjugate(gg(b,a,t1)) \ -conjugate(gg(b,a,t1-t2))+gg(b,a,t2))) A = A.replace(w*hh_minus(a,t1)**2,w*(gg(a,a,t1)+conjugate(gg(a,a,t1)))) A = A.replace(w*hh_plus(a,t1)*hh_minus(b,t2), \ w*(-gg(a,b,t1) \ +gg(a,b,t1+t2)-gg(a,b,t2))) A = A.replace(w*hh_minus(a,t1)*hh_plus(b,t2), \ w*(conjugate(-gg(b,a,t1) \ +gg(b,a,t1+t2)-gg(b,a,t2)))) """ Replacement rules for ggs First daggered ggs ( and that the normal ones gg_plus ---> gg \hat{g}^{(-)}_{ab}(t) ---> g^{*}_{ab}(t) """ A = A.replace(w*Dagger(gg_plus(a,b,t1)),w*conjugate(gg(a,b,t1))) A = A.replace(w*Dagger(gg_minus(a,b,t1)),w*gg(a,b,t1)) A = A.replace(w*gg_plus(a,b,t1),w*gg(a,b,t1)) A = A.replace(w*gg_minus(a,b,t1),w*conjugate(gg(a,b,t1))) """ Replacement rules for dVs and their combinations with hh """ A = A.replace(w*dV(a,t1)*dV(b,t2),w*g2(a,b,t1-t2)) A = A.replace(w*dV(a,t1)**2,w*g2(a,a,0)) A = A.replace(w*dV(a,t1)*hh_plus(b,t2),w*(-g1(a,b,t1-t2)+g1(a,b,t1))) A = A.replace(w*dV(a,t1)*hh_minus(b,t2),w*(g1(a,b,t1+t2)-g1(a,b,t1))) A = A.replace(w*hh_plus(a,t1)*dV(b,t2), \ w*(g1(a,b,t1-t2)+conjugate(g1(b,a,t2)))) A = A.replace(w*hh_minus(a,t1)*dV(b,t2), w*conjugate(g1(b,a,t1+t2)-g1(b,a,t2))) #A = A.replace(w*hh_plus(a,t1)*dV(b,t2), \ # w*(g1(a,b,t1-t2)-conjugate(g1(a,b,t2)))) #A = A.replace(w*hh_minus(a,t1)*dV(b,t2), # w*conjugate(g1(a,b,t1+t2)-g1(a,b,t2))) return A
def test_hermitian_ZGate(): z = ZGate(1, 2) z_dagger = Dagger(z) assert (z == z_dagger)
def lindblad_dissipator(a, rho): """ Lindblad dissipator """ return (a * rho * Dagger(a) - rho * Dagger(a) * a / 2 - Dagger(a) * a * rho / 2)
def test_unitary_YGate(): y = YGate(1, 2) y_dagger = Dagger(y) assert (y * y_dagger == 1)
def operator_lindblad_dissipator(a, rho): """ Lindblad operator dissipator """ return (Dagger(a) * rho * a - rho * Dagger(a) * a / 2 - Dagger(a) * a * rho / 2)
real=True, negative=False) #energy decay for atom levels Omega = sym.symbols('Omega', real=False, negative=False) #pump Rabi frequency rho11, rho12, rho13, rho21, rho22, rho23, rho31, rho32, rho33 = sym.symbols( 'rho_11 rho_12 rho_13 rho_21 rho_22 rho_23 rho_31 rho_32 rho_33' ) #Density matrix elements a, b = sym.symbols( 'a b') #classical amplitudes of the optical and microwave fields #ar,ai=sym.symbols('a_r a_i', real=True) go, gm = sym.symbols( 'g_o, g_mu', real=False, negative=False) #coupling strengths for optical and microwave fields lam = sym.symbols('lambda') H_sys = Omega * s32 + gm * s21 * b + go * s31 * a H_sys = H_sys + Dagger(H_sys) H_sys = H_sys + (delao - delo) * s33 + (delam - delm) * s22 LH = -I * spre(H_sys) + I * spost(H_sys) L21 = gammamu * (nbath + 1) * collapse(s12) L12 = gammamu * nbath * collapse(s21) L32 = gamma23 * collapse(s23) L31 = gamma13 * collapse(s13) L22 = gamma2d * collapse(s22) L33 = gamma3d * collapse(s33) L = LH + L21 + L12 + L32 + L31 + L22 + L33 L = L.row_insert(0, Matrix([[1, 0, 0, 0, 1, 0, 0, 0, 1]])) L.row_del(1)
from sympy.physics.quantum import Commutator, Dagger, Operator from sympy.abc import x, y A = Operator('A') B = Operator('B') C = Operator('C') comm = Commutator(A, B) #[A,B] comm.doit() #A*B - B*A comm = Commutator(B, A) comm #-[A,B] Commutator(3 * x * A, x * y * B) #3*x**2*y*[A,B] Commutator(A + B, C).expand(commutator=True) #[A,C] + [B,C] Commutator(A, B + C).expand(commutator=True) #[A,B] + [A,C] Commutator(A * B, C).expand(commutator=True) #[A,C]*B + A*[B,C] Commutator(A, B * C).expand(commutator=True) #[A,B]*C + B*[A,C] Dagger(Commutator(A, B)) #-[Dagger(A),Dagger(B)]
def test_hermitian_XGate(): x = XGate(1, 2) x_dagger = Dagger(x) assert (x == x_dagger)
def nested_commutator_cc(nested_level, cluster_levels, max_cu=3, max_n_open=6, min_n_open=0, for_commutator=True, expand_hole=True, single_reference=False, unitary=False, n_process=1, hermitian_tensor=True): """ Compute the BCH nested commutator in coupled cluster theory. :param nested_level: the level of nested commutator :param cluster_levels: a list of integers for cluster operator, e.g., [1,2,3] for T1 + T2 + T3 :param max_cu: max value of cumulant allowed for contraction :param max_n_open: the max number of open indices for contractions kept for return :param min_n_open: the min number of open indices for contractions kept for return :param for_commutator: compute only non-zero terms for commutators if True :param expand_hole: expand HoleDensity to Kronecker minus Cumulant if True :param single_reference: use single-reference amplitudes if True :param unitary: use unitary formalism if True :param n_process: number of processes launched for tensor canonicalization :param hermitian_tensor: assume tensors being Hermitian if True :return: a list of contracted canonicalized Term objects """ if not isinstance(nested_level, int): raise ValueError("Invalid nested_level (must be an integer)") if not isinstance(cluster_levels, Iterable): raise ValueError("Invalid type for cluster_operator") if not all(isinstance(t, int) for t in cluster_levels): raise ValueError( "Invalid content in cluster_operator (must be all integers)") scale_factor = 1.0 / factorial(nested_level) out = [] hole_label = 'c' if single_reference else 'h' particle_label = 'v' if single_reference else 'p' # symbolic evaluate nested commutator t = sum(Operator(f'T{i}') for i in cluster_levels) h = HermitianOperator('H1') + HermitianOperator('H2') a = t - Dagger(t) if unitary else t for term in sympy_nested_commutator_recursive(nested_level, h, a).doit().expand().args: coeff, tensors = term.as_coeff_mul() factor = scale_factor * int(coeff) tensor_names = [] for tensor in tensors: if isinstance(tensor, Pow): if isinstance(tensor.base, Dagger): tensor_names += ['X' + str(tensor.base.args[0])[1:]] * int( tensor.exp) else: tensor_names += [str(tensor.base)] * int(tensor.exp) else: if isinstance(tensor, Dagger): tensor_names.append('X' + str(tensor.args[0])[1:]) else: tensor_names.append(str(tensor)) list_of_terms = [] start = 0 for name in tensor_names: real_name, n_body = name[0], int(name[1:]) if real_name == 'T' or real_name == 'X': list_of_terms.append( cluster_operator(n_body, start=start, excitation=(real_name == 'T'), hole_label=hole_label, particle_label=particle_label)) start += n_body else: list_of_terms.append(hamiltonian_operator(n_body)) out += contract_terms(list_of_terms, max_cu, max_n_open, min_n_open, factor, for_commutator, expand_hole, n_process, hermitian_tensor) return combine_terms(out)