コード例 #1
0
def get_engine_sampler(processor_id: str, gate_set_name: str,
                       project_id: Optional[str] = None) \
        -> 'cirq.google.QuantumEngineSampler':
    """Get an EngineSampler assuming some sensible defaults.

    This uses the environment variable GOOGLE_CLOUD_PROJECT for the Engine
    project_id, unless set explicitly.

    Args:
        processor_id: Engine processor ID (from Cloud console or
            ``Engine.list_processors``).
        gate_set_name: One of ['sqrt_iswap', 'sycamore'].
            See `cirq.google.NAMED_GATESETS`.
        project_id: Optional explicit Google Cloud project id. Otherwise,
            this defaults to the environment variable GOOGLE_CLOUD_PROJECT.
            By using an environment variable, you can avoid hard-coding
            personal project IDs in shared code.

    Returns:
        A `QuantumEngineSampler` instance.

    Raises:
         ValueError: If the supplied gate set is not a supported gate set name.
         EnvironmentError: If no project_id is specified and the environment
            variable GOOGLE_CLOUD_PROJECT is not set.
    """
    try:
        gate_set = gate_sets.NAMED_GATESETS[gate_set_name]
    except KeyError:
        raise ValueError(f"Please use one of the following gateset names: "
                         f"{sorted(gate_sets.NAMED_GATESETS.keys())}")

    return engine.get_engine(project_id).sampler(processor_id=processor_id,
                                                 gate_set=gate_set)
コード例 #2
0
def engine_from_environment() -> 'cirq.google.Engine':
    """Returns an Engine instance configured using environment variables.

    If the environment variables are set, but incorrect, an authentication
    failure will occur when attempting to run jobs on the engine.

    Required Environment Variables:
        CIRQ_QUANTUM_ENGINE_DEFAULT_PROJECT_ID: The name of a google cloud
            project, with the quantum engine enabled, that you have access to.

    Raises:
        EnvironmentError: The environment variables are not set.
    """
    project_id = os.environ.get(ENV_PROJECT_ID)
    if not project_id:
        raise EnvironmentError(
            'Environment variable {} is not set.'.format(ENV_PROJECT_ID))
    return engine.get_engine(project_id)