Cirq Quantum Computing Framework
Cirq is an open-source Python framework developed by Google for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits. It provides tools for designing quantum algorithms and running them on simulators or quantum hardware. Cirq maintains an active release schedule, typically with major updates every 1-2 months, focusing on new features, performance improvements, and compatibility with Google's quantum hardware.
Common errors
-
ModuleNotFoundError: No module named 'cirq.rigetti'
cause The `cirq-rigetti` subpackage was removed from Cirq's main distribution in v1.6.0.fixIf you require `cirq-rigetti`, install its last standalone version: `pip install cirq-rigetti==1.5.0`. -
ModuleNotFoundError: No module named 'cirq_ft'
cause The fault-tolerant functionality moved to the separate `qualtran` library.fixInstall the `qualtran` library: `pip install qualtran`. Update your imports to reference `qualtran` instead of `cirq_ft`. -
Your Python version is 3.10.x but cirq requires >=3.11.0.
cause Cirq v1.6.0 and later require a minimum Python version of 3.11.fixUpgrade your Python environment to 3.11 or newer. Alternatively, install an older compatible Cirq version, e.g., `pip install 'cirq<1.6.0'`. -
TypeError: 'numpy.ndarray' object is not callable
cause Commonly occurs when using an older Cirq version with NumPy 2.x, which changed some array behaviors.fixUpgrade Cirq to v1.5.0 or newer (`pip install --upgrade cirq`), which added NumPy 2.x compatibility. Or, downgrade NumPy to a version prior to 2.0 (`pip install 'numpy<2.0.0'`).
Warnings
- breaking The `cirq-rigetti` subpackage was fully removed from Cirq as of v1.6.0. Attempts to import it from `cirq` will fail.
- breaking The `cirq-ft` package was removed from Cirq as of v1.4.0, having moved to its own repository under the `qualtran` library. Direct imports will fail.
- breaking Cirq v1.6.0 and later versions require Python 3.11 or higher.
- gotcha Support for NumPy 2.x was added in Cirq v1.5.0. Older Cirq versions may have compatibility issues or breakages if used with NumPy 2.x.
- gotcha Cirq has undergone significant API stabilization since its v1.0.0 release. Code written for pre-1.0 versions may use deprecated or removed symbols.
Install
-
pip install cirq
Imports
- Circuit
import cirq circuit = cirq.Circuit(...)
- Simulator
import cirq simulator = cirq.Simulator()
- GridQubit
import cirq q = cirq.GridQubit(0, 0)
- H
import cirq circuit = cirq.Circuit(cirq.H(q))
- rigetti
import cirq.rigetti
pip install cirq-rigetti==1.5.0 import cirq_rigetti
- cirq_ft
import cirq_ft
pip install qualtran import qualtran
Quickstart
import cirq
# Define a qubit
q0 = cirq.GridQubit(0, 0)
# Create a quantum circuit for a Bell state
circuit = cirq.Circuit(
cirq.H(q0), # Apply Hadamard gate
cirq.CNOT(q0, cirq.GridQubit(0, 1)), # Apply CNOT with another qubit
cirq.measure(q0, cirq.GridQubit(0, 1), key='result') # Measure both qubits
)
print("Circuit:")
print(circuit)
# Simulate the circuit
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=100) # Run 100 times
print("\nResults:")
print(result.histogram(key='result')) # Print measurement outcome histogram