Skip to main content

Template for optimization use cases.

Project description

Luna UseCase Template

A Python SDK for defining optimization use cases in the Luna quantum optimization framework.

⚠️ Under Active Development This project is under active development and has not yet reached a stable release (< 1.0.0). The API may change at any time. Minor version bumps (e.g. 0.1.00.2.0) may include breaking changes.

Overview

luna-usecase-template provides a structured template for defining optimization problems with:

  • Data — Problem input data (UcData)
  • Formulation — How the problem maps to a mathematical model (UcFormulation)
  • Solution — Interpretation of solver output (UcSolution)
  • Instance — A concrete pairing of data + formulation (UcInstance)
  • Collection — Groups of instances (UcInstanceCollection)

All types support JSON serialization and polymorphic deserialization via a central Registry.

Requirements

  • Python >= 3.11.0

Quick Start

Define a use case

from typing import Literal, override

from luna_model import Model, Solution
from luna_usecase_template import Registry, UcData, UcFormulation, UcInstance, UcSolution


class MyData(UcData):
    name: Literal["my_data"] = "my_data"
    values: list[float]

    @override
    def to_string(self) -> str:
        return str(self.values)


class MySolution(UcSolution):
    name: Literal["my_solution"] = "my_solution"
    result: float

    @override
    def to_string(self) -> str:
        return str(self.result)


class MyFormulation(UcFormulation[MyData, MySolution]):
    name: Literal["my_formulation"] = "my_formulation"

    @override
    @staticmethod
    def to_string(data: MyData) -> str:
        return "minimize sum(values)"

    @override
    @staticmethod
    def formulate(data: MyData) -> Model:
        m = Model("MyProblem")
        # ... build your model ...
        return m

    @override
    @staticmethod
    def interpret(solution: Solution, data: MyData) -> MySolution:
        return MySolution(result=0.0)


class MyInstance(UcInstance):
    data: MyData
    formulation: MyFormulation

Register and serialize

# Register classes (also works as @Registry.add decorator)
Registry.add(MyData)
Registry.add(MySolution)
Registry.add(MyFormulation)
Registry.add(MyInstance)

# Create and serialize
data = MyData(data_name="example", values=[1.0, 2.0, 3.0])
instance = MyInstance(data=data, formulation=MyFormulation())
instance.store("instance.json")

# Deserialize with polymorphic type dispatch
loaded = MyInstance.load("instance.json")

Architecture

Every concrete UcData, UcFormulation, and UcSolution subclass must define a name field as a Literal["unique_string"]. This enables the Registry to distinguish types during deserialization.

Abstract base classes use Pydantic wrap validators to automatically dispatch validation to the correct registered subclass, so you can deserialize from a base type without knowing the concrete type ahead of time.

Development

uv sync                # Install dependencies
uv run ruff check . --fix     # Lint with auto-fix
uv run ruff format .          # Format code
uv build               # Build package

License

Apache-2.0

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

luna_usecase_template-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for luna_usecase_template-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b8e3f2aad6fd356ce51f2eb3a5621756e053b2eb4ddded85c4fd248f8349f85
MD5 2f34a872a56d2c6c535e437a037c0e16
BLAKE2b-256 61fb47b2da67c6449ad2ddaaab0148c462dd1ab7f095113df5920a38991205ca

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