Skip to content

Commit

Permalink
Remove XMON as engine default (#3138)
Browse files Browse the repository at this point in the history
- Engine now raises ValueError if you don't specify gate_set.
- While technically backwards incompatible, I don't think anyone
will have working code that doesn't specify a gate_set here.
  • Loading branch information
dstrain115 committed Jul 15, 2020
1 parent 1249276 commit 8071b0c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
18 changes: 8 additions & 10 deletions cirq/google/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from google.protobuf import any_pb2

from cirq import circuits, study, value
from cirq.google import gate_sets
from cirq.google import serializable_gate_set as sgs
from cirq.google.api import v1, v2
from cirq.google.engine import (engine_client, engine_program, engine_job,
Expand Down Expand Up @@ -208,7 +207,8 @@ def run(
Returns:
A single TrialResult for this run.
"""
gate_set = gate_set or gate_sets.XMON
if not gate_set:
raise ValueError('No gate set provided')
return list(
self.run_sweep(program=program,
program_id=program_id,
Expand Down Expand Up @@ -269,7 +269,8 @@ def run_sweep(
An EngineJob. If this is iterated over it returns a list of
TrialResults, one for each parameter sweep.
"""
gate_set = gate_set or gate_sets.XMON
if not gate_set:
raise ValueError('No gate set provided')
engine_program = self.create_program(program, program_id, gate_set,
program_description,
program_labels)
Expand Down Expand Up @@ -374,7 +375,8 @@ def create_program(
Returns:
A EngineProgram for the newly created program.
"""
gate_set = gate_set or gate_sets.XMON
if not gate_set:
raise ValueError('No gate set provided')

if not program_id:
program_id = _make_random_id('prog-')
Expand Down Expand Up @@ -436,12 +438,8 @@ def create_batch_program(
new_program,
batch_mode=True)

def _serialize_program(self,
program: 'cirq.Circuit',
gate_set: Optional[sgs.SerializableGateSet] = None
) -> any_pb2.Any:
gate_set = gate_set or gate_sets.XMON

def _serialize_program(self, program: 'cirq.Circuit',
gate_set: sgs.SerializableGateSet) -> any_pb2.Any:
if not isinstance(program, circuits.Circuit):
raise TypeError(f'Unrecognized program type: {type(program)}')
program.device.validate_circuit(program)
Expand Down
51 changes: 35 additions & 16 deletions cirq/google/engine/engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ def test_run_circuit(client):
result = engine.run(program=_CIRCUIT,
program_id='prog',
job_id='job-id',
processor_ids=['mysim'])
processor_ids=['mysim'],
gate_set=cg.XMON)

assert result.repetitions == 1
assert result.params.param_dict == {'a': 1}
Expand Down Expand Up @@ -323,15 +324,27 @@ def test_circuit_device_validation_fails():
cirq.Z(cirq.NamedQubit("dorothy"))]))
engine = cg.Engine(project_id='project-id')
with pytest.raises(ValueError, match='Unsupported qubit type'):
engine.run_sweep(program=circuit)
engine.run_sweep(program=circuit, gate_set=cg.XMON)
with pytest.raises(ValueError, match='Unsupported qubit type'):
engine.create_program(circuit)
engine.create_program(circuit, gate_set=cg.XMON)


def test_no_gate_set():
circuit = cirq.Circuit(device=cg.Sycamore)
engine = cg.Engine(project_id='project-id')
with pytest.raises(ValueError, match='No gate set'):
engine.run(program=circuit)
with pytest.raises(ValueError, match='No gate set'):
engine.run_sweep(program=circuit)
with pytest.raises(ValueError, match='No gate set'):
engine.create_program(program=circuit)


def test_unsupported_program_type():
engine = cg.Engine(project_id='project-id')
with pytest.raises(TypeError, match='program'):
engine.run(program="this isn't even the right type of thing!")
engine.run(program="this isn't even the right type of thing!",
gate_set=cg.XMON)


@mock.patch('cirq.google.engine.engine_client.EngineClient')
Expand All @@ -358,7 +371,7 @@ def test_run_circuit_failed(client):
RuntimeError,
match='Job projects/proj/programs/prog/jobs/job-id on processor'
' myqc failed. SYSTEM_ERROR: Not good'):
engine.run(program=_CIRCUIT)
engine.run(program=_CIRCUIT, gate_set=cg.XMON)


@mock.patch('cirq.google.engine.engine_client.EngineClient')
Expand All @@ -384,7 +397,7 @@ def test_run_circuit_failed_missing_processor_name(client):
RuntimeError,
match='Job projects/proj/programs/prog/jobs/job-id on processor'
' UNKNOWN failed. SYSTEM_ERROR: Not good'):
engine.run(program=_CIRCUIT)
engine.run(program=_CIRCUIT, gate_set=cg.XMON)


@mock.patch('cirq.google.engine.engine_client.EngineClient')
Expand All @@ -405,7 +418,7 @@ def test_run_circuit_cancelled(client):
with pytest.raises(RuntimeError,
match='Job projects/proj/programs/prog/jobs/job-id'
' failed in state CANCELLED.'):
engine.run(program=_CIRCUIT)
engine.run(program=_CIRCUIT, gate_set=cg.XMON)


@mock.patch('cirq.google.engine.engine_client.EngineClient')
Expand All @@ -425,7 +438,7 @@ def test_run_circuit_timeout(patched_time_sleep, client):

engine = cg.Engine(project_id='project-id', timeout=600)
with pytest.raises(RuntimeError, match='Timed out'):
engine.run(program=_CIRCUIT)
engine.run(program=_CIRCUIT, gate_set=cg.XMON)


@mock.patch('cirq.google.engine.engine_client.EngineClient')
Expand All @@ -436,7 +449,8 @@ def test_run_sweep_params(client):
job = engine.run_sweep(
program=_CIRCUIT,
params=[cirq.ParamResolver({'a': 1}),
cirq.ParamResolver({'a': 2})])
cirq.ParamResolver({'a': 2})],
gate_set=cg.XMON)
results = job.results()
assert len(results) == 2
for i, v in enumerate([1, 2]):
Expand Down Expand Up @@ -467,7 +481,8 @@ def test_run_sweep_v1(client):
proto_version=cg.engine.engine.ProtoVersion.V1)
job = engine.run_sweep(program=_CIRCUIT,
job_id='job-id',
params=cirq.Points('a', [1, 2]))
params=cirq.Points('a', [1, 2]),
gate_set=cg.XMON)
results = job.results()
assert engine.context.proto_version == cg.engine.engine.ProtoVersion.V1
assert len(results) == 2
Expand All @@ -493,7 +508,7 @@ def test_run_multiple_times(client):

engine = cg.Engine(project_id='proj',
proto_version=cg.engine.engine.ProtoVersion.V1)
program = engine.create_program(program=_CIRCUIT)
program = engine.create_program(program=_CIRCUIT, gate_set=cg.XMON)
program.run(param_resolver=cirq.ParamResolver({'a': 1}))
run_context = v1.program_pb2.RunContext()
client().create_job.call_args[1]['run_context'].Unpack(run_context)
Expand Down Expand Up @@ -528,7 +543,8 @@ def test_run_sweep_v2(client):
)
job = engine.run_sweep(program=_CIRCUIT,
job_id='job-id',
params=cirq.Points('a', [1, 2]))
params=cirq.Points('a', [1, 2]),
gate_set=cg.XMON)
results = job.results()
assert len(results) == 2
for i, v in enumerate([1, 2]):
Expand Down Expand Up @@ -638,7 +654,8 @@ def test_bad_result_proto(client):
proto_version=cg.engine.engine.ProtoVersion.V2)
job = engine.run_sweep(program=_CIRCUIT,
job_id='job-id',
params=cirq.Points('a', [1, 2]))
params=cirq.Points('a', [1, 2]),
gate_set=cg.XMON)
with pytest.raises(ValueError, match='invalid result proto version'):
job.results()

Expand All @@ -647,9 +664,9 @@ def test_bad_program_proto():
engine = cg.Engine(project_id='project-id',
proto_version=cg.engine.engine.ProtoVersion.UNDEFINED)
with pytest.raises(ValueError, match='invalid program proto version'):
engine.run_sweep(program=_CIRCUIT)
engine.run_sweep(program=_CIRCUIT, gate_set=cg.XMON)
with pytest.raises(ValueError, match='invalid program proto version'):
engine.create_program(_CIRCUIT)
engine.create_program(_CIRCUIT, gate_set=cg.XMON)


def test_get_program():
Expand All @@ -659,7 +676,9 @@ def test_get_program():
@mock.patch('cirq.google.engine.engine_client.EngineClient')
def test_create_program(client):
client().create_program.return_value = ('prog', qtypes.QuantumProgram())
result = cg.Engine(project_id='proj').create_program(_CIRCUIT, 'prog')
result = cg.Engine(project_id='proj').create_program(_CIRCUIT,
'prog',
gate_set=cg.XMON)
client().create_program.assert_called_once()
assert result.program_id == 'prog'

Expand Down

0 comments on commit 8071b0c

Please sign in to comment.