From 9491c1830a309d3f1372897eddbc0f13bb5f8f6f Mon Sep 17 00:00:00 2001 From: Guillermo Alonso-Linaje <65235481+KetpuntoG@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:37:49 -0500 Subject: [PATCH] Remove warning example qsvt (#6693) **Context:** - The example shows a warning due to the deprecation of qml.qsvt_legacy. It has been updated - This PR also add a little modification to accept scalars and arrays as qsvt input ([solves this comment](https://github.com/PennyLaneAI/qml/pull/1276#discussion_r1876824006)) --------- Co-authored-by: Jay Soni --- doc/releases/changelog-dev.md | 1 + pennylane/templates/subroutines/qsvt.py | 10 +++++----- tests/templates/test_subroutines/test_qsvt.py | 11 +++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 2899afeee90..1482bf0c51b 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -247,6 +247,7 @@ such as `shots`, `rng` and `prng_key`. * The `qml.qsvt` function has been improved to be more user-friendly. Old functionality is moved to `qml.qsvt_legacy` and it will be deprecated in release v0.40. [(#6520)](https://github.com/PennyLaneAI/pennylane/pull/6520/) + [(#6693)](https://github.com/PennyLaneAI/pennylane/pull/6693)

Other Improvements

diff --git a/pennylane/templates/subroutines/qsvt.py b/pennylane/templates/subroutines/qsvt.py index 3cf7cc409aa..9477e097d6c 100644 --- a/pennylane/templates/subroutines/qsvt.py +++ b/pennylane/templates/subroutines/qsvt.py @@ -229,7 +229,7 @@ def qsvt(A, poly, encoding_wires=None, block_encoding=None, **kwargs): @qml.qnode(dev) def circuit(): - qml.qsvt(hamiltonian, poly, encoding_wires=[0]) + qml.qsvt(hamiltonian, poly, encoding_wires=[0], block_encoding="prepselprep") return qml.state() @@ -328,9 +328,9 @@ def circuit(): if encoding_wires is None or block_encoding is None or "wires" in kwargs.keys(): warnings.warn( - "You may be trying to use the old `qsvt` functionality (now `qml.qsvt_legacy`)." - "Make sure you pass a polynomial instead of angles." - "Set a value for `block_encoding` to silence this warning.", + "You may be trying to use the old `qsvt` functionality (now `qml.qsvt_legacy`).\n" + "Make sure you pass a polynomial instead of angles.\n" + "Set a value for `block_encoding` to silence this warning.\n", qml.PennyLaneDeprecationWarning, ) @@ -369,7 +369,7 @@ def circuit(): "block_encoding = {block_encoding} not supported for A of type {type(A)}. When A is a matrix block_encoding should take the value 'embedding' or 'fable'. Otherwise, please provide an input with a Pauli decomposition. For more details, see the 'qml.pauli_decompose' function." ) - A = qml.math.array(A) + A = qml.math.atleast_2d(A) max_dimension = 1 if len(qml.math.array(A).shape) == 0 else max(A.shape) if block_encoding == "fable": diff --git a/tests/templates/test_subroutines/test_qsvt.py b/tests/templates/test_subroutines/test_qsvt.py index ffddc951288..3e16541c2b1 100644 --- a/tests/templates/test_subroutines/test_qsvt.py +++ b/tests/templates/test_subroutines/test_qsvt.py @@ -696,6 +696,12 @@ def test_qsvt_warning(self): "fable", [0, 1, 2, 3, 4], ), + ( + 0.3, + [0.2, 0, 0.3], + "embedding", + [0], + ), ], ) def test_matrix_input(self, A, poly, encoding_wires, block_encoding): @@ -707,10 +713,11 @@ def circuit(): qml.qsvt(A, poly, encoding_wires, block_encoding) return qml.state() + A_matrix = qml.math.atleast_2d(A) # Calculation of the polynomial transformation on the input matrix - expected = sum(coef * matrix_power(A, i) for i, coef in enumerate(poly)) + expected = sum(coef * matrix_power(A_matrix, i) for i, coef in enumerate(poly)) - assert np.allclose(qml.matrix(circuit)()[: len(A), : len(A)].real, expected) + assert np.allclose(qml.matrix(circuit)()[: len(A_matrix), : len(A_matrix)].real, expected) @pytest.mark.parametrize( ("A", "poly", "block_encoding", "encoding_wires"),