Skip to main content

A torch addition package that adds parametrized quantum circuits to use in hybrid quantum-classical machine learning models.

Project description

torch-quantum

Purpose

This package serves as an addition to Pytorch that supplements it with layers based on variational quantum circuits allowing to create hybrid quantum-classical machine learning models.

Installation

pip install torch-quantum

Usage examples

Using models

from torch.utils.data import DataLoader
from torchqml.models import RotationalClassifier

train_loader = ... # load dataset

model = RotationalClassifier(n_qubits=4)

optimizer, criterion = ... # train entities

model.train()
for data, target in train_loader:
    optimizer.zero_grad()
    output = model(data)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()

Creating models

Model creation completely replicates Pytorch's approach.

import torch.nn as nn
from torchqml.layers import RotationLayer


class MyModel(nn.Module):
    def __init__(self, n_inputs, n_qubits, n_outputs):
        super().__init__()
        self.init_layer = nn.Linear(n_inputs, n_qubits)
        self.q_layer = RotationLayer(n_qubits)
        self.out_layer = nn.Linear(n_qubits, n_outputs)
    
    def forward(self, x):
        x = self.init_layer(x)
        x = self.q_layer(x)
        x = x.float()
        res = self.out_layer(x)
        return res

Creating layers

When creating a layer derived from QuantumLayer make sure to initialize weights and qnode attributes.

import torch
import torch.nn as nn
import pennylane as qml
from torchqml.layers import QuantumLayer


class MyQuantumLayer(QuantumLayer):
    def __init__(self, n_inputs, encoding='rx', device='default.qubit'):
        super().__init__(n_inputs, encoding, device)
        weight_shape = (self.n_qubits,)
        self.weights = nn.Parameter(torch.randn(weight_shape) * 0.1)
        self.qnode = self.make_qnode()

    def make_qnode(self):
        @qml.qnode(self.device, interface='torch', diff_method='backprop')
        def qnode(inputs):
            self.encode(inputs, self.n_qubits)
            for i in range(self.n_qubits):
                self.ansatz(self.weights[i], wires=[i])
            return [qml.expval(qml.PauliZ(wires=i)) for i in range(self.n_qubits)]
        return qnode

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

torch_pennylane_vqc-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

torch_pennylane_vqc-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file torch_pennylane_vqc-0.1.0.tar.gz.

File metadata

  • Download URL: torch_pennylane_vqc-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for torch_pennylane_vqc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 800ad0d4f21642faa1526a1134ddb6e6b612dd991c78b74e868e97060a1b7f8f
MD5 4a131b7b773f59c750813821ca0dc8bc
BLAKE2b-256 6c3bbd46a7e28a691ad23845b833af6c36d99c45557f0c1a2f0eb4785bc22f7a

See more details on using hashes here.

File details

Details for the file torch_pennylane_vqc-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_pennylane_vqc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8459ff4de8db17589e0d50f86d93a3c78ade20ba1f05bbe202950b32f40500f8
MD5 e9186458d4b87eaf8bf6c3183045c2ef
BLAKE2b-256 8633d726828138f06d95e7f670ccadc818b914111edff8294093167f5021ef20

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