Skip to content

Commit

Permalink
Implement and enforce explicit re-export for cirq modules (quantumlib…
Browse files Browse the repository at this point in the history
…#6722)

* Enable and enforce the `no_implicit_reexport` mypy rule for cirq modules
* Update `__init__.py` files so they explicitly re-export public symbols, but
  do not re-export local symbols or submodules already in parent namespace
* Fix few instances of imports from incorrect modules

Fixes quantumlib#6717
---------

Co-authored-by: Pavol Juhas <juhas@google.com>
  • Loading branch information
ashiq-firoz and pavoljuhas authored Sep 11, 2024
1 parent e00c56c commit 370cd88
Show file tree
Hide file tree
Showing 47 changed files with 1,705 additions and 1,365 deletions.
1,153 changes: 585 additions & 568 deletions cirq/__init__.py

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions cirq/circuits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@

"""Circuit classes, mutators, and outputs."""

from cirq.circuits.text_diagram_drawer import TextDiagramDrawer
from cirq.circuits.text_diagram_drawer import TextDiagramDrawer as TextDiagramDrawer
from cirq.circuits.qasm_output import QasmOutput as QasmOutput

from cirq.circuits.qasm_output import QasmOutput
from cirq.circuits.circuit import (
AbstractCircuit as AbstractCircuit,
Alignment as Alignment,
Circuit as Circuit,
)
from cirq.circuits.circuit_operation import CircuitOperation as CircuitOperation
from cirq.circuits.frozen_circuit import FrozenCircuit as FrozenCircuit
from cirq.circuits.insert_strategy import InsertStrategy as InsertStrategy

from cirq.circuits.circuit import AbstractCircuit, Alignment, Circuit
from cirq.circuits.circuit_operation import CircuitOperation
from cirq.circuits.frozen_circuit import FrozenCircuit
from cirq.circuits.insert_strategy import InsertStrategy
from cirq.circuits.moment import Moment as Moment

from cirq.circuits.moment import Moment

from cirq.circuits.optimization_pass import PointOptimizer, PointOptimizationSummary
from cirq.circuits.optimization_pass import (
PointOptimizer as PointOptimizer,
PointOptimizationSummary as PointOptimizationSummary,
)
4 changes: 2 additions & 2 deletions cirq/contrib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
from cirq.contrib import acquaintance
from cirq.contrib import graph_device
from cirq.contrib import quirk
from cirq.contrib.qcircuit import circuit_to_latex_using_qcircuit
from cirq.contrib.qcircuit import circuit_to_latex_using_qcircuit as circuit_to_latex_using_qcircuit
from cirq.contrib import json
from cirq.contrib.circuitdag import CircuitDag, Unique
from cirq.contrib.circuitdag import CircuitDag as CircuitDag, Unique as Unique
73 changes: 44 additions & 29 deletions cirq/contrib/acquaintance/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,70 @@
# limitations under the License.

"""Tools for creating and using acquaintance strategies."""
from cirq.contrib.acquaintance.bipartite import (
BipartiteGraphType as BipartiteGraphType,
BipartiteSwapNetworkGate as BipartiteSwapNetworkGate,
)

from cirq.contrib.acquaintance.bipartite import BipartiteGraphType, BipartiteSwapNetworkGate

from cirq.contrib.acquaintance.devices import get_acquaintance_size, UnconstrainedAcquaintanceDevice
from cirq.contrib.acquaintance.devices import (
get_acquaintance_size as get_acquaintance_size,
UnconstrainedAcquaintanceDevice as UnconstrainedAcquaintanceDevice,
)

from cirq.contrib.acquaintance.executor import (
AcquaintanceOperation,
GreedyExecutionStrategy,
StrategyExecutorTransformer,
AcquaintanceOperation as AcquaintanceOperation,
GreedyExecutionStrategy as GreedyExecutionStrategy,
StrategyExecutorTransformer as StrategyExecutorTransformer,
)

from cirq.contrib.acquaintance.gates import acquaint, AcquaintanceOpportunityGate, SwapNetworkGate
from cirq.contrib.acquaintance.gates import (
acquaint as acquaint,
AcquaintanceOpportunityGate as AcquaintanceOpportunityGate,
SwapNetworkGate as SwapNetworkGate,
)

from cirq.contrib.acquaintance.inspection_utils import get_logical_acquaintance_opportunities
from cirq.contrib.acquaintance.inspection_utils import (
get_logical_acquaintance_opportunities as get_logical_acquaintance_opportunities,
)

from cirq.contrib.acquaintance.mutation_utils import (
expose_acquaintance_gates,
rectify_acquaintance_strategy,
replace_acquaintance_with_swap_network,
expose_acquaintance_gates as expose_acquaintance_gates,
rectify_acquaintance_strategy as rectify_acquaintance_strategy,
replace_acquaintance_with_swap_network as replace_acquaintance_with_swap_network,
)

from cirq.contrib.acquaintance.optimizers import remove_redundant_acquaintance_opportunities
from cirq.contrib.acquaintance.optimizers import (
remove_redundant_acquaintance_opportunities as remove_redundant_acquaintance_opportunities,
)

from cirq.contrib.acquaintance.permutation import (
LinearPermutationGate,
PermutationGate,
SwapPermutationGate,
update_mapping,
get_logical_operations,
display_mapping,
return_to_initial_mapping,
uses_consistent_swap_gate,
EXPAND_PERMUTATION_GATES,
DECOMPOSE_PERMUTATION_GATES,
LinearPermutationGate as LinearPermutationGate,
PermutationGate as PermutationGate,
SwapPermutationGate as SwapPermutationGate,
update_mapping as update_mapping,
get_logical_operations as get_logical_operations,
display_mapping as display_mapping,
return_to_initial_mapping as return_to_initial_mapping,
uses_consistent_swap_gate as uses_consistent_swap_gate,
EXPAND_PERMUTATION_GATES as EXPAND_PERMUTATION_GATES,
DECOMPOSE_PERMUTATION_GATES as DECOMPOSE_PERMUTATION_GATES,
)

from cirq.contrib.acquaintance.shift import CircularShiftGate
from cirq.contrib.acquaintance.shift import CircularShiftGate as CircularShiftGate

from cirq.contrib.acquaintance.shift_swap_network import ShiftSwapNetworkGate
from cirq.contrib.acquaintance.shift_swap_network import (
ShiftSwapNetworkGate as ShiftSwapNetworkGate,
)

from cirq.contrib.acquaintance.strategies import (
complete_acquaintance_strategy,
cubic_acquaintance_strategy,
quartic_paired_acquaintance_strategy,
complete_acquaintance_strategy as complete_acquaintance_strategy,
cubic_acquaintance_strategy as cubic_acquaintance_strategy,
quartic_paired_acquaintance_strategy as quartic_paired_acquaintance_strategy,
)

from cirq.contrib.acquaintance.topological_sort import (
is_topologically_sorted,
random_topological_sort,
is_topologically_sorted as is_topologically_sorted,
random_topological_sort as random_topological_sort,
)

from cirq.contrib.acquaintance import testing
12 changes: 9 additions & 3 deletions cirq/contrib/acquaintance/strategies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

"""Acquaintance strategies."""

from cirq.contrib.acquaintance.strategies.complete import complete_acquaintance_strategy
from cirq.contrib.acquaintance.strategies.cubic import cubic_acquaintance_strategy
from cirq.contrib.acquaintance.strategies.quartic_paired import quartic_paired_acquaintance_strategy
from cirq.contrib.acquaintance.strategies.complete import (
complete_acquaintance_strategy as complete_acquaintance_strategy,
)
from cirq.contrib.acquaintance.strategies.cubic import (
cubic_acquaintance_strategy as cubic_acquaintance_strategy,
)
from cirq.contrib.acquaintance.strategies.quartic_paired import (
quartic_paired_acquaintance_strategy as quartic_paired_acquaintance_strategy,
)
4 changes: 3 additions & 1 deletion cirq/contrib/bayesian_network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from cirq.contrib.bayesian_network.bayesian_network_gate import BayesianNetworkGate
from cirq.contrib.bayesian_network.bayesian_network_gate import (
BayesianNetworkGate as BayesianNetworkGate,
)
2 changes: 1 addition & 1 deletion cirq/contrib/circuitdag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

"""Tools for representing a Circuit as a directed acyclic graph."""

from cirq.contrib.circuitdag.circuit_dag import CircuitDag, Unique
from cirq.contrib.circuitdag.circuit_dag import CircuitDag as CircuitDag, Unique as Unique
14 changes: 6 additions & 8 deletions cirq/contrib/circuitdag/circuit_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Callable, Dict, Generic, Iterator, TypeVar, cast, TYPE_CHECKING

from typing import Any, Callable, Dict, Generic, Iterator, TypeVar, cast

import functools
import networkx

import cirq
from cirq import ops
from cirq.circuits import circuit

if TYPE_CHECKING:
import cirq

T = TypeVar('T')

Expand Down Expand Up @@ -98,7 +96,7 @@ def make_node(op: 'cirq.Operation') -> Unique:

@staticmethod
def from_circuit(
circuit: circuit.Circuit,
circuit: cirq.Circuit,
can_reorder: Callable[['cirq.Operation', 'cirq.Operation'], bool] = _disjoint_qubits,
) -> 'CircuitDag':
return CircuitDag.from_ops(circuit.all_operations(), can_reorder=can_reorder)
Expand Down Expand Up @@ -179,8 +177,8 @@ def all_operations(self) -> Iterator['cirq.Operation']:
def all_qubits(self):
return frozenset(q for node in self.nodes for q in node.val.qubits)

def to_circuit(self) -> circuit.Circuit:
return circuit.Circuit(self.all_operations(), strategy=circuit.InsertStrategy.EARLIEST)
def to_circuit(self) -> cirq.Circuit:
return cirq.Circuit(self.all_operations(), strategy=cirq.InsertStrategy.EARLIEST)

def findall_nodes_until_blocked(
self, is_blocker: Callable[['cirq.Operation'], bool]
Expand Down
16 changes: 8 additions & 8 deletions cirq/contrib/graph_device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

"""Tools for representing a device as an edge-labelled graph."""

from cirq.contrib.graph_device.hypergraph import UndirectedHypergraph
from cirq.contrib.graph_device.hypergraph import UndirectedHypergraph as UndirectedHypergraph

from cirq.contrib.graph_device.graph_device import (
is_undirected_device_graph,
is_crosstalk_graph,
FixedDurationUndirectedGraphDeviceEdge,
UndirectedGraphDevice,
UnconstrainedUndirectedGraphDeviceEdge,
is_undirected_device_graph as is_undirected_device_graph,
is_crosstalk_graph as is_crosstalk_graph,
FixedDurationUndirectedGraphDeviceEdge as FixedDurationUndirectedGraphDeviceEdge,
UndirectedGraphDevice as UndirectedGraphDevice,
UnconstrainedUndirectedGraphDeviceEdge as UnconstrainedUndirectedGraphDeviceEdge,
)

from cirq.contrib.graph_device.uniform_graph_device import (
uniform_undirected_graph_device,
uniform_undirected_linear_device,
uniform_undirected_graph_device as uniform_undirected_graph_device,
uniform_undirected_linear_device as uniform_undirected_linear_device,
)
11 changes: 5 additions & 6 deletions cirq/contrib/noise_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from cirq.contrib.noise_models.noise_models import (
DepolarizingNoiseModel,
ReadoutNoiseModel,
DampedReadoutNoiseModel,
DepolarizingWithReadoutNoiseModel,
DepolarizingWithDampedReadoutNoiseModel,
DepolarizingNoiseModel as DepolarizingNoiseModel,
ReadoutNoiseModel as ReadoutNoiseModel,
DampedReadoutNoiseModel as DampedReadoutNoiseModel,
DepolarizingWithReadoutNoiseModel as DepolarizingWithReadoutNoiseModel,
DepolarizingWithDampedReadoutNoiseModel as DepolarizingWithDampedReadoutNoiseModel,
)
28 changes: 18 additions & 10 deletions cirq/contrib/paulistring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,31 @@

"""Methods related to optimizing and transforming PauliStrings."""

from cirq.contrib.paulistring.clifford_target_gateset import CliffordTargetGateset
from cirq.contrib.paulistring.clifford_target_gateset import (
CliffordTargetGateset as CliffordTargetGateset,
)

from cirq.contrib.paulistring.separate import (
convert_and_separate_circuit,
pauli_string_half,
regular_half,
convert_and_separate_circuit as convert_and_separate_circuit,
pauli_string_half as pauli_string_half,
regular_half as regular_half,
)

from cirq.contrib.paulistring.pauli_string_dag import (
pauli_string_dag_from_circuit,
pauli_string_reorder_pred,
pauli_string_dag_from_circuit as pauli_string_dag_from_circuit,
pauli_string_reorder_pred as pauli_string_reorder_pred,
)

from cirq.contrib.paulistring.recombine import move_pauli_strings_into_circuit
from cirq.contrib.paulistring.recombine import (
move_pauli_strings_into_circuit as move_pauli_strings_into_circuit,
)

from cirq.contrib.paulistring.pauli_string_optimize import pauli_string_optimized_circuit
from cirq.contrib.paulistring.pauli_string_optimize import (
pauli_string_optimized_circuit as pauli_string_optimized_circuit,
)

from cirq.contrib.paulistring.clifford_optimize import clifford_optimized_circuit
from cirq.contrib.paulistring.clifford_optimize import (
clifford_optimized_circuit as clifford_optimized_circuit,
)

from cirq.contrib.paulistring.optimize import optimized_circuit
from cirq.contrib.paulistring.optimize import optimized_circuit as optimized_circuit
4 changes: 2 additions & 2 deletions cirq/contrib/qasm_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from cirq.contrib.qasm_import.exception import QasmException
from cirq.contrib.qasm_import.qasm import circuit_from_qasm
from cirq.contrib.qasm_import.exception import QasmException as QasmException
from cirq.contrib.qasm_import.qasm import circuit_from_qasm as circuit_from_qasm
10 changes: 6 additions & 4 deletions cirq/contrib/qcircuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

"""Converts cirq circuits into latex using qcircuit."""

from cirq.contrib.qcircuit.qcircuit_diagram import circuit_to_latex_using_qcircuit
from cirq.contrib.qcircuit.qcircuit_diagram import (
circuit_to_latex_using_qcircuit as circuit_to_latex_using_qcircuit,
)

from cirq.contrib.qcircuit.qcircuit_diagram_info import (
escape_text_for_latex,
get_multigate_parameters,
get_qcircuit_diagram_info,
escape_text_for_latex as escape_text_for_latex,
get_multigate_parameters as get_multigate_parameters,
get_qcircuit_diagram_info as get_qcircuit_diagram_info,
)
14 changes: 7 additions & 7 deletions cirq/contrib/quantum_volume/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"""Utilities running the Quantum Volume benchmark on devices"""

from cirq.contrib.quantum_volume.quantum_volume import (
generate_model_circuit,
compute_heavy_set,
sample_heavy_set,
compile_circuit,
calculate_quantum_volume,
QuantumVolumeResult,
CompilationResult,
generate_model_circuit as generate_model_circuit,
compute_heavy_set as compute_heavy_set,
sample_heavy_set as sample_heavy_set,
compile_circuit as compile_circuit,
calculate_quantum_volume as calculate_quantum_volume,
QuantumVolumeResult as QuantumVolumeResult,
CompilationResult as CompilationResult,
)
29 changes: 16 additions & 13 deletions cirq/contrib/quimb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# pylint: disable=wrong-or-nonexistent-copyright-notice
from cirq.contrib.quimb.state_vector import (
circuit_for_expectation_value,
tensor_expectation_value,
circuit_to_tensors,
tensor_state_vector,
tensor_unitary,
circuit_for_expectation_value as circuit_for_expectation_value,
tensor_expectation_value as tensor_expectation_value,
circuit_to_tensors as circuit_to_tensors,
tensor_state_vector as tensor_state_vector,
tensor_unitary as tensor_unitary,
)

from cirq.contrib.quimb.density_matrix import (
tensor_density_matrix,
circuit_to_density_matrix_tensors,
tensor_density_matrix as tensor_density_matrix,
circuit_to_density_matrix_tensors as circuit_to_density_matrix_tensors,
)

from cirq.contrib.quimb.grid_circuits import simplify_expectation_value_circuit, get_grid_moments
from cirq.contrib.quimb.grid_circuits import (
simplify_expectation_value_circuit as simplify_expectation_value_circuit,
get_grid_moments as get_grid_moments,
)

from cirq.contrib.quimb.mps_simulator import (
MPSOptions,
MPSSimulator,
MPSSimulatorStepResult,
MPSState,
MPSTrialResult,
MPSOptions as MPSOptions,
MPSSimulator as MPSSimulator,
MPSSimulatorStepResult as MPSSimulatorStepResult,
MPSState as MPSState,
MPSTrialResult as MPSTrialResult,
)
2 changes: 1 addition & 1 deletion cirq/contrib/quirk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

"""Converts cirq circuits into quirk circuits."""

from cirq.contrib.quirk.export_to_quirk import circuit_to_quirk_url
from cirq.contrib.quirk.export_to_quirk import circuit_to_quirk_url as circuit_to_quirk_url
Loading

0 comments on commit 370cd88

Please sign in to comment.