Skip to main content

Streamlining quantum and classical machine learning model development.

Project description

QuLearn

CI codecov Last Commit GitHub issues MIT license PyPI Documentation Status

Welcome to QuLearn, a Python package designed to simplify the development and application of quantum and classical machine learning models. This project remained a hobby and is not actively developed anymore. It is very difficult to get people to build something they don't have to...

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.

Getting Started

Installation

pip install qulearn

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qulearn-0.8.2.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qulearn-0.8.2-py3-none-any.whl (34.4 kB view details)

Uploaded Python 3

File details

Details for the file qulearn-0.8.2.tar.gz.

File metadata

  • Download URL: qulearn-0.8.2.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.11.0-1015-azure

File hashes

Hashes for qulearn-0.8.2.tar.gz
Algorithm Hash digest
SHA256 e26fe7e2920e81c4a50a94e2ed066398e78f7315002aef1181436eb405d092d7
MD5 fe957f239b66ad6af86a76ba8a59b53f
BLAKE2b-256 0099cb9864577c74050c47c7faae88a11acbc3a29d8d3050076f71ad89f8dfba

See more details on using hashes here.

File details

Details for the file qulearn-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: qulearn-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 34.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.11.0-1015-azure

File hashes

Hashes for qulearn-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d4c7a47de7ad9b178978f7eda98ba4c460f4a03442527a6d27d5cc81bbbab6f5
MD5 06d385e801d232dca5615cd853e9b70d
BLAKE2b-256 3cf4978409f7c084b2ab3780dab405c3d757b1f3129aa81471d6bfef7b47c524

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page