def test_executable_result(): rtinfo = cg.RuntimeInfo(execution_index=5) er = cg.ExecutableResult( spec=_get_example_spec(name='test-spec'), runtime_info=rtinfo, raw_data=cirq.ResultDict(params=cirq.ParamResolver(), measurements={'z': np.ones((1_000, 4))}), ) cg_assert_equivalent_repr(er)
def test_executable_group_result(tmpdir): egr = cg.ExecutableGroupResult( runtime_configuration=cg.QuantumRuntimeConfiguration( processor=_MockEngineProcessor(), run_id='unit-test', ), shared_runtime_info=cg.SharedRuntimeInfo(run_id='my run'), executable_results=[ cg.ExecutableResult( spec=_get_example_spec(name=f'test-spec-{i}'), runtime_info=cg.RuntimeInfo(execution_index=i), raw_data=cirq.Result(params=cirq.ParamResolver(), measurements={'z': np.ones((1_000, 4))}), ) for i in range(3) ], )
def test_filesystem_saver(tmpdir, patch_cirq_default_resolvers): assert patch_cirq_default_resolvers run_id = 'asdf' fs_saver = _FilesystemSaver(base_data_dir=tmpdir, run_id=run_id) rt_config = cg.QuantumRuntimeConfiguration( processor=_MockEngineProcessor(), run_id=run_id) shared_rt_info = cg.SharedRuntimeInfo(run_id=run_id) fs_saver.initialize(rt_config, shared_rt_info=shared_rt_info) # Test 1: assert fs_saver.initialize() has worked. rt_config2 = cirq.read_json_gzip( f'{tmpdir}/{run_id}/QuantumRuntimeConfiguration.json.gz') shared_rt_info2 = cirq.read_json_gzip( f'{tmpdir}/{run_id}/SharedRuntimeInfo.json.gz') assert rt_config == rt_config2 assert shared_rt_info == shared_rt_info2 # Test 2: assert `consume_result()` works. # you shouldn't actually mutate run_id in the shared runtime info, but we want to test # updating the shared rt info object: shared_rt_info.run_id = 'updated_run_id' exe_result = cg.ExecutableResult( spec=None, runtime_info=cg.RuntimeInfo(execution_index=0), raw_data=cirq.Result(params=cirq.ParamResolver({}), measurements={'z': np.ones((100, 5))}), ) fs_saver.consume_result(exe_result=exe_result, shared_rt_info=shared_rt_info) shared_rt_info3 = cirq.read_json_gzip( f'{tmpdir}/{run_id}/SharedRuntimeInfo.json.gz') exe_result3 = cirq.read_json_gzip( f'{tmpdir}/{run_id}/ExecutableResult.0.json.gz') assert shared_rt_info == shared_rt_info3 assert exe_result == exe_result3 # Test 3: assert loading egr_record works. egr_record: cg.ExecutableGroupResultFilesystemRecord = cirq.read_json_gzip( f'{fs_saver.data_dir}/ExecutableGroupResultFilesystemRecord.json.gz') assert egr_record == fs_saver.egr_record exegroup_result: cg.ExecutableGroupResult = egr_record.load( base_data_dir=tmpdir) assert exegroup_result.shared_runtime_info == shared_rt_info assert exegroup_result.runtime_configuration == rt_config assert exegroup_result.executable_results[0] == exe_result
def test_print_logger(capsys): pl = _PrintLogger(n_total=10) shared_rt_info = cg.SharedRuntimeInfo(run_id='hi mom') pl.initialize() for i in range(10): exe_result = cg.ExecutableResult( spec=None, runtime_info=cg.RuntimeInfo(execution_index=i), raw_data=cirq.Result(params=cirq.ParamResolver({}), measurements={}), ) pl.consume_result(exe_result, shared_rt_info) pl.finalize() assert capsys.readouterr().out == ('\n\r1 / 10' '\r2 / 10' '\r3 / 10' '\r4 / 10' '\r5 / 10' '\r6 / 10' '\r7 / 10' '\r8 / 10' '\r9 / 10' '\r10 / 10\n')