`CircuitDag` and `Unique` can be removed from the `cirq.contrib` level

Open
#8,533 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api

Research direction

Start in cirq-core/cirq/contrib/__init__.py and remove the CircuitDag and Unique re-export. Update the listed references in circuit_dag.py, circuit_dag_test.py, acquaintance/topological_sort.py, acquaintance/topological_sort_test.py, and json_test_data/spec.py; add redirects for the old API documentation pages. Done when callers import the classes from cirq.contrib.circuitdag and the updated tests and documentation references are consistent.

Written by the indexing model from the issue text.

Description

area/contrib area/qcircuit area/quantikz kind/design-issue skill/maintainer triage/discuss
image

CircuitDag, Unique, and circuit_to_latex_using_qcircuit are anomalous in the Cirq contrib module in that they are defined within contrib submodules yet exposed at the cirq.contrib namespace level. This is visible in the documentation for the contrib module (screenshot attached →).

The qcircuit module will soon go away because it is being deprecated (#8355). As part of a small cleanup of the contrib module (motivated by the deprecation of qcircuit and a goal to improve the quantikz module), I propose that CircuitDag and Unique be removed from the cirq.contrib level. The net effect will be to require callers to import CircuitDag and Unique from cirq.contrib.circuitdag instead of from cirq.contrib.

What will need to change in Cirq if this is done?

The main change is simply to remove the last line in cirq-core/cirq/contrib/__init__.py:

from cirq.contrib import graph_device  # noqa: F401
from cirq.contrib import quirk  # noqa: F401
from cirq.contrib.qcircuit import circuit_to_latex_using_qcircuit as circuit_to_latex_using_qcircuit
from cirq.contrib import json  # noqa: F401
from cirq.contrib.circuitdag import CircuitDag as CircuitDag, Unique as Unique

Since these classes are only used by other contrib modules, the other changes are fairly limited:

  • Unique.__repr__ in cirq/contrib/circuitdag/circuit_dag.py:46 hardcodes the string 'cirq.contrib.Unique(...)'.

  • cirq/contrib/circuitdag/circuit_dag_test.py: about 24 uses of cirq.contrib.CircuitDag and cirq.contrib.Unique.

  • cirq/contrib/acquaintance/topological_sort.py:31: uses the annotation dag: cirq.contrib.CircuitDag. This is annotation only, because of from __future__ import annotations.

  • cirq/contrib/acquaintance/topological_sort_test.py:28: calls cirq.contrib.CircuitDag.from_circuit(...) inside a module-level parametrize.

  • cirq/contrib/json_test_data/spec.py:27: has should_not_be_serialized=["Unique", "CircuitDag"], which is checked against the members of cirq.contrib.

  • API documentation: the published pages currently document the two classes at reference/python/cirq/contrib/CircuitDag and .../Unique. We should add redirects so that the old references point to the new pages, which will be reference/python/cirq/contrib/circuitdag/CircuitDag and .../Unique.

All the production code in Cirq already does either from cirq.contrib import circuitdag or from cirq.contrib.circuitdag import CircuitDag. None of the notebooks, Markdown docs/snippets, doctests, or other Cirq subpackages (cirq-google, cirq-ft, cirq-aqt, cirq-ionq, cirq-pasqal, cirq-rigetti, cirq-web) reference CircuitDag, Unique, or circuitdag.

What will be affected outside of Cirq?

A GitHub code search finds that almost all cases where CircuitDag is accessed from cirq.contrib are either forks of the Cirq repo or vendored copies of the Cirq sources. The only non-fork/non-vendored cases I found are:

  • QuEraComputing/bloqade-circuit in src/bloqade/cirq_utils/parallelize.py, and it already imports from cirq.contrib.circuitdag.circuit_dag, so it is unaffected.

  • conda-forge/conda-forge-bot-data in import_to_pkg_maps/4/a/8/d/e/cirq.contr.json. This seems to be an automatically-generated module map; presumably, if the Cirq repo is updated, then this will eventually get auto-updated too.

It should be noted that the Cirq contrib module is not bound by Cirq's backward compatibility guarantees.

Why are things structured the way they are currently?

Some investigation of the commit history shows that CircuitDag and Unique were originally top-level cirq. Following Issue #547, Casey Duckering created cirq/circuits/circuit_dag.py and flattened CircuitDag into the top-level cirq namespace (cirq.CircuitDag) in #759 (2018), then flattened Unique (cirq.Unique) two days later in #761 so that cirq/contrib/paulistring/recombine.py could use it in type annotations.

In #3816 ("Is anyone using CircuitDag?") and #4494, maintainers concluded that CircuitDag was only used by contrib modules (acquaintance, paulistring, routing) and was not suitable for core Cirq 1.0, deciding to deprecate it from core and move it into cirq.contrib. The re-exports were later added to __init__.py to give users a drop-in replacement when CircuitDag was taken out of core in PR #5481 ("Move CircuitDag to contrib", Doug Strain, 2022-06-13). The commit message and the deprecation decorators state the intent: "cirq.CircuitDag is now deprecated and now must be changed to cirq.contrib.CircuitDag." Exporting CircuitDag and Unique directly in cirq/contrib/init.py allowed existing callers of import cirq; cirq.CircuitDag and cirq.Unique to migrate by inserting .contrib without adding a separate submodule import.

Dominant language
Python
Stars
5.1k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
48

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quantumlib/Cirq

All issues in quantumlib/Cirq

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.