Coder Social home page Coder Social logo

mazenali / qulearn Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 9.1 MB

A Python package streamlining development and application of quantum and classical machine learning models. Includes a collection of QML applications from Fraunhofer ITWM.

License: MIT License

Makefile 0.02% Batchfile 0.03% Python 5.52% Jupyter Notebook 94.43%

qulearn's Introduction

QuLearn

Welcome to QuLearn, a Python package designed to simplify the development and application of quantum and classical machine learning models. It includes a collection of QML applications from Fraunhofer ITWM.

About

QuLearn is built on top of PyTorch and PennyLane, two well-established libraries in the realms of machine learning and quantum computing. Our goal is to streamline the process of setting up, training, and testing machine learning models, be they classical, quantum, or a mix of both.

QuLearn is suitable for various research applications and aims to democratize access to the exciting field of quantum machine learning. It serves as a platform for researchers, developers, and enthusiasts to implement, experiment, and contribute to this rapidly evolving field.

QuLearn also houses QML applications from Fraunhofer ITWM.

Getting Started

Installation

(Installation instructions will be added soon.)

Basic Usage

Creating Models

The idea is to create classical, quantum or hybrid models with a simple syntax similar to PyTorch. In PyTorch, models are created by defining layers and feeding the input through the layers (forward). Similarly to classical models, we can build a quantum model using layers. Unlike classical models, not every quantum layer will return classical output. Thus, we distinguish between two types of quantum layers: circuit layers and measurement layers. The former can be applied to input, but returns no output. It only transforms the quantum state. The latter can be applied to a circuit layer (or empty circuuit = zero state) and produces classical output, much like a classical model.

Create a data embedding layer, a (trainable) variational layer and add a measurement layer on top.

import pennylane as qml
from qulearn.layer import IQPEmbeddingLayer, RYCZLayer, MeasurementLayer, MeasurementType

# parameters
num_wires = 3
num_reup = 2
num_layers = 3
observable = qml.PauliZ(0)

# model
upload_layer = IQPEmbeddingLayer(num_wires, num_reup)
var_layer = RYCZLayer(num_wires, num_layers)
model = MeasurementLayer(upload_layer, var_layer, measurement_type=MeasurementType.Expectation, observable=observable)

model is a subclass of a PyTorch model and behaves the same. We can use it for predictions:

y = model(X)

or, we can train this model

from torch.optim import Adam
from qulearn.trainer import RegressionTrainer

opt = Adam(model.parameters(), lr=0.01, amsgrad=True)
loss_fn = torch.nn.MSELoss()
trainer = RegressionTrainer(opt, loss_fn, num_epochs=100)
trainer.train(model, loader_train, loader_valid)

We can add more layers to the model

model = MeasurementLayer(upload_layer, var_layer, var_layer, upload_layer, var_layer, measurement_type=MeasurementType.Expectation, observable=observable)

We can add parametrized observables to the (quantum) model:

from qulearn.qlayer import HamiltonianLayer

observables = [qml.PauliZ(0), qml.PauliX(1)]
model = HamiltonianLayer(upload_layer, var_layer, observables=observables)

We can create our own circuit layer and a hybrid quantum-classical model:

from qulearn.qlayer import CircuitLayer

class MyQuantumCircuit(CircuitLayer):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        # set my attributes here


    def circuit(self, x) -> None:
        qml.Hadamard(wires=0)
        qml.RY(0.5, wires=1)

        # define your circuit...


class MyHybridModel(torch.nn.Module):

    def __init__(self):
        super().__init__()

        # first a classical linear layer
        self.ll_in = nn.Linear(3, 3)

        # followed by a quantum layer
        circuit = MyQuantumCircuit()
        observables = [qml.PauliZ(0), qml.PauliX(1)]
        self.qnn = HamiltonianLayer(circuit, observables=observables)

        # concluding with another classical linear layer
        self.ll_out = nn.Linear(1, 1)

    def forward(self, x):

        y = self.ll_in(x)
        y = self.qnn(y)
        y = self.ll_out(y)

        return y

Contributing

We greatly appreciate contributions to the QuLearn project! If you're a newcomer, please take a look at our Contribution Guide, which provides a detailed guide to get you started. You can contribute in many ways, including but not limited to, reporting bugs, suggesting new features, improving documentation, or writing code patches.

Please remember to follow our Code of Conduct, and ensure all your commits follow the Semantic Versioning format.

For feature additions or bug fixes, please create a new branch and submit a merge request. For main branch protection, at least one other developer is required to review your commit before merging.

License

This project is licensed under the terms of the MIT License.

Contact

qulearn's People

Contributors

mazenali avatar

Stargazers

Pingzai Wang avatar

Watchers

 avatar  avatar

Forkers

pingzaiwang

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.