def mp_get_Psca_from_expansion(k, R, dist, zeta): csqrs = [] rhos = [] lbda = mp_get_lagrange_mul_from_expansion(k, R, dist, zeta, csqrs, rhos) limit = len(csqrs) print(limit) print(lbda) i = 0 lhs = mpmath.mpf(0.0) rhs = lhs oldlhs = lhs oldrhs = rhs tol = 1e-6 while (i < limit): if (i == len(csqrs)): #here the l quantum number is i+1 cNl = mp_get_normalized_RgNl0_coeff_for_zdipole_field( k, R, dist, i + 1) csqrs.append(mpmath.re(mpmath.conj(cNl) * cNl)) rhos.append(mp_rho_N(i + 1, k * R)) denom = 1.0 + lbda * zeta * rhos[i] lhs += zeta * csqrs[i] * (1.0 + lbda) * ( 1.0 + 2 * zeta * rhos[i] * lbda) / 4 / denom**2 rhs += zeta * csqrs[i] * (1.0 + lbda) * lbda / 4 / denom**2 i += 1 if (i == limit) and ((lhs - oldlhs) > tol * lhs or (rhs - oldrhs) > tol * rhs): limit = int(np.ceil(limit * 1.2)) oldlhs = lhs oldrhs = rhs return 0.5 * k * (lhs - rhs)
def cholesky_decomposition(input_matrix: np.matrix.__class__): n_dims = input_matrix.shape[0] L = input_matrix.copy() pivots = np.empty(3) for i in range(n_dims): for j in range(i, n_dims): accumulator = L[i, j] for k in range(i-1, -1, -1): accumulator -= L[i, k]*conj(L[j, k]) if i == j: if accumulator.real <= 0: raise ValueError('Singular or non-hermitian matrix') pivots[i] = sqrt(accumulator.real) L[i, i] = pivots[i] else: L[j, i] = accumulator/pivots[i] return np.tril(L)
def conj(): global a, listindex a[listindex] = mpmath.conj(a[listindex])
def getConjugate( n ): return conj( n )
def sp(a,b): return mpmath.exp(-(abs(a)**2 + abs(b)**2)/2 + mpmath.conj(a)*b)
def get_unary(self, item): return { **{ k: calc2.UnaryOperator(s, f, 80) for k, s, f in [ ('abs', 'abs', abs), ('fac', 'fac', mpmath.factorial), ('sqrt', 'sqrt', mpmath.sqrt), ('_/', 'sqrt', mpmath.sqrt), ('√', 'sqrt', mpmath.sqrt), ('ln', 'ln', mpmath.ln), ('lg', 'log10', mpmath.log10), ('exp', 'e^', mpmath.exp), ('floor', 'floor', mpmath.floor), ('ceil', 'ceil', mpmath.ceil), ('det', 'det', mpmath.det), ] }, 'pcn': calc2.UnaryOperator('a%', lambda x: x / 100, 80), '+': calc2.UnaryOperator('(+)', lambda x: x, 0), '-': calc2.UnaryOperator('+/-', lambda x: -x, 80), 'conj': calc2.UnaryOperator( 'conj', lambda x: x.conjugate() if isinstance(x, mpmath.matrix) else mpmath.conj(x), 80), '~': calc2.UnaryOperator( 'conj', lambda x: x.conjugate() if isinstance(x, mpmath.matrix) else mpmath.conj(x), 80), 'O': calc2.UnaryOperator( '[O]', lambda x: mpmath.zeros(*OpList.__analyse_as_pair(x)), 80), 'I': calc2.UnaryOperator( '[I]', lambda x: mpmath.ones(*OpList.__analyse_as_pair(x)), 80), 'E': calc2.UnaryOperator('[E]', lambda x: mpmath.eye(int(x)), 80), 'diag': calc2.UnaryOperator( '[diag]', lambda x: mpmath.diag(OpList.__analyse_list(x)), 80), 'log': calc2.UnaryOperator( 'logbA', lambda x: OpList.__log(*OpList.__analyse_pair(x)), 80), 'tran': calc2.UnaryOperator( '[T]', lambda x: x.transpose() if isinstance(x, mpmath.matrix) else x, 80), **{ k: calc2.UnaryOperator(k, (lambda u: lambda x: u(self._drg2r(x)))(v), 80) for k, v in { 'sin': mpmath.sinpi, 'cos': mpmath.cospi, 'tan': lambda x: mpmath.sinpi(x) / mpmath.cospi(x), 'cot': lambda x: mpmath.cospi(x) / mpmath.sinpi(x), 'sec': lambda x: 1 / mpmath.cospi(x), 'csc': lambda x: 1 / mpmath.sinpi(x), }.items() }, **{ k: calc2.UnaryOperator(k, (lambda u: lambda x: self._r2drg(1 / v(x)))(v), 80) for k, v in { 'asin': mpmath.asin, 'acos': mpmath.acos, 'atan': mpmath.atan, 'acot': mpmath.acot, 'asec': mpmath.asec, 'acsc': mpmath.acsc }.items() }, **{ k: calc2.UnaryOperator(k, v, 80) for k, v in { 'sinh': mpmath.sinh, 'cosh': mpmath.cosh, 'tanh': mpmath.tanh, 'coth': mpmath.coth, 'sech': mpmath.sech, 'csch': mpmath.csch, 'asinh': mpmath.asinh, 'acosh': mpmath.acosh, 'atanh': mpmath.atanh, 'acoth': mpmath.acoth, 'asech': mpmath.asech, 'acsch': mpmath.acsch }.items() } }[item]
def getConjugateOperator(n): return conj(n)
print len(psi_t) for i, psi in enumerate(psi_t): pylab.subplot(2,3,i+1) b = psi.basis # Calculate <a> a = b.destroy() exp_a.append(np.dot(psi.dual().conj(), a*psi)) exp_a_numpy = np.array(exp_a, dtype=complex) # Calculate Q-function Q = qo.qfunc(psi, X, Y) Q_numpy = np.array(Q.tolist(), dtype=float) # Basis states states = np.array(psi.basis.states, dtype=complex) z = np.array(log(np.abs(psi.dual().conj()*psi)), dtype=float) pylab.scatter(states.real, states.imag, c=z) pylab.imshow(Q_numpy, origin="lower", extent=(0,12,0,12)) pylab.contour(X,Y,Q) pylab.plot(exp_a_numpy.real, exp_a_numpy.imag) sp0 = b.coherent_scalar_product(mp.mpf("2"), b.states) sp1 = b.coherent_scalar_product(mp.conj(exp_a[-1]), b.states) error_sv0 = 2*(1-(np.dot(sp0, psi)).real) error_sv1 = 2*(1-(np.dot(sp1, psi)).real) print(error_sv0, error_sv1) print(exp_a_numpy) pylab.show()