Comprehensive Cirq
Battle-tested skill for quantum, computing, framework, building. Includes structured workflows, validation checks, and reusable patterns for scientific.
Comprehensive Cirq
A scientific computing skill for quantum computing using Cirq β Google Quantum AI's open-source Python framework for designing, simulating, and running quantum circuits on quantum processors and simulators.
When to Use This Skill
Choose Comprehensive Cirq when:
- Designing and simulating quantum circuits
- Implementing quantum algorithms (Grover's, VQE, QAOA)
- Working with Google's quantum hardware specifications
- Studying quantum noise models and error mitigation
Consider alternatives when:
- You prefer IBM's ecosystem (use Qiskit)
- You need a hardware-agnostic framework (use PennyLane)
- You're doing quantum machine learning specifically (use TensorFlow Quantum)
- You need quantum chemistry specifically (use OpenFermion with Cirq)
Quick Start
claude "Create a Bell state circuit and simulate it with Cirq"
import cirq import numpy as np # Create qubits q0, q1 = cirq.LineQubit.range(2) # Build a Bell state circuit circuit = cirq.Circuit([ cirq.H(q0), # Hadamard on first qubit cirq.CNOT(q0, q1), # CNOT entangles the pair cirq.measure(q0, q1, key="result") ]) print(circuit) # 0: βββHβββ@βββM('result')βββ # β β # 1: βββββββXβββMβββββββββββββ # Simulate simulator = cirq.Simulator() result = simulator.run(circuit, repetitions=1000) # Analyze results counts = result.histogram(key="result") print(f"Results: {dict(counts)}") # Expect: {0: ~500, 3: ~500} (|00β© and |11β© with equal probability)
Core Concepts
Cirq Building Blocks
| Concept | Description | Example |
|---|---|---|
| Qubit | Quantum bit | cirq.LineQubit(0) |
| Gate | Quantum operation | cirq.H, cirq.CNOT, cirq.Rz |
| Moment | Time slice of parallel gates | cirq.Moment([cirq.H(q0)]) |
| Circuit | Sequence of moments | cirq.Circuit(...) |
| Simulator | Classical simulation engine | cirq.Simulator() |
| Noise Model | Error simulation | cirq.ConstantQubitNoiseModel |
Common Quantum Gates
import cirq q = cirq.LineQubit.range(3) # Single-qubit gates cirq.H(q[0]) # Hadamard cirq.X(q[0]) # Pauli-X (NOT) cirq.Y(q[0]) # Pauli-Y cirq.Z(q[0]) # Pauli-Z cirq.S(q[0]) # Phase gate (βZ) cirq.T(q[0]) # T gate (βS) cirq.Rx(rads=0.5)(q[0]) # Rotation around X cirq.Ry(rads=0.5)(q[0]) # Rotation around Y cirq.Rz(rads=0.5)(q[0]) # Rotation around Z # Two-qubit gates cirq.CNOT(q[0], q[1]) # Controlled-NOT cirq.CZ(q[0], q[1]) # Controlled-Z cirq.SWAP(q[0], q[1]) # Swap cirq.ISWAP(q[0], q[1]) # βSWAP variant (Sycamore native) # Three-qubit gates cirq.TOFFOLI(q[0], q[1], q[2]) # Controlled-CNOT cirq.FREDKIN(q[0], q[1], q[2]) # Controlled-SWAP
Noise Simulation
# Add depolarizing noise to simulation noise_model = cirq.ConstantQubitNoiseModel( qubit_noise_gate=cirq.DepolarizingChannel(p=0.01) ) noisy_simulator = cirq.DensityMatrixSimulator(noise=noise_model) noisy_result = noisy_simulator.run(circuit, repetitions=1000) noisy_counts = noisy_result.histogram(key="result") print(f"Noisy results: {dict(noisy_counts)}")
Configuration
| Parameter | Description | Default |
|---|---|---|
simulator_type | Simulator, DensityMatrixSimulator | Simulator |
repetitions | Number of measurement shots | 1000 |
noise_model | Noise model for simulation | None |
qubit_type | LineQubit, GridQubit, NamedQubit | LineQubit |
seed | Random seed for reproducibility | None |
Best Practices
-
Use
GridQubitfor hardware-aware circuit design. Google's quantum processors use a 2D grid topology. Designing circuits withGridQubitfrom the start ensures your circuits map naturally to hardware without expensive qubit routing. -
Decompose gates to the native gate set. Cirq's
cirq.google.optimized_for_sycamore()function converts arbitrary gates to the Sycamore processor's native gate set (βISWAP, Rz, etc.). Always optimize before submitting to hardware. -
Start with the state vector simulator for debugging. Use
cirq.Simulator()(state vector) for debugging circuit logic, then switch toDensityMatrixSimulatorwith noise models for realistic performance estimates. State vector simulation is exact but limited to ~25 qubits. -
Use parameterized circuits for variational algorithms. Define gates with
sympysymbols, then resolve parameters at simulation time. This avoids rebuilding circuits for each parameter update in VQE or QAOA optimization loops. -
Profile circuit depth and gate count. Use
len(circuit)for depth (number of moments) and count two-qubit gates β these are the primary source of errors on real hardware. Minimize both for better hardware results.
Common Issues
Simulation is too slow for many qubits. State vector simulation scales exponentially β ~20 qubits is practical, ~30 is the limit on most machines. For larger circuits, use Cirq's density matrix simulator with approximations, or consider tensor network simulators like qsim.
Circuit contains non-native gates for target hardware. Use cirq.google.optimized_for_sycamore(circuit) or the appropriate device optimizer. Non-native gates must be decomposed into native gates, which increases circuit depth and error rates.
Measurement results don't match theoretical predictions. With noise, decoherence corrupts results. Increase measurement repetitions (10,000+), implement error mitigation techniques (zero-noise extrapolation, probabilistic error cancellation), and verify your circuit on the noiseless simulator first.
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Full-Stack Code Reviewer
Comprehensive code review skill that checks for security vulnerabilities, performance issues, accessibility, and best practices across frontend and backend code.
Test Suite Generator
Generates comprehensive test suites with unit tests, integration tests, and edge cases. Supports Jest, Vitest, Pytest, and Go testing.
Pro Architecture Workspace
Battle-tested skill for architectural, decision, making, framework. Includes structured workflows, validation checks, and reusable patterns for development.