Cirq
A Python framework for creating, editing, and invoking Noisy Intermediate-Scale Quantum (NISQ) circuits.
Top Related Projects
Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
A Python library for quantum programming using Quil.
Microsoft Quantum Development Kit Samples
QuTiP: Quantum Toolbox in Python
Quick Overview
Cirq is an open-source framework for creating, manipulating, and optimizing quantum circuits and running them on quantum computers and simulators. Developed by Google's Quantum AI team, Cirq provides a Python library for writing, manipulating, and optimizing quantum algorithms.
Pros
- Flexible and extensible architecture for quantum circuit design
- Strong integration with Google's quantum hardware and simulators
- Comprehensive set of tools for quantum circuit optimization and error mitigation
- Active community and regular updates from Google's Quantum AI team
Cons
- Steeper learning curve compared to some other quantum computing frameworks
- Limited support for non-Google quantum hardware
- Documentation can be complex for beginners in quantum computing
- Some advanced features may require in-depth knowledge of quantum mechanics
Code Examples
- Creating a simple quantum circuit:
import cirq
# Create two qubits
q0, q1 = cirq.LineQubit.range(2)
# Create a circuit
circuit = cirq.Circuit(
cirq.H(q0), # Hadamard gate on q0
cirq.CNOT(q0, q1), # CNOT gate with q0 as control and q1 as target
cirq.measure(q0, q1, key='result') # Measure both qubits
)
print(circuit)
- Running a simulation:
import cirq
# Create a circuit (using the previous example)
# ...
# Create a simulator
simulator = cirq.Simulator()
# Run the simulation
result = simulator.run(circuit, repetitions=1000)
# Print the results
print(result.histogram(key='result'))
- Using a noise model:
import cirq
# Create a circuit (using the previous example)
# ...
# Define a noise model
noise_model = cirq.depolarize(p=0.1)
# Create a noisy simulator
noisy_simulator = cirq.DensityMatrixSimulator(noise=noise_model)
# Run the noisy simulation
noisy_result = noisy_simulator.run(circuit, repetitions=1000)
print(noisy_result.histogram(key='result'))
Getting Started
To get started with Cirq:
-
Install Cirq using pip:
pip install cirq
-
Import Cirq in your Python script:
import cirq
-
Create a simple quantum circuit:
q0 = cirq.LineQubit(0) circuit = cirq.Circuit( cirq.H(q0), cirq.measure(q0, key='result') )
-
Run a simulation:
simulator = cirq.Simulator() result = simulator.run(circuit, repetitions=100) print(result.histogram(key='result'))
Competitor Comparisons
Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
Pros of Qiskit
- Extensive documentation and tutorials for beginners
- Strong integration with IBM Quantum hardware and cloud services
- Comprehensive suite of tools for quantum chemistry and finance applications
Cons of Qiskit
- Steeper learning curve for users new to quantum computing
- Less flexibility in low-level circuit manipulation compared to Cirq
Code Comparison
Qiskit:
from qiskit import QuantumCircuit, execute, Aer
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1000)
result = job.result()
Cirq:
import cirq
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.H(q0),
cirq.CNOT(q0, q1),
cirq.measure(q0, q1, key='m')
)
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=1000)
Both Qiskit and Cirq are powerful quantum computing frameworks, each with its own strengths. Qiskit excels in its integration with IBM's quantum hardware and provides extensive resources for various quantum applications. Cirq offers more flexibility for low-level circuit manipulation and is particularly suited for researchers and developers working on custom quantum algorithms.
PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Pros of PennyLane
- Supports a wider range of quantum hardware and software platforms
- Offers automatic differentiation for quantum-classical hybrid computations
- Provides a more extensive library of built-in quantum operations and templates
Cons of PennyLane
- Steeper learning curve for beginners due to its more comprehensive feature set
- Less focus on low-level circuit manipulation compared to Cirq
- Smaller community and fewer educational resources available
Code Comparison
PennyLane:
import pennylane as qml
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(1))
Cirq:
import cirq
q0, q1 = cirq.LineQubit.range(2)
def circuit(params):
return cirq.Circuit(
cirq.rx(params[0])(q0),
cirq.CNOT(q0, q1),
cirq.measure(q1, key='result')
)
Both examples demonstrate a simple quantum circuit with a parameterized rotation and a CNOT gate. PennyLane uses a decorator-based approach and built-in expectation value calculation, while Cirq focuses on explicit circuit construction and measurement.
A Python library for quantum programming using Quil.
Pros of pyquil
- Tighter integration with Rigetti's quantum hardware and cloud services
- More extensive support for quantum-classical hybrid algorithms
- Robust simulation capabilities, including noise models
Cons of pyquil
- Steeper learning curve for beginners
- Less extensive documentation compared to Cirq
- Primarily focused on Rigetti's ecosystem, potentially limiting flexibility
Code Comparison
pyquil:
from pyquil import Program
from pyquil.gates import H, CNOT
p = Program()
p += H(0)
p += CNOT(0, 1)
Cirq:
import cirq
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.H(q0),
cirq.CNOT(q0, q1)
)
Both examples create a simple quantum circuit with a Hadamard gate followed by a CNOT gate. pyquil uses a more imperative style, while Cirq adopts a more declarative approach. Cirq's syntax may be more intuitive for those familiar with Python's object-oriented programming.
Microsoft Quantum Development Kit Samples
Pros of Quantum
- More comprehensive development environment with Q# language and Visual Studio integration
- Extensive documentation and learning resources, including tutorials and samples
- Strong support for quantum algorithm development and quantum chemistry simulations
Cons of Quantum
- Steeper learning curve due to Q# language specificity
- Less flexibility for low-level circuit manipulation compared to Cirq
- More focused on Microsoft's quantum hardware and simulators
Code Comparison
Quantum (Q#):
operation BellPair(q1 : Qubit, q2 : Qubit) : Unit {
H(q1);
CNOT(q1, q2);
}
Cirq:
def bell_pair(q1, q2):
yield cirq.H(q1)
yield cirq.CNOT(q1, q2)
Both examples create a Bell pair, but Quantum uses Q# with its specific syntax, while Cirq uses Python with a more familiar approach for many developers. Cirq's implementation is more concise and may be easier for those with Python experience to understand quickly. However, Quantum's Q# offers more quantum-specific features and optimizations that may be beneficial for complex quantum algorithms.
QuTiP: Quantum Toolbox in Python
Pros of QuTiP
- More comprehensive for open quantum systems and master equation solvers
- Extensive library of pre-built quantum objects and operations
- Strong support for visualization and data analysis of quantum systems
Cons of QuTiP
- Steeper learning curve for beginners in quantum computing
- Less focus on quantum circuit design and implementation
- Slower execution speed for large-scale quantum simulations
Code Comparison
QuTiP example:
import qutip as qt
q = qt.Qobj([[1], [0]]) # Create a qubit in state |0⟩
H = qt.sigmax() # Hadamard gate
result = H * q # Apply Hadamard gate to qubit
Cirq example:
import cirq
q = cirq.NamedQubit('q') # Create a qubit
circuit = cirq.Circuit(cirq.H(q)) # Apply Hadamard gate
result = cirq.Simulator().simulate(circuit)
Both libraries offer quantum computing capabilities, but QuTiP is more focused on open quantum systems and advanced quantum mechanics, while Cirq is tailored for quantum circuit design and near-term quantum algorithms. QuTiP provides a richer set of pre-built quantum objects and operations, making it powerful for theoretical quantum physics. Cirq, on the other hand, offers a more intuitive approach to building quantum circuits and is better suited for practical quantum algorithm implementation.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Python package for writing, manipulating, and running quantum circuits on quantum computers and simulators.
Features – Installation – Quick Start – Documentation – Integrations – Community – Citing Cirq – Contact
Features
Cirq provides useful abstractions for dealing with todayâs noisy intermediate-scale quantum (NISQ) computers, where the details of quantum hardware are vital to achieving state-of-the-art results. Some of its features include:
- Flexible gate definitions and custom gates
- Parameterized circuits with symbolic variables
- Circuit transformation, compilation and optimization
- Hardware device modeling
- Noise modeling
- Multiple built-in quantum circuit simulators
- Integration with qsim for high-performance simulation
- Interoperability with NumPy and SciPy
- Cross-platform compatibility
Installation
Cirq supports Python version 3.10 and later, and can be used on Linux, MacOS, and Windows, as well as Google Colab. For complete installation instructions, please refer to the Install section of the online Cirq documentation.
Quick Start â âHello Qubitâ Example
Here is a simple example to get you up and running with Cirq after you have installed it. Start a Python interpreter, and then type the following:
import cirq
# Pick a qubit.
qubit = cirq.GridQubit(0, 0)
# Create a circuit.
circuit = cirq.Circuit(
cirq.X(qubit)**0.5, # Square root of NOT.
cirq.measure(qubit, key='m') # Measurement.
)
print("Circuit:")
print(circuit)
# Simulate the circuit several times.
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=20)
print("Results:")
print(result)
Python should then print output similar to this:
Circuit:
(0, 0): âââX^0.5âââM('m')âââ
Results:
m=11000111111011001000
Congratulations! You have run your first quantum simulation in Cirq. You can continue to learn more by exploring the many Cirq tutorials described below.
Cirq Documentation
The primary documentation site for Cirq is the Cirq home page on the Quantum AI website. There and elsewhere, a variety of documentation for Cirq is available.
Tutorials
- Video tutorials on YouTube are an engaging way to learn Cirq.
- Jupyter notebook-based tutorials let you learn Cirq from your browser â no installation needed.
- Text-based tutorials on the Cirq home page are great when combined with a local installation of Cirq on your computer. After starting with the basics, you'll be ready to dive into tutorials on circuit building and circuit simulation under the Build and Simulate tabs, respectively. Check out the other tabs for more!
Reference Documentation
- Docs for the current stable release correspond to what you get with
pip install cirq
. - Docs for the pre-release correspond to what you get with
pip install cirq~=1.0.dev
.
Examples
- The examples subdirectory of the Cirq GitHub repo has many programs illustrating the application of Cirq to everything from common textbook algorithms to more advanced methods.
- The Experiments page on the Cirq documentation site has yet more examples, from simple to advanced.
Change log
- The Cirq releases page on GitHub lists the changes in each release.
Integrations
Google Quantum AI has a suite of open-source software that lets you do more with Cirq. From high-performance simulators, to novel tools for expressing and analyzing fault-tolerant quantum algorithms, our software stack lets you develop quantum programs for a variety of applications.
Your interests | Software to explore |
---|---|
Quantum algorithms? Fault-tolerant quantum computing (FTQC)? | Qualtran |
Large circuits and/or a lot of simulations? | qsim |
Circuits with thousands of qubits and millions of Clifford operations? | Stim |
Quantum error correction (QEC)? | Stim |
Chemistry and/or material science? | OpenFermion OpenFermion-FQE OpenFermion-PySCF OpenFermion-Psi4 |
Quantum machine learning (QML)? | TensorFlow Quantum |
Real experiments using Cirq? | ReCirq |
Community
Cirq has benefited from open-source contributions by over 200 people and counting. We are dedicated to cultivating an open and inclusive community to build software for quantum computers, and have a code of conduct for our community.
Announcements
Stay on top of Cirq developments using the approach that best suits your needs:
- For releases and major announcements: sign up to the low-volume mailing list
cirq-announce
. - For releases only:
- Via GitHub notifications: configure repository notifications for Cirq.
- Via Atom/RSS from GitHub: subscribe to the GitHub Cirq releases Atom feed.
- Via RSS from PyPI: subscribe to the PyPI releases RSS feed for Cirq.
Cirq releases take place approximately every quarter.
Questions and Discussions
- Do you have questions about using Cirq? Post them to the Quantum Computing
Stack Exchange and tag them with the
cirq
tag. You can also search past questions using that tag â it's a great way to learn! - Would you like to get more involved in Cirq development? Cirq Cynq is our biweekly virtual meeting of contributors to discuss everything from issues to ongoing efforts, as well as to ask questions. Become a member of cirq-dev to get an automatic meeting invitation!
Issues and Pull Requests
- Do you have a feature request or want to report a bug? Open an issue on GitHub to report it!
- Do you have a code contribution? Read our contribution guidelines, then open a pull request!
Citing Cirq
When publishing articles or otherwise writing about Cirq, please cite the Cirq version you use â it will help others reproduce your results. We use Zenodo to preserve releases. The following links let you download the bibliographic record for the latest stable release of Cirq in some popular formats:
For formatted citations and records in other formats, as well as records for all releases of Cirq past and present, please visit the Cirq page on Zenodo.
Contact
For any questions or concerns not addressed here, please email quantum-oss-maintainers@google.com.
Disclaimer
Cirq is not an official Google product. Copyright 2019 The Cirq Developers.
Top Related Projects
Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
A Python library for quantum programming using Quil.
Microsoft Quantum Development Kit Samples
QuTiP: Quantum Toolbox in Python
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot