示例#1
0
 def on_job_result(self, job: collector.CircuitSampleJob,
                   result: study.TrialResult):
     job_id = cast(ops.PauliString, job.tag)
     parities = result.histogram(key='out',
                                 fold_func=lambda bits: np.sum(bits) % 2)
     self._zeros[job_id] += parities[0]
     self._ones[job_id] += parities[1]
示例#2
0
    def get_job_results(self, job_resource_name: str) -> List[TrialResult]:
        """Returns the actual results (not metadata) of a completed job.

        Params:
            job_resource_name: A string of the form
                `projects/project_id/programs/program_id/jobs/job_id`.

        Returns:
            An iterable over the TrialResult, one per parameter in the
            parameter sweep.
        """
        response = self.service.projects().programs().jobs().getResult(
            parent=job_resource_name).execute()
        trial_results = []
        for sweep_result in response['result']['sweepResults']:
            sweep_repetitions = sweep_result['repetitions']
            key_sizes = [(m['key'], len(m['qubits']))
                         for m in sweep_result['measurementKeys']]
            for result in sweep_result['parameterizedResults']:
                data = base64.standard_b64decode(result['measurementResults'])
                measurements = unpack_results(data, sweep_repetitions,
                                              key_sizes)

                trial_results.append(TrialResult(
                    params=ParamResolver(
                        result.get('params', {}).get('assignments', {})),
                    repetitions=sweep_repetitions,
                    measurements=measurements))
        return trial_results
示例#3
0
    def run_sweep(self,
                  program: Union[Circuit, Schedule],
                  params: Sweepable = ParamResolver({}),
                  repetitions: int = 1,
                  qubit_order: ops.QubitOrderOrList = ops.QubitOrder.DEFAULT,
                  extensions: Extensions = None) -> List[TrialResult]:
        """Runs the entire supplied Circuit, mimicking the quantum hardware.

        If one wants access to the wave function (both setting and getting),
        the "simulate" methods should be used.

        The initial state of the  run methods is the all zeros state in the
        computational basis.

        Args:
            program: The circuit or schedule to simulate.
            params: Parameters to run with the program.
            repetitions: The number of repetitions to simulate.
            qubit_order: Determines the canonical ordering of the qubits used to
                define the order of amplitudes in the wave function.
            extensions: Extensions that will be applied while trying to
                decompose the circuit's gates into XmonGates. If None, this
                uses the default of xmon_gate_ext.

        Returns:
            TrialResult list for this run; one for each possible parameter
            resolver.
        """
        circuit = (program
                   if isinstance(program, Circuit) else program.to_circuit())
        param_resolvers = self._to_resolvers(params or ParamResolver({}))

        trial_results = []  # type: List[TrialResult]
        qubit_order = ops.QubitOrder.as_qubit_order(qubit_order)
        for param_resolver in param_resolvers:
            xmon_circuit, keys = self._to_xmon_circuit(
                circuit, param_resolver, extensions or xmon_gate_ext)
            if xmon_circuit.are_all_measurements_terminal():
                measurements = self._run_sweep_sample(xmon_circuit,
                                                      repetitions, qubit_order)
            else:
                measurements = self._run_sweep_repeat(keys, xmon_circuit,
                                                      repetitions, qubit_order)
            trial_results.append(
                TrialResult(params=param_resolver,
                            repetitions=repetitions,
                            measurements={
                                k: np.array(v)
                                for k, v in measurements.items()
                            }))
        return trial_results
示例#4
0
文件: engine.py 项目: zhiyuzh/Cirq
    def _get_job_results_v1(self, result: Dict[str, Any]) -> List[TrialResult]:
        trial_results = []
        for sweep_result in result['sweepResults']:
            sweep_repetitions = sweep_result['repetitions']
            key_sizes = [(m['key'], len(m['qubits']))
                         for m in sweep_result['measurementKeys']]
            for result in sweep_result['parameterizedResults']:
                data = base64.standard_b64decode(result['measurementResults'])
                measurements = api_v1.unpack_results(data, sweep_repetitions,
                                                     key_sizes)

                trial_results.append(
                    TrialResult.from_single_parameter_set(
                        params=ParamResolver(
                            result.get('params', {}).get('assignments', {})),
                        measurements=measurements))
        return trial_results