def loadtxt(title, verbose=True): import re from SimpleQmap import State, ScaleInfo with open(title, "r") as file: for i, line in enumerate(file): if re.search('DIM', line): dim = int(line.split(" ")[2]) if re.search('QMIN', line): qmin = float(line.split(" ")[2]) if re.search('QMAX', line): qmax = float(line.split(" ")[2]) if re.search('PMIN', line): pmin = float(line.split(" ")[2]) if re.search('PMAX', line): pmax = float(line.split(" ")[2]) if re.search('REP', line): rep = line.split(" ")[2].replace("\n", "") if rep not in ["q", "p"]: raise TypeError("must be q-rep or p-rep text file") data = numpy.loadtxt(title).transpose() vec = data[2] + 1.j * data[3] scl = ScaleInfo(dim, [[qmin, qmax], [pmin, pmax]]) state = State(scl, vec) if rep == "p": state = state.p2q() if verbose: t = "load:%s\n" % title t += "dim:%d\n" % dim t += "domain:[%f,%f]x[%f,%f]\n" % (qmin, qmax, pmin, pmax) t += "representation:q" if rep == "q" else "!!Warning!!\nconvert original data (p-rep.) to q-rep.\n" print(t) return state
def coherent(self, q_c, p_c, x): """ minimum-uncertainty Gaussian wave packet centered at (q_c,p_c, x) .. math:: \langle x | \psi \\rangle = \exp[-(x-q_c)^2/2\hbar + p_c(x-q_c)/\hbar] .. warning:: 周期的境界条件を課してないので特別な理由がない限り使わないで下さい. Parameters ---------- q_c, p_c : float Centroid (q_c,p_c,x) of the wave packet x : array """ re = -(x - q_c) * (x - q_c) * numpy.pi / (self.scaleinfo.h) im = (x - q_c) * p_c * twopi / self.scaleinfo.h res = State(self.scaleinfo, data=numpy.exp(re + 1.j * im)) norm2 = numpy.abs(res.inner(res)) return res / numpy.sqrt(norm2)
def coherent(self, q_c, p_c, x): """ minimum-uncertainty Gaussian wave packet centered at (q_c,p_c, x) .. math:: \langle x | \psi \\rangle = \exp[-(x-q_c)^2/2\hbar + p_c(x-q_c)/\hbar] .. warning:: 周期的境界条件を課してないので特別な理由がない限り使わないで下さい. Parameters ---------- q_c, p_c : float Centroid (q_c,p_c,x) of the wave packet x : array """ re = -(x - q_c)*(x - q_c)*numpy.pi/(self.scaleinfo.h) im = (x - q_c)*p_c*twopi/self.scaleinfo.h res = State(self.scaleinfo, data = numpy.exp(re+ 1.j*im)) norm2 = numpy.abs(res.inner(res)) return res/numpy.sqrt(norm2)
def loadtxt(title,verbose=True): import re from SimpleQmap import State,ScaleInfo with open(title, "r") as file: for i, line in enumerate(file): if re.search('DIM', line): dim = int(line.split(" ")[2]) if re.search('QMIN', line): qmin = float(line.split(" ")[2]) if re.search('QMAX', line): qmax = float(line.split(" ")[2]) if re.search('PMIN', line): pmin = float(line.split(" ")[2]) if re.search('PMAX', line): pmax = float(line.split(" ")[2]) if re.search('REP', line): rep = line.split(" ")[2].replace("\n","") if rep not in ["q","p"]: raise TypeError("must be q-rep or p-rep text file") data = numpy.loadtxt(title).transpose() vec = data[2] + 1.j*data[3] scl = ScaleInfo(dim, [[qmin,qmax], [pmin, pmax]]) state = State(scl, vec) if rep == "p": state = state.p2q() if verbose: t = "load:%s\n" % title t += "dim:%d\n" % dim t += "domain:[%f,%f]x[%f,%f]\n" % (qmin,qmax, pmin, pmax) t += "representation:q" if rep =="q" else "!!Warning!!\nconvert original data (p-rep.) to q-rep.\n" print(t) return state
def eig(self): data = numpy.squeeze(numpy.asarray(self)) (evals, V) = numpy.linalg.eig(data) evecs = [] for i in range(len(evals)): evecs.append(State(self.scaleinfo, V[:, i])) return evals, evecs
def p2q(self): """ Fourier (inverse) transformation from the momentum :math:`(p)` representation to the position :math:`(q)` representation. .. math:: \\langle q | \\psi\\rangle = \\sum_p \\langle q | p\\rangle \\!\\langle p | \\psi \\rangle """ data = State(self.scaleinfo, self) if self.scaleinfo.domain[1][0]*self.scaleinfo.domain[1][1] < 0: data = numpy.fft.fftshift(data) data = numpy.fft.ifft(data)*numpy.sqrt(self.scaleinfo.dim)#/numpy.sqrt(self.scaleinfo.dim) return State(self.scaleinfo, data)
def eigh(self): data = numpy.squeeze(numpy.asarray(self)) (evals, V) = numpy.linalg.eigh(data) index = numpy.argsort(evals) evecs = [] for i in index: evecs.append(State(self.scaleinfo, V[:, i])) return evals, evecs
def cs(self, q_c, p_c): """ create new State object which minimum-uncertainty Gaussian wave packet centered at (q_c,p_c) on periodic boundary condition. Parameters ---------- q_c, p_c : float Centroid (q_c,p_c) of wave packet Examples ---------- >>> from state import State, ScaleInfo >>> scl = ScaleInfo(10, [[0,1],[-0.5,0.5]]) >>> state = State(scl) >>> state.cs(0.5,0.1) State([ -5.19214101e-04 +4.91950621e-47j, -3.55650243e-03 -2.58395027e-03j, -1.22265106e-02 -3.76293303e-02j, 5.88151479e-02 -1.81014412e-01j, 3.95164004e-01 -2.87103454e-01j, 6.68740103e-01 -8.71513377e-71j, 3.95164004e-01 +2.87103454e-01j, 5.88151479e-02 +1.81014412e-01j, -1.22265106e-02 +3.76293303e-02j, -3.55650243e-03 +2.58395027e-03j]) .. seealso:: Module :state:`coherent` """ qrange = self.scaleinfo.domain[0] d = qrange[1] - qrange[0] lqmin, lqmax = qrange[0] - 2 * d, qrange[1] + 2 * d long_q = numpy.linspace(lqmin, lqmax, 5 * self.scaleinfo.dim, endpoint=False) coh_state = self.coherent(q_c, p_c, long_q) vec = numpy.zeros(self.scaleinfo.dim, dtype=numpy.complex128) m = int(len(coh_state) / self.scaleinfo.dim) coh_state = coh_state.reshape(m, self.scaleinfo.dim) for i in range(m): vec += coh_state[i][::1] norm2 = numpy.dot(vec, numpy.conj(vec)) return State(self.scaleinfo, vec / numpy.sqrt(norm2))
def q2p(self): """ Fourier (forward) transformation from the position :math:`(q)` representation to the momentum :math:`(p)` representation. .. math:: \\langle p | \\psi\\rangle = \\sum_q \\langle p | q\\rangle \\!\\langle q | \\psi \\rangle """ data = numpy.fft.fft(self) / numpy.sqrt(self.scaleinfo.dim) if self.scaleinfo.domain[1][0] * self.scaleinfo.domain[1][1] < 0: data = numpy.fft.fftshift(data) return State(self.scaleinfo, data) #*numpy.sqrt(self.scaleinfo.dim)
def copy(self): return State(self.scaleinfo, self.toarray())
def inner(self, phi): """ return :math:`\\langle \\phi | \\psi\\rangle` as complex value constant (numpy.complex128) """ res = numpy.sum(self * numpy.conj(phi)) return State(self.scaleinfo, res)
def prep(self): """ return :math:`\\langle p | \\psi\\rangle` as a State object """ return State(self.scaleinfo, self.q2p())
def state(self): return State(self)
def state(self, data=[]): if len(data) == 0: return State(self) else: return State(self, data)