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

Aperta
#8,533 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
76/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
api

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
5.1k
Fork
1.3k
Merge medio
1g 18h
PR unite (30g)
41

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di quantumlib/Cirq

Tutte le issue di quantumlib/Cirq

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.