Skip to content
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

poly_to_angles fixing #6978 #6979

Merged
merged 29 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
46cf8ef
fixing bug list
KetpuntoG Feb 19, 2025
e0405cf
change log
KetpuntoG Feb 19, 2025
a4098f2
Update qsvt.py
KetpuntoG Feb 19, 2025
6f12696
Update test_qsvt.py
KetpuntoG Feb 19, 2025
70d9d34
Update test_qsvt.py
KetpuntoG Feb 19, 2025
b4f4a96
Update test_qsvt.py
KetpuntoG Feb 19, 2025
4d48984
Update tests/templates/test_subroutines/test_qsvt.py
KetpuntoG Feb 21, 2025
44b76cd
Merge branch 'master' into poly_to_angles_fix
KetpuntoG Feb 21, 2025
4a41489
docs use arrays
KetpuntoG Feb 24, 2025
a24ff8b
Update doc/releases/changelog-dev.md
KetpuntoG Feb 24, 2025
a8e790d
Apply suggestions from code review
KetpuntoG Feb 25, 2025
515b34e
Utkarsh suggestions
KetpuntoG Feb 25, 2025
48f1258
Merge branch 'master' into poly_to_angles_fix
KetpuntoG Feb 25, 2025
0ed0252
Merge branch 'master' into poly_to_angles_fix
KetpuntoG Feb 25, 2025
4ef9f0d
Tensor
KetpuntoG Feb 25, 2025
cfcaf0c
tf shape
KetpuntoG Feb 25, 2025
4fde712
casting numpy
KetpuntoG Feb 25, 2025
cd9965a
Merge branch 'master' into poly_to_angles_fix
KetpuntoG Feb 25, 2025
2012efe
trim_zeros
KetpuntoG Feb 26, 2025
a867f88
Merge branch 'poly_to_angles_fix' of https://github.com/PennyLaneAI/p…
KetpuntoG Feb 26, 2025
5a05430
Update pennylane/templates/subroutines/qsvt.py
KetpuntoG Feb 26, 2025
d9c2e20
Update qsvt.py
KetpuntoG Feb 26, 2025
943edb3
Delete patata.py
KetpuntoG Feb 26, 2025
cbca45b
Update qsvt.py
KetpuntoG Feb 26, 2025
a187bf6
Merge branch 'poly_to_angles_fix' of https://github.com/PennyLaneAI/p…
KetpuntoG Feb 26, 2025
02c92cb
Update doc/releases/changelog-dev.md
KetpuntoG Feb 26, 2025
1eafc95
Update pennylane/templates/subroutines/qsvt.py
KetpuntoG Feb 26, 2025
a27b6e0
Merge branch 'master' into poly_to_angles_fix
KetpuntoG Feb 26, 2025
507ea89
Merge branch 'master' into poly_to_angles_fix
KetpuntoG Feb 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@
passed to the `QNode`, instead of assuming the method is always `deferred`.
[(#6903)](https://github.com/PennyLaneAI/pennylane/pull/6903)

* The `poly_to_angles` function has been improved to correctly work with different interfaces and
no longer manipulate the input angles tensor internally.
[(#6979)](https://github.com/PennyLaneAI/pennylane/pull/6979)

* The `QROM` template is upgraded to decompose more efficiently when `work_wires` are not used.
[#6967)](https://github.com/PennyLaneAI/pennylane/pull/6967)

Expand Down
14 changes: 5 additions & 9 deletions pennylane/templates/subroutines/qsvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class QSVT(Operation):

.. code-block::

poly = [0, -1, 0, 0.5, 0, 0.5]
poly = np.array([0, -1, 0, 0.5, 0, 0.5])
angles = qml.poly_to_angles(poly, "QSVT")
input_matrix = np.array([[0.2, 0.1], [0.1, -0.1]])

Expand Down Expand Up @@ -825,7 +825,7 @@ def transform_angles(angles, routine1, routine2):

.. code-block::

poly = [0, 1.0, 0, -1/2, 0, 1/3]
poly = np.array([0, 1.0, 0, -1/2, 0, 1/3])

qsp_angles = qml.poly_to_angles(poly, "QSP")
qsvt_angles = qml.transform_angles(qsp_angles, "QSP", "QSVT")
Expand Down Expand Up @@ -911,7 +911,7 @@ def poly_to_angles(poly, routine, angle_solver: Literal["root-finding"] = "root-

.. code-block::

>>> poly = [0, 1.0, 0, -1/2, 0, 1/3]
>>> poly = np.array([0, 1.0, 0, -1/2, 0, 1/3])
>>> qsvt_angles = qml.poly_to_angles(poly, "QSVT")
>>> print(qsvt_angles)
[-5.49778714 1.57079633 1.57079633 0.5833829 1.61095884 0.74753829]
Expand All @@ -925,7 +925,7 @@ def poly_to_angles(poly, routine, angle_solver: Literal["root-finding"] = "root-

.. code-block::

poly = [0, 1.0, 0, -1/2, 0, 1/3]
poly = np.array([0, 1.0, 0, -1/2, 0, 1/3])

qsvt_angles = qml.poly_to_angles(poly, "QSVT")

Expand Down Expand Up @@ -953,11 +953,7 @@ def circuit_qsvt():

"""

# Trailing zeros are removed from the array
for _ in range(len(poly)):
if not np.isclose(poly[-1], 0.0):
break
poly.pop()
poly = qml.math.trim_zeros(qml.math.array(poly, like="numpy"), trim="b")

if len(poly) == 1:
raise AssertionError("The polynomial must have at least degree 1.")
Expand Down
63 changes: 63 additions & 0 deletions tests/templates/test_subroutines/test_qsvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,3 +792,66 @@ def test_raise_error(self, poly, routine, angle_solver, msg_match):

with pytest.raises(AssertionError, match=msg_match):
_ = qml.poly_to_angles(poly, routine, angle_solver)

def test_immutable_input(self):
"""Test `poly_to_angles` does not modify the input"""

poly = [0, 1.0, 0, -1 / 2, 0, 1 / 3, 0]
poly_copy = poly.copy()
qml.poly_to_angles(poly, "QSVT")

assert len(poly) == len(poly_copy)
assert np.allclose(poly, poly_copy)

def test_interface_numpy(self):
"""Test `poly_to_angles` works with numpy"""

poly = [0, 1.0, 0, -1 / 2, 0, 1 / 3, 0]
angles = qml.poly_to_angles(poly, "QSVT")

poly_numpy = np.array(poly)
angles_numpy = qml.poly_to_angles(poly_numpy, "QSVT")

assert qml.math.allclose(angles, angles_numpy)

@pytest.mark.jax
def test_interface_jax(self):
"""Test `poly_to_angles` works with jax"""

import jax

poly = [0, 1.0, 0, -1 / 2, 0, 1 / 3, 0]
angles = qml.poly_to_angles(poly, "QSVT")

poly_jax = jax.numpy.array(poly)
angles_jax = qml.poly_to_angles(poly_jax, "QSVT")

assert qml.math.allclose(angles, angles_jax)

@pytest.mark.torch
def test_interface_torch(self):
"""Test `poly_to_angles` works with torch"""

import torch

poly = [0, 1.0, 0, -1 / 2, 0, 1 / 3, 0]
angles = qml.poly_to_angles(poly, "QSVT")

poly_torch = torch.tensor(poly)
angles_torch = qml.poly_to_angles(poly_torch, "QSVT")

assert qml.math.allclose(angles, angles_torch)

@pytest.mark.tf
def test_interface_tf(self):
"""Test `poly_to_angles` works with tensorflow"""

import tensorflow as tf

poly = [0, 1.0, 0, -1 / 2, 0, 1 / 3, 0]
angles = qml.poly_to_angles(poly, "QSVT")

poly_tf = tf.Variable(poly)
angles_tf = qml.poly_to_angles(poly_tf, "QSVT")

assert qml.math.allclose(angles, angles_tf)