예제 #1
0
def test_simulation_export_import(tmp_path):
    """Try subsequent export and import to pickle."""
    sim_params = {
        'model_class':
        'XXZChain',
        'model_params': {
            'bc_MPS': 'infinite',
            'L': 2
        },
        'algorithm_class':
        'DummyAlgorithmSleep',
        'algorithm_params': {
            'N_steps': 3
        },  # only one step
        # 'initial_state_builder': 'KagomeInitialStateBuilder',
        'initial_state_params': {
            'method': 'lat_product_state',
            'product_state': [['up'], ['down']]
        },
        'connect_measurements': [
            ('tenpy.simulations.measurement', 'onsite_expectation_value', {
                'opname': 'Sz'
            }),
        ],
    }
    sim_params[
        'directory'] = tmp_path  # go into temporary directory to avoid leaving data behind
    sim_params['output_filename'] = filename = 'my_results.pkl'
    sim = tenpy.simulations.simulation.Simulation(sim_params)
    data_direct = sim.run()
    with (tmp_path / filename).open('rb') as f:
        data_imported = pickle.load(f)
    assert 'psi' in data_direct
    assert '<Sz>' in data_direct['measurements']
    io_test.assert_equal_data(data_imported, data_direct)
예제 #2
0
def test_pickle():
    """Try subsequent export and import to pickle."""
    data = io_test.gen_example_data()
    with tempfile.TemporaryDirectory() as tdir:
        filename = 'test.pkl'
        with open(os.path.join(tdir, filename), 'wb') as f:
            pickle.dump(data, f)
        with open(os.path.join(tdir, filename), 'rb') as f:
            data_imported = pickle.load(f)
    io_test.assert_equal_data(data_imported, data)
예제 #3
0
def test_hdf5_export_import():
    """Try subsequent export and import to pickle."""
    data = io_test.gen_example_data()
    with tempfile.TemporaryDirectory() as tdir:
        filename = 'test.hdf5'
        with h5py.File(os.path.join(tdir, filename), 'w') as f:
            hdf5_io.save_to_hdf5(f, data)
        with h5py.File(os.path.join(tdir, filename), 'r') as f:
            data_imported = hdf5_io.load_from_hdf5(f)
    io_test.assert_equal_data(data_imported, data)
예제 #4
0
def test_import_from_datadir(fn):
    print("import ", fn)
    filename = os.path.join(io_test.datadir, fn)
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=FutureWarning)
        with open(filename, 'rb') as f:
            data = pickle.load(f)
    if 'version' in data:
        data_expected = io_test.gen_example_data(data['version'])
    else:
        data_expected = io_test.gen_example_data('0.4.0')
    io_test.assert_equal_data(data, data_expected)
예제 #5
0
def test_pickle(tmp_path):
    """Try subsequent export and import to pickle."""
    data = io_test.gen_example_data()
    io_test.assert_event_handler_example_works(
        data)  #if this fails, it's not import/export
    filename = tmp_path / 'test.pkl'
    with filename.open('wb') as f:
        pickle.dump(data, f)
    with filename.open('rb') as f:
        data_imported = pickle.load(f)
    io_test.assert_equal_data(data_imported, data)
    io_test.assert_event_handler_example_works(data_imported)
예제 #6
0
def test_hdf5_export_import(tmp_path):
    """Try subsequent export and import to pickle."""
    data = io_test.gen_example_data()
    io_test.assert_event_handler_example_works(
        data)  #if this fails, it's not import/export
    filename = tmp_path / 'test.hdf5'
    with h5py.File(str(filename), 'w') as f:
        hdf5_io.save_to_hdf5(f, data)
    with h5py.File(str(filename), 'r') as f:
        data_imported = hdf5_io.load_from_hdf5(f)
    io_test.assert_equal_data(data_imported, data)
    io_test.assert_event_handler_example_works(data_imported)
예제 #7
0
def test_import_from_datadir(fn):
    print("import ", fn)
    filename = os.path.join(io_test.datadir, fn)
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=FutureWarning)
        with h5py.File(filename, 'r') as f:
            data = hdf5_io.load_from_hdf5(f)
    if 'version' in data:
        data_expected = io_test.gen_example_data(data['version'])
    else:
        data_expected = io_test.gen_example_data('0.4.0')
    io_test.assert_equal_data(data, data_expected)
    io_test.assert_event_handler_example_works(data)