def GRAPE(T, n, N, rho_0, rho_f, tau_c, eta_0, stepsize, amps, epsilon=0.01): pulses = buildGrapePulses(amps, T) ts = np.linspace(0., T, n+1) Us_k = [] # Every U_k is actually a list of U_m new_amps = [] def gen_Um(pulse, t0, te): js = generateJumpTimes((te - t0), tau_c) js = [j + t0 for j in js] return generateU_k(pulse, generateEta(js, eta_0, t0, te), stepsize=stepsize, t0=t0, te=te) def genUs_m(m): t0 = ts[m] te = ts[m+1] pulse = pulses[m] t_avg = (t0 + te) / 2. U_m = gen_Um(pulse, t0, te)(te) return U_m for k in range(N): # if not profiling and parallel: # Us_m = pool.map(genUs_m, range(n)) # else: Us_m = map(genUs_m, range(n)) Us_k.append(Us_m) # if k == 3: # Um = reduce(np.dot, Us_m) # Uk = ezGenerateU_k(a_pi, T, tau_c, eta_0) # A = np.dot(Um, rho_0) # B = np.dot(Uk(T), rho_0) # print(A) # print(B) # print(B - A) # sys.exit() grads = [] def makeNewAmps(m): a_m = amps[m] d_phi_d_am = np.real(grad_phi_am(T, N, m, Us_k, rho_0, rho_f)) grads.append(d_phi_d_am) # Eq. 13 new_amp = a_m + epsilon*a_max*d_phi_d_am if new_amp <= -a_max: new_amp = .95 * -a_max if new_amp >= a_max: new_amp = .95 * a_max # new_amp = max(new_amp, -a_max) # new_amp = min(new_amp, a_max) return new_amp new_amps = map(makeNewAmps, range(n)) # print(grads) # for m in range(n): # a_m = amps[m] # d_phi_d_am = grad_phi_am(T, N, m, Us_k, rho_0, rho_f) # # Eq. 13 # new_amp = a_m + epsilon*a_max*d_phi_d_am # new_amp = max(new_amp, -a_max) # new_amp = min(new_amp, a_max) # new_amps.append(new_amp) return new_amps
def plotShape(pulsef, name, end=14*pi/3): shape = plt.figure() pulse_time = np.linspace(0, end, 5000) plt.plot(pulse_time, [pulsef(t) for t in pulse_time], "b-", label=name) plt.legend(loc="best") plt.ylabel(r"Amplitude in $a_{max}$") plt.xlabel(r"t in $\hbar/a_{max}$") plt.ylim([-1.1,1.1]) plt.xlim([0.,end]) filename = base + name.replace(" ", "_") + "-shape.png" shape.savefig(filename)
def buildGrapePulses(amps, T): n = len(amps) ts = np.linspace(0, T, n + 1) pulses = [] for m in range(1, len(ts)): t0 = ts[m-1] te = ts[m] amp = amps[m-1] pulse = pulseMaker(t0, te, amp) pulses.append(pulse) return pulses
def csamplef(f, s, e, sections): bins = np.linspace(s, e, sections) dbin = bins[1] - bins[0] fsections = [f(t) for t in bins] def g(t): if t < 0: return 0 if t >= e: return 0 bin_num = int((t+dbin/2.) / dbin) return fsections[bin_num] return g
def at(self, t): start = time.time() steps = self.steps ts = np.linspace(0., t, steps) dt = ts[1] - ts[0] Ss = [self.G(t) for t in ts] C = reduce(BCH, Ss) # U_count[0] += 1 # end = time.time() # delts = str(end - start) # sys.stdout.write("\rU++: " + str(U_count[0]) + "; " + delts) # sys.stdout.flush() # u_time[0] += (end - start) return linmax.powerexp(C)
def U_k(t): start = time.time() eta = self.etaProvider.get().at def G(t_): return np.array(-(1.j/hbar) * H_t(self.pulse(t_), eta(t_))) steps = 420 ts = np.linspace(0., t, steps) dt = ts[1] - ts[0] Ss = [G(t) for t in ts] C = reduce(BCH, Ss) U_count[0] += 1 end = time.time() delts = str(end - start) sys.stdout.write("\rU++: " + str(U_count[0]) + "; " + delts) sys.stdout.flush() return expm(C)
def U_k(t): if t < t0: return identity if te is not None \ and te < t: return identity start = time.time() steps = int(np.ceil(t / stepsize)) # steps = 700 ts = np.linspace(t0, t, steps) dt = ts[1] - ts[0] cvec = np.array(dt * -(1j), np.complex128) def G(t_): if profiling: start = time.time() hp = H_p(a, eta_k, t_) teatime = time.time() th_time[0] += teatime - start res = np.dot(cvec, hp) g_time[0] += time.time() - teatime return res return np.dot(cvec, H_t2(a(t_), eta_k(t_))) C = G(ts[0]) for i in range(1, len(ts)): # G_avg = 0.5 * (G(ts[i-1]) + G(ts[i])) C = BCH(C, G(ts[i]), order=5) # C = sum(Ss) # U_count[0] += 1 end = time.time() delts = str(end - start) # sys.stdout.write("\rU++: " + str(U_count[0]) + "; " + delts) # sys.stdout.flush() # return linmax.powerexp(C) return expm(C)
def minmax(f, s, e): ts = np.linspace(s, e, 3000) fs = [(f(t)) for t in ts] ma = max(fs) mi = min(fs) return ma, mi
def ezmap(f, xs): if parallel: return pool.map(f, xs) return map(f, xs) p_t = [] # XXX SHAPE pulseshape = donorm(x2p(2., periods=_periods), 0, 2 * _periods, normproc="none") # pulseshape = X_factory(pi, 2, True, 1, normproc="full") tis = np.linspace(0, 6, 1000) pulseshape_data = [pulseshape(ti) for ti in tis] pshape = plt.figure() plt.plot(tis, pulseshape_data, 'b-') # plt.show() plt.ion() fig, ax = plt.subplots() fig.suptitle(wave + ":" + ptitle) p1, = plt.plot(p_t, sym1, 'b--', label="t=1") p3, = plt.plot(p_t, sym3, 'r-', label="t=3") p15, = plt.plot(p_t, sym12, 'g--', label="t=12") plt.xlabel(r"width in $\hbar / a_max$") plt.ylabel(r"$\phi(\rho_f, \rho_0)$")
def maximize(f, s, e): ts = np.linspace(s, e, 3000) m = max([abs(f(t)) for t in ts]) return m
print("POOL") pool = Pool(processes=cpus) tau_c = tau_c_0 tau_cs = [tau_c] while tau_c < tau_c_f: tau_c += dtau_c tau_cs.append(tau_c) # tau_cs = [0.3, 3.0, 29.] # sanity check checkGrape = False # checkGrape = True if checkGrape: pulses = buildGrapePulses([1.,0.5,0.25,0.75,1.],3) ts = np.linspace(0, 3, 1000) az = [] for t in ts: dt = 3. / len(pulses) t_bin = int(np.floor((t / dt))) t_bin = min(t_bin, len(pulses) - 1) az.append(pulses[t_bin](t)) # print(pulses[0](0.)) # print(pulses[0](0)) # print(az) plt.plot(ts, az, 'r-') plt.show() raw_input() # T_G = 5. * hoa # sousa figure 2 T_G = T_SC # can just make them the same
# return { # ampf: ampf, # time: time, # fids: [] # } # pulse_infos = { # pi: mkInfo(a_pi, T_pi), # corpse: mkInfo(a_C, T_C), # scorpse: mkInfo(a_SC, T_SC), # sym: mkInfo(sym_pi, tau) # } p_t = [] shapefig = plt.figure() chi_time = np.linspace(0 - 1, mytau + 1, 1000) if do_sym: plt.plot(chi_time, [sym_pi(t) for t in chi_time], "c-", label="Symmetric Pulse shape") if do_asym: plt.plot(chi_time, [asym_pi(t) for t in chi_time], "m-", label="Antisymmetric Pulse shape") plt.legend(loc="best") plt.ion() fig, ax = plt.subplots() pulse_plots = [] if do_pi:
print("POOL") pool = Pool(processes=cpus) tau_c = tau_c_0 tau_cs = [tau_c] while tau_c < tau_c_f: tau_c += dtau_c tau_cs.append(tau_c) # tau_cs = [0.3, 3.0, 29.] # sanity check checkGrape = False # checkGrape = True if checkGrape: pulses = buildGrapePulses([1., 0.5, 0.25, 0.75, 1.], 3) ts = np.linspace(0, 3, 1000) az = [] for t in ts: dt = 3. / len(pulses) t_bin = int(np.floor((t / dt))) t_bin = min(t_bin, len(pulses) - 1) az.append(pulses[t_bin](t)) # print(pulses[0](0.)) # print(pulses[0](0)) # print(az) plt.plot(ts, az, 'r-') plt.show() raw_input() T_G = 7 * pi / 3. * hoa # sousa figure 2 n = 7 # number of different pulse amplitudes