Skip to content

Add FQK derivatives using a new lowlevel class FidelityKernelExpectationValue #320

Merged
David-Kreplin merged 23 commits intosQUlearn:developfrom
RobertoFlorez:FQK_derivatives_develop
Apr 16, 2025
Merged

Add FQK derivatives using a new lowlevel class FidelityKernelExpectationValue #320
David-Kreplin merged 23 commits intosQUlearn:developfrom
RobertoFlorez:FQK_derivatives_develop

Conversation

@RobertoFlorez
Copy link
Contributor

Hi!

Here is the implementation for the derivatives of the FQKs for both 1-D and N-D variables: dKdx, dKdxdx, dKdxdy, dKdydx, dKdp, etc

Basically, it implements a FQK as a QNN ( U(y)^\dagger U(x) |0>) and measures against the operator P_0 = |0><0|^\otimes n.

For this, I had to:

  1. Modify EncodingCircuitBase

    • create .inverse() such that squlearn encoding circuits can be inverted
    • create .compose method instead of ("+") such that the sum of two circuits includes new features and uses the same parameters. I modified ("+") such that it is an especial case of .compose.
  2. Created the FidelityKernelExpectationValue class that creates the QNN in the QK format, and implemented .evaluate_derivatives that calculates the derivatives from the QNN.

    • I also implemented a first version of _evaluate_duplicates = "none", for this class. The routine only calculates the lower triangle values of the kernel matrix "K" and then reshapes it appropriately. I only implemented it for "K", for the derivatives I have not implemented anything yet.
  3. I benchmarked the derivatives with an analytical example and created three tests: single variable derivatives, multi variable derivatives and duplicate tests.

Also perhaps FidelityKernelExpectationValue is not a good name, as always calculating a QK requires obtaining expectation values.

Feel free to give me feedback and let me know what should be improved.

Thank you very much for your help! 🚀
Roberto

PD: I used black but I dont know why it is failing the tests :/

@RobertoFlorez RobertoFlorez marked this pull request as ready for review March 6, 2025 12:13
Copy link
Collaborator

@David-Kreplin David-Kreplin left a comment

Choose a reason for hiding this comment

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

The overall code looks good - thank you very much!

However, the docstrings in fidelity_kernel_expectation_value.py are currently a bit chaotic.
Additionally, many comments and strings exceed 100 characters. Could you fix this?

I also have the impression that the Args and Parameters sections were modified using search and replace, which introduced some inconsistencies.

Please ensure that the following format is used consistently throughout the code:

"""
Single-line summary.

Detailed description.

Args:
    variable_name (variable_type): Variable description.

Returns:
    return_type: Return value description.
"""

David-Kreplin and others added 7 commits March 10, 2025 18:34
…h:`U(x)^\dagger U(y) |0\rangle` and measuring the expectation value of the operator

    :math:`P_0 = |0\rangle\langle 0|^{\otimes n}`, where :math:`U(x)` is the encoding circuit unitary
    evaluated for data `x`.
@RobertoFlorez
Copy link
Contributor Author

Hi @David-Kreplin!

I think you can already review it again. I think I implemented all your comments and try to make the docstrings less chaotic. If you think they still need improvement, please let me know.

Also, two more points:

  1. For some reason the latex code that I am using breaks the documentation. There are two placeholders #LATEXHERE. Could you replace them with:

:math:U(x)^\dagger U(y) |0\rangle and measuring the expectation value of the operator :math:P_0 = |0\rangle\langle 0|^{\otimes n}, where :math:U(x) is the encoding circuit unitary evaluated for data x.

  1. Sorry for the formatting check fail, my black it is not working properly.

Thank you very much! Again, if there is anything else, please let me know 😄

Roberto

@David-Kreplin David-Kreplin merged commit 585d930 into sQUlearn:develop Apr 16, 2025
6 checks passed
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.

2 participants