CQCL / lambeq

A high-level Python library for Quantum Natural Language Processing
https://cqcl.github.io/lambeq/
Apache License 2.0
447 stars 107 forks source link

UnitaryHACK: Serialise ansatz and include in model #86

Closed nikhilkhatri closed 1 year ago

nikhilkhatri commented 1 year ago

Task Description

lambeq's training pipeline, shows that QNLP model training does not occur on string diagrams themselves, but on the concrete quantum circuits or tensor-networks, which are created from the string diagrams using ansätze. Akin to classical ML training, the Model and Trainer classes (and subclasses) use multiple hyperparameters to adjust the training procedure.

We would like to treat the choice of ansatz as a training hyperparameter.

Since the choice of ansätze plays a significant role in training, it is something we would like to be able to adjust from Model automatically. Currently, a change in ansatz requires converting the diagrams to circuits / tensor-networks using the new anstaz, and instantiating a new Model.

Furthermore, the models have a checkpoint system that allows restoring the model from a previous training run. If we then wanted to use a different ansatz, it may not be possible since the model only stores the circuits, and not the original diagrams.

Completion of this task involves the following changes:

  1. Rewrite the interface of the trainers so that it has an ansatz attribute.
  2. Wherever a (public) method takes circuit or tensor-network input, rewrite it to take in a string diagram input instead. Then, use the ansatz to transform these into circuits. Relevant Model methods include from_diagrams and get_diagram_output.
  3. Rewrite checkpoint saving and loading code to use string diagrams, and to save the ansatz. This will require writing a method to serialise and deserialise ansatze.

Bonus: instead of passing the ansatz as the parameter, rewrite the interface so that we can pass in the ansatz class and the arguments for initialising the ansatz.

Notes

Resources

ACE07-Sev commented 1 year ago

I would like to have a try at this. May I be assigned?

dimkart commented 1 year ago

@ACE07-Sev Hi, thanks for your interest! The way this hackathon works is that you have to open a PR that will be approved and merged, then the issue/task will be assigned to you and closed. If more than one coders submit PRs for the same issue, the maintainers will chose the solution they'll consider the best, or even decide to split the bounty. Hope that helps.

Vishwa-docs commented 1 year ago

Hello, has this issue been resolved or is it still open?

dimkart commented 1 year ago

@Vishwa-docs Hi, this is still open, feel free to try.

akve17 commented 1 year ago

Hi, what will a suitable environment to run this . Can I run the code on jupyter Notebook or I need some other installations for running the code. Please guide me.

nikhilkhatri commented 1 year ago

Hi @akve17 , I'm not sure what code in specific you're trying to run. The installation page describes how to set up lambeq. Beyond this, I'd recommend taking a look at the linked Resources in the issue description, particularly the training part of the documentation.

akve17 commented 1 year ago

Hi , I tried the installation but I am getting this error " Could not import Cython, which is required to build depccg extension modules. Please install cython and numpy prior to installing depccg." I have installed cython and numpy ,but still the error doesn't resolve. What could be the possible workaround for this?

dimkart commented 1 year ago

@akve17 Why are you installing depccg? By default, lambeq is using Bobcat which is installed with the main package. Not sure if this is any helpful, but what you need to do is the following:

  1. Fork lambeq repo
  2. Clone your copy of the lambeq repo into your computer
  3. Modify the code by doing what the task is asking
  4. Commit and push your changes into your private repo
  5. Open a PR in lambeq's repo.

Work with notebooks is not required at any stage of the modifications, to answer your first question.

akve17 commented 1 year ago

Hi @dimkart Thanks for replying. Is visual studio a correct platform to run and debug the code or do I need to use something else.

dimkart commented 1 year ago

@neiljdo Please reply to this comment so we can assign you to the issue.

neiljdo commented 1 year ago

@dimkart replying for issue assignment. The associated PR is #101. Thank you for the heads up!

dimkart commented 1 year ago

This is now considered complete, thanks to everyone for your work.