コード例 #1
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_run_circuit_failed(build):
    service = mock.Mock()
    build.return_value = service
    programs = service.projects().programs()
    jobs = programs.jobs()
    programs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test'
    }
    jobs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'READY'
        }
    }
    jobs.get().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'FAILURE'
        }
    }

    with pytest.raises(RuntimeError, match='It is in state FAILURE'):
        Engine(api_key="key").run(
            cirq.Circuit(),
            JobConfig('project-id', gcs_prefix='gs://bucket/folder'))
コード例 #2
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_bad_job_config_inference_order(build):
    eng = Engine(api_key="key")
    config = JobConfig()

    with pytest.raises(ValueError):
        eng._infer_gcs_prefix(config)
    config.project_id = 'project'

    with pytest.raises(ValueError):
        eng._infer_gcs_results(config)
    with pytest.raises(ValueError):
        eng._infer_gcs_program(config)
    eng._infer_gcs_prefix(config)

    eng._infer_gcs_results(config)
    eng._infer_gcs_program(config)
コード例 #3
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_cancel(build):
    service = mock.Mock()
    build.return_value = service
    programs = service.projects().programs()
    jobs = programs.jobs()
    programs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test'
    }
    jobs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'READY'
        }
    }
    jobs.get().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'CANCELLED'
        }
    }

    job = Engine(api_key="key").run_sweep(
        cirq.Circuit(), JobConfig('project-id',
                                  gcs_prefix='gs://bucket/folder'))
    job.cancel()
    assert job.job_resource_name == ('projects/project-id/programs/test/'
                                     'jobs/test')
    assert job.status() == 'CANCELLED'
    assert jobs.cancel.call_args[1][
        'name'] == 'projects/project-id/programs/test/jobs/test'
コード例 #4
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_circuit_device_validation_passes_non_xmon_gate(build):
    service = mock.Mock()
    build.return_value = service
    programs = service.projects().programs()
    jobs = programs.jobs()
    programs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test'
    }
    jobs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'READY'
        }
    }
    jobs.get().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'SUCCESS'
        }
    }
    jobs.getResult().execute.return_value = {
        'result': MessageToDict(_A_RESULT)
    }

    circuit = cirq.Circuit.from_ops(cirq.H.on(XmonQubit(0, 1)))
    result = Engine(api_key="key").run(circuit, JobConfig('project-id'),
                                       Foxtail)
    assert result.repetitions == 1
コード例 #5
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_implied_job_config_project_id(build):
    eng = Engine(api_key="key")
    with pytest.raises(ValueError, match='project id'):
        _ = eng.implied_job_config(None)
    with pytest.raises(ValueError, match='project id'):
        _ = eng.implied_job_config(JobConfig())
    assert eng.implied_job_config(
        JobConfig(project_id='specific')).project_id == 'specific'

    eng_with = Engine(api_key="key", default_project_id='default')

    # Fallback to default.
    assert eng_with.implied_job_config(None).project_id == 'default'

    # Override default.
    assert eng_with.implied_job_config(
        JobConfig(project_id='specific')).project_id == 'specific'
コード例 #6
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_schedule_and_device_both_not_supported(build):
    scheduled_op = cirq.ScheduledOperation(time=None,
                                           duration=None,
                                           operation=cirq.H.on(
                                               cirq.NamedQubit("dorothy")))
    schedule = cirq.Schedule(device=Foxtail,
                             scheduled_operations=[scheduled_op])
    eng = Engine(api_key="key")
    with pytest.raises(ValueError, match='Device'):
        eng.run(schedule, JobConfig('project-id'), device=Foxtail)
コード例 #7
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_schedule_device_validation_fails(build):
    scheduled_op = cirq.ScheduledOperation(time=None,
                                           duration=None,
                                           operation=cirq.H.on(
                                               cirq.NamedQubit("dorothy")))
    schedule = cirq.Schedule(device=Foxtail,
                             scheduled_operations=[scheduled_op])

    with pytest.raises(ValueError):
        Engine(api_key="key").run(schedule, JobConfig('project-id'))
コード例 #8
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_implied_job_config(build):
    eng = Engine(api_key="key")

    # Infer all from project id.
    implied = eng.implied_job_config(JobConfig(project_id='project_id'))
    assert implied.project_id == 'project_id'
    assert re.fullmatch(r'prog-[0-9A-Z]+', implied.program_id)
    assert implied.job_id == 'job-0'
    assert implied.gcs_prefix == 'gs://gqe-project_id/'
    assert re.fullmatch(
        r'gs://gqe-project_id/programs/prog-[0-9A-Z]+/prog-[0-9A-Z]+',
        implied.gcs_program)
    assert re.fullmatch(
        r'gs://gqe-project_id/programs/prog-[0-9A-Z]+/jobs/job-0',
        implied.gcs_results)

    # Force program id.
    implied = eng.implied_job_config(JobConfig(project_id='j', program_id='g'))
    assert implied.project_id == 'j'
    assert implied.program_id == 'g'
    assert implied.job_id == 'job-0'
    assert implied.gcs_prefix == 'gs://gqe-j/'
    assert implied.gcs_program == 'gs://gqe-j/programs/g/g'
    assert implied.gcs_results == 'gs://gqe-j/programs/g/jobs/job-0'

    # Force all.
    implied = eng.implied_job_config(
        JobConfig(project_id='a',
                  program_id='b',
                  job_id='c',
                  gcs_prefix='gs://d',
                  gcs_program='e',
                  gcs_results='f'))
    assert implied.project_id == 'a'
    assert implied.program_id == 'b'
    assert implied.job_id == 'c'
    assert implied.gcs_prefix == 'gs://d/'
    assert implied.gcs_program == 'e'
    assert implied.gcs_results == 'f'
