Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cirq/google/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ def run_batch(
of the format 'job-################YYMMDD' will be generated,
where # is alphanumeric and YYMMDD is the current year, month,
and day.
params_list: Parameter sweeps to use with the circuits. The number
params_list: Parameter sweeps to use with the circuits. The number
of sweeps should match the number of circuits and will be
paired in order with the circuits.
paired in order with the circuits. If this is None, it is
assumed that the circuits are not parameterized and do not
require sweeps.
repetitions: Number of circuit repetitions to run. Each sweep value
of each circuit in the batch will run with the same repetitions.
processor_ids: The engine processors that should be candidates
Expand All @@ -347,7 +349,9 @@ def run_batch(
for a circuit are listed in the order imposed by the associated
parameter sweep.
"""
if not params_list or len(programs) != len(params_list):
if params_list is None:
params_list = [None] * len(programs)
elif len(programs) != len(params_list):
raise ValueError('Number of circuits and sweeps must match')
if not processor_ids:
raise ValueError('Processor id must be specified.')
Expand Down
35 changes: 31 additions & 4 deletions cirq/google/engine/engine_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self,
program_id: Unique ID of the program within the parent project.
context: Engine configuration and context to use.
_program: The optional current program state.
result_type: Whether the program was created using a BatchProgram.
result_type: The type of program that was created.
"""
self.project_id = project_id
self.program_id = program_id
Expand Down Expand Up @@ -135,7 +135,9 @@ def run_batch(
where # is alphanumeric and YYMMDD is the current year, month,
and day.
params_list: Parameter sweeps to run with the program. There must
be one Sweepable object for each circuit in the batch.
be one Sweepable object for each circuit in the batch. If this
is None, it is assumed that the circuits are not parameterized
and do not require sweeps.
repetitions: The number of circuit repetitions to run.
processor_ids: The engine processors that should be candidates
to run the program. Only one of these will be scheduled for
Expand All @@ -149,16 +151,20 @@ def run_batch(
first, then the TrialResults for the second, etc. The TrialResults
for a circuit are listed in the order imposed by the associated
parameter sweep.

Raises:
ValueError: if the program was not a batch program or no processors
were supplied.
"""
import cirq.google.engine.engine as engine_base
if self.result_type != ResultType.Batch:
raise ValueError('Can only use run_batch() in batch mode.')
if params_list is None:
params_list = [None] * self.batch_size()
if not job_id:
job_id = engine_base._make_random_id('job-')
if not processor_ids:
raise ValueError('No processors specified')
if not params_list:
raise ValueError('No parameter list specified')

# Pack the run contexts into batches
batch = v2.batch_pb2.BatchRunContext()
Expand Down Expand Up @@ -464,6 +470,27 @@ def get_circuit(self, program_num: Optional[int] = None) -> 'Circuit':
self.project_id, self.program_id, True)
return self._deserialize_program(self._program.code, program_num)

def batch_size(self) -> int:
"""Returns the number of programs in a batch program.

Raises:
ValueError: if the program created was not a batch program.
"""
if self.result_type != ResultType.Batch:
raise ValueError('Program was not a batch program but instead '
f'was of type {self.result_type}.')
import cirq.google.engine.engine as engine_base
if not self._program or not self._program.HasField('code'):
self._program = self.context.client.get_program(
self.project_id, self.program_id, True)
code = self._program.code
code_type = code.type_url[len(engine_base.TYPE_PREFIX):]
if code_type == 'cirq.google.api.v2.BatchProgram':
batch = v2.batch_pb2.BatchProgram.FromString(code.value)
return len(batch.programs)
raise ValueError('Program was not a batch program but instead was of '
f'type {code_type}.')

@staticmethod
def _deserialize_program(code: qtypes.any_pb2.Any,
program_num: Optional[int] = None) -> 'Circuit':
Expand Down
Loading