-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Consistent typehints for NLocal
family
#10479
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,9 @@ | |
"""The n-local circuit class.""" | ||
|
||
from __future__ import annotations | ||
|
||
import typing | ||
from typing import Union, Optional, Any, Sequence, Callable, Mapping | ||
from collections.abc import Callable, Mapping, Sequence | ||
|
||
from itertools import combinations | ||
|
||
import numpy | ||
|
@@ -90,10 +90,9 @@ def __init__( | |
skip_unentangled_qubits: bool = False, | ||
initial_state: QuantumCircuit | None = None, | ||
name: str | None = "nlocal", | ||
flatten: Optional[bool] = None, | ||
flatten: bool | None = None, | ||
) -> None: | ||
"""Create a new n-local circuit. | ||
|
||
""" | ||
Args: | ||
num_qubits: The number of qubits of the circuit. | ||
rotation_blocks: The blocks used in the rotation layers. If multiple are passed, | ||
|
@@ -123,9 +122,6 @@ def __init__( | |
to set this flag to ``True`` to avoid a large performance | ||
overhead for parameter binding. | ||
|
||
Examples: | ||
TODO | ||
|
||
Raises: | ||
ValueError: If ``reps`` parameter is less than or equal to 0. | ||
TypeError: If ``reps`` parameter is not an int value. | ||
|
@@ -207,7 +203,7 @@ def flatten(self, flatten: bool) -> None: | |
self._invalidate() | ||
self._flatten = flatten | ||
|
||
def _convert_to_block(self, layer: Any) -> QuantumCircuit: | ||
def _convert_to_block(self, layer: typing.Any) -> QuantumCircuit: | ||
Comment on lines
-210
to
+206
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this can probably be changed to |
||
"""Try to convert ``layer`` to a QuantumCircuit. | ||
|
||
Args: | ||
|
@@ -289,17 +285,9 @@ def entanglement_blocks( | |
@property | ||
def entanglement( | ||
self, | ||
) -> Union[ | ||
str, | ||
list[str], | ||
list[list[str]], | ||
list[int], | ||
list[list[int]], | ||
list[list[list[int]]], | ||
list[list[list[list[int]]]], | ||
Callable[[int], str], | ||
Callable[[int], list[list[int]]], | ||
]: | ||
) -> str | list[str] | list[list[str]] | list[int] | list[list[int]] | list[ | ||
list[list[int]] | ||
] | list[list[list[list[int]]]] | Callable[[int], str] | Callable[[int], list[list[int]]]: | ||
"""Get the entanglement strategy. | ||
|
||
Returns: | ||
|
@@ -311,19 +299,16 @@ def entanglement( | |
@entanglement.setter | ||
def entanglement( | ||
self, | ||
entanglement: Optional[ | ||
Union[ | ||
str, | ||
list[str], | ||
list[list[str]], | ||
list[int], | ||
list[list[int]], | ||
list[list[list[int]]], | ||
list[list[list[list[int]]]], | ||
Callable[[int], str], | ||
Callable[[int], list[list[int]]], | ||
] | ||
], | ||
entanglement: str | ||
| list[str] | ||
| list[list[str]] | ||
| list[int] | ||
| list[list[int]] | ||
| list[list[list[int]]] | ||
| list[list[list[list[int]]]] | ||
| Callable[[int], str] | ||
| Callable[[int], list[list[int]]] | ||
| None, | ||
) -> None: | ||
"""Set the entanglement strategy. | ||
|
||
|
@@ -730,7 +715,7 @@ def parameter_bounds(self, bounds: list[tuple[float, float]]) -> None: | |
|
||
def add_layer( | ||
self, | ||
other: Union["NLocal", qiskit.circuit.Instruction, QuantumCircuit], | ||
other: QuantumCircuit | qiskit.circuit.Instruction, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
entanglement: list[int] | str | list[list[int]] | None = None, | ||
front: bool = False, | ||
) -> "NLocal": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,12 @@ | |
|
||
"""The real-amplitudes 2-local circuit.""" | ||
|
||
from typing import Union, Optional, List, Tuple, Callable, Any | ||
from __future__ import annotations | ||
from collections.abc import Callable | ||
|
||
import numpy as np | ||
|
||
from qiskit.circuit import QuantumCircuit | ||
from qiskit.circuit.library.standard_gates import RYGate, CXGate | ||
from .two_local import TwoLocal | ||
|
||
|
@@ -115,19 +118,18 @@ class RealAmplitudes(TwoLocal): | |
|
||
def __init__( | ||
self, | ||
num_qubits: Optional[int] = None, | ||
entanglement: Union[str, List[List[int]], Callable[[int], List[int]]] = "reverse_linear", | ||
num_qubits: int | None = None, | ||
entanglement: str | list[list[int]] | Callable[[int], list[int]] = "full", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a change in default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, that change was not on purpose 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it's always better to do "reverse_linear" and not "full" since it provides the same unitary, but with O(n) CX gates instead of O(n^2) |
||
reps: int = 3, | ||
skip_unentangled_qubits: bool = False, | ||
skip_final_rotation_layer: bool = False, | ||
parameter_prefix: str = "θ", | ||
insert_barriers: bool = False, | ||
initial_state: Optional[Any] = None, | ||
initial_state: QuantumCircuit | None = None, | ||
name: str = "RealAmplitudes", | ||
flatten: Optional[bool] = None, | ||
flatten: bool | None = None, | ||
) -> None: | ||
"""Create a new RealAmplitudes 2-local circuit. | ||
|
||
""" | ||
Args: | ||
num_qubits: The number of qubits of the RealAmplitudes circuit. | ||
reps: Specifies how often the structure of a rotation layer followed by an entanglement | ||
|
@@ -178,7 +180,7 @@ def __init__( | |
) | ||
|
||
@property | ||
def parameter_bounds(self) -> List[Tuple[float, float]]: | ||
def parameter_bounds(self) -> list[tuple[float, float]]: | ||
"""Return the parameter bounds. | ||
|
||
Returns: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this change deliberately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I thought I left a comment on that -- yes this is on purpose, we deprecated
Any
a few months ago (it's already gone in some NLocal circuits) but it seems we forgot it here 🙂Any
was mainly for backwards compatibility with old applications code so we don't need it anymore