Cirq Google Plugin
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
-
ModuleNotFoundError: No module named 'cirq_rigetti'
cause The `cirq-rigetti` subpackage was removed starting from cirq-google v1.6.0.fixIf your code explicitly uses `cirq_rigetti`, you need to downgrade your cirq-google installation to version 1.5.0 or earlier (e.g., `pip install cirq-google==1.5.0`). Alternatively, refactor your code to use the standalone `pyquil` library. -
ModuleNotFoundError: No module named 'cirq_ft'
cause The `cirq-ft` package was removed from `cirq` and `cirq-google` starting from v1.4.0, as its functionality moved to the `Qualtran` library.fixIf your code depends on `cirq_ft`, you need to install and use the `Qualtran` library directly from its GitHub repository (`https://github.com/quantumlib/Qualtran.git`). Alternatively, downgrade your `cirq-google` installation to `cirq-google<1.4.0` if you need the old `cirq-ft` functionality. -
RuntimeError: Python version must be >= 3.11.0
cause You are trying to install or run `cirq-google` (version 1.6.0 or newer) with an unsupported Python version (e.g., 3.10 or older).fixUpgrade your Python environment to version 3.11 or later. For example, create a new virtual environment with `python3.11 -m venv .venv` and activate it. -
cirq_google.engine.calibration.engine_client.QuantumEngineError: Could not find or validate client credentials.
cause The Cirq Google client failed to authenticate with the Google Quantum Engine. This means Google Cloud credentials are not properly set up or are expired.fixEnsure you are logged into Google Cloud via `gcloud auth application-default login` in your terminal, or set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to a valid service account key file. Also, verify that the specified GCP project ID is correct and has the Quantum Engine API enabled.
Warnings
- breaking Cirq-Rigetti subpackage has been removed. If your project depends on `cirq-rigetti`, you must use an older version of Cirq (e.g., `cirq-rigetti-1.5.0`) or install `cirq-rigetti` explicitly if available.
- breaking The minimum required Python version has been increased to 3.11.0. Older Python versions (e.g., 3.10 or earlier) are no longer supported.
- breaking The `cirq-ft` package was removed. Its functionality has moved to the `Qualtran` repository.
- gotcha Authentication to Google Quantum Engine requires setting up Google Cloud credentials. Without proper authentication, `cirq_google.QuantumEngine` initialization or usage will fail.
Install
-
pip install cirq cirq-google
Imports
- QuantumEngine
from cirq_google import QuantumEngine
- Sycamore
from cirq_google.devices import Sycamore
Quickstart
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)