Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transpile: narrow the return type depending on the circuits argument #9799

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions qiskit/compiler/transpiler.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can maybe avoid a lot of the duplication here if we use a bound TypeVar on the first argument rather than a full overload. We're in the fortunate situation here where there's only one parameter type and the return type change between the overloads, and those two types are equal.

So I think this is the same as doing:

from typing import List, TypeVar, Union

_CircuitT = TypeVar("_CircuitT", bound=Union[QuantumCircuit, List[QuantumCircuit]])

def transpile(
    circuits: _CircuitT,
    backend: Optional[Backend] = None,
    ...
) -> _CircuitT

We won't need the overload and the duplication in that case. If there are any other arguments that also change (I don't think there are, or at least, passing lists to the other arguments is deprecated and we don't want to support it in type hinting), then yeah we'd need the overload. But I think this case can actually just be a type bound.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks! I updated the branch accordingly.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pickle
import sys
from time import time
from typing import List, Union, Dict, Callable, Any, Optional, Tuple, Iterable
from typing import List, Union, Dict, Callable, Any, Optional, Tuple, Iterable, TypeVar
import warnings

from qiskit import user_config
Expand Down Expand Up @@ -57,9 +57,11 @@

logger = logging.getLogger(__name__)

_CircuitT = TypeVar("_CircuitT", bound=Union[QuantumCircuit, List[QuantumCircuit]])


def transpile(
circuits: Union[QuantumCircuit, List[QuantumCircuit]],
circuits: _CircuitT,
backend: Optional[Backend] = None,
basis_gates: Optional[List[str]] = None,
inst_map: Optional[List[InstructionScheduleMap]] = None,
Expand All @@ -79,13 +81,13 @@ def transpile(
callback: Optional[Callable[[BasePass, DAGCircuit, float, PropertySet, int], Any]] = None,
output_name: Optional[Union[str, List[str]]] = None,
unitary_synthesis_method: str = "default",
unitary_synthesis_plugin_config: dict = None,
target: Target = None,
unitary_synthesis_plugin_config: Optional[dict] = None,
target: Optional[Target] = None,
hls_config: Optional[HLSConfig] = None,
init_method: str = None,
optimization_method: str = None,
init_method: Optional[str] = None,
optimization_method: Optional[str] = None,
ignore_backend_supplied_default_methods: bool = False,
) -> Union[QuantumCircuit, List[QuantumCircuit]]:
) -> _CircuitT:
"""Transpile one or more circuits, according to some desired transpilation targets.

.. deprecated:: 0.23.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Improved the typing annotations on the top-level :func:`.transpile` function.
The return type is now narrowed correctly depending on whether a
single circuit or a list of circuit was passed.