def test_zero_basis(self): ARMAs = [SchWARMA.ARMA([.5, .25], [1])] ARMAs.append(SchWARMA.ARMA([], [1])) D = {'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex)))} sch = SchWARMA.SchWARMA(ARMAs, [s_z, np.zeros((8, 2))], D) self.assertEqual(sch.N, 2) self.assertTrue(all([b.shape == (8, 8) for b in sch.basis])) self.assertEqual(la.norm(1j * s_z - sch.basis[0][:2, :2]), 0) self.assertEqual(la.norm(sch.basis[0][2:, :]), 0) self.assertEqual(la.norm(sch.basis[0][:, 2:]), 0) out = sch['I'] self.assertEqual(out.rank(), 1) self.assertEqual(out.chi()[1, 1], 0) self.assertEqual(out.chi()[2, 2], 0) avg = ch.QuantumChannel( np.mean([sch['I'].choi() for _ in range(3)], 0), 'choi') self.assertEqual(avg.rank(), 2) self.assertEqual(avg.chi()[1, 1], 0) self.assertEqual(avg.chi()[2, 2], 0)
def test_init(self): ARMAS = [ SchWARMA.ARMA([], np.random.randn(np.random.randint(5)) + 2) for _ in range(3) ] D = {'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex)))} MA = SchWARMA.SchWMA(ARMAS, [s_x, s_y, s_z], D)
def test_hamiltonian_basis(self): ARMAs = [SchWARMA.ARMA([.5, .25], [1])] ARMAs.append(SchWARMA.ARMA([], [1])) ARMAs.append(SchWARMA.ARMA([], [1, 1, 1])) D = {'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex)))} sch = SchWARMA.SchWARMA(ARMAs, [s_x, s_y, s_z], D) self.assertTrue( all([la.norm(b + b.conj().T < 1e-9) for b in sch.basis]))
def test_zero_basis(self): ARMAs = [SchWARMA.ARMA([.5, .25], [1])] ARMAs.append(SchWARMA.ARMA([], [1])) D = {'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex)))} sch = SchWARMA.SchWARMA(ARMAs, [np.zeros((8, 2)), np.zeros((2, 2))], D) self.assertEqual(sch.N, 2) self.assertTrue(all([b.shape == (8, 8) for b in sch.basis])) self.assertEqual(la.norm(sch['I'].liouvillian() - np.eye(4)), 0)
def test_1d_fit(self): D = {'I': ch.QuantumChannel(np.array(np.eye(4)))} AR = SchWARMA.SchWAR([], [s_z], D) alpha = np.sqrt(.00001) sigma = 1.#1./2. arg = lambda t: alpha**2*sigma*(np.sqrt(np.pi)*t*sp.erf(t/sigma)+sigma*(np.exp(-t**2/sigma**2)-1)) args = np.exp(-2*np.array([arg(t) for t in range(100)])) args = (1.-args)/2. AR.fit([args], 5) self.assertEqual(5,len(AR.models[0].a)) self.assertEqual(1,len(AR.models[0].b)) AR.init_ARMAs() AR['I'] #Note now I have brackets on the second argument AR.fit([args], [7]) self.assertEqual(7,len(AR.models[0].a)) self.assertEqual(1,len(AR.models[0].b)) AR.init_ARMAs() L = AR['I'].liouvillian() #Known structure of \sigma_z arma self.assertEqual(la.norm(L-np.diag(np.diag(L))),0.0) self.assertEqual(L[0,0], L[3,3]) self.assertAlmostEqual(L[0,0], 1.,9) self.assertEqual(L[1,1], np.conj(L[2,2]))
def test_no_SchWARMA(self): ARMAs = [SchWARMA.ARMA([], [0])] ARMAs.append(SchWARMA.ARMA([], [0])) ARMAs.append(SchWARMA.ARMA([], [0])) D = { 'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex))), 'X': ch.QuantumChannel(np.kron(s_x.conj(), s_x)) } sch = SchWARMA.SchWARMA(ARMAs, [s_x, s_y, s_z], D) sch.init_ARMAs self.assertEqual(la.norm(D['I'].choi() - sch['I'].choi()), 0) self.assertEqual(la.norm(D['X'].choi() - sch['X'].choi()), 0)
def test_mixed_basis(self): ARMAs = [SchWARMA.ARMA([.5, .25], [1])] ARMAs.append(SchWARMA.ARMA([], [1])) ARMAs.append(SchWARMA.ARMA([], [1, 1, 1])) D = { 'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex))), 'X': ch.QuantumChannel(np.kron(s_x.conj(), s_x)) } ad = np.zeros((4, 2), dtype=complex) ad[2, 1] = .1 sch = SchWARMA.SchWARMA(ARMAs, [s_x, 1j * s_y, ad], D) self.assertEqual(sch.N, 2) self.assertTrue( all([la.norm(b + b.conj().T < 1e-9) for b in sch.basis]))
def test_bad_init(self): ARMAS = [ SchWARMA.ARMA(np.random.randn(1), np.random.randn(np.random.randint(5)) + 2) for _ in range(3) ] D = {'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex)))} self.assertRaises(AssertionError, SchWARMA.SchWMA, *[ARMAS, [s_x, s_y, s_z], D])
def test_downconvert_from_SchWARMA(self): b = si.firwin(128, .2) b2 = SchWARMA.downconvert_from_SchWARMA(b, 2) fb = var_2_fid(acv_2_var(acv_from_b(b), np.arange(100)))[::2] fb2 = var_2_fid(acv_2_var(acv_from_b(b2), np.arange(100)))[:50] self.assertEqual(fb[0], 1.0) self.assertEqual(fb2[0], 1.0) self.assertLess(np.max(np.abs(fb - fb2)), 1e-3)
def test_init(self): ARMAs = [SchWARMA.ARMA([.5, .25], [1])] ARMAs.append(SchWARMA.ARMA([], [1])) ARMAs.append(SchWARMA.ARMA([], [1, 1, 1])) D = {'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex)))} sch = SchWARMA.SchWARMA(ARMAs, [s_x, s_y, s_z], D) xs = [copy.copy(model.x) for model in sch.models] ys = [copy.copy(model.y) for model in sch.models] sch.init_ARMAs() xs2 = [copy.copy(model.x) for model in sch.models] ys2 = [copy.copy(model.y) for model in sch.models] xdiff = np.array([la.norm(x - xx) for x, xx in zip(xs, xs2)]) ydiff = np.array([la.norm(yy) for y, yy in zip(ys, ys2)]) self.assertTrue(np.all(xdiff > 0)) self.assertTrue(np.all(ydiff == 0))
def MA_NNLS_est(self, ps, PhiRecon, b_len): S = self.NNLS_recon(ps, PhiRecon) Sfull = np.concatenate([S, S[1:][::-1]]) N = PhiRecon.shape[1] * 2 R = np.real(np.fft.fft(Sfull)[:len(ps)]) / N x0 = np.zeros(b_len) x0[0] = np.sqrt(R[0] + 2 * np.sum(R[1:])) out = op.minimize( lambda x: np.sum((sch.acv_from_b(x) - R[:b_len])**2), x0) return out['x']
def test_3d_fit(self): D = {'I': ch.QuantumChannel(np.array(np.eye(4,dtype=complex)))} AR = SchWARMA.SchWAR([], [s_z], D) alpha = np.sqrt(.00001) sigma = 1.#1./2. arg = lambda t: alpha**2*sigma*(np.sqrt(np.pi)*t*sp.erf(t/sigma)+sigma*(np.exp(-t**2/sigma**2)-1)) args = np.exp(-2*np.array([arg(t) for t in range(100)])) args = (1.-args)/2. #Note now I have brackets on the second argument AR.fit([args, args, args], [3,5,7]) self.assertEqual(3,len(AR.models[0].a)) self.assertEqual(5,len(AR.models[1].a)) self.assertEqual(7,len(AR.models[2].a)) AR.init_ARMAs() L = AR['I']
def test_avg(self): D = {'I': ch.QuantumChannel(np.array(np.eye(4, dtype=complex)))} MA = SchWARMA.SchWMA([], [s_z], D) alpha = np.sqrt(.00001) sigma = 1. #1./2. arg = lambda t: alpha**2 * sigma * (np.sqrt(np.pi) * t * sp.erf( t / sigma) + sigma * (np.exp(-t**2 / sigma**2) - 1)) args = np.exp(-2 * np.array([arg(t) for t in range(100)])) #args = (1.-args)/2. MA.fit([(1. - args) / 2.], 5) self.assertEqual(5, len(MA.models[0].b)) self.assertEqual(0, len(MA.models[0].a)) MA.init_ARMAs() MA['I'] L1 = MA.avg(1, [ch.QuantumChannel(s_z, 'unitary').choi()]).liouvillian() tL1 = np.array(np.eye(4, dtype=complex)) tL1[1, 1] = args[1] tL1[2, 2] = args[1] self.assertAlmostEqual(la.norm(L1 - tL1), 0.0, 6) L2 = MA.avg(10, [ch.QuantumChannel(s_z, 'unitary').choi()]).liouvillian() tL2 = np.eye(4, dtype=complex) tL2[1, 1] = args[10] tL2[2, 2] = args[10] self.assertAlmostEqual(la.norm(L2 - tL2), 0.0, 5) #Not a great test, but should be true self.assertLess(la.norm(L2 - tL2), la.norm(tL1**10 - L2))
def test_fits_gauss(self): T_max = 100 # Doing 100 gates trotter_ratio = 50 # mezze trotter time steps per unit of time (small so I can do a lot of MC) NN_spec = 10 corr_time = 10.0 amp = .002 config_specs = { 'T_end': T_max, 'steps': T_max * trotter_ratio, 'Δsteps': 10, 'num_runs': 10, 'shsteps': NN_spec } # steps : the time step for simulations, Δsteps: recording interval, shteps: the number of SchWARMA time steps noise_specs = { 'type': 'gauss', 'amp': amp, 'corr_time': corr_time } # noise parameters. # First define config, that determines the simulation parameters for the time of evolution and timesteps config = mezze.simulation.SimulationConfig( time_length=config_specs['T_end'], num_steps=config_specs['steps']) config.parallel = True # parallelizes over available cpu cores config.num_runs = config_specs[ 'num_runs'] # number of monte-carlo runs config.num_cpus = 4 # number of cpus to parallelize over # config.get_unitary = True #report unitary config.time_sampling = True # set to true to get the channel at intermediate times config.sampling_interval = config_specs[ 'Δsteps'] # records the channel after every 10 time-steps config.realization_sampling = False # set true if you want to record every noise(monte-carlo) realization # # Setup PMD pmd0 = mezze.pmd.PMD(control_dim=0, noise_dim=1, num_qubits=1, num_lindblad=0) pmd0.single_qubit_noise[3] = lambda c, gamma: gamma[ 0 ] # sets single_qubit noise along z direction. [3] corresponds to σz pauli basis pmd0.noise_hints[0] = noise_specs # build Hamiltonian and simulation ham = mezze.hamiltonian.HamiltonianFunction(pmd0) ctrls = np.zeros( (config_specs['steps'], 1) ) # ctrls matrix as a function of time is zero since there is no control pham = mezze.hamiltonian.PrecomputedHamiltonian( pmd0, ctrls, config, ham.get_function()) sim = mezze.simulation.Simulation(config, pmd0, pham) rx = SchWARMA.extract_sim_autocorrs(sim, 1. / sim.config.dt)[0] Tgate = 1.0 Tgate2 = 2.5 b_fit = extract_and_downconvert_from_sim(sim, Tgate)[0] b_fit2 = extract_and_downconvert_from_sim(sim, Tgate2)[0] Tgauss = 1.5 b_gauss = make_gauss_approximation(sim, Tgauss)[0] # print(np.max(np.abs(b_fit-b_fit2)),np.max(b_fit)) fids_fast = var_2_fid( acv_2_var(rx, ks=np.arange(T_max / sim.config.dt))) fids_slow = var_2_fid( acv_2_var(acv_from_b(b_fit), np.arange(T_max / Tgate))) fids_slow2 = var_2_fid( acv_2_var(acv_from_b(b_fit2), np.arange(T_max / Tgate2))) fids_gauss = var_2_fid( acv_2_var(acv_from_b(b_gauss), np.arange(T_max / Tgauss))) self.assertEqual(fids_fast[0], 1.0) self.assertEqual(fids_fast[0], fids_slow[0]) self.assertEqual(fids_fast[0], fids_slow2[0]) self.assertEqual(fids_fast[0], fids_gauss[0]) ff = fids_fast[::int(Tgate / sim.config.dt)] fs = fids_slow self.assertAlmostEqual(ff[1], fs[1]) self.assertLess(np.max(np.abs(ff - fs)), 1e-4) ff = fids_fast[::int(Tgate2 / sim.config.dt)] fs = fids_slow2 self.assertAlmostEqual(ff[1], fs[1]) self.assertLess(np.max(np.abs(ff - fs)), 1e-4) ff = fids_fast[::int(Tgauss / sim.config.dt)] fs = fids_gauss self.assertAlmostEqual(ff[1], fs[1], 6) self.assertLess(np.max(np.abs(ff - fs)), 1e-4)