def fid(target_unitary, error_channel_operators, density_matrix, symbolic=1): """Fidelity between a unitary gate and a non-necessarily unitary gate, for a given initial density matrix. This is later used when calculating the worst case fidelity. Notice that the input format of the general channel is a list of Kraus operators instead of a process matrix. The input format of the target unitary is just the matrix itself, not its process matrix. symbolic = 1 is the case when the the input matrices are sympy, while symbolic = 0 is used when the input matrices are numpy. """ V, K, rho = target_unitary, error_channel_operators, density_matrix if symbolic: Tra = (((V.H)*K[0])*rho).trace() fid = Tra*(fun.conjugate(Tra)) for i in range(1,len(K)): Tra = (((V.H)*K[i])*rho).trace() fid += Tra*(fun.conjugate(Tra)) return fid.expand() else: Tra = np.trace((V.H)*K[0]*rho) fid = Tra*(Tra.conjugate()) for i in range(1,len(K)): Tra = np.trace((V.H)*K[i]*rho) fid += Tra*(Tra.conjugate()) return fid
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x)**cos(x)) == "Sin[x]^Cos[x]" assert mcode(conjugate(x)) == "Conjugate[x]" assert mcode(Max(x, y, z) * Min(y, z)) == "Max[x, y, z]*Min[y, z]" assert mcode(fresnelc(x)) == "FresnelC[x]" assert mcode(fresnels(x)) == "FresnelS[x]" assert mcode(gamma(x)) == "Gamma[x]" assert mcode(uppergamma(x, y)) == "Gamma[x, y]" assert mcode(polygamma(x, y)) == "PolyGamma[x, y]" assert mcode(loggamma(x)) == "LogGamma[x]" assert mcode(erf(x)) == "Erf[x]" assert mcode(erfc(x)) == "Erfc[x]" assert mcode(erfi(x)) == "Erfi[x]" assert mcode(erf2(x, y)) == "Erf[x, y]" assert mcode(expint(x, y)) == "ExpIntegralE[x, y]" assert mcode(erfcinv(x)) == "InverseErfc[x]" assert mcode(erfinv(x)) == "InverseErf[x]" assert mcode(erf2inv(x, y)) == "InverseErf[x, y]" assert mcode(Ei(x)) == "ExpIntegralEi[x]" assert mcode(Ci(x)) == "CosIntegral[x]" assert mcode(li(x)) == "LogIntegral[x]" assert mcode(Si(x)) == "SinIntegral[x]" assert mcode(Shi(x)) == "SinhIntegral[x]" assert mcode(Chi(x)) == "CoshIntegral[x]" assert mcode(beta(x, y)) == "Beta[x, y]" assert mcode(factorial(x)) == "Factorial[x]" assert mcode(factorial2(x)) == "Factorial2[x]" assert mcode(subfactorial(x)) == "Subfactorial[x]" assert mcode(FallingFactorial(x, y)) == "FactorialPower[x, y]" assert mcode(RisingFactorial(x, y)) == "Pochhammer[x, y]" assert mcode(catalan(x)) == "CatalanNumber[x]" assert mcode(harmonic(x)) == "HarmonicNumber[x]" assert mcode(harmonic(x, y)) == "HarmonicNumber[x, y]"
def test_Trace(): assert isinstance(Trace(A), Trace) assert not isinstance(Trace(A), MatrixExpr) raises(ShapeError, lambda: Trace(C)) assert trace(eye(3)) == 3 assert trace(Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])) == 15 assert adjoint(Trace(A)) == trace(Adjoint(A)) assert conjugate(Trace(A)) == trace(Adjoint(A)) assert transpose(Trace(A)) == Trace(A) A / Trace(A) # Make sure this is possible # Some easy simplifications assert trace(Identity(5)) == 5 assert trace(ZeroMatrix(5, 5)) == 0 assert trace(OneMatrix(1, 1)) == 1 assert trace(OneMatrix(2, 2)) == 2 assert trace(OneMatrix(n, n)) == n assert trace(2*A*B) == 2*Trace(A*B) assert trace(A.T) == trace(A) i, j = symbols('i j') F = FunctionMatrix(3, 3, Lambda((i, j), i + j)) assert trace(F) == (0 + 0) + (1 + 1) + (2 + 2) raises(TypeError, lambda: Trace(S.One)) assert Trace(A).arg is A assert str(trace(A)) == str(Trace(A).doit()) assert Trace(A).is_commutative is True
def test_transpose(): Sq = MatrixSymbol('Sq', n, n) assert transpose(A) == Transpose(A) assert Transpose(A).shape == (m, n) assert Transpose(A * B).shape == (l, n) assert transpose(Transpose(A)) == A assert isinstance(Transpose(Transpose(A)), Transpose) assert adjoint(Transpose(A)) == Adjoint(Transpose(A)) assert conjugate(Transpose(A)) == Adjoint(A) assert Transpose(eye(3)).doit() == eye(3) assert Transpose(S(5)).doit() == S(5) assert Transpose(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]]) assert transpose(trace(Sq)) == trace(Sq) assert trace(Transpose(Sq)) == trace(Sq) assert Transpose(Sq)[0, 1] == Sq[1, 0] assert Transpose(A * B).doit() == Transpose(B) * Transpose(A)
def construct(self): for i in range(self.size): for j in range(i, self.size): self.H[i,j] = sympy.symbols(self.entry_template.format(i,j,"Re"), real=True) if j > i: self.H[i,j] += sympy.I * sympy.symbols(self.entry_template.format(i,j,"Im"), real=True) self.H[j,i] = conjugate(self.H[i,j])
def test_trace(): assert isinstance(Trace(A), Trace) assert not isinstance(Trace(A), MatrixExpr) raises(ShapeError, lambda: Trace(C)) assert Trace(eye(3)) == 3 assert Trace(Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])) == 15 assert adjoint(Trace(A)) == Trace(Adjoint(A)) assert conjugate(Trace(A)) == Trace(Adjoint(A)) assert transpose(Trace(A)) == Trace(A) A / Trace(A) # Make sure this is possible # Some easy simplifications assert Trace(Identity(5)) == 5 assert Trace(ZeroMatrix(5, 5)) == 0 assert Trace(2 * A * B) == 2 * Trace(A * B) assert Trace(A.T) == Trace(A) i, j = symbols('i j') F = FunctionMatrix(3, 3, Lambda((i, j), i + j)) assert Trace(F).doit() == (0 + 0) + (1 + 1) + (2 + 2) raises(TypeError, lambda: Trace(S.One)) assert Trace(A).arg is A
def run(): A, lam, x, om, t = symbols("A lam x om t", real=True) assumptions.assume.global_assumptions.add(Q.positive(lam)) psi = Function('psi')(x) psi = A * exp(-lam * Abs(x) - I * om * t) ret = simplify(conjugate(psi) * psi) # 简化 print(ret) # A**2*exp(-2*lam*Abs(x)) ret = integrate(conjugate(psi) * psi, (x, -oo, oo)) print(ret) # (A**2/lam, Abs(arg(lam)) < pi/2) # 所以求得系数 A = sqrt(lam) psi = sqrt(lam) * exp(-lam * Abs(x) - I * om * t) # 已知波函数了,求解<x>和<x**2> print(integrate(conjugate(psi) * x * psi, (x, -oo, oo))) # 0 print(integrate(conjugate(psi) * x * x * psi, (x, -oo, oo))) #
def test_adjoint(): Sq = MatrixSymbol('Sq', n, n) assert Adjoint(A).shape == (m, n) assert Adjoint(A*B).shape == (l, n) assert adjoint(Adjoint(A)) == A assert isinstance(Adjoint(Adjoint(A)), Adjoint) assert conjugate(Adjoint(A)) == Transpose(A) assert transpose(Adjoint(A)) == Adjoint(Transpose(A)) assert Adjoint(eye(3)).doit() == eye(3) assert Adjoint(S(5)).doit() == S(5) assert Adjoint(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]]) assert adjoint(Trace(Sq)) == conjugate(Trace(Sq)) assert Trace(adjoint(Sq)) == conjugate(Trace(Sq)) assert Adjoint(Sq)[0, 1] == conjugate(Sq[1, 0]) assert Adjoint(A*B).doit() == Adjoint(B) * Adjoint(A)
def test_adjoint(): Sq = MatrixSymbol("Sq", n, n) assert Adjoint(A).shape == (m, n) assert Adjoint(A * B).shape == (l, n) assert adjoint(Adjoint(A)) == A assert isinstance(Adjoint(Adjoint(A)), Adjoint) assert conjugate(Adjoint(A)) == Transpose(A) assert transpose(Adjoint(A)) == Adjoint(Transpose(A)) assert Adjoint(eye(3)).doit() == eye(3) assert Adjoint(S(5)).doit() == S(5) assert Adjoint(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]]) assert adjoint(trace(Sq)) == conjugate(trace(Sq)) assert trace(adjoint(Sq)) == conjugate(trace(Sq)) assert Adjoint(Sq)[0, 1] == conjugate(Sq[1, 0]) assert Adjoint(A * B).doit() == Adjoint(B) * Adjoint(A)
def test_Function_change_name(): assert mcode(abs(x)) == "abs(x)" assert mcode(ceiling(x)) == "ceil(x)" assert mcode(arg(x)) == "angle(x)" assert mcode(im(x)) == "imag(x)" assert mcode(re(x)) == "real(x)" assert mcode(conjugate(x)) == "conj(x)" assert mcode(chebyshevt(y, x)) == "chebyshevT(y, x)" assert mcode(chebyshevu(y, x)) == "chebyshevU(y, x)" assert mcode(laguerre(x, y)) == "laguerreL(x, y)" assert mcode(Chi(x)) == "coshint(x)" assert mcode(Shi(x)) == "sinhint(x)" assert mcode(Ci(x)) == "cosint(x)" assert mcode(Si(x)) == "sinint(x)" assert mcode(li(x)) == "logint(x)" assert mcode(loggamma(x)) == "gammaln(x)" assert mcode(polygamma(x, y)) == "psi(x, y)" assert mcode(RisingFactorial(x, y)) == "pochhammer(x, y)" assert mcode(DiracDelta(x)) == "dirac(x)" assert mcode(DiracDelta(x, 3)) == "dirac(3, x)" assert mcode(Heaviside(x)) == "heaviside(x)" assert mcode(Heaviside(x, y)) == "heaviside(x, y)"
def conjugate(self): return conjugate(self)
def _get_const_characteristic_eq_sols(r, func, order): r""" Returns the roots of characteristic equation of constant coefficient linear ODE and list of collectterms which is later on used by simplification to use collect on solution. The parameter `r` is a dict of order:coeff terms, where order is the order of the derivative on each term, and coeff is the coefficient of that derivative. """ x = func.args[0] # First, set up characteristic equation. chareq, symbol = S.Zero, Dummy('x') for i in r.keys(): if type(i) == str or i < 0: pass else: chareq += r[i] * symbol**i chareq = Poly(chareq, symbol) # Can't just call roots because it doesn't return rootof for unsolveable # polynomials. chareqroots = roots(chareq, multiple=True) if len(chareqroots) != order: chareqroots = [rootof(chareq, k) for k in range(chareq.degree())] chareq_is_complex = not all(i.is_real for i in chareq.all_coeffs()) # Create a dict root: multiplicity or charroots charroots = defaultdict(int) for root in chareqroots: charroots[root] += 1 # We need to keep track of terms so we can run collect() at the end. # This is necessary for constantsimp to work properly. collectterms = [] gensols = [] conjugate_roots = [] # used to prevent double-use of conjugate roots # Loop over roots in theorder provided by roots/rootof... for root in chareqroots: # but don't repoeat multiple roots. if root not in charroots: continue multiplicity = charroots.pop(root) for i in range(multiplicity): if chareq_is_complex: gensols.append(x**i * exp(root * x)) collectterms = [(i, root, 0)] + collectterms continue reroot = re(root) imroot = im(root) if imroot.has(atan2) and reroot.has(atan2): # Remove this condition when re and im stop returning # circular atan2 usages. gensols.append(x**i * exp(root * x)) collectterms = [(i, root, 0)] + collectterms else: if root in conjugate_roots: collectterms = [(i, reroot, imroot)] + collectterms continue if imroot == 0: gensols.append(x**i * exp(reroot * x)) collectterms = [(i, reroot, 0)] + collectterms continue conjugate_roots.append(conjugate(root)) gensols.append(x**i * exp(reroot * x) * sin(abs(imroot) * x)) gensols.append(x**i * exp(reroot * x) * cos(imroot * x)) # This ordering is important collectterms = [(i, reroot, imroot)] + collectterms return gensols, collectterms
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x)**cos(x)) == "Sin[x]^Cos[x]" assert mcode(conjugate(x)) == "Conjugate[x]"
def _eval_adjoint(self): return conjugate(self.arg)
def _entry(self, i, j, **kwargs): return conjugate(self.arg._entry(j, i, **kwargs))
def _entry(self, i, j): return conjugate(self.arg._entry(j, i))
def test_Function(): assert mcode(f(x, y, z)) == "f[x, y, z]" assert mcode(sin(x) ** cos(x)) == "Sin[x]^Cos[x]" assert mcode(conjugate(x)) == "Conjugate[x]"
def _eval_trace(self): from sympy.matrices.expressions.trace import Trace return conjugate(Trace(self.arg))
def _eval_transpose(self): return conjugate(self.arg)
def ctranspose(matrix): return matrix.transpose().applyfunc(lambda i: conjugate(i))