Skip to main content

Template for optimization use cases.

Project description

Use Case Library

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

Overview

usecase-lib 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.13.0

Quick Start

Define a use case

from typing import Literal, override

from luna_model import Model, Solution
from usecase_lib 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
ruff check . --fix     # Lint with auto-fix
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.0.2a2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file luna_usecase_template-0.0.2a2-py3-none-any.whl.

File metadata

File hashes

Hashes for luna_usecase_template-0.0.2a2-py3-none-any.whl
Algorithm Hash digest
SHA256 4bd3c10b23bcd1880ecf9ad6403c69b88d5dd3ac47037b00a6511be6673af9c5
MD5 f590d71330db04ea93ba66f352312308
BLAKE2b-256 aa23a903c95d6a0c15ae3236ca91f39a04a2c40e08bf7b7e4fd2c1df2731bf13

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