Skip to content

Commit

Permalink
Remove code for GCS locations since they are no longer used. (#2668)
Browse files Browse the repository at this point in the history
* Remove code for GCS locations since they are no longer used.
* Ran incremental format script.
  • Loading branch information
wcourtney committed Jan 3, 2020
1 parent 02ee285 commit f306544
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 143 deletions.
57 changes: 4 additions & 53 deletions cirq/google/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import enum
import json
import random
import re
import string
import sys
import time
Expand All @@ -51,7 +50,6 @@
if TYPE_CHECKING:
import cirq

gcs_prefix_pattern = re.compile('gs://[a-z0-9._/-]+')
TYPE_PREFIX = 'type.googleapis.com/'


Expand Down Expand Up @@ -106,36 +104,22 @@ class JobConfig:
called a Job. This object contains the configuration for a job.
"""

def __init__(self,
job_id: Optional[str] = None,
gcs_prefix: Optional[str] = None,
gcs_results: Optional[str] = None) -> None:
def __init__(self, job_id: Optional[str] = None) -> None:
"""Configuration for a job that is run on Quantum Engine.
Args:
job_id: Id of the job to create, defaults to 'job-0'.
gcs_prefix: Google Cloud Storage bucket and object prefix to use
for storing programs and results. The bucket will be created if
needed. Must be in the form "gs://bucket-name/object-prefix/".
gcs_results: Explicit override for the results storage location.
"""
self.job_id = job_id
self.gcs_prefix = gcs_prefix
self.gcs_results = gcs_results

def copy(self) -> 'JobConfig':
return JobConfig(job_id=self.job_id,
gcs_prefix=self.gcs_prefix,
gcs_results=self.gcs_results)
return JobConfig(job_id=self.job_id)

def _value_equality_values_(self):
return (self.job_id, self.gcs_prefix, self.gcs_results)
return (self.job_id)

def __repr__(self):
return ('cirq.google.JobConfig(job_id={!r}, '
'gcs_prefix={!r}, '
'gcs_results={!r})').format(self.job_id, self.gcs_prefix,
self.gcs_results)
return ('cirq.google.JobConfig(job_id={!r})').format(self.job_id)


class Engine:
Expand Down Expand Up @@ -171,7 +155,6 @@ def __init__(self,
project_id: str,
version: Optional[str] = None,
discovery_url: Optional[str] = None,
default_gcs_prefix: Optional[str] = None,
proto_version: ProtoVersion = ProtoVersion.V1,
service_args: Optional[Dict] = None,
verbose: bool = True) -> None:
Expand All @@ -185,9 +168,6 @@ def __init__(self,
version: API version.
discovery_url: Discovery url for the API to select a non-default
backend for the Engine. Incompatible with `version` argument.
default_gcs_prefix: A fallback gcs_prefix to use when one isn't
specified in the JobConfig given to 'run' methods.
See JobConfig for more information on gcs_prefix.
service_args: A dictionary of arguments that can be used to
configure options on the underlying apiclient. See
https://github.com/googleapis/google-api-python-client
Expand All @@ -203,7 +183,6 @@ def __init__(self,

self.project_id = project_id
self.discovery_url = discovery_url or discovery.V2_DISCOVERY_URI
self.default_gcs_prefix = default_gcs_prefix
self.max_retry_delay = 3600 # 1 hour
self.proto_version = proto_version
self.verbose = verbose
Expand Down Expand Up @@ -339,11 +318,6 @@ def create_job(
# Create job.
request = {
'name': '%s/jobs/%s' % (program_name, job_config.job_id),
'output_config': {
'gcs_results_location': {
'uri': job_config.gcs_results
}
},
'scheduling_config': {
'priority': priority,
'processor_selector': {
Expand All @@ -367,37 +341,14 @@ def implied_job_config(self, job_config: Optional[JobConfig]) -> JobConfig:
if job_config is None else job_config.copy())

# Note: inference order is important. Later ones may need earlier ones.
self._infer_gcs_prefix(implied_job_config)
self._infer_job_id(implied_job_config)
self._infer_gcs_results(implied_job_config)

return implied_job_config

def _infer_gcs_prefix(self, job_config: JobConfig) -> None:
project_id = self.project_id
gcs_prefix = (job_config.gcs_prefix or self.default_gcs_prefix or
'gs://gqe-' + project_id[project_id.rfind(':') + 1:])
if gcs_prefix and not gcs_prefix.endswith('/'):
gcs_prefix += '/'

if not gcs_prefix_pattern.match(gcs_prefix):
raise ValueError('gcs_prefix must be of the form "gs://'
'<bucket name and optional object prefix>/"')

job_config.gcs_prefix = gcs_prefix

def _infer_job_id(self, job_config: JobConfig) -> None:
if job_config.job_id is None:
job_config.job_id = _make_random_id('job-')

def _infer_gcs_results(self, job_config: JobConfig) -> None:
if job_config.gcs_prefix is None:
raise ValueError("Must infer gcs_prefix before gcs_results.")

if job_config.gcs_results is None:
job_config.gcs_results = '{}jobs/{}'.format(job_config.gcs_prefix,
job_config.job_id)

def _make_request(self, request: HttpRequest) -> Dict:
retryable_error_codes = [500, 503]
current_delay = 0.1 #100ms
Expand Down
4 changes: 1 addition & 3 deletions cirq/google/engine/engine_job_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ def test_calibration_from_job_with_no_calibration(build):

calibrations = service.projects().processors().calibrations()
engine = cg.Engine(project_id='project-id')
job = engine.run_sweep(
program=cirq.Circuit(),
job_config=cg.JobConfig(gcs_prefix='gs://bucket/folder'))
job = engine.run_sweep(program=cirq.Circuit())

calibration = job.get_calibration()
assert not calibration
Expand Down
98 changes: 11 additions & 87 deletions cirq/google/engine/engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""Tests for engine."""
import base64
import copy
import re
from unittest import mock
import numpy as np
import pytest
Expand Down Expand Up @@ -211,9 +210,7 @@


def test_job_config_repr():
v = cirq.google.JobConfig(job_id='my-job-id',
gcs_prefix='pre',
gcs_results='gc')
v = cirq.google.JobConfig(job_id='my-job-id')
cirq.testing.assert_equivalent_repr(v)


Expand All @@ -236,8 +233,7 @@ def test_run_circuit(build):

engine = cg.Engine(project_id='project-id')
result = engine.run(program=_CIRCUIT,
job_config=cg.JobConfig(
'job-id', gcs_prefix='gs://bucket/folder'),
job_config=cg.JobConfig('job-id'),
processor_ids=['mysim'])
assert result.repetitions == 1
assert result.params.param_dict == {'a': 1}
Expand All @@ -253,11 +249,6 @@ def test_run_circuit(build):
'parent': 'projects/project-id/programs/test',
'body': {
'name': 'projects/project-id/programs/test/jobs/job-id',
'output_config': {
'gcs_results_location': {
'uri': 'gs://bucket/folder/jobs/job-id'
}
},
'scheduling_config': {
'priority': 50,
'processor_selector': {
Expand Down Expand Up @@ -445,7 +436,7 @@ def test_run_sweep_params(build):
engine = cg.Engine(project_id='project-id')
job = engine.run_sweep(
program=_CIRCUIT,
job_config=cg.JobConfig('project-id', gcs_prefix='gs://bucket/folder'),
job_config=cg.JobConfig('project-id'),
params=[cirq.ParamResolver({'a': 1}),
cirq.ParamResolver({'a': 2})])
results = job.results()
Expand Down Expand Up @@ -501,7 +492,7 @@ def test_run_sweep_params_old_proto(build):
engine = cg.Engine(project_id='project-id')
job = engine.run_sweep(
program=_CIRCUIT,
job_config=cg.JobConfig('project-id', gcs_prefix='gs://bucket/folder'),
job_config=cg.JobConfig('project-id'),
params=[cirq.ParamResolver({'a': 1}),
cirq.ParamResolver({'a': 2})])
results = job.results()
Expand Down Expand Up @@ -548,8 +539,7 @@ def test_run_sweep_v1(build):

engine = cg.Engine(project_id='project-id')
job = engine.run_sweep(program=_CIRCUIT,
job_config=cg.JobConfig(
'project-id', gcs_prefix='gs://bucket/folder'),
job_config=cg.JobConfig('project-id'),
params=cirq.Points('a', [1, 2]))
results = job.results()
assert engine.proto_version == cg.engine.engine.ProtoVersion.V1
Expand Down Expand Up @@ -660,8 +650,7 @@ def test_run_sweep_v2(build):
proto_version=cg.engine.engine.ProtoVersion.V2,
)
job = engine.run_sweep(program=_CIRCUIT,
job_config=cg.JobConfig(
'project-id', gcs_prefix='gs://bucket/folder'),
job_config=cg.JobConfig('project-id'),
params=cirq.Points('a', [1, 2]))
results = job.results()
assert engine.proto_version == cg.engine.engine.ProtoVersion.V2
Expand Down Expand Up @@ -717,8 +706,7 @@ def test_run_sweep_v2_old_proto(build):
proto_version=cg.engine.engine.ProtoVersion.V2,
)
job = engine.run_sweep(program=_CIRCUIT,
job_config=cg.JobConfig(
'project-id', gcs_prefix='gs://bucket/folder'),
job_config=cg.JobConfig('project-id'),
params=cirq.Points('a', [1, 2]))
results = job.results()
assert engine.proto_version == cg.engine.engine.ProtoVersion.V2
Expand Down Expand Up @@ -781,8 +769,7 @@ def test_bad_result_proto(build):
engine = cg.Engine(project_id='project-id',
proto_version=cg.engine.engine.ProtoVersion.V2)
job = engine.run_sweep(program=_CIRCUIT,
job_config=cg.JobConfig(
'project-id', gcs_prefix='gs://bucket/folder'),
job_config=cg.JobConfig('project-id'),
params=cirq.Points('a', [1, 2]))
with pytest.raises(ValueError, match='invalid result proto version'):
job.results()
Expand Down Expand Up @@ -829,8 +816,7 @@ def test_cancel(build):

engine = cg.Engine(project_id='project-id')
job = engine.run_sweep(program=_CIRCUIT,
job_config=cg.JobConfig(
'project-id', gcs_prefix='gs://bucket/folder'))
job_config=cg.JobConfig('project-id'))
job.cancel()
assert job.job_resource_name == ('projects/project-id/programs/test/'
'jobs/test')
Expand Down Expand Up @@ -899,49 +885,6 @@ def body():
assert body()['labelFingerprint'] == 'abcdef'


@mock.patch.object(discovery, 'build')
def test_implied_job_config_gcs_prefix(build):
eng = cg.Engine(project_id='project_id')
config = cg.JobConfig()

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

# Bad default.
eng_with_bad = cg.Engine(project_id='project-id',
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 = cg.Engine(project_id='project-id',
default_gcs_prefix='gs://good')
assert eng_with.implied_job_config(config).gcs_prefix == 'gs://good/'

# Good default with slash.
eng_with = cg.Engine(project_id='project-id',
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/'


# uses re.fullmatch
@mock.patch.object(discovery, 'build')
def test_implied_job_config(build):
eng = cg.Engine(project_id='project_id')
Expand All @@ -950,27 +893,10 @@ def test_implied_job_config(build):
implied = eng.implied_job_config(cg.JobConfig())
assert implied.job_id.startswith('job-')
assert len(implied.job_id) == 26
assert implied.gcs_prefix == 'gs://gqe-project_id/'
assert re.match(r'gs://gqe-project_id/jobs/job-', implied.gcs_results)

# Force all.
implied = eng.implied_job_config(
cg.JobConfig(job_id='c', gcs_prefix='gs://d', gcs_results='f'))
implied = eng.implied_job_config(cg.JobConfig(job_id='c'))
assert implied.job_id == 'c'
assert implied.gcs_prefix == 'gs://d/'
assert implied.gcs_results == 'f'


@mock.patch.object(discovery, 'build')
def test_bad_job_config_inference_order(build):
eng = cg.Engine(project_id='project-id')
config = cg.JobConfig()

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

eng._infer_gcs_results(config)


@mock.patch.object(discovery, 'build')
Expand Down Expand Up @@ -1064,9 +990,7 @@ def test_calibration_from_job(build):
calibrations.get().execute.return_value = _CALIBRATION

engine = cg.Engine(project_id='project-id')
job = engine.run_sweep(
program=_CIRCUIT,
job_config=cg.JobConfig(gcs_prefix='gs://bucket/folder'))
job = engine.run_sweep(program=_CIRCUIT)

calibration = job.get_calibration()
assert calibration.timestamp == 1562544000021
Expand Down

0 comments on commit f306544

Please sign in to comment.