def _execute_and_read_result( quantum_computer: QuantumComputer, executable: QuantumExecutable, measurement_id_map: Dict[str, str], resolver: cirq.ParamResolverOrSimilarType, memory_map: Optional[Dict[str, Union[int, float, Sequence[int], Sequence[float]]]] = None, ) -> cirq.Result: """Execute the `pyquil.api.QuantumExecutable` and parse the measurements into a `cirq.Result`. Args: quantum_computer: The `pyquil.api.QuantumComputer` on which to execute and from which to read results. executable: The fully compiled `pyquil.api.QuantumExecutable` to run. measurement_id_map: A dict mapping cirq measurement keys to pyQuil read out regions. resolver: The `cirq.ParamResolverOrSimilarType` to include on the returned `cirq.Result`. memory_map: A dict of values to write to memory values on the `quantum_computer`. The `pyquil.api.QuantumAbstractMachine` reads these values into memory regions on the pre-compiled `executable` during execution. Returns: A `cirq.Result` with measurements read from the `quantum_computer`. Raises: ValueError: measurement_id_map references an undefined pyQuil readout region. """ if memory_map is None: memory_map = {} for region_name, values in memory_map.items(): executable.write_memory(region_name=region_name, value=values) qam_execution_result = quantum_computer.qam.run(executable) measurements = {} # For every key, value in QuilOutput#measurement_id_map, use the value to read # Rigetti QCS results and assign to measurements by key. for cirq_memory_key, pyquil_region in measurement_id_map.items(): readout = qam_execution_result.readout_data.get(pyquil_region) if readout is None: raise ValueError( f'readout data does not have values for region "{pyquil_region}"' ) measurements[cirq_memory_key] = readout logger.debug(f"measurement_id_map {measurement_id_map}") logger.debug(f"measurements {measurements}") # collect results in a cirq.Result. result = cirq.Result( params=cast(cirq.ParamResolver, resolver or cirq.ParamResolver({})), measurements=measurements, ) # noqa return result
def execute(self, executable: QuantumExecutable) -> QVMExecuteResponse: """ Synchronously execute the input program to completion. """ executable = executable.copy() if not isinstance(executable, Program): raise TypeError( f"`QVM#executable` argument must be a `Program`; got {type(executable)}" ) result_memory = {} for region in executable.declarations.keys(): result_memory[region] = np.ndarray((executable.num_shots, 0), dtype=np.int64) trials = executable.num_shots classical_addresses = get_classical_addresses_from_program(executable) if self.noise_model is not None: executable = apply_noise_model(executable, self.noise_model) executable._set_parameter_values_at_runtime() request = qvm_run_request( executable, classical_addresses, trials, self.measurement_noise, self.gate_noise, self.random_seed, ) response = self._qvm_client.run_program(request) ram = {key: np.array(val) for key, val in response.results.items()} result_memory.update(ram) return QVMExecuteResponse(executable=executable, memory=result_memory)
def execute(self, executable: QuantumExecutable) -> QPUExecuteResponse: """ Enqueue a job for execution on the QPU. Returns a ``QPUExecuteResponse``, a job descriptor which should be passed directly to ``QPU.get_result`` to retrieve results. """ executable = executable.copy() assert isinstance( executable, EncryptedProgram ), "QPU#execute requires an rpcq.EncryptedProgram. Create one with QuantumComputer#compile" assert ( executable.ro_sources is not None ), "To run on a QPU, a program must include ``MEASURE``, ``CAPTURE``, and/or ``RAW-CAPTURE`` instructions" request = RunProgramRequest( id=str(uuid.uuid4()), priority=self.priority, program=executable.program, patch_values=self._build_patch_values(executable), ) job_id = self._qpu_client.run_program(request).job_id return QPUExecuteResponse(_executable=executable, job_id=job_id)