def test_description(get_job): job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob(description='hello')) assert job.description() == 'hello' get_job.return_value = qtypes.QuantumJob(description='hello') assert cg.EngineJob('a', 'b', 'steve', EngineContext()).description() == 'hello' get_job.assert_called_once_with('a', 'b', 'steve', False)
def test_get_repetitions_and_sweeps_v1(get_job): job = cg.EngineJob('a', 'b', 'steve', EngineContext()) get_job.return_value = qtypes.QuantumJob(run_context=_to_any( v1.program_pb2.RunContext( parameter_sweeps=[v1.params_pb2.ParameterSweep(repetitions=10)]))) with pytest.raises(ValueError, match='v1 RunContext is not supported'): job.get_repetitions_and_sweeps()
def test_results_len(get_job_results): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.SUCCESS)) get_job_results.return_value = RESULTS job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qjob) assert len(job) == 2
def test_get_calibration(get_calibration): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( calibration_name='projects/a/processors/p/calibrations/123')) calibration = qtypes.QuantumCalibration(data=_to_any( Merge( """ timestamp_ms: 123000, metrics: [{ name: 'xeb', targets: ['0_0', '0_1'], values: [{ double_val: .9999 }] }, { name: 't1', targets: ['0_0'], values: [{ double_val: 321 }] }, { name: 'globalMetric', values: [{ int32_val: 12300 }] }] """, v2.metrics_pb2.MetricsSnapshot()))) get_calibration.return_value = calibration job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qjob) assert list(job.get_calibration()) == ['xeb', 't1', 'globalMetric'] get_calibration.assert_called_once_with('a', 'p', 123)
def test_labels(): job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob(labels={'t': '1'})) assert job.labels() == {'t': '1'}
def test_timeout(patched_time_sleep, get_job): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.RUNNING)) get_job.return_value = qjob job = cg.EngineJob('a', 'b', 'steve', EngineContext(timeout=500)) with pytest.raises(RuntimeError, match='Timed out'): job.results()
def test_add_labels(add_job_labels): job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob(labels={})) assert job.labels() == {} add_job_labels.return_value = qtypes.QuantumJob(labels={ 'a': '1', }) assert job.add_labels({'a': '1'}).labels() == {'a': '1'} add_job_labels.assert_called_with('a', 'b', 'steve', {'a': '1'}) add_job_labels.return_value = qtypes.QuantumJob(labels={ 'a': '2', 'b': '1' }) assert job.add_labels({ 'a': '2', 'b': '1' }).labels() == { 'a': '2', 'b': '1' } add_job_labels.assert_called_with('a', 'b', 'steve', {'a': '2', 'b': '1'})
def test_str(): engine = mock.Mock() job = qtypes.QuantumJob(name='projects/a/programs/b/jobs/steve') job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) assert str(job) == 'EngineJob(projects/a/programs/b/jobs/steve)'
def test_calibration_results_not_a_calibration(get_job_results): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.SUCCESS)) get_job_results.return_value = RESULTS job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qjob) with pytest.raises(ValueError, match='calibration results'): job.calibration_results()
def test_get_repetitions_and_sweeps_unsupported(get_job): job = cg.EngineJob('a', 'b', 'steve', EngineContext()) get_job.return_value = qtypes.QuantumJob(run_context=qtypes.any_pb2.Any( type_url='type.googleapis.com/unknown.proto')) with pytest.raises(ValueError, match='unsupported run_context type: unknown.proto'): job.get_repetitions_and_sweeps()
def test_get_repetitions_and_sweeps(get_job): job = cg.EngineJob('a', 'b', 'steve', EngineContext()) get_job.return_value = qtypes.QuantumJob(run_context=_to_any( v2.run_context_pb2.RunContext(parameter_sweeps=[ v2.run_context_pb2.ParameterSweep(repetitions=10) ]))) assert job.get_repetitions_and_sweeps() == (10, [cirq.UnitSweep]) get_job.assert_called_once_with('a', 'b', 'steve', True)
def test_status(get_job): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.RUNNING)) get_job.return_value = qjob job = cg.EngineJob('a', 'b', 'steve', EngineContext()) assert job.status() == 'RUNNING' get_job.assert_called_once()
def test_get_cancel(): engine = mock.Mock() job = qtypes.QuantumJob(name='projects/a/programs/b/jobs/steve') job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) job.cancel() engine.cancel_job.assert_called_once()
def test_set_description(set_job_description): job = cg.EngineJob('a', 'b', 'steve', EngineContext()) set_job_description.return_value = qtypes.QuantumJob(description='world') assert job.set_description('world').description() == 'world' set_job_description.assert_called_with('a', 'b', 'steve', 'world') set_job_description.return_value = qtypes.QuantumJob(description='') assert job.set_description('').description() == '' set_job_description.assert_called_with('a', 'b', 'steve', '')
def test_failure_with_no_error(): job = cg.EngineJob( 'a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.SUCCESS, ))) assert not job.failure()
def test_str(): engine = mock.Mock() job = { 'name': 'projects/a/programs/b/jobs/steve', } job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) assert str(job) == 'EngineJob(projects/a/programs/b/jobs/steve)'
def test_set_labels(set_job_labels): job = cg.EngineJob('a', 'b', 'steve', EngineContext()) set_job_labels.return_value = qtypes.QuantumJob(labels={'a': '1', 'b': '1'}) assert job.set_labels({'a': '1', 'b': '1'}).labels() == {'a': '1', 'b': '1'} set_job_labels.assert_called_with('a', 'b', 'steve', {'a': '1', 'b': '1'}) set_job_labels.return_value = qtypes.QuantumJob() assert job.set_labels({}).labels() == {} set_job_labels.assert_called_with('a', 'b', 'steve', {})
def test_create_time(): job = cg.EngineJob( 'a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob(create_time=qtypes.timestamp_pb2.Timestamp( seconds=1581515101))) assert job.create_time() == datetime.datetime(2020, 2, 12, 13, 45, 1)
def test_processor_ids(): job = cg.EngineJob( 'a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob(scheduling_config=qtypes.SchedulingConfig( processor_selector=qtypes.SchedulingConfig.ProcessorSelector( processor_names=['projects/a/processors/p'])))) assert job.processor_ids() == ['p']
def test_results_getitem(get_job_results): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.SUCCESS)) get_job_results.return_value = RESULTS job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qjob) assert str(job[0]) == 'q=0110' assert str(job[1]) == 'q=1010' with pytest.raises(IndexError): _ = job[2]
def test_results_iter(get_job_results): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.SUCCESS)) get_job_results.return_value = RESULTS job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qjob) results = [str(r) for r in job] assert len(results) == 2 assert results[0] == 'q=0110' assert results[1] == 'q=1010'
def test_status(): engine = mock.Mock() job = qtypes.QuantumJob(name='projects/a/programs/b/jobs/steve', execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.RUNNING)) engine.get_job.return_value = job job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) assert job.status() == 'RUNNING' engine.get_job.assert_called_once()
def test_timeout(patched_time_sleep): engine = mock.Mock() job = qtypes.QuantumJob(name='projects/a/programs/b/jobs/steve', execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.RUNNING)) engine.get_job.return_value = job job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) with pytest.raises(RuntimeError, match='Timed out'): job.results()
def test_calibration__with_no_calibration(get_calibration): job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob( name='projects/project-id/programs/test/jobs/test', execution_status={'state': 'SUCCESS'})) calibration = job.get_calibration() assert not calibration assert not get_calibration.called
def test_results(get_job_results): qjob = qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.SUCCESS)) get_job_results.return_value = RESULTS job = cg.EngineJob('a', 'b', 'steve', EngineContext(), _job=qjob) data = job.results() assert len(data) == 2 assert str(data[0]) == 'q=0110' assert str(data[1]) == 'q=1010' get_job_results.assert_called_once_with('a', 'b', 'steve')
def test_get_calibration(): engine = mock.Mock() job = qtypes.QuantumJob( name='projects/a/programs/b/jobs/steve', execution_status=qtypes.ExecutionStatus(calibration_name='hobbes')) calibration = mock.Mock() engine.get_calibration.return_value = calibration job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) assert job.get_calibration() == calibration engine.get_calibration.assert_called_once_with('hobbes')
def test_failure(): job = cg.EngineJob( 'a', 'b', 'steve', EngineContext(), _job=qtypes.QuantumJob(execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.FAILURE, failure=qtypes.ExecutionStatus.Failure( error_code=qtypes.ExecutionStatus.Failure.Code.SYSTEM_ERROR, error_message='boom')))) assert job.failure() == ('SYSTEM_ERROR', 'boom')
def test_results(): engine = mock.Mock() job = qtypes.QuantumJob(name='projects/a/programs/b/jobs/steve', execution_status=qtypes.ExecutionStatus( state=qtypes.ExecutionStatus.State.SUCCESS)) results = mock.Mock() engine.get_job_results.return_value = results job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) assert job.results() == results engine.get_job_results.assert_called_once()
def test_timeout(patched_time_sleep): engine = mock.Mock() job = { 'name': 'projects/a/programs/b/jobs/steve', 'executionStatus': { 'state': 'RUNNING' } } engine.get_job.return_value = job job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) with pytest.raises(RuntimeError, match='Timed out'): job.results()
def test_status(): engine = mock.Mock() job = { 'name': 'projects/a/programs/b/jobs/steve', 'executionStatus': { 'state': 'RUNNING' } } engine.get_job.return_value = job job = cg.EngineJob(job_config=cg.JobConfig(job_id='steve'), job=job, engine=engine) assert job.status() == 'RUNNING' engine.get_job.assert_called_once()