Skip to main content

Library to serialise objects arising from scientific and Machine Learning libraries, including NetKet.

Project description

nqxpack

NQXPack

A library to save and load objects coming from Scientific Machine Learning libraries, with a special attention to Neural Quantum States from NetKet.

Goals:

  • Simple format, possible to hand-edit and inspect manually;
  • Compatibility among Python version;
  • Allows to load Neural Networks with a single load command;

Usage

Install with

uv add git+https://github.com/NeuralQXLab/nqxpack.git

or (but seriously, stop using pip and start using uv)

pip install git+https://github.com/NeuralQXLab/nqxpack.git

With flax.linen

Save a dictionary containing the model and the parameters. Note that you cannot serialise jax arrays for the time-being but it could easily be added (I'd need to think about how to handle sharding...)

import nqxpack
import jax
from flax import linen as nn
import numpy as np

model = nn.Sequential((
    nn.Dense(features=2),
    nn.gelu,
    nn.Dense(features=1),
    jax.numpy.squeeze,
))

variables = model.init(jax.random.key(1), jax.numpy.ones((2,4)))
variables_np = jax.tree.map(np.asarray, variables)

# for the moment cannot serialise jax arrays.
# Could easily be implemented
nqxpack.save({'model':model, 'variables':jax.tree.map(np.asarray, variables)}, "mymodel.nk")

loaded_dict = nqxpack.load("mymodel.nk")
loaded_model, loaded_variables = loaded_dict['model'], loaded_dict['variables']

With flax.nnx (WIP, not working yet)

import nqxpack
import jax
from flax import nnx
import numpy as np

rngs = nnx.Rngs(0)
model = nnx.Sequential(
  nnx.Linear(1, 4, rngs=rngs),  # data
  nnx.Linear(4, 2, rngs=rngs),  # data
)

graphdef, variables = nnx.split(model)
variables_np = jax.tree.map(np.asarray, variables.to_pure_dict())

# for the moment cannot serialise jax arrays.
# Could easily be implemented
nqxpack.save({'graphdef':graphdef, 'variables':variables_np}, "mymodel.nk")

loaded_dict = nqxpack.load("mymodel.nk")
loaded_graphdef, loaded_variables = loaded_dict['model'], loaded_dict['variables']

loaded_model = nnx.merge(loaded_graphdef, loaded_variables)

With NetKet

import nqxpack
import netket as nk

hi = nk.hilbert.Spin(0.5, 10)
operator = nk.operator.spin.sigmax(nqs_state.hilbert, 1)

nqs_state = nk.vqs.MCState(nk.sampler.MetropolisLocal(hi), nk.models.RBM(alpha=4))
# print expectation value:
nqs_state.expect(operator)

nqxpack.save(nqs_state, "nqs_state.nk")
nqs_state_loaded = nqxpack.load("nqs_state.nk")

nqs_state_loaded.expect(operator)

The format

The format is a single zip file. You can decompress it yourself and look into it.

Feedback required

If you use this library, please let us know of any issue you might find.

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

nqxpack-0.1.13.tar.gz (1.6 MB view details)

Uploaded Source

Built Distribution

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

nqxpack-0.1.13-py3-none-any.whl (32.5 kB view details)

Uploaded Python 3

File details

Details for the file nqxpack-0.1.13.tar.gz.

File metadata

  • Download URL: nqxpack-0.1.13.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nqxpack-0.1.13.tar.gz
Algorithm Hash digest
SHA256 903d31210dd28be838e003efcd49ef818f4f4e55aee364bea3dd8a90fc3d445a
MD5 9d7ead6b181ad039be8aa4bb5009ddf0
BLAKE2b-256 ec66f311cadee7f1c0a5dfc85c9e12da13ccaf637f1db81bae13f41a2b8b415b

See more details on using hashes here.

Provenance

The following attestation bundles were made for nqxpack-0.1.13.tar.gz:

Publisher: release.yml on NeuralQXLab/nqxpack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nqxpack-0.1.13-py3-none-any.whl.

File metadata

  • Download URL: nqxpack-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 32.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nqxpack-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 5393de6b2beb33ed669ab13ae1dd0ee0b0663a00ca29c75d6d7ad8ab1e0c5605
MD5 0076b4d37566f915e8aa2cbdfede0ead
BLAKE2b-256 2bd337c6014bbeb6d31df4b1776a0e28c4e899708178e21fc4ec71d7c20deb9b

See more details on using hashes here.

Provenance

The following attestation bundles were made for nqxpack-0.1.13-py3-none-any.whl:

Publisher: release.yml on NeuralQXLab/nqxpack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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