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

[Feature] Add quantum information tools #16

Merged
merged 73 commits into from
Jun 6, 2024

Conversation

inafergra
Copy link
Collaborator

This PR follows the work of this old qadence PR. It adds a subpackage qinfo_tools containing different quantum information-related functionalities. The three files of the subpackage are:

  • qadence_libs/qinfo_tools/qfi.py contains code related to the calculation of the Quantum Fisher Information (QFI) matrix. The function get_quantum_fisher() calculates the QFI matrix of a QuantumCircuit in an exact way, while the get_quantum_fisher_spsa() can be used to iteratively construct an approximation of the QFI matrix through the SPSA algorithm.
  • qadence_libs/qinfo_tools/spsa.pycontains the code to calculate the SPSA gradients. Only the second order gradient is used in the QNG-SPSA optimizer but the first order gradient is also implemented for completeness.
  • The main contribution is the qadence_libs/qinfo_tools/qng.py file implementing the Quantum Natural Gradient (QNG) and the QNG-SPSA optimizers. Both optimizers use the QFI matrix in the parameter update rule to better navigate the gradient descent. The QNG uses the exact QFI matrix, which takes $\mathcal{O}(n^2)$ circuit evaluations per iteration ($n$ being the number of variational params in the circuit), making the QNG optimizer very slow. The QNG-SPSA uses the SPSA approximation of the QFI matrix (https://arxiv.org/pdf/2103.09232.pdf) resulting a much more efficient optimizer: it only takes 6 circuit evaluations per iteration to calculate the approximation of the QFI matrix (constant overhead $\mathcal{O}(1)$ ).

A new section in the docs has also been created for quantum information related topics, with a file explaining how to use the QNG and QNG-SPSA optimizers for an easy QCL problem. Since there was yet no files in the documentation, this PR also adds a docs/docsutils.py file with a fig_to_html() function to print images in the docs (same as in the Qadence repo) and a docs/environment.yml for the python environment (also same as in Qadence).

@gvelikova gvelikova self-requested a review April 2, 2024 12:34
@gvelikova gvelikova added the enhancement New feature or request label Apr 2, 2024
@RolandMacDoland
Copy link
Collaborator

Hey @inafergra thank you for this. Is this ready for review ? In case, you should un-draft it.

@inafergra inafergra marked this pull request as ready for review April 3, 2024 08:23
@inafergra
Copy link
Collaborator Author

Hey @inafergra thank you for this. Is this ready for review ? In case, you should un-draft it.

It is ready for review now

@gvelikova
Copy link
Collaborator

Thanks Ignacio! I think you should add the qinfo_tools imports to the init.py of the package in the same way as the constructors. I am testing it with some PDEs and will try to add a proper review soon.

@RolandMacDoland RolandMacDoland requested review from RolandMacDoland and removed request for dominikandreasseitz April 3, 2024 13:52
@RolandMacDoland
Copy link
Collaborator

Thanks @inafergra. Is this reviewable ? Another quick question: would it make sense to move SPSA to PyQ as another differentiation method ?

@dominikandreasseitz
Copy link
Collaborator

Thanks @inafergra. Is this reviewable ? Another quick question: would it make sense to move SPSA to PyQ as another differentiation method ?

@Roland-djee we can add spsa in pyq but the implementation in this MR is highly geared towards ignacios usecase, it for example works only for overlap models which do not exist atm in pyq

@inafergra
Copy link
Collaborator Author

@Roland-djee I still need to add some tests related to the indexing order of the vparams. I'll mark this PR as a draft until it's done. Regarding the SPSA approximation, I agree with Dominik, it can make sense to place it in PyQ in the future but that will require generalizing the module quite a bit.

@inafergra inafergra marked this pull request as draft May 10, 2024 11:07
@inafergra inafergra marked this pull request as ready for review May 27, 2024 17:30
@inafergra
Copy link
Collaborator Author

The PR is ready for review now. I have tried to implement the changes as discussed in the comments and in our meetings, mainly:

  • The QFI functions now take a dictionary with the variational parameters names and tensors, to avoid dependence on the ordering of the parameters (before they were passed as a Sequence).
  • However, inside the QNG optimizer the parameters still need to be passed as a Sequence (this is the expected behaviour of the torch.optimizer base class). I assemble a dictionary inside the QNG optimizer to pass to the QFI functions and I make sure the ordering is correct in the tests.
  • In the parameter update we now solve the least square problems instead of computing the inverse, as Joao pointed out this is way faster and more stable.
  • I added a custom type for the different QFI approximations
  • Added more tests, particularly for the symmetric matrix square root

Copy link
Collaborator

@RolandMacDoland RolandMacDoland left a comment

Choose a reason for hiding this comment

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

Thanks @inafergra. Looking good to me. Will approve as soon as comments are addressed.

docs/qinfo_tools/qng.md Outdated Show resolved Hide resolved
mkdocs.yml Outdated Show resolved Hide resolved
qadence_libs/qinfo_tools/qfi.py Show resolved Hide resolved
qadence_libs/qinfo_tools/qfi.py Outdated Show resolved Hide resolved
qadence_libs/qinfo_tools/qfi.py Outdated Show resolved Hide resolved
qadence_libs/qinfo_tools/qng.py Outdated Show resolved Hide resolved
qadence_libs/qinfo_tools/qng.py Outdated Show resolved Hide resolved
qadence_libs/qinfo_tools/qng.py Outdated Show resolved Hide resolved
qadence_libs/qinfo_tools/spsa.py Outdated Show resolved Hide resolved
tests/qinfo_tools/test_spsa.py Outdated Show resolved Hide resolved
@inafergra
Copy link
Collaborator Author

Thank you for the comments @Roland-djee, all addressed. Ready to merge

Copy link
Collaborator

@RolandMacDoland RolandMacDoland left a comment

Choose a reason for hiding this comment

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

Thanks @inafergra looking very nice.

docs/qinfo_tools/qng.md Outdated Show resolved Hide resolved
@inafergra
Copy link
Collaborator Author

Thanks @Roland-djee, will merge now!

@inafergra inafergra merged commit 8edbb41 into pasqal-io:main Jun 6, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants