Cirq Google Plugin

1.6.1 · active · verified Fri Apr 17

Cirq Google (`cirq-google`) is the module for the Cirq quantum computing framework that provides tools and access to the Google Quantum Computing Service. It enables users to run quantum circuits on Google's quantum processors and high-performance simulators. The current version is 1.6.1, and it typically releases new versions in sync with the main `cirq` library, which has a release cadence of monthly/bi-monthly patches and quarterly minor versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `QuantumEngine` client and run a simple Cirq circuit. For actual remote execution, ensure your Google Cloud environment is authenticated and `GOOGLE_CLOUD_PROJECT` (or a hardcoded project ID) is correctly configured. The example includes a fallback to a local Cirq simulator for immediate runnability without a full GCP setup.

import cirq
import cirq_google
import os

# Replace with your Google Cloud Project ID (e.g., 'your-project-id')
# For local testing, any string can be used, but for actual hardware access,
# it must be a valid GCP project ID with Quantum Engine API enabled.
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "gcp-project-for-cirq-testing")

try:
    # Initialize the QuantumEngine client
    # This assumes you have authenticated via 'gcloud auth application-default login'
    # or set the GOOGLE_APPLICATION_CREDENTIALS environment variable.
    print(f"Attempting to initialize QuantumEngine for project: {PROJECT_ID}")
    engine = cirq_google.QuantumEngine(project_id=PROJECT_ID)
    
    # For quickstart, we'll try to use a local simulator
    # For actual hardware/remote simulator usage, you would use:
    # processor = engine.get_processor('willow_pink') # Example processor
    # job = engine.run_circuit(program=circuit, processor_ids=[processor.name], repetitions=10)
    # results = job.results()

    # Define a simple circuit
    qubit = cirq.GridQubit(0, 0)
    circuit = cirq.Circuit(
        cirq.H(qubit),
        cirq.measure(qubit, key='m')
    )

    # Run the circuit on a local Cirq simulator for quick demonstration
    print("Running circuit on local Cirq simulator...")
    simulator = cirq.Simulator()
    results = simulator.run(circuit, repetitions=10)
    print("Local simulation results:")
    print(results)

except Exception as e:
    print(f"Could not initialize QuantumEngine or connect to remote resources ({e}).")
    print("Falling back to local Cirq simulator for demonstration.")
    
    # Define a simple circuit
    qubit = cirq.GridQubit(0, 0)
    circuit = cirq.Circuit(
        cirq.H(qubit),
        cirq.measure(qubit, key='m')
    )

    # Run the circuit on a local Cirq simulator
    simulator = cirq.Simulator()
    results = simulator.run(circuit, repetitions=10)
    print("Local simulation results (fallback):")
    print(results)

view raw JSON →