示例#1
0
def estimate_run_sweep_time(
    program: cirq.AbstractCircuit,
    params: cirq.Sweepable = None,
    repetitions: int = 1000,
    latency: Optional[float] = _BASE_LATENCY,
) -> float:
    """Compute the estimated time for running a parameter sweep across a single Circuit.

    This should approximate, in seconds, the time for the execution of a batch of circuits
    using Engine.run_sweep() on QCS at a time where there is no queue (such as a reserved slot).
    This estimation should be considered a rough approximation.  Many factors can contribute to
    the execution time of a circuit, and the run time can also vary as the service's code changes
    frequently.

    Args:
        program: circuit to be executed
        params: a parameter sweep of variable resolvers to use with the circuit
        repetitions: number of repetitions to execute per parameter sweep
        latency: Optional latency to add (defaults to 1.5 seconds)
    """
    width = len(program.all_qubits())
    depth = len(program)
    sweeps = len(list(cirq.to_resolvers(params)))
    return _estimate_run_time_seconds(width, depth, sweeps, repetitions,
                                      latency)
示例#2
0
    def run_sweep(
        self, program: cirq.AbstractCircuit, params: cirq.Sweepable, repetitions: int = 1
    ) -> Sequence[cirq.Result]:
        """Samples from the given Circuit.

        In contrast to run, this allows for sweeping over different parameter
        values.

        Args:
            program: The circuit to simulate.
            Should be generated using AQTSampler.generate_circuit_from_list
            params: Parameters to run with the program.
            repetitions: The number of repetitions to simulate.

        Returns:
            Result list for this run; one for each possible parameter
            resolver.
        """
        # TODO: Use measurement name from circuit.
        # Github issue: https://github.com/quantumlib/Cirq/issues/2199
        meas_name = 'm'
        trial_results: List[cirq.Result] = []
        for param_resolver in cirq.to_resolvers(params):
            id_str = uuid.uuid1()
            num_qubits = len(program.all_qubits())
            json_str = self._generate_json(circuit=program, param_resolver=param_resolver)
            results = self._send_json(
                json_str=json_str, id_str=id_str, repetitions=repetitions, num_qubits=num_qubits
            )
            results = results.astype(bool)
            res_dict = {meas_name: results}
            trial_results.append(cirq.ResultDict(params=param_resolver, measurements=res_dict))
        return trial_results
示例#3
0
def estimate_run_time(program: cirq.AbstractCircuit,
                      repetitions: int,
                      latency: Optional[float] = _BASE_LATENCY) -> float:
    """Compute the estimated time for running a single circuit.

    This should approximate, in seconds, the time for the execution of a batch of circuits
    using Engine.run() on QCS at a time where there is no queue (such as a reserved slot).
    This estimation should be considered a rough approximation.  Many factors can contribute to
    the execution time of a circuit, and the run time can also vary as the service's code changes
    frequently.

    Args:
        program: circuit to be executed
        repetitions: number of repetitions to execute
        latency: Optional latency to add (defaults to 1.5 seconds)
    """
    width = len(program.all_qubits())
    depth = len(program)
    return _estimate_run_time_seconds(width, depth, 1, repetitions, latency)
示例#4
0
    def serialize(self, circuit: cirq.AbstractCircuit) -> SerializedProgram:
        """Serialize the given circuit.

        Raises:
            ValueError: if the circuit has gates that are not supported or is otherwise invalid.
        """
        self._validate_circuit(circuit)
        num_qubits = self._validate_qubits(circuit.all_qubits())

        serialized_ops = self._serialize_circuit(circuit)

        # IonQ API does not support measurements, so we pass the measurement keys through
        # the metadata field.  Here we split these out of the serialized ops.
        body = {
            'qubits': num_qubits,
            'circuit': [op for op in serialized_ops if op['gate'] != 'meas'],
        }
        metadata = self._serialize_measurements(op for op in serialized_ops if op['gate'] == 'meas')
        return SerializedProgram(body=body, metadata=metadata)