def test_pulse_from_func(): """Test instantiating pulse from a simple function""" tgrid = pulse_tgrid(10, 100) ampl = lambda t: 100 * blackman(t, 0, 10) pulse1 = Pulse.from_func(tgrid, ampl, ampl_unit='MHz', time_unit='ns') pulse2 = Pulse(tgrid, ampl(tgrid), ampl_unit='MHz', time_unit='ns') assert pulse1 == pulse2
def ampl_2freq(tgrid, E0, T, freq_1, freq_2, a_1, a_2, phi): return (E0 * blackman(tgrid, 0, T) * carrier( tgrid, 'ns', freq=(freq_1, freq_2), freq_unit='GHz', weights=(a_1, a_2), phases=(0.0, phi), ).real)
def ampl_2freq_rwa(tgrid, E0, T, freq_1, freq_2, a_1, a_2, phi, w_d): # note: amplitude reduction by 1/2 is included in construction of ham return (E0 * blackman(tgrid, 0, T) * carrier( tgrid, 'ns', freq=(freq_1 - w_d, freq_2 - w_d), freq_unit='GHz', weights=(a_1, a_2), phases=(0.0, phi), complex=True, ))
def ampl_CRAB_rwa(tgrid, E0, T, r, a, b, w_d): # note that w_d is neccessary a pulse parameter, even though it does not # occur in the formula: the simplex adapts the config file based on the w_d # parameter in the pulse. # # frequencies are freq[k] = 2*pi*k*(1+r_k)/T, so the r vector must take # values in [-0.5, 0.5] n = len(a) freq = np.array([2 * np.pi * k * (1 + r[k]) / float(T) for k in range(n)]) crab_shape = CRAB_carrier(tgrid, 'ns', freq, 'GHz', a, b, normalize=True) # note: amplitude reduction by 1/2 is included in construction of ham a = blackman(tgrid, 0, T) * crab_shape if np.max(np.abs(a)) > 1.0e-16: return E0 * a / np.max(np.abs(a)) else: return np.zeros(len(a))
def ampl_5freq(tgrid, E0, T, freq_low, a_low, b_low, freq_high, a_high, b_high): norm_carrier = CRAB_carrier(tgrid, 'ns', freq_high, 'GHz', a_high, b_high, normalize=True) crab_shape = CRAB_carrier(tgrid, 'ns', freq_low, 'GHz', a_low, b_low, normalize=True) a = blackman(tgrid, 0, T) * crab_shape * norm_carrier return E0 * a / np.max(np.abs(a))
def ampl_5freq_rwa(tgrid, E0, T, freq_low, a_low, b_low, freq_high, a_high, b_high, w_d): norm_carrier = CRAB_carrier( tgrid, 'ns', freq_high - w_d, 'GHz', a_high, b_high, normalize=True, complex=True, ) crab_shape = CRAB_carrier(tgrid, 'ns', freq_low, 'GHz', a_low, b_low, normalize=True) # note: amplitude reduction by 1/2 is included in construction of ham a = blackman(tgrid, 0, T) * crab_shape * norm_carrier return E0 * a / np.max(np.abs(a))
def test_level_model(tmpdir, request, H0, H1, L1, L2, pop1, pop2): """Test a simple two-qubit model""" filename = request.module.__file__ test_dir, _ = os.path.splitext(filename) psi = np.array([0, 1, 1, 0], dtype=np.complex128) / np.sqrt(2.0) pulse = AnalyticalPulse.from_func( partial(blackman, t_start=0, t_stop=50), ampl_unit='unitless', time_unit='ns', ) model = two_level_model(H0, H1, L1, L2, pop1, pop2, pulse, psi) model.user_data['my_str'] = 'This is a custom string value' model.user_data['i'] = 1 model.user_data['j'] = 2 model.user_data['propagate'] = True model.user_data['liouville_space'] = False model.user_data['precision'] = 1e-8 model.write_to_runfolder(str(tmpdir.join('model_rf'))) assert filecmp.cmp( os.path.join(test_dir, 'config'), str(tmpdir.join('model_rf', 'config')), shallow=False, ) def file_matches_matrix(filename, matrix, limit=0.0): """Check that the indexed matrix in `filename` matches `matrix`, up to `limit`""" file_matrix = read_indexed_matrix( str(tmpdir.join('model_rf', filename)), expand_hermitian=False, shape=(4, 4), ) return norm(file_matrix - matrix) <= limit def file_matches_psi(filename, psi, limit=0.0): """Check that the psi in `filename` matches `psi`, up to `limit`""" file_psi = read_psi_amplitudes( str(tmpdir.join('model_rf', filename)), n=4 ) return norm(file_psi - psi) <= limit assert file_matches_matrix('H0.dat', H0) assert file_matches_matrix('H1.dat', H1) assert model.ham() == [H0, H1] assert model.ham(with_attribs=True) == [ (H0, OrderedDict([])), (H1, OrderedDict([('pulse_id', 1)])), ] assert model.observables() == [pop1, pop2] assert model.observables(with_attribs=True) == [ ( pop1, OrderedDict( [ ('outfile', 'pops.dat'), ('exp_unit', 'unitless'), ('is_real', True), ('time_unit', 'ns'), ('column_label', '<P_1> (q1)'), ('op_unit', 'unitless'), ] ), ), ( pop2, OrderedDict( [ ('outfile', 'pops.dat'), ('exp_unit', 'unitless'), ('is_real', True), ('time_unit', 'ns'), ('column_label', '<P_1> (q2)'), ('op_unit', 'unitless'), ] ), ), ] assert model.lindblad_ops() == [L1, L2] assert model.lindblad_ops(with_attribs=True) == [ ( L1, OrderedDict( [('add_to_H_jump', 'banded'), ('conv_to_superop', False)] ), ), ( L2, OrderedDict( [('add_to_H_jump', 'banded'), ('conv_to_superop', False)] ), ), ] assert file_matches_matrix('L1.dat', L1) assert file_matches_matrix('L2.dat', L2) assert file_matches_matrix('O1.dat', pop1) assert file_matches_matrix('O2.dat', pop2) assert file_matches_psi('psi.dat', psi, limit=1e-15) num_pulse = Pulse.read(str(tmpdir.join('model_rf', 'pulse1.dat'))) assert num_pulse.time_unit == 'ns' assert num_pulse.T == '50_ns' expected_amplitude = blackman( num_pulse.tgrid, t_start=float(num_pulse.t0), t_stop=float(num_pulse.T) ) assert np.max(np.abs(num_pulse.amplitude - expected_amplitude)) < 1e-15
def ampl_1freq_0(tgrid, E0, T, w_L=0.0): return E0 * blackman(tgrid, 0, T) * carrier(tgrid, 'ns', w_L, 'GHz').real
def ampl_1freq_rwa(tgrid, E0, T, w_L, w_d): # note: amplitude reduction by 1/2 is included in construction of ham return (E0 * blackman(tgrid, 0, T) * carrier(tgrid, 'ns', (w_L - w_d), 'GHz', complex=True))
def test_level_model(tmpdir, request, H0, H1, L1, L2, pop1, pop2): """Test a simple two-qubit model""" filename = request.module.__file__ test_dir, _ = os.path.splitext(filename) psi = np.array([0, 1, 1, 0], dtype=np.complex128) / np.sqrt(2.0) pulse = AnalyticalPulse.from_func( partial(blackman, t_start=0, t_stop=50), ampl_unit='unitless', time_unit='ns', ) model = two_level_model(H0, H1, L1, L2, pop1, pop2, pulse, psi) model.user_data['my_str'] = 'This is a custom string value' model.user_data['i'] = 1 model.user_data['j'] = 2 model.user_data['propagate'] = True model.user_data['liouville_space'] = False model.user_data['precision'] = 1e-8 model.write_to_runfolder(str(tmpdir.join('model_rf'))) assert filecmp.cmp( os.path.join(test_dir, 'config'), str(tmpdir.join('model_rf', 'config')), shallow=False, ) def file_matches_matrix(filename, matrix, limit=0.0): """Check that the indexed matrix in `filename` matches `matrix`, up to `limit`""" file_matrix = read_indexed_matrix( str(tmpdir.join('model_rf', filename)), expand_hermitian=False, shape=(4, 4), ) return norm(file_matrix - matrix) <= limit def file_matches_psi(filename, psi, limit=0.0): """Check that the psi in `filename` matches `psi`, up to `limit`""" file_psi = read_psi_amplitudes(str(tmpdir.join('model_rf', filename)), n=4) return norm(file_psi - psi) <= limit assert file_matches_matrix('H0.dat', H0) assert file_matches_matrix('H1.dat', H1) assert model.ham() == [H0, H1] assert model.ham(with_attribs=True) == [ (H0, OrderedDict([])), (H1, OrderedDict([('pulse_id', 1)])), ] assert model.observables() == [pop1, pop2] assert model.observables(with_attribs=True) == [ ( pop1, OrderedDict([ ('outfile', 'pops.dat'), ('exp_unit', 'unitless'), ('is_real', True), ('time_unit', 'ns'), ('column_label', '<P_1> (q1)'), ('op_unit', 'unitless'), ]), ), ( pop2, OrderedDict([ ('outfile', 'pops.dat'), ('exp_unit', 'unitless'), ('is_real', True), ('time_unit', 'ns'), ('column_label', '<P_1> (q2)'), ('op_unit', 'unitless'), ]), ), ] assert model.lindblad_ops() == [L1, L2] assert model.lindblad_ops(with_attribs=True) == [ ( L1, OrderedDict([('add_to_H_jump', 'banded'), ('conv_to_superop', False)]), ), ( L2, OrderedDict([('add_to_H_jump', 'banded'), ('conv_to_superop', False)]), ), ] assert file_matches_matrix('L1.dat', L1) assert file_matches_matrix('L2.dat', L2) assert file_matches_matrix('O1.dat', pop1) assert file_matches_matrix('O2.dat', pop2) assert file_matches_psi('psi.dat', psi, limit=1e-15) num_pulse = Pulse.read(str(tmpdir.join('model_rf', 'pulse1.dat'))) assert num_pulse.time_unit == 'ns' assert num_pulse.T == '50_ns' expected_amplitude = blackman(num_pulse.tgrid, t_start=float(num_pulse.t0), t_stop=float(num_pulse.T)) assert np.max(np.abs(num_pulse.amplitude - expected_amplitude)) < 1e-15