Skip to main content

PyTorch plugin.

Project description

This plugin provides an interface between D-Wave’s quantum computers and the PyTorch framework, including neural network modules for building and training Boltzmann Machines along with various sampler utility functions.

Example

Boltzmann Machines are probabilistic generative models for high-dimensional binary data. The following example walks through a typical workflow for fitting Boltzmann Machines via maximum likelihood.

Define a Graph-Restricted Boltzmann Machine with a square graph

import torch
from torch.optim import SGD

from dwave.plugins.torch.models import GraphRestrictedBoltzmannMachine as GRBM
from dwave.plugins.torch.samplers import BlockSampler

grbm = GRBM(nodes=["a", "b", "c", "d"], edges=[("a", "b"), ("b", "c"), ("c", "d"), ("d", "a")])
print("Linear weights:", grbm.linear)
print("Quadratic weights:", grbm.quadratic)

Instantiate a block-Gibbs sampler. Variables “a” and “c” are in block 0; variables “b” and “d” are in block 1. The sampler consists of three parallel Markov chains of length ten each. Each Markov chain samples at a constant unit inverse temperature.

sampler = BlockSampler(grbm=grbm, crayon=lambda v: v in {"b", "d"}, num_chains=3, schedule=[1]*10)

Create a batch of data and perform one likelihood-optimization step

x_data = torch.tensor([[1, -1, 1, -1], [-1, 1, 1, 1]], dtype=torch.float32)
optimizer = SGD(grbm.parameters(), lr=1)
x_model = sampler.sample()
grbm.quasi_objective(x_data, x_model).backward()
optimizer.step()
print("Updated quadratic weights:", grbm.quadratic)

To use a dimod sampler, replace the sampler = BlockSampler(...) line with

from dwave.plugins.torch.samplers import DimodSampler
from dwave.samplers import RandomSampler
sampler = DimodSampler(grbm=grbm, sampler=RandomSampler(),
                       prefactor=1, sample_kwargs=dict(num_reads=5))

License

Released under the Apache License 2.0. See LICENSE file.

Contributing

Ocean’s contributing guide has guidelines for contributing to Ocean packages.

Release Notes

dwave-pytorch-plugin uses reno to manage its release notes.

When making a contribution to dwave-pytorch-plugin that will affect users, create a new release note file by running

reno new your-short-descriptor-here

You can then edit the file created under releasenotes/notes/. Remove any sections not relevant to your changes. Commit the file along with your changes.

See reno’s user guide for details.

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

dwave_pytorch_plugin-0.3.0.tar.gz (52.2 kB view details)

Uploaded Source

Built Distribution

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

dwave_pytorch_plugin-0.3.0-py3-none-any.whl (73.9 kB view details)

Uploaded Python 3

File details

Details for the file dwave_pytorch_plugin-0.3.0.tar.gz.

File metadata

  • Download URL: dwave_pytorch_plugin-0.3.0.tar.gz
  • Upload date:
  • Size: 52.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for dwave_pytorch_plugin-0.3.0.tar.gz
Algorithm Hash digest
SHA256 6871f85d08b357b9b41613895d94ac443db5797034f670467de51b56d593a0d0
MD5 c6075152231d8aab89becdadda6492ed
BLAKE2b-256 9e3cc382974e4c8630b42ee7fef08b93d5c7591e4f37588040347f51dea7b9b3

See more details on using hashes here.

File details

Details for the file dwave_pytorch_plugin-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dwave_pytorch_plugin-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac9e85dc03b962956853db26d5e3cd97e362442802e2ac899f3c29b382c8f167
MD5 4f6db70d4e005ee329dfe65bef0d3c31
BLAKE2b-256 b48671289466e9f618dc9e847a621346c742d5e4f2f0e225bbad49361afbe2e7

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