コード例 #9
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_run_sweep_params(build):
    service = mock.Mock()
    build.return_value = service
    programs = service.projects().programs()
    jobs = programs.jobs()
    programs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test'
    }
    jobs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'READY'
        }
    }
    jobs.get().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'SUCCESS'
        }
    }
    jobs.getResult().execute.return_value = {'result': MessageToDict(_RESULTS)}

    job = Engine(api_key="key").run_sweep(
        cirq.moment_by_moment_schedule(cirq.UnconstrainedDevice,
                                       cirq.Circuit()),
        JobConfig('project-id', gcs_prefix='gs://bucket/folder'),
        params=[cirq.ParamResolver({'a': 1}),
                cirq.ParamResolver({'a': 2})])
    results = job.results()
    assert len(results) == 2
    for i, v in enumerate([1, 2]):
        assert results[i].repetitions == 1
        assert results[i].params.param_dict == {'a': v}
        assert results[i].measurements == {'q': np.array([[0]], dtype='uint8')}
    build.assert_called_with(
        'quantum',
        'v1alpha1',
        discoveryServiceUrl=('https://{api}.googleapis.com'
                             '/$discovery/rest?version='
                             '{apiVersion}&key=key'))
    assert programs.create.call_args[1]['parent'] == 'projects/project-id'
    sweeps = programs.create.call_args[1]['body']['code']['parameterSweeps']
    assert len(sweeps) == 2
    for i, v in enumerate([1, 2]):
        assert sweeps[i]['repetitions'] == 1
        assert sweeps[i]['sweep']['factors'][0]['sweeps'][0]['points'][
            'points'] == [v]
    assert jobs.create.call_args[1][
        'parent'] == 'projects/project-id/programs/test'
    assert jobs.get().execute.call_count == 1
    assert jobs.getResult().execute.call_count == 1
コード例 #10
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_implied_job_config_gcs_prefix(build):
    eng = Engine(api_key="key")
    config = JobConfig(project_id='project_id')

    # Implied by project id.
    assert eng.implied_job_config(config).gcs_prefix == 'gs://gqe-project_id/'

    # Bad default.
    eng_with_bad = Engine(api_key="key", default_gcs_prefix='bad_prefix')
    with pytest.raises(ValueError, match='gcs_prefix must be of the form'):
        _ = eng_with_bad.implied_job_config(config)

    # Good default without slash.
    eng_with = Engine(api_key="key", default_gcs_prefix='gs://good')
    assert eng_with.implied_job_config(config).gcs_prefix == 'gs://good/'

    # Good default with slash.
    eng_with = Engine(api_key="key", default_gcs_prefix='gs://good/')
    assert eng_with.implied_job_config(config).gcs_prefix == 'gs://good/'

    # Bad override.
    config.gcs_prefix = 'bad_prefix'
    with pytest.raises(ValueError, match='gcs_prefix must be of the form'):
        _ = eng.implied_job_config(config)
    with pytest.raises(ValueError, match='gcs_prefix must be of the form'):
        _ = eng_with_bad.implied_job_config(config)

    # Good override without slash.
    config.gcs_prefix = 'gs://better'
    assert eng.implied_job_config(config).gcs_prefix == 'gs://better/'
    assert eng_with.implied_job_config(config).gcs_prefix == 'gs://better/'

    # Good override with slash.
    config.gcs_prefix = 'gs://better/'
    assert eng.implied_job_config(config).gcs_prefix == 'gs://better/'
    assert eng_with.implied_job_config(config).gcs_prefix == 'gs://better/'
コード例 #11
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_run_circuit(build):
    service = mock.Mock()
    build.return_value = service
    programs = service.projects().programs()
    jobs = programs.jobs()
    programs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test'
    }
    jobs.create().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'READY'
        }
    }
    jobs.get().execute.return_value = {
        'name': 'projects/project-id/programs/test/jobs/test',
        'executionStatus': {
            'state': 'SUCCESS'
        }
    }
    jobs.getResult().execute.return_value = {
        'result': MessageToDict(_A_RESULT)
    }

    result = Engine(api_key="key").run(
        cirq.Circuit(), JobConfig('project-id',
                                  gcs_prefix='gs://bucket/folder'))
    assert result.repetitions == 1
    assert result.params.param_dict == {'a': 1}
    assert result.measurements == {'q': np.array([[0]], dtype='uint8')}
    build.assert_called_with(
        'quantum',
        'v1alpha1',
        discoveryServiceUrl=('https://{api}.googleapis.com'
                             '/$discovery/rest?version='
                             '{apiVersion}&key=key'))
    assert programs.create.call_args[1]['parent'] == 'projects/project-id'
    assert jobs.create.call_args[1][
        'parent'] == 'projects/project-id/programs/test'
    assert jobs.get().execute.call_count == 1
    assert jobs.getResult().execute.call_count == 1
コード例 #12
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_bad_priority(build):
    eng = Engine(api_key="key")
    with pytest.raises(ValueError, match='priority must be'):
        eng.run(cirq.Circuit(),
                JobConfig('project-id', gcs_prefix='gs://bucket/folder'),
                priority=1001)
コード例 #13
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_unsupported_program_type(build):
    eng = Engine(api_key="key")
    with pytest.raises(TypeError, match='program'):
        eng.run(program="this isn't even the right type of thing!",
                job_config=JobConfig('project-id'),
                device=Foxtail)
コード例 #14
0
ファイル: engine_test.py プロジェクト: YZNIU/Cirq
def test_circuit_device_validation_fails(build):
    circuit = cirq.Circuit.from_ops(cirq.H.on(cirq.NamedQubit("dorothy")))
    with pytest.raises(ValueError, match='Unsupported qubit type'):
        Engine(api_key="key").run(circuit, JobConfig('project-id'), Foxtail)