Esempio n. 1
0
def test_sweep_to_proto(sweep, expected):
    proto = params.sweep_to_proto(sweep)
    out = params.sweep_from_proto(proto)
    assert out == expected
Esempio n. 2
0
def test_sweep_to_proto_fail(bad_sweep):
    with pytest.raises(ValueError):
        params.sweep_to_proto(bad_sweep)
Esempio n. 3
0
File: engine.py Progetto: YZNIU/Cirq
    def run_sweep(
        self,
        program: Union[Circuit, Schedule],
        job_config: Optional[JobConfig] = None,
        device: Device = None,
        params: Sweepable = None,
        repetitions: int = 1,
        priority: int = 500,
        target_route: 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.
            device: The device on which to run a circuit. The circuit will be
                validated against this device before sending to the engine.
                If device is None, no validation will be done. Can only be
                supplied if program is a Circuit, otherwise the device from
                the Schedule will be used.
            params: Parameters to run with the program.
            repetitions: The number of circuit repetitions to run.
            priority: The priority to run at, 0-100.
            target_route: The engine route 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, device)

        # 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({}))
        proto_program = program_pb2.Program()
        for sweep in sweeps:
            sweep_proto = proto_program.parameter_sweeps.add()
            sweep_to_proto(sweep, sweep_proto)
            sweep_proto.repetitions = repetitions
        program_dict = MessageToDict(proto_program)
        program_dict['operations'] = [
            MessageToDict(op) for op in schedule_to_proto(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,
                'target_route': target_route
            },
        }
        response = self.service.projects().programs().jobs().create(
            parent=response['name'], body=request).execute()

        return EngineJob(job_config, response, self)