Cirq modules

Cirq has a modular architecture and is organized in a monorepo, all of the modules follow the same folder structure. Each module is structured as follows. Let's take as example a module named cirq-example:

├── cirq_example
│   ├──
│   ├──
│   ├──
│   └── json_test_data
│       ├──
│       └──
├── README.rst
├── requirements.txt
├── setup.cfg

Note that typically there is only a single top level package, cirq_example - but there might be exceptions.

Additionally, there is a metapackage "cirq" that's a completely different beast and just depends on the modules. This enables pip install cirq to have all the included modules to be installed for our users.

All modules should depend on cirq-core, which is the central, core library for Cirq.


Each package gets published to PyPi as a separate package. To build all the wheel files locally, use

dev_tools/packaging/ ./dist `./dev_tools/packaging/`

Packages are versioned together, share the same version number, and are released together.

Setting up a new module

To setup a new module follow these steps:

  1. Create the folder structure above, copy the files based on an existing module
    1. LICENSE should be the same
    2. README.rst will be the documentation that appears in PyPi
    3. should specify an install_requires configuration that has cirq-core=={module.version} at the minimum
  2. Setup JSON serialization for each top level python package

Setting up JSON serialization

  1. Add the <top_level_package>/ file

    @functools.lru_cache()  # coverage: ignore
    def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:  # coverage: ignore
        return {}
  2. Register the resolver cache - at the end of the <top_level_package>/

    # Registers cirq_example's public classes for JSON serialization.
    from cirq.protocols.json_serialization import _register_resolver
    from cirq_example.json_resolver_cache import _class_resolver_dictionary
  3. Add the <top_level_package>/json_test_data folder with the following content:

    1. contains the core test specification for JSON testing, that plugs into the central framework:

      import pathlib
      import cirq_example
      from cirq_example.json_resolver_cache import _class_resolver_dictionary
      from cirq.testing.json import ModuleJsonTestSpec
      TestSpec = ModuleJsonTestSpec(
    2. should import TestSpec from

    3. in cirq/protocols/ add 'cirq_example':None to the TESTED_MODULES variable. TESTED_MODULES is also used to prepare the test framework for deprecation warnings. With new modules, we useNone as there is no deprecation setup.

You can run check/pytest-changed-files and that should execute the as well.

That's it! Now, you can follow the Serialization guide for adding and removing serializable objects.


List modules

To iterate through modules, you can list them by invoking dev_tools/

python dev_tools/ --list 

There are different modes of listing (e.g the folder, package-path, top level package), you can refer to python dev_tools/ --list --help for the most up to date features.