Model hub

The model layer downloads, verifies, and loads the two fine-tuned quantum-specific BERT classifiers:

  • Task 1 — quantum relatedness (binary): is this SCCU quantum-specific?

  • Task 2 — quantum category (six classes): Mathematics, Algorithms, Circuit model, Hardware, Software framework, Others.

cc4q.models is a thin re-export facade. The canonical implementation (and the only module with the full helper set) is cc4q.models.hub.

Checkpoint layout

Checkpoints are stored under a single root (models_checkpoints/ in a source checkout, ~/.cache/cc4q/models_checkpoints for wheel installs):

models_checkpoints/
├── quantum_related/
│   ├── config.json
│   ├── model.safetensors
│   └── tokenizer_config.json
└── quantum_category/
    ├── config.json
    ├── model.safetensors
    └── tokenizer_config.json

Released assets are downloaded from GitHub Releases and checked against the SHA256 digests in CHECKPOINTS.

Model hub: checkpoint download/verification and lazy classifier loading.

Focuses on the two Quantum-Specific classifiers:
  • quantum_related (Binary: Quantum-Specific vs. Non-Quantum)

  • quantum_category (6-Class Quantum Knowledge Taxonomy)

Constant

Value / meaning

RELEASE

Release tag of the published checkpoints.

RELEASE_REPO

GitHub repository that hosts the release assets.

BASE_URL

Download prefix for release assets.

CHECKPOINTS

Mapping of archive name -> (axis_dir, sha256) for the two quantum classifiers.

BASE_MODEL

Optional bert-base-uncased archive used for from-scratch fine-tuning.

REQUIRED_FILES

Files that must exist for a checkpoint directory to be usable.

Device and checkpoint helpers

cc4q.models.hub.default_model_dir()[source]

Return the default checkpoint directory.

Resolution order: $CC4Q_MODEL_DIR > models_checkpoints/ next to a source checkout > ~/.cache/cc4q/models_checkpoints for wheel installs.

cc4q.models.hub.resolve_device(device=None)[source]

Resolve the torch device used for inference.

Resolution order: explicit device > $CC4Q_DEVICE > CUDA when available > CPU. MPS is never selected automatically because some torch builds hang on it; opt in with CC4Q_DEVICE=mps.

Parameters:

device – Optional device override, e.g. "cuda", "cpu", "mps", or "cuda:1".

Returns:

The normalized device string.

cc4q.models.hub.checkpoint_complete(model_dir, name)[source]

Return whether a checkpoint directory contains every required file.

Parameters:
  • model_dir – Directory containing the per-axis checkpoint directories.

  • name – Axis directory name, e.g. "quantum_related".

Returns:

True when config.json, model.safetensors, and tokenizer_config.json are all present.

cc4q.models.hub.missing_checkpoints(model_dir=None)[source]

List the released classifier axes whose checkpoints are missing.

Parameters:

model_dir – Optional checkpoint root; defaults to default_model_dir().

Returns:

Axis names ("quantum_related" / "quantum_category") that are not complete on disk. An empty list means both are ready.

cc4q.models.hub.ensure_checkpoints(model_dir=None, verbose=True, with_base=False)[source]

Download, SHA256-verify, and extract any missing checkpoint.

Parameters:
  • model_dir – Optional checkpoint root; defaults to default_model_dir().

  • verbose – Print download/extraction progress.

  • with_base – Also fetch the bert-base-uncased base model. Only needed for from-scratch fine-tuning, not for inference.

Returns:

A list of checkpoint names that are still missing after the attempt. An empty list means everything is ready.

Raises:

RuntimeError – If a release asset cannot be downloaded, its SHA256 does not match, or extraction does not produce a usable checkpoint.

Loading classifiers

class cc4q.models.hub.ModelHandle(models, device, model_dir)[source]

The fine-tuned quantum classifiers plus the device they live on.

A handle behaves like a small mapping over the two axes (quantum_related and quantum_category). Each value is a (tokenizer, model) tuple; both models are in eval mode.

device

Torch device string used by both classifiers.

model_dir

Checkpoint root the handle was loaded from.

Example

>>> models = load_models()
>>> tokenizer, model = models["quantum_related"]
>>> models.quantum_category[1].eval()
cc4q.models.hub.load_models(model_dir=None, device=None, auto_download=True)[source]

Load both quantum classifiers in eval mode.

Parameters:
  • model_dir – Optional checkpoint root; defaults to default_model_dir().

  • device – Optional torch device override; see resolve_device().

  • auto_download – Download missing checkpoints instead of raising.

Returns:

A ModelHandle with a (tokenizer, model) tuple for each axis.

Raises:

RuntimeError – If checkpoints are missing and auto_download is False, or the download cannot be completed.

cc4q.models.hub.load_models_from(axis_dirs, device=None, auto_download=True)[source]

Compose a handle from per-axis model directories.

Parameters:
  • axis_dirs – Mapping from axis name ("quantum_related" / "quantum_category") to a Hugging Face model directory. Missing or None axes fall back to the released checkpoint.

  • device – Optional torch device override.

  • auto_download – Download missing released checkpoints instead of raising.

Returns:

A ModelHandle combining the provided and released models.

Raises:

RuntimeError – If a required released checkpoint is missing and auto_download is False.

Example

from cc4q.models.hub import load_models, load_models_from

# released checkpoints (downloaded on first use)
models = load_models(device="cuda")

# mix a user-trained Task-2 classifier with the released Task-1 model
mixed = load_models_from(
    {"quantum_related": None,
     "quantum_category": "outputs/my_category_model"},
    device="cpu",
)