Skip to content

Commit

Permalink
Remove warning example qsvt (#6693)
Browse files Browse the repository at this point in the history
**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](PennyLaneAI/qml#1276 (comment)))

---------

Co-authored-by: Jay Soni <[email protected]>
  • Loading branch information
2 people authored and mudit2812 committed Dec 13, 2024
1 parent a0366f1 commit 9491c18
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<h4>Other Improvements</h4>

Expand Down
10 changes: 5 additions & 5 deletions pennylane/templates/subroutines/qsvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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":
Expand Down
11 changes: 9 additions & 2 deletions tests/templates/test_subroutines/test_qsvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"),
Expand Down

0 comments on commit 9491c18

Please sign in to comment.