Ejemplo n.º 1
0
    def _serialize_program_v1(
            self, program: Program, sweeps: List[Sweep],
            repetitions: int) -> Tuple[Dict[str, Any], Dict[str, Any]]:
        schedule = self.program_as_schedule(program)
        schedule.device.validate_schedule(schedule)

        program_descriptor = v1.program_pb2.Program.DESCRIPTOR
        program_dict = {}  # type: Dict[str, Any]
        program_dict['@type'] = TYPE_PREFIX + program_descriptor.full_name
        program_dict['operations'] = [
            op for op in schedule_to_proto_dicts(schedule)
        ]

        context_descriptor = v1.program_pb2.RunContext.DESCRIPTOR
        context_dict = {}  # type: Dict[str, Any]
        context_dict['@type'] = TYPE_PREFIX + context_descriptor.full_name
        context_dict['parameter_sweeps'] = [
            sweep_to_proto_dict(sweep, repetitions) for sweep in sweeps
        ]
        return program_dict, context_dict
Ejemplo n.º 2
0
    def run_sweep(
            self,
            *,  # Force keyword args.
            program: Union[circuits.Circuit, Schedule],
            job_config: Optional[JobConfig] = None,
            params: Sweepable = None,
            repetitions: int = 1,
            priority: int = 500,
            processor_ids: Sequence[str] = ('xmonsim',)) -> 'EngineJob':
        """Runs the supplied Circuit or Schedule via Quantum Engine.

        In contrast to run, this runs across multiple parameter sweeps, and
        does not block until a result is returned.

        Args:
            program: The Circuit or Schedule to execute. If a circuit is
                provided, a moment by moment schedule will be used.
            job_config: Configures the names of programs and jobs.
            params: Parameters to run with the program.
            repetitions: The number of circuit repetitions to run.
            priority: The priority to run at, 0-100.
            processor_ids: The engine processors to run against.

        Returns:
            An EngineJob. If this is iterated over it returns a list of
            TrialResults, one for each parameter sweep.
        """

        job_config = self.implied_job_config(job_config)
        schedule = self.program_as_schedule(program)

        # Check program to run and program parameters.
        if not 0 <= priority < 1000:
            raise ValueError('priority must be between 0 and 1000')

        schedule.device.validate_schedule(schedule)

        # Create program.
        sweeps = _sweepable_to_sweeps(params or ParamResolver({}))
        program_dict = {}  # type: Dict[str, Any]

        program_dict['parameter_sweeps'] = [
            sweep_to_proto_dict(sweep, repetitions) for
            sweep in sweeps]
        program_dict['operations'] = [op for op in
                                      schedule_to_proto_dicts(schedule)]
        code = {
            '@type': 'type.googleapis.com/cirq.api.google.v1.Program'}
        code.update(program_dict)
        request = {
            'name': 'projects/%s/programs/%s' % (job_config.project_id,
                                                 job_config.program_id,),
            'gcs_code_location': {'uri': job_config.gcs_program},
            'code': code,
        }
        response = self.service.projects().programs().create(
            parent='projects/%s' % job_config.project_id,
            body=request).execute()

        # Create job.
        request = {
            'name': '%s/jobs/%s' % (response['name'], job_config.job_id),
            'output_config': {
                'gcs_results_location': {
                    'uri': job_config.gcs_results
                }
            },
            'scheduling_config': {
                'priority': priority,
                'processor_selector': {
                    'processor_names': [
                        'projects/%s/processors/%s' %
                        (job_config.project_id, processor_id)
                        for processor_id in processor_ids
                    ]
                }
            },
        }
        response = self.service.projects().programs().jobs().create(
            parent=response['name'], body=request).execute()

        return EngineJob(job_config, response, self)
Ejemplo n.º 3
0
def test_sweep_to_proto_fail(bad_sweep):
    with pytest.raises(ValueError):
        params.sweep_to_proto_dict(bad_sweep)
Ejemplo n.º 4
0
def test_sweep_to_proto_dict(sweep, expected):
    proto = params.sweep_to_proto_dict(sweep)
    out = params.sweep_from_proto_dict(proto)
    assert out == expected