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 8 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
6 changes: 6 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,16 @@
passed to the `QNode`, instead of assuming the method is always `deferred`.
[(#6903)](https://github.com/PennyLaneAI/pennylane/pull/6903)

* The `poly_to_angles` functionality now correctly works with different tensor interfaces.
Also, now it does not modify the input.
[(#6979)](https://github.com/PennyLaneAI/pennylane/pull/6979)


<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):

Guillermo Alonso,
Utkarsh Azad,
Henry Chang,
Yushao Chen,
Expand Down
23 changes: 12 additions & 11 deletions pennylane/templates/subroutines/qsvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,42 +953,43 @@ def circuit_qsvt():

"""

poly_list = list(poly)
# Trailing zeros are removed from the array
for _ in range(len(poly)):
if not np.isclose(poly[-1], 0.0):
for _ in range(len(poly_list)):
if not np.isclose(poly_list[-1], 0.0):
break
poly.pop()
poly_list.pop()

if len(poly) == 1:
if len(poly_list) == 1:
raise AssertionError("The polynomial must have at least degree 1.")

for x in [-1, 0, 1]:
if qml.math.abs(qml.math.sum(coeff * x**i for i, coeff in enumerate(poly))) > 1:
if qml.math.abs(qml.math.sum(coeff * x**i for i, coeff in enumerate(poly_list))) > 1:
# Check that |P(x)| ≤ 1. Only points -1, 0, 1 will be checked.
raise AssertionError("The polynomial must satisfy that |P(x)| ≤ 1 for all x in [-1, 1]")

if routine in ["QSVT", "QSP"]:
if not (
np.isclose(qml.math.sum(qml.math.abs(poly[::2])), 0.0)
or np.isclose(qml.math.sum(qml.math.abs(poly[1::2])), 0.0)
np.isclose(qml.math.sum(qml.math.abs(poly_list[::2])), 0.0)
or np.isclose(qml.math.sum(qml.math.abs(poly_list[1::2])), 0.0)
):
raise AssertionError(
"The polynomial has no definite parity. All odd or even entries in the array must take a value of zero."
)
assert np.allclose(
np.array(poly, dtype=np.complex128).imag, 0
np.array(poly_list, dtype=np.complex128).imag, 0
), "Array must not have an imaginary part"

if routine == "QSVT":
if angle_solver == "root-finding":
return transform_angles(_compute_qsp_angle(poly), "QSP", "QSVT")
return transform_angles(_compute_qsp_angle(poly_list), "QSP", "QSVT")
raise AssertionError("Invalid angle solver method. We currently support 'root-finding'")

if routine == "QSP":
if angle_solver == "root-finding":
return _compute_qsp_angle(poly)
return _compute_qsp_angle(poly_list)
raise AssertionError("Invalid angle solver method. Valid value is 'root-finding'")

if routine == "GQSP":
return _compute_gqsp_angles(poly)
return _compute_gqsp_angles(poly_list)
raise AssertionError("Invalid routine. Valid values are 'QSP' and 'QSVT'")
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)
Loading