From 6fca06699551d34c0ff8c52a01a31bfa0fef4966 Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Mon, 9 May 2022 11:05:11 -0400 Subject: [PATCH] Fix PennyLane v0.23.1 to work with Autoray 0.3.1 (#2548) * Fix PennyLane to work with Autoray 0.3.1 * fix * update * add new changelog file * Update doc/releases/changelog-0.23.1.md Co-authored-by: antalszava Co-authored-by: antalszava --- doc/development/release_notes.md | 1 + doc/releases/changelog-0.23.0.md | 2 +- doc/releases/changelog-0.23.1.md | 14 ++++++++++++++ pennylane/_version.py | 2 +- pennylane/math/multi_dispatch.py | 8 +++++++- pennylane/math/single_dispatch.py | 11 ----------- pennylane/math/utils.py | 4 ++-- requirements.txt | 2 +- setup.py | 2 +- tests/math/test_functions.py | 2 -- 10 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 doc/releases/changelog-0.23.1.md diff --git a/doc/development/release_notes.md b/doc/development/release_notes.md index aaebd6e1588..47962836e92 100644 --- a/doc/development/release_notes.md +++ b/doc/development/release_notes.md @@ -3,6 +3,7 @@ Release notes This page contains the release notes for PennyLane. +.. mdinclude:: ../releases/changelog-0.23.1.md .. mdinclude:: ../releases/changelog-0.23.0.md diff --git a/doc/releases/changelog-0.23.0.md b/doc/releases/changelog-0.23.0.md index 2121d6c349f..42fca99e7fd 100644 --- a/doc/releases/changelog-0.23.0.md +++ b/doc/releases/changelog-0.23.0.md @@ -1,6 +1,6 @@ :orphan: -# Release 0.23.0 (current release) +# Release 0.23.0

New features since last release

diff --git a/doc/releases/changelog-0.23.1.md b/doc/releases/changelog-0.23.1.md new file mode 100644 index 00000000000..e28f64aa955 --- /dev/null +++ b/doc/releases/changelog-0.23.1.md @@ -0,0 +1,14 @@ +:orphan: + +# Release 0.23.1 (current release) + +

Bug fixes

+ +* Fixed a bug enabling PennyLane to work with the latest version of Autoray. + [(#2548)](https://github.com/PennyLaneAI/pennylane/pull/2548) + +

Contributors

+ +This release contains contributions from (in alphabetical order): + +Josh Izaac diff --git a/pennylane/_version.py b/pennylane/_version.py index 88cab729579..146e3baa675 100644 --- a/pennylane/_version.py +++ b/pennylane/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.23.0" +__version__ = "0.23.1" diff --git a/pennylane/math/multi_dispatch.py b/pennylane/math/multi_dispatch.py index cbe91448d71..4a7d6d77ccd 100644 --- a/pennylane/math/multi_dispatch.py +++ b/pennylane/math/multi_dispatch.py @@ -574,7 +574,13 @@ def where(condition, x=None, y=None): interface = _multi_dispatch([condition]) return np.where(condition, like=interface) - return np.where(condition, x, y, like=_multi_dispatch([condition, x, y])) + interface = _multi_dispatch([condition, x, y]) + res = np.where(condition, x, y, like=interface) + + if interface == "tensorflow": + return np.transpose(np.stack(res)) + + return res @multi_dispatch(argnum=[0, 1]) diff --git a/pennylane/math/single_dispatch.py b/pennylane/math/single_dispatch.py index d5a6a7ff2b1..271ada39c31 100644 --- a/pennylane/math/single_dispatch.py +++ b/pennylane/math/single_dispatch.py @@ -320,17 +320,6 @@ def _to_numpy_torch(x): ar.register_function("torch", "shape", lambda x: tuple(x.shape)) ar.register_function("torch", "gather", lambda x, indices: x[indices]) -try: - if semantic_version.match(">=1.10", _i("torch").__version__): - # Autoray uses the deprecated torch.symeig as an alias for eigh, however this has - # been deprecated in favour of torch.linalg.eigh. - # autoray.py:84: UserWarning: torch.symeig is deprecated in favor of torch.linalg.eigh - # and will be removed in a future PyTorch release. - del ar.autoray._FUNCS["torch", "linalg.eigh"] -except ImportError: - pass - - ar.register_function( "torch", "sqrt", diff --git a/pennylane/math/utils.py b/pennylane/math/utils.py index b9398e92429..b6a93981de5 100644 --- a/pennylane/math/utils.py +++ b/pennylane/math/utils.py @@ -59,7 +59,7 @@ def allclose(a, b, rtol=1e-05, atol=1e-08, **kwargs): # Some frameworks may provide their own allclose implementation. # Try and use it if available. res = np.allclose(a, b, rtol=rtol, atol=atol, **kwargs) - except (TypeError, AttributeError): + except (TypeError, AttributeError, ImportError): # Otherwise, convert the input to NumPy arrays. # # TODO: replace this with a bespoke, framework agnostic @@ -111,7 +111,7 @@ def cast(tensor, dtype): if not isinstance(dtype, str): try: dtype = np.dtype(dtype).name - except (AttributeError, TypeError): + except (AttributeError, TypeError, ImportError): dtype = getattr(dtype, "name", dtype) return ar.astype(tensor, ar.to_backend_dtype(dtype, like=ar.infer_backend(tensor))) diff --git a/requirements.txt b/requirements.txt index 8f17ae9da69..9642ba93f21 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,7 +11,7 @@ toml==0.10 appdirs==1.4 semantic_version==2.6 dask[delayed]==2021.10 -autoray>=0.2.5 +autoray==0.3.1 matplotlib==3.4 black>=21 opt_einsum==3.3 \ No newline at end of file diff --git a/setup.py b/setup.py index 8f5790d3f84..12d91320f0f 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ "toml", "appdirs", "semantic_version==2.6", - "autoray", + "autoray>=0.3.1", "cachetools", "pennylane-lightning>=0.23", ] diff --git a/tests/math/test_functions.py b/tests/math/test_functions.py index 341991dbed7..a352421fa3c 100644 --- a/tests/math/test_functions.py +++ b/tests/math/test_functions.py @@ -1412,8 +1412,6 @@ def test_where(interface, t): [0, 0, 1, 1, 2, 0, 0, 2, 2], [0, 1, 0, 1, 1, 0, 1, 0, 1], ) - if interface == "tf": - expected = qml.math.T(expected) assert all(fn.allclose(_res, _exp) for _res, _exp in zip(res, expected))