Convert Figma logo to code with AI

quantumlib logoCirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.

4,228
1,010
4,228
282

Top Related Projects

5,026

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.

1,396

A Python library for quantum programming using Quil.

3,865

Microsoft Quantum Development Kit Samples

1,671

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

  1. 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)
  1. 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'))
  1. 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:

  1. Install Cirq using pip:

    pip install cirq
    
  2. Import Cirq in your Python script:

    import cirq
    
  3. Create a simple quantum circuit:

    q0 = cirq.LineQubit(0)
    circuit = cirq.Circuit(
        cirq.H(q0),
        cirq.measure(q0, key='result')
    )
    
  4. Run a simulation:

    simulator = cirq.Simulator()
    result = simulator.run(circuit, repetitions=100)
    print(result.histogram(key='result'))
    

Competitor Comparisons

5,026

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.

1,396

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.

3,865

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.

1,671

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

.. image:: https://raw.githubusercontent.com/quantumlib/Cirq/main/docs/images/Cirq_logo_color.png :target: https://github.com/quantumlib/cirq :alt: Cirq :width: 500px

Cirq is a Python library for writing, manipulating, and optimizing quantum circuits and running them against quantum computers and simulators.

.. image:: https://github.com/quantumlib/Cirq/actions/workflows/ci.yml/badge.svg?event=schedule :target: https://github.com/quantumlib/Cirq :alt: Build Status

.. image:: https://codecov.io/gh/quantumlib/Cirq/branch/main/graph/badge.svg :target: https://codecov.io/gh/quantumlib/Cirq

.. image:: https://badge.fury.io/py/cirq.svg :target: https://badge.fury.io/py/cirq

Installation and Documentation

Cirq documentation is available at quantumai.google/cirq <https://quantumai.google/cirq>_.

Documentation for the latest pre-release version of cirq (tracks the repository's main branch; what you get if you pip install cirq~=1.0.dev), is available here <https://quantumai.google/reference/python/cirq/all_symbols?version=nightly>__.

Documentation for the latest stable version of cirq (what you get if you pip install cirq) is available here <https://quantumai.google/reference/python/cirq/all_symbols>__.

  • Installation <https://quantumai.google/cirq/start/install>_
  • Documentation <https://quantumai.google/cirq>_
  • Tutorials <https://quantumai.google/cirq/build>_

For a comprehensive list all of the interactive Jupyter Notebooks in our repo (including the ones not yet published to the site) open our repo in Colab <https://colab.research.google.com/github/quantumlib/Cirq>_.

For the latest news regarding Cirq, sign up to the Cirq-announce email list <https://groups.google.com/forum/#!forum/cirq-announce>__!

Hello Qubit

A simple example to get you up and running:

.. code-block:: python

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)

Example output:

.. code-block::

Circuit: (0, 0): ───X^0.5───M('m')─── Results: m=11000111111011001000

Feature requests / Bugs / Questions

If you have feature requests or you found a bug, please file them on GitHub <https://github.com/quantumlib/Cirq/issues/new/choose>__.

For questions about how to use Cirq post to Quantum Computing Stack Exchange <https://quantumcomputing.stackexchange.com/>__ with the cirq <https://quantumcomputing.stackexchange.com/questions/tagged/cirq>__ tag.

How to cite Cirq

Cirq is uploaded to Zenodo automatically. Click on the badge below to see all the citation formats for all versions.

.. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.4062499.svg :target: https://doi.org/10.5281/zenodo.4062499 :alt: DOI

Cirq Contributors Community

We welcome contributions! Before opening your first PR, a good place to start is to read our guidelines <https://github.com/quantumlib/cirq/blob/main/CONTRIBUTING.md>__.

We are dedicated to cultivating an open and inclusive community to build software for near term quantum computers. Please read our code of conduct <https://github.com/quantumlib/cirq/blob/main/CODE_OF_CONDUCT.md>__ for the rules of engagement within our community.

Cirq Cynque is our weekly meeting for contributors to discuss upcoming features, designs, issues, community and status of different efforts. To get an invitation please join the cirq-dev email list <https://groups.google.com/forum/#!forum/cirq-dev>__ which also serves as yet another platform to discuss contributions and design ideas.

See Also

For those interested in using quantum computers to solve problems in chemistry and materials science, we encourage exploring OpenFermion <https://github.com/quantumlib/openfermion>__ and its sister library for compiling quantum simulation algorithms in Cirq, OpenFermion-Cirq <https://github.com/quantumlib/openfermion-cirq>__.

For machine learning enthusiasts, Tensorflow Quantum <https://github.com/tensorflow/quantum>__ is a great project to check out!

For a powerful quantum circuit simulator that integrates well with Cirq, we recommend looking at qsim <https://github.com/quantumlib/qsim>__.

Finally, ReCirq <https://github.com/quantumlib/ReCirq>__ contains real world experiments using Cirq.

Cirq is not an official Google product. Copyright 2019 The Cirq Developers