def update(self, rho): """ Calculate the probability function for the given state of an harmonic oscillator (as density matrix) """ if isket(rho): rho = ket2dm(rho) self.data = np.zeros(len(self.xvecs[0]), dtype=complex) M, N = rho.shape for m in range(M): k_m = pow(self.omega / pi, 0.25) / \ sqrt(2 ** m * factorial(m)) * \ exp(-self.xvecs[0] ** 2 / 2.0) * \ np.polyval(hermite(m), self.xvecs[0]) for n in range(N): k_n = pow(self.omega / pi, 0.25) / \ sqrt(2 ** n * factorial(n)) * \ exp(-self.xvecs[0] ** 2 / 2.0) * \ np.polyval(hermite(n), self.xvecs[0]) self.data += np.conjugate(k_n) * k_m * rho.data[m, n]
def update_rho(self, rho): """ calculate probability distribution for quadrature measurement outcomes given a two-mode density matrix """ X1, X2 = np.meshgrid(self.xvecs[0], self.xvecs[1]) p = np.zeros((len(self.xvecs[0]), len(self.xvecs[1])), dtype=complex) N = rho.dims[0][0] M1 = np.zeros((N, N, len(self.xvecs[0]), len(self.xvecs[1])), dtype=complex) M2 = np.zeros((N, N, len(self.xvecs[0]), len(self.xvecs[1])), dtype=complex) for m in range(N): for n in range(N): M1[m,n] = exp(-1j * self.theta1 * (m - n)) / \ sqrt(pi * 2 ** (m + n) * factorial(n) * factorial(m)) * \ exp(-X1 ** 2) * np.polyval(hermite(m), X1) * np.polyval(hermite(n), X1) M2[m,n] = exp(-1j * self.theta2 * (m - n)) / \ sqrt(pi * 2 ** (m + n) * factorial(n) * factorial(m)) * \ exp(-X2 ** 2) * np.polyval(hermite(m), X2) * np.polyval(hermite(n), X2) for n1 in range(N): for n2 in range(N): i = state_number_index([N, N], [n1, n2]) for p1 in range(N): for p2 in range(N): j = state_number_index([N, N], [p1, p2]) p += M1[n1, p1] * M2[n2, p2] * rho.data[i, j] self.data = p
def HG_split_diode_coefficient_numerical(n1,n2): """ Compute beam coefficient between mode n1 and n2 on a split photo detector using numerical integration, tested up to n1,n2 = 40. This is primarily used by finesse_split_photodiode()""" A = 2.0 * np.sqrt(2.0/np.pi) * np.sqrt(1.0 / (2.0**(n1+n2) * factorial(n1) * factorial(n2))) f = lambda x: hermite(n1)(np.sqrt(2.0)*x) * math.exp(-2.0*x*x) * hermite(n2)(np.sqrt(2.0)*x) c_n1n2= quad(f, 0.0, np.Inf, epsabs=1e-10, epsrel=1e-10, limit=200) return A*c_n1n2[0]
def gen_hermite_array(n): pad_len = n # initial polynomial h = pad(hermite(0).coeffs,pad_len) for i in range(1,n): h_temp = pad(hermite(i).coeffs,pad_len) h = numpy.append(h,h_temp) return h
def genHermiteMode(x, m, W, a1, b1, a2, b2): if a1 <= x <= b1: return ((1 / (np.sqrt(np.pi) * np.power(2, m - 0.5) * fact(m) * W))** 0.5) * hermite(m)(np.sqrt(2) * (x / W)) * np.exp(-x**2 / W**2) elif a2 <= x <= b2: return ((1 / (np.sqrt(np.pi) * np.power(2, m - 0.5) * fact(m) * W))** 0.5) * hermite(m)(np.sqrt(2) * (x / W)) * np.exp(-x**2 / W**2) else: return 0
def eval_z_x(self, z, x): kz = self.ksi_z * z kx = self.ksi_x * x norm = np.sqrt(self.ksi_z * self.ksi_x) / np.sqrt( (2**(self.n_z + self.n_x)) * special.factorial(self.n_z) * special.factorial(self.n_x) * u.pi) gaussian = np.exp(-((kz**2) + (kx**2)) / 2) hermite = special.hermite(self.n_z)(kz) * special.hermite(self.n_x)(kx) return (norm * gaussian * hermite).astype(np.complex128)
def eigenstates(nx,ny,wx,wy,X,Y,m=1,hbar=1): betax = np.sqrt(m*wx/hbar) resx = np.sqrt(betax)*np.exp(-(m*wx*X**2)/(2*hbar))*hermite(nx)(betax*X)*1.0/np.sqrt(2**nx*factorial(nx)) betay = np.sqrt(m*wy/hbar) resy = np.sqrt(betay)*np.exp(-(m*wy*Y**2)/(2*hbar))*hermite(ny)(betay*Y)*1.0/np.sqrt(2**ny*factorial(ny)) res = resx*resy*1/np.sqrt(np.pi)+0j normalizer = abs(res*res).sum() res = res/np.sqrt(normalizer) return res
def __init__(self, qx, qy=None, n=0, m=0): self._qx = copy.deepcopy(qx) self._2pi_qrt = math.pow(2.0/math.pi, 0.25) if qy is None: self._qy = copy.deepcopy(qx) else: self._qy = copy.deepcopy(qy) self._n = int(n) self._m = int(m) self._hn = hermite(self._n) self._hm = hermite(self._m) self._calc_constants()
def __init__(self, n, xshift=0, yshift=0): self.n = n self.xshift = xshift self.yshift = yshift self.E = n + 0.5 self.coef = 1 / sqrt(2**n * factorial(n)) * (1 / pi)**(1 / 4) self.hermite = hermite(n)
def mapmri_phi_1d(n, q, mu): r""" One dimensional MAPMRI basis function from [1]_ Eq. 4. Parameters ------- n : unsigned int order of the basis q : array, shape (N,) points in the q-space in which evaluate the basis mu : float scale factor of the basis References ---------- .. [1] Ozarslan E. et. al, "Mean apparent propagator (MAP) MRI: A novel diffusion imaging method for mapping tissue microstructure", NeuroImage, 2013. """ qn = 2 * np.pi * mu * q H = hermite(n)(qn) i = np.complex(0, 1) f = factorial(n) k = i**(-n) / np.sqrt(2**(n) * f) phi = k * np.exp(-qn**2 / 2) * H return phi
def mapmri_psi_1d(n, x, mu): r""" One dimensional MAPMRI propagator basis function from [1]_ Eq. 10. Parameters ---------- n : unsigned int order of the basis x : array, shape (N,) points in the r-space in which evaluate the basis mu : float scale factor of the basis References ---------- .. [1] Ozarslan E. et. al, "Mean apparent propagator (MAP) MRI: A novel diffusion imaging method for mapping tissue microstructure", NeuroImage, 2013. """ H = hermite(n)(x / mu) f = factorial(n) k = 1 / (np.sqrt(2**(n + 1) * np.pi * f) * mu) psi = k * np.exp(-x**2 / (2 * mu**2)) * H return psi
def hermite(self, n, nx, a, b, c, amplitude, x): """ --------------------------------------------------------------------------- constructe 1d hermite mode with hermite function. ref: "coherent-mode representations in optics" args: n - 1d hermite mode index. nx - sampling. a - ref.equation 1.61 b - ref.equation 1.61 c - ref.equation 1.61 amplitude - the gaussian sigma of spectral intensity. return: 1d hermite mode --------------------------------------------------------------------------- """ from scipy import special ratio = self.ratio(n, nx, a, b, c, amplitude) # ref.equation 1.59 hermite = ((2 * c / np.pi)**0.25 * (1 / np.sqrt(2**n * special.factorial(n))) * special.hermite(n)(x * np.sqrt(2 * c)) * np.exp(-c * x**2)) return ratio, hermite
def get_eigenfunction(self, number=0): from scipy.special import hermite from math import factorial, sqrt, pi n = number w = self.frequency return lambda x: 0j + hermite(n)(sqrt(w) * x) * _np.exp(- 0.5 * w * x ** 2) * \ ((w / pi) ** 0.25) / sqrt(2 ** n * factorial(n))
def sho_evec(n, params): m = params["C"] ω = 1/np.sqrt(params["C"]*params["Lo"]) norm = 1/np.sqrt(2**n * factorial(n))*(m*ω/π/ħ)**0.25 ψ = lambda Q: norm * np.exp(-m*ω*Φₒ**2*Q**2/4/π**2/2/ħ)*hermite(n)(Φₒ*Q/2/π) + 0j #* np.exp(1j*E_val*t/ħ) return ψ
def calculate_harmonic_oscillator_eigenfunctions( eigenfunction_positions, eigenfunction_index, harmonic_oscillator_width=1.0): """ :param eigenfunction_positions: x-values in a Numpy array :param eigenfunction_index: n :param harmonic_oscillator_width: a :return: psi_n(x) from x_minimum to x_maximum """ # Assuming k = 4 / (2p) [2p = latus rectum of parabola], ω^2 = k/m (m = 1) force_constant = 1. / harmonic_oscillator_width angular_frequency = np.sqrt(force_constant) # A_n = (m ω / π ħ)^(1/4) (1 / sqrt(2^n n!)), m = ħ = 1 normalization_factor = np.power(angular_frequency / np.pi, 1 / 4) normalization_factor /= np.sqrt( np.power(2., eigenfunction_index) * factorial(eigenfunction_index)) print('A = {:.5f}'.format(normalization_factor)) # ξ = sqrt(m ω / ħ) x, m = ħ = 1 xi = np.sqrt(angular_frequency) * eigenfunction_positions # psi_n(x) = A_n H_n(xi) exp(-xi^2 / 2) harmonic_oscillator_eigenfunction_values = normalization_factor * hermite( eigenfunction_index)(xi) harmonic_oscillator_eigenfunction_values *= np.exp(-0.5 * xi**2) return harmonic_oscillator_eigenfunction_values
def Single_prof(self, nn, z, pos): """Returns the Hermite-Gaussian profile in a single x or y direction.""" gouy = sp.sqrt( sp.exp(1j * (2 * nn + 1) * self.Gouy(z)) / (2**nn * math.factorial(nn) * self.Spot_size(z))) expon = sp.exp(-1j * self.k * z - 1j * self.k * pos**2 / (2 * self.R_curve(z)) - pos**2 / self.Spot_size(z)**2) return (2 / sp.pi)**(0.25) * gouy * hermite(nn)(pos) * expon
def phi(v, R, alpha): y = R * np.sqrt(alpha) Nv = ((alpha / np.pi)**0.25) / np.sqrt(2**v * np.math.factorial(v)) Hv = hermite(v) sum = 0.0 for i, h in enumerate(Hv.coeffs[::-1]): sum += h * (y**i) return (-1)**v * sum * Nv * np.exp(-y**2 / 2)
def _positionWaveFunc(n, q, theta, hbar = 1): #lim = np.sqrt(n * 2) + 3 # heuristic n_ = np.arange(n)[:, np.newaxis] H = hermite(n_, q) A = (np.pi * hbar) ** 0.25 / np.sqrt(2 ** n_ * fact(n)) psi = A * H * np.exp(-q ** 2 / 2 / hbar) * np.exp(-1j * n_ * theta) C = np.sum(np.abs(psi) ** 2, axis=1) + 0j # sum of squeres psi = psi / np.sqrt(C)[:, np.newaxis] # normalized wave functions return psi
def phin(n, x): """Routine to get shapelets basis functions at some NumPy array of position `x`.""" # Get the H_n function from scipy. hn = spec.hermite(n) # Evaluate it at our values of x. hn_x = hn(x) # Now put in the exp factor. phi_n_x = hn_x * np.exp(-(x**2) / 2.) # Finally, normalize it. phi_n_x /= np.sqrt((2.**n) * np.sqrt(np.pi) * math.factorial(n)) return phi_n_x
def harmonic_wavefcn(n): """ Return the normalized wavefunction of a harmonic oscillator, where $n$ denotes that it is an n-th excited state, or ground state for $n=0$. :param n: quantum number of the excited state :return: a normalized wavefunction :type n: int :rtype: function """ const = 1/sqrt(2**n * tail_factorial(n)) * 1/sqrt(sqrt(pi)) return lambda x: const * np.exp(-0.5*x*x) * hermite(n)(x)
def __call__(self, x): """ Warning: for large enough n (>= ~60) this will fail due to n! overflowing. """ norm = ((self.mass * self.omega / (u.pi * u.hbar)) **(1 / 4)) / (np.float64(2**(self.n / 2)) * np.sqrt(np.float64(special.factorial(self.n)))) exp = np.exp(-self.mass * self.omega * (x**2) / (2 * u.hbar)) herm = special.hermite(self.n)( np.sqrt(self.mass * self.omega / u.hbar) * x) return self.amplitude * (norm * exp * herm).astype(np.complex128)
def _dog_function(order, x_in): """ Returns ------- float DOG function. """ p_hpoly = hermite(order)[int(x_in / np.power(2, 0.5))] herm = p_hpoly / (np.power(2, float(order) / 2)) return ((-1)**(order + 1)) / np.sqrt(gamma(order + 0.5)) * herm
def harmonic_psi(x, m, om, n=0): s = (cnst.hbar / (m * om)) ** 0.5 Hn = hermite(n) return ( 1.0 / (2 ** n * factorial(n)) ** 0.5 * (np.pi ** -0.25) * (s ** -0.5) * np.exp(-0.5 * (x / s) ** 2) * Hn(x / s) )
def harmonic_oscillator_eigenfunc(x_arr, n, omega): """ evaluate n-th energy eigenfunction of simple harmonic oscillator an atomic unit is used """ hermite_n = hermite(n) _func = np.exp(-omega / 2.0 * np.square(x_arr)) * hermite_n( np.sqrt(omega) * x_arr) _func *= 1.0 / np.sqrt((2**n) * factorial(n)) * (omega / np.pi)**(0.25) return _func
def compute_C_beta(self, beta): #First, create hermite objects for the 1D components H_beta_1(), H_beta_2(), etc H_beta_is = [hermite(beta_i).c for beta_i in beta] #Then, make a list of the summands: #the evaluted h_beta for each source (indexed by j), #times the delta fraction coefficient summands = [(self.delta/delta_j)**( nd_abs(beta)/2)*evaluate_nd_hermite_function(H_beta_is, powers,hermite_coeff) \ for (delta_j,powers,hermite_coeff) in \ zip(self.delta_j_set,self.powers,self.hermite_coeffs)] output = (1. / (nd_factorial(beta))) * np.sum(summands) return output
def I(a, b, umax=5): # sqrt(pi)/2 * int_{-inf}^{inf} exp(-t^2) erf(at+b) dt # http://alumnus.caltech.edu/~amir/bivariate.pdf # n.b. need a<1 for convergence uarr = np.arange(umax + 1) I1 = np.pi * (0.5 - Phi(-np.sqrt(2) * b)) H = np.array([special.hermite(2 * u + 1)(b) for u in uarr]) # vectorize if hasattr(a, 'shape'): uarr = np.broadcast_to(uarr, a.shape[::-1] + (umax + 1, )).T summand = (a / 2.)**(2 * uarr + 2) / special.gamma(uarr + 2) * H I2 = np.sqrt(np.pi) * np.exp(-b**2.) * np.sum(summand, axis=0) return I1 - I2
def diffFreqOverlap(Ln, Lm, d): """ Ln and Lm are lists where L[0] is the state number, L[1] is the frequency in wavenumbers. d is the change in normal coordinate (in bohr) """ # If the excited state frequency (Ln[1]) is greater than the ground state # frequency (Lm[1]) then we must swap Ln and Lm for the program, but then # take the absolute value of the result. if (Ln[1] > Lm[1]): Ln, Lm = Lm, Ln n = Ln[0] m = Lm[0] wn_wavenumbers = Ln[1] wm_wavenumbers = Lm[1] wn = wn_wavenumbers / 8065.5 / 27.2116 wm = wm_wavenumbers / 8065.5 / 27.2116 f = wn / wm # w = wm # F is the (massless) force constant for the mode. But which w? # F = w ** 2 #convertedQSquared = deltaQ**2/(6.02214*(10**23) * 9.1094*(10**-28)) convertedQSquared = d**2 # X is defined as such in Siders, Marcus 1981 Average frequency? X = convertedQSquared / 2 P0 = (-1)**(m + n) # Should data be alternating plus minus? P1 = sqrt(2 * sqrt(f) / (1.0 + f)) P2 = ((2**(m + n) * factorial(m) * factorial(n))**(-0.5) * exp(-X * f / (1.0 + f))) P3 = (abs((1 - f) / (1 + f)))**((float(m) + n) / 2.0) l = min(m, n) P4 = 0 for i in range(l + 1): #In Siders and Marcus's 'F_n(x)' is equal to my G(x) G = iHermite(n - i) # G = hermite(n-i) H = hermite(m - i) P4 += ((factorial(m) * factorial(n) / (float(factorial(i)) * float(factorial(m - i)) * float(factorial(n - i)))) * (4 * sqrt(f) / abs(1 - f))**i * H(sqrt(abs(2 * X * f / (1 - f**2)))) * G(f * sqrt(abs(2 * X / (1 - f**2))))) fc = P0 * P1 * P2 * P3 * P4 return fc
def GaussHermite(n, m, A, w0, Fin): """ Fout = GaussHermite(m, n, A, w0, Fin) :ref:`Substitutes a Hermite-Gauss mode (beam waist) in the field. <GaussHermite>` :math:`F_{m,n}(x,y,z=0) = A H_m\\left(\\dfrac{\\sqrt{2}x}{w_0}\\right)H_n\\left(\\dfrac{\\sqrt{2}y}{w_0}\\right)e^{-\\frac{x^2+y^2}{w_0^2}}` Args:: m, n: mode indices A: Amplitude w0: Guaussian spot size parameter in the beam waist (1/e amplitude point) Fin: input field Returns:: Fout: output field (N x N square array of complex numbers). Reference:: A. Siegman, "Lasers", p. 642 """ Fout = Field.copy(Fin) Y, X = Fout.mgrid_cartesian #Y = Y - y_shift #X = X - x_shift sqrt2w0 = _np.sqrt(2.0) / w0 w02 = w0 * w0 Fout.field = A * hermite(m)(sqrt2w0 * X) * hermite(n)( sqrt2w0 * Y) * _np.exp(-(X * X + Y * Y) / w02) return Fout
def update_rho(self, rho): """ calculate probability distribution for quadrature measurement outcomes given a two-mode density matrix """ X1, X2 = np.meshgrid(self.xvecs[0], self.xvecs[1]) p = np.zeros((len(self.xvecs[0]), len(self.xvecs[1])), dtype=complex) N = rho.dims[0][0] M1 = np.zeros((N, N, len(self.xvecs[0]), len(self.xvecs[1])), dtype=complex) M2 = np.zeros((N, N, len(self.xvecs[0]), len(self.xvecs[1])), dtype=complex) for m in range(N): for n in range(N): M1[m, n] = exp(-1j * self.theta1 * (m - n)) / \ sqrt(pi * 2 ** (m + n) * factorial(n) * factorial(m)) * \ exp(-X1 ** 2) * np.polyval( hermite(m), X1) * np.polyval(hermite(n), X1) M2[m, n] = exp(-1j * self.theta2 * (m - n)) / \ sqrt(pi * 2 ** (m + n) * factorial(n) * factorial(m)) * \ exp(-X2 ** 2) * np.polyval( hermite(m), X2) * np.polyval(hermite(n), X2) for n1 in range(N): for n2 in range(N): i = state_number_index([N, N], [n1, n2]) for p1 in range(N): for p2 in range(N): j = state_number_index([N, N], [p1, p2]) p += M1[n1, p1] * M2[n2, p2] * rho.data[i, j] self.data = p
def update_psi(self, psi): """ calculate probability distribution for quadrature measurement outcomes given a two-mode wavefunction """ X1, X2 = np.meshgrid(self.xvecs[0], self.xvecs[1]) p = np.zeros((len(self.xvecs[0]), len(self.xvecs[1])), dtype=complex) N = psi.dims[0][0] for n1 in range(N): kn1 = exp(-1j * self.theta1 * n1) / \ sqrt(sqrt(pi) * 2 ** n1 * factorial(n1)) * \ exp(-X1 ** 2 / 2.0) * np.polyval(hermite(n1), X1) for n2 in range(N): kn2 = exp(-1j * self.theta2 * n2) / \ sqrt(sqrt(pi) * 2 ** n2 * factorial(n2)) * \ exp(-X2 ** 2 / 2.0) * np.polyval(hermite(n2), X2) i = state_number_index([N, N], [n1, n2]) p += kn1 * kn2 * psi.data[i, 0] self.data = abs(p)**2
def _hermite_mode(self, x: float): """ A normalised Hermite-Gaussian function """ # On 22.11.2017, Matteo changed all the self.pump_width to self.__correct_pump_width # _result = hermite(self.pump_temporal_mode)((self.pump_center - x) / # self.pump_width) * \ # exp(-(self.pump_center - x)**2 / (2 * self.pump_width**2)) /\ # sqrt(factorial(self.pump_temporal_mode) * sqrt(pi) * # 2**self.pump_temporal_mode * self.pump_width) # TODO: Check the correctness of the __correct_pump_width parameter _result = hermite(self.pump_temporal_mode)((self.pump_centre - x) / self.__correct_pump_width) * \ np.exp(-(self.pump_centre - x) ** 2 / (2 * self.__correct_pump_width ** 2)) / \ np.sqrt(factorial(self.pump_temporal_mode) * np.sqrt(np.pi) * 2 ** self.pump_temporal_mode * self.__correct_pump_width) return _result
def phi(self, n, x): c_h_n = special.hermite(n) c = self.c() #res = np.sqrt(2.0*c)**0.5 * (1/np.pi)**0.25 * (1.0/np.sqrt(2**n * misc.factorial(n) )) #res = (1.0/np.sqrt(np.sqrt(np.pi) * 2**n * misc.factorial(n) ))**0.5 #h_n = np.polyval(c_h_n, x * np.sqrt(2*c)) h_n = np.polyval(c_h_n, x * np.sqrt(2*c) ) # h_n *= 2 ** -(n/2.0) res = ((2*c/np.pi) ** 0.25) * np.exp(-c * x**2) res *= h_n * (1.0/np.sqrt(2**n * misc.factorial(n) )) return res