def poly_fit_eta(): from numpy.polynomial.polynomial import Polynomial x = np.linspace(1,0,2000) for i in range(10): U1, U2 = [unitary_group.rvs(4) for _ in range(2)] A1,A2 = [np.random.rand(16).reshape(4,4) + 1j*np.random.rand(16).reshape(4,4) for _ in range(2)] H1 = 0.5*(A1 + A1.conj().T) H2 = 0.5*(A2 + A2.conj().T) te1 = expm(1j*H1*0.1) te2 = expm(1j*H2*0.1) U1_ = (te1 @ U1).conj().T U2_ = (te2 @ U2).conj().T RE = RightEnvironment() params = [np.pi/4,0,0,0,0,0] # np.random.rand(6) C = CircuitSolver() M = C.M(params) M_ = RE.circuit(r(U1), r(U2), r(U1_), r(U2_), M) scores = [] for p in x: scores.append(np.linalg.norm(p * M - M_)) coefs = Polynomial.fit(x[:10],scores[:10],deg = 2, domain = (1,0.9)) new_vals = [coefs(a) for a in x] plt.plot(x, scores, label = "Exact") plt.plot(x, new_vals, label = "Poly Fit") plt.legend() plt.show()
def osciallating_param_costs(): U1, U2, U1_, U2_ = [unitary_group.rvs(4).reshape(2,2,2,2) for _ in range(4)] RE = RightEnvironment() params = np.random.rand(6) C = CircuitSolver() for i in range(len(params)): Ms = [] M_s = [] for p in np.linspace(0,np.pi*2, 200): params[i] = p M = C.M(params) M_ = RE.circuit(U1, U2, U1_, U2_, M) Ms.append(M.reshape(4,)) M_s.append(M_.reshape(4,)) scores = np.linalg.norm(np.array(Ms) - np.array(M_s), axis = 1) plt.plot(scores, label = f"{i}") plt.legend() plt.show()
def grad_desc_comp(): for dt in [0, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5]: Me = random_RE(dt) cf = partial(compile_ansatz_cost_function, exact = Me) grad = partial(approx_fprime, f = cf, epsilon = 1e-8) res = compile_ansatz_sgd(cf, grad) M_c = CircuitSolver().M(res["X"]) # assert np.allclose(np.linalg.norm(M_c), 1) print("Time Step = ", dt) print("\n") print(res["M"]) print("\n") print("Cost Function: ", res["V"]) print("\n") print("Number Evaluations: ", res["N"]) print("\n") print("Params: ", res["X"]) print("\n") print("Exact:") print(Me) print("\n") print("Compiled:") print(M_c) print("\n") print("################################")
def compilation_vs_time(): for dt in [0, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5]: Me = random_RE(dt) cf = partial(compile_ansatz_cost_function, exact = Me) grad = partial(approx_fprime, f = cf, epsilon = 1e-8) res = compile_ansatz(cf, grad) M_c = CircuitSolver().M(res.x) # assert np.allclose(np.linalg.norm(M_c), 1) print("Time Step = ", dt) print("\n") print(res.message) print("\n") print("Cost Function: ", res.fun) print("\n") print("Number Evaluations: ", res.nfev) print("\n") print("Params: ", res.x) print("\n") print("Exact:") print(Me) print("\n") print("Compiled:") print(M_c) print("\n") print("################################")
def time_evolve(init_p, H): C = CircuitSolver() Re = BWMPS.Represent() U = expm(-1j * H * 0.01) results = [] for _ in tqdm(range(400)): results.append(init_p) res = minimize(obj3, init_p, method="Nelder-Mead", options={ "maxiter": 10000, "disp": True }, tol=1e-3, args=(init_p, U, Re, C)) init_p = res.x return results
def state_from_params(p, l): U1, U2 = CircuitSolver().paramU(p) return bwMPS([U2, U1], l).state()
if __name__ == "__main__": ################ # Set up a random environment calculation U1, U2, U1_, U2_ = [unitary_group.rvs(4).reshape(2,2,2,2) for _ in range(4)] RE = RightEnvironment() R = Represent() R.U1 = U1 R.U2 = U2 R.U1_ = U1_ R.U2_ = U2_ ############### # Demonstrate the sinusoidal cost Function params = np.random.rand(6) C = CircuitSolver() params = [1] + list(np.random.rand(6)) CostFuncs = [] x = np.linspace(0,2*np.pi, 200) for p in x: params[1]=p CF = R.cost_function(params) CostFuncs.append(CF) plt.plot(x, CostFuncs, label = "Exact") ############### # Attempt to fit an exact curve suing the RotoSolve Calcs: params[1] = 0
def compile_ansatz_cost_function(params, exact): return np.linalg.norm( exact - CircuitSolver().M(params) )