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

angle solver qsp/qsvt #6483

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open

angle solver qsp/qsvt #6483

wants to merge 43 commits into from

Conversation

KetpuntoG
Copy link
Contributor

@KetpuntoG KetpuntoG commented Oct 30, 2024

Context:

This PR appears in the context of the QSVT project, specifically focusing on the implementation of the angle solver, functionality to calculate the angles needed to apply a polynomial transformation.

Description of the Change:

Added qml.poly_to_angles, the main functionality of the PR that takes as input a polynomial and returns the angles.
Also included is the function qml.transform_angles, which transforms the angle format between qsp and qsvt.
Both these functions and other auxiliary functions have been added in qsvt.py.

Benefits:

Calculating angles is not a simple task and existing libraries have some limitations. With this new functionality we will facilitate the use of routines such as qsp and qsvt.

Possible Drawbacks:

Currently only one solver has been introduced. In the future it is expected to add more that will work for more extreme cases: larger polynomials (degree > 1000) or degenerate polynomials

[sc-72631]

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link

codecov bot commented Oct 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.46%. Comparing base (828c1ec) to head (91eb3a8).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6483   +/-   ##
=======================================
  Coverage   99.46%   99.46%           
=======================================
  Files         454      454           
  Lines       42649    42716   +67     
=======================================
+ Hits        42420    42487   +67     
  Misses        229      229           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@soranjh soranjh changed the title Angle solver qsp/qsvt [prototype] angle solver qsp/qsvt Oct 30, 2024
@soranjh soranjh added the do not merge ⚠️ Do not merge the pull request until this label is removed label Oct 30, 2024
@@ -123,6 +124,7 @@ def qsvt(A, angles, wires, convention=None):
A = qml.math.reshape(A, [1, 1])

c, r = qml.math.shape(A)
global_phase, global_phase_op = None, None
Copy link
Contributor Author

@KetpuntoG KetpuntoG Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to put this in because codefactor complained that I was using global_phase before giving it any value in line 148

@KetpuntoG KetpuntoG removed the do not merge ⚠️ Do not merge the pull request until this label is removed label Nov 20, 2024
@KetpuntoG KetpuntoG changed the title [prototype] angle solver qsp/qsvt angle solver qsp/qsvt Nov 20, 2024
Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly small comments on the doc-strings and general questions. Great work! The PR looks good.

pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
poly_degree = len(P) - 1

# Build the polynomial R(z) = z^degree * (1 - conj(P(1/z)) * P(z)), deduced from (eq.33) and (eq.34)
R = Polynomial.basis(poly_degree) - Polynomial(P) * Polynomial(np.conj(P[::-1]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem like it matches the comment?

Suggested change
R = Polynomial.basis(poly_degree) - Polynomial(P) * Polynomial(np.conj(P[::-1]))
R = Polynomial.basis(poly_degree) * (1 - Polynomial(P) * Polynomial(np.conj(P[::-1])))

Also why do you need to do P[::-1]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, actually this is a bit unintuitive at first glance

The key here is that if you have $P(z) = a + bz^1 + cz^2$, then
$P(1/z) = a + bz^{-1} + cz^{-2}$
Which is equal to
$P(1/z)= z^{-2} (a z^2 + bz + c) $
Which is equal to
$P(1/z) = z^{-2}(P[::-1])$

and it can be simplified as the expression showed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, here we decomposed the reflection into reversal + trasnlation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider adding a brief description as comment to the code, specially if you have modified the equations of the paper.

pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
tests/templates/test_subroutines/test_qsvt.py Outdated Show resolved Hide resolved
([-0.4, 2, 0.4, 0, -0.1, 0, 0.1]),
],
)
def test_transform_angles(self, angles):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just tests that transform(transform(angles, "QSP", "QSVT"), "QSVT", "QSP") is the identity operation. We should check that for a particular polynomial's QSP angles should produce the same poly with transformed QSVT angles.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internally when I ask for the QSVT angles, I calculate the QSP angles and apply the function. So it is going always fulfilled (whether the function works correctly or not).

Copy link
Contributor

@soranjh soranjh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @KetpuntoG, left my first round of comments.

pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved
pennylane/templates/subroutines/qsvt.py Outdated Show resolved Hide resolved

for x in [-1, 0, 1]:
if qml.math.abs(qml.math.sum(coeff * x**i for i, coeff in enumerate(poly))) > 1:
# It is not a property that we can check globally but checking these three points is useful
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P should mee that |P(x)| ≤ 1
As it is not always easy to detect and the user may not be aware, I am checking that it is true at least in x = -1, x = 0, x = 1. I updated the comment 👍

pennylane/templates/subroutines/qsvt.py Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants