Skip to content

Commit

Permalink
Merge branch 'master' into rot_math
Browse files Browse the repository at this point in the history
  • Loading branch information
dwierichs committed Jul 24, 2024
2 parents 4dc44a5 + db7b281 commit ea3fc47
Show file tree
Hide file tree
Showing 157 changed files with 740 additions and 702 deletions.
1 change: 1 addition & 0 deletions doc/code/qml_tape.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ and a reduction in unintended side effects, ``QuantumScript`` is strictly used i
.. automodapi:: pennylane.tape
:no-main-docstr:
:include-all-objects:
:skip: QuantumTapeBatch
:inheritance-diagram:
4 changes: 2 additions & 2 deletions pennylane/_qubit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import logging
import warnings
from collections import defaultdict
from typing import List, Union
from typing import Union

import numpy as np

Expand Down Expand Up @@ -1777,7 +1777,7 @@ def _adjoint_jacobian_processing(jac):
# must be 2-dimensional
return tuple(tuple(np.array(j_) for j_ in j) for j in jac)

def _get_diagonalizing_gates(self, circuit: QuantumTape) -> List[Operation]:
def _get_diagonalizing_gates(self, circuit: QuantumTape) -> list[Operation]:
"""Returns the gates that diagonalize the measured wires such that they
are in the eigenbasis of the circuit observables.
Expand Down
5 changes: 3 additions & 2 deletions pennylane/capture/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
This submodule defines the abstract classes and primitives for capture.
"""

from collections.abc import Callable
from functools import lru_cache
from typing import Callable, Optional, Tuple, Type
from typing import Optional, Type

import pennylane as qml

Expand Down Expand Up @@ -107,7 +108,7 @@ def __init__(
self._n_wires = n_wires
self.has_eigvals: bool = has_eigvals

def abstract_eval(self, num_device_wires: int, shots: int) -> Tuple[Tuple, type]:
def abstract_eval(self, num_device_wires: int, shots: int) -> tuple[tuple, type]:
"""Calculate the shape and dtype for an evaluation with specified number of device
wires and shots.
Expand Down
4 changes: 2 additions & 2 deletions pennylane/capture/switches.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Contains the switches to (de)activate the capturing mechanism, and a
status reporting function on whether it is enabled or not.
"""
from typing import Callable
from collections.abc import Callable

has_jax = True
try:
Expand All @@ -24,7 +24,7 @@
has_jax = False


def _make_switches() -> [Callable[[], None], Callable[[], None], Callable[[], bool]]:
def _make_switches() -> tuple[Callable[[], None], Callable[[], None], Callable[[], bool]]:
r"""Create three functions, corresponding to an activation switch, a deactivation switch
and a status query, in that order.
Expand Down
21 changes: 10 additions & 11 deletions pennylane/data/attributes/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
of Dataset attributes."""


import typing
from collections.abc import Mapping
from typing import Dict, Generic, Union
from collections.abc import Iterator, Mapping, MutableMapping
from typing import Generic, Union

from pennylane.data.base.attribute import DatasetAttribute
from pennylane.data.base.hdf5 import HDF5Any, HDF5Group
Expand All @@ -27,35 +26,35 @@

class DatasetDict( # pylint: disable=too-many-ancestors
Generic[T],
DatasetAttribute[HDF5Group, typing.Mapping[str, T], typing.Mapping[str, T]],
typing.MutableMapping[str, T],
DatasetAttribute[HDF5Group, Mapping[str, T], Mapping[str, T]],
MutableMapping[str, T],
MapperMixin,
):
"""Provides a dict-like collection for Dataset attribute types. Keys must
be strings."""

type_id = "dict"

def __post_init__(self, value: typing.Mapping[str, T]):
def __post_init__(self, value: Mapping[str, T]):
super().__post_init__(value)
self.update(value)

@classmethod
def default_value(cls) -> Dict:
def default_value(cls) -> dict:
return {}

def hdf5_to_value(self, bind: HDF5Group) -> typing.MutableMapping[str, T]:
def hdf5_to_value(self, bind: HDF5Group) -> MutableMapping[str, T]:
return self

def value_to_hdf5(self, bind_parent: HDF5Group, key: str, value: None) -> HDF5Group:
grp = bind_parent.create_group(key)

return grp

def copy_value(self) -> Dict[str, T]:
def copy_value(self) -> dict[str, T]:
return {key: attr.copy_value() for key, attr in self._mapper.items()}

def copy(self) -> Dict[str, T]:
def copy(self) -> dict[str, T]:
"""Returns a copy of this mapping as a builtin ``dict``, with all
elements copied."""
return self.copy_value()
Expand Down Expand Up @@ -93,7 +92,7 @@ def __eq__(self, __value: object) -> bool:

return all(__value[key] == self[key] for key in __value.keys())

def __iter__(self) -> typing.Iterator[str]:
def __iter__(self) -> Iterator[str]:
return (key for key in self.bind.keys())

def __str__(self) -> str:
Expand Down
25 changes: 11 additions & 14 deletions pennylane/data/attributes/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
"""Contains an DatasetAttribute that allows for heterogeneous lists of dataset
types."""

import typing
from collections.abc import Sequence
from typing import Generic, List, Union, overload
from collections.abc import Iterable, MutableSequence, Sequence
from typing import Generic, Union, overload

from pennylane.data.base.attribute import DatasetAttribute
from pennylane.data.base.hdf5 import HDF5Any, HDF5Group
Expand All @@ -26,37 +25,35 @@

class DatasetList( # pylint: disable=too-many-ancestors
Generic[T],
DatasetAttribute[HDF5Group, typing.Sequence[T], typing.Iterable[T]],
typing.MutableSequence[T],
DatasetAttribute[HDF5Group, Sequence[T], Iterable[T]],
MutableSequence[T],
MapperMixin,
):
"""Provides a list-like collection type for Dataset Attributes."""

type_id = "list"

def __post_init__(self, value: typing.Iterable[T]):
def __post_init__(self, value: Iterable[T]):
super().__post_init__(value)

self.extend(value)

@classmethod
def default_value(cls) -> typing.Iterable[T]:
def default_value(cls) -> Iterable[T]:
return []

def hdf5_to_value(self, bind: HDF5Group) -> typing.MutableSequence[T]:
def hdf5_to_value(self, bind: HDF5Group) -> MutableSequence[T]:
return self

def value_to_hdf5(
self, bind_parent: HDF5Group, key: str, value: typing.Iterable[T]
) -> HDF5Group:
def value_to_hdf5(self, bind_parent: HDF5Group, key: str, value: Iterable[T]) -> HDF5Group:
grp = bind_parent.create_group(key)

return grp

def copy_value(self) -> List[T]:
def copy_value(self) -> list[T]:
return [self._mapper[str(i)].copy_value() for i in range(len(self))]

def copy(self) -> List[T]:
def copy(self) -> list[T]:
"""Returns a copy of this list as a builtin ``list``, with all
elements copied.."""
return self.copy_value()
Expand Down Expand Up @@ -97,7 +94,7 @@ def __repr__(self) -> str:
return f"[{items_repr}]"

@overload
def __getitem__(self, index: slice) -> typing.List[T]:
def __getitem__(self, index: slice) -> list[T]:
pass

@overload
Expand Down
4 changes: 2 additions & 2 deletions pennylane/data/attributes/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
"""Contains DatasetAttribute definition for ``pennylane.qchem.Molecule``."""

from typing import Tuple, Type
from typing import Type

from pennylane.data.base.attribute import DatasetAttribute
from pennylane.data.base.hdf5 import HDF5Group
Expand All @@ -27,7 +27,7 @@ class DatasetMolecule(DatasetAttribute[HDF5Group, Molecule, Molecule]):
type_id = "molecule"

@classmethod
def consumes_types(cls) -> Tuple[Type[Molecule]]:
def consumes_types(cls) -> tuple[Type[Molecule]]:
return (Molecule,)

def hdf5_to_value(self, bind: HDF5Group) -> Molecule:
Expand Down
4 changes: 2 additions & 2 deletions pennylane/data/attributes/none.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
"""Contains DatasetAttribute definition for None"""

from typing import Literal, Tuple, Type
from typing import Literal, Type

from pennylane.data.base.attribute import DatasetAttribute
from pennylane.data.base.hdf5 import HDF5Array, HDF5Group
Expand All @@ -29,7 +29,7 @@ def default_value(cls) -> Literal[None]:
return None

@classmethod
def consumes_types(cls) -> Tuple[Type[None]]:
def consumes_types(cls) -> tuple[Type[None]]:
return (type(None),)

def hdf5_to_value(self, bind) -> None:
Expand Down
12 changes: 6 additions & 6 deletions pennylane/data/attributes/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
of operators."""

import json
import typing
from collections.abc import Sequence
from functools import lru_cache
from typing import Dict, FrozenSet, Generic, List, Type, TypeVar
from typing import Generic, Type, TypeVar

import numpy as np

Expand Down Expand Up @@ -52,7 +52,7 @@ class DatasetOperator(Generic[Op], DatasetAttribute[HDF5Group, Op, Op]):

@classmethod
@lru_cache(1)
def supported_ops(cls) -> FrozenSet[Type[Operator]]:
def supported_ops(cls) -> frozenset[Type[Operator]]:
"""Set of supported operators."""
return frozenset(
(
Expand Down Expand Up @@ -198,7 +198,7 @@ def hdf5_to_value(self, bind: HDF5Group) -> Op:
return self._hdf5_to_ops(bind)[0]

def _ops_to_hdf5(
self, bind_parent: HDF5Group, key: str, value: typing.Sequence[Operator]
self, bind_parent: HDF5Group, key: str, value: Sequence[Operator]
) -> HDF5Group:
"""Serialize op sequence ``value``, and create nested sequences for any
composite ops in ``value``.
Expand Down Expand Up @@ -247,7 +247,7 @@ def _ops_to_hdf5(

return bind

def _hdf5_to_ops(self, bind: HDF5Group) -> List[Operator]:
def _hdf5_to_ops(self, bind: HDF5Group) -> list[Operator]:
"""Load list of serialized ops from ``bind``."""
ops = []

Expand Down Expand Up @@ -293,6 +293,6 @@ def _hdf5_to_ops(self, bind: HDF5Group) -> List[Operator]:

@classmethod
@lru_cache(1)
def _supported_ops_dict(cls) -> Dict[str, Type[Operator]]:
def _supported_ops_dict(cls) -> dict[str, Type[Operator]]:
"""Returns a dict mapping ``Operator`` subclass names to the class."""
return {op.__name__: op for op in cls.supported_ops()}
6 changes: 3 additions & 3 deletions pennylane/data/attributes/sparse_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Contains DatasetAttribute definition for ``scipy.sparse.csr_array``."""

from functools import lru_cache
from typing import Dict, Generic, Tuple, Type, TypeVar, Union, cast
from typing import Generic, Type, TypeVar, Union, cast

import numpy as np
from scipy.sparse import (
Expand Down Expand Up @@ -64,7 +64,7 @@ def sparse_array_class(self) -> Type[SparseT]:
@classmethod
def consumes_types(
cls,
) -> Tuple[Type[Union[SparseArray, SparseMatrix]], ...]:
) -> tuple[Type[Union[SparseArray, SparseMatrix]], ...]:
return (
bsr_array,
coo_array,
Expand Down Expand Up @@ -122,6 +122,6 @@ def value_to_hdf5(self, bind_parent: HDF5Group, key: str, value: SparseT) -> HDF

@classmethod
@lru_cache(1)
def _supported_sparse_dict(cls) -> Dict[str, Type[Union[SparseArray, SparseMatrix]]]:
def _supported_sparse_dict(cls) -> dict[str, Type[Union[SparseArray, SparseMatrix]]]:
"""Returns a dict mapping sparse array class names to the class."""
return {op.__name__: op for op in cls.consumes_types()}
4 changes: 2 additions & 2 deletions pennylane/data/attributes/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Contains an DatasetAttribute for str objects."""


from typing import Tuple, Type
from typing import Type

from pennylane.data.base.attribute import DatasetAttribute
from pennylane.data.base.hdf5 import HDF5Array, HDF5Group
Expand All @@ -26,7 +26,7 @@ class DatasetString(DatasetAttribute[HDF5Array, str, str]):
type_id = "string"

@classmethod
def consumes_types(cls) -> Tuple[Type[str]]:
def consumes_types(cls) -> tuple[Type[str]]:
return (str,)

def hdf5_to_value(self, bind: HDF5Array) -> str:
Expand Down
13 changes: 6 additions & 7 deletions pennylane/data/attributes/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"""Contains an DatasetAttribute that allows for heterogeneous tuples of dataset
types."""

import typing
from typing import Generic
from typing import Generic, Type

from pennylane.data.base.attribute import DatasetAttribute
from pennylane.data.base.hdf5 import HDF5Group
Expand All @@ -25,21 +24,21 @@

class DatasetTuple(
Generic[T],
DatasetAttribute[HDF5Group, typing.Tuple[T], typing.Tuple[T]],
DatasetAttribute[HDF5Group, tuple[T], tuple[T]],
):
"""Type for tuples."""

type_id = "tuple"

@classmethod
def consumes_types(cls) -> typing.Tuple[typing.Type[tuple]]:
def consumes_types(cls) -> tuple[Type[tuple]]:
return (tuple,)

@classmethod
def default_value(cls) -> typing.Tuple[()]:
def default_value(cls) -> tuple[()]:
return tuple()

def value_to_hdf5(self, bind_parent: HDF5Group, key: str, value: typing.Tuple[T]) -> HDF5Group:
def value_to_hdf5(self, bind_parent: HDF5Group, key: str, value: tuple[T]) -> HDF5Group:
grp = bind_parent.create_group(key)

mapper = AttributeTypeMapper(grp)
Expand All @@ -48,7 +47,7 @@ def value_to_hdf5(self, bind_parent: HDF5Group, key: str, value: typing.Tuple[T]

return grp

def hdf5_to_value(self, bind: HDF5Group) -> typing.Tuple[T]:
def hdf5_to_value(self, bind: HDF5Group) -> tuple[T]:
mapper = AttributeTypeMapper(bind)

return tuple(mapper[str(i)].copy_value() for i in range(len(self.bind)))
3 changes: 2 additions & 1 deletion pennylane/data/base/_lazy_modules.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Contains a lazy-loaded interface to the HDF5 module. For internal use only."""

import importlib
from collections.abc import Callable
from types import ModuleType
from typing import Any, Callable, Optional, Union
from typing import Any, Optional, Union

_MISSING_MODULES_EXC = ImportError(
"This feature requires the 'aiohttp', 'h5py' and 'fsspec' packages. "
Expand Down
Loading

0 comments on commit ea3fc47

Please sign in to comment.