Inference

Prediction uses the two-stage taxonomy: Task 1 decides whether a unit is quantum-specific; Task 2 assigns one of six quantum-knowledge categories only to quantum units. Both stages return calibrated softmax probabilities so downstream sampling/annotation can prioritize uncertain units.

Output fields

predict_corpus() and predict_snippet() add these fields to each SCCU:

Field

Meaning

quantum_related

1/0 in corpus output, boolean in snippet/detailed output.

quantum_prob

Probability that the unit is quantum-specific.

quantum_category

One of the six category names, or None for non-quantum units.

category_confidence

Probability of the predicted category, or None.

category_probs

Full category distribution (detailed/snippet output only).

Category order

The order is exposed programmatically as CATEGORY_LIST (names) and CATEGORY_MAPPING (index -> name) in cc4q.predict.

Index

Category

0

Mathematics for quantum information

1

Quantum algorithms

2

Quantum circuit model

3

Quantum hardware

4

Quantum software framework

5

Others

Functions

cc4q.predict.predict_quantum_detailed(texts, models, batch=32)[source]

Run the two-stage quantum classifiers with confidence scores.

Task 1 (binary) runs on every text; Task 2 (six categories) runs only on texts classified as quantum-specific.

Parameters:
Returns:

One dict per text with these keys –

quantum_related

True when the unit is quantum-specific.

quantum_confidence

Probability of the predicted binary class.

quantum_prob

Probability that the unit is quantum-specific.

quantum_category

Category name, or None for non-quantum units.

category_confidence

Probability of the predicted category, or None.

category_probs

Mapping from category name to probability, or None.

cc4q.predict.predict_labels(texts, models, batch=32)[source]

Run the quantum classifiers over raw texts.

Parameters:
Returns:

A (quantum_related, quantum_category) pair of lists, where quantum_related contains booleans and quantum_category contains category names or None for non-quantum units.

cc4q.predict.predict_corpus(units_or_path, models=None, batch=32, auto_download=True)[source]

Label an SCCU corpus on both quantum axes.

Parameters:
  • units_or_path – A JSON file (list of records), a JSONL path, or an iterable of SCCU dictionaries. Records need a comment_text or text field.

  • models – Optional model handle; loaded lazily when None.

  • batch – Inference batch size.

  • auto_download – Download checkpoints when models is None and the released checkpoints are missing.

Returns:

The labeled records. Each record gains quantum_related (0/1), quantum_prob, quantum_category, and category_confidence. Records are labeled in place and the list is returned for convenience.

cc4q.predict.predict_snippet(code, models=None, auto_download=True)[source]

Analyze and label one code snippet.

Parameters:
  • code – Python source text.

  • models – Optional model handle; loaded lazily when None.

  • auto_download – Download checkpoints when models is None and the released checkpoints are missing.

Returns:

The (units, parse_error, fallback_used) tuple from analyze_snippet(), with each unit labeled.

Snippet pipeline

cc4q.predict.extract_units(source_code)[source]

Extract and segment a Python snippet into SCCU dictionaries.

Parameters:

source_code – Python source text.

Returns:

A (units, error) tuple. On parse failure, units is empty and error is a human-readable message; callers can fall back to raw_text_units().

cc4q.predict.classify_units(units, models, batch=32)[source]

Label SCCU dictionaries in place with detailed quantum predictions.

Parameters:
  • units – Mutable unit dictionaries (uses text or comment_text).

  • models – A cc4q.models.hub.ModelHandle.

  • batch – Inference batch size.

Returns:

The same units list, with quantum_related, quantum_confidence, quantum_prob, quantum_category, category_confidence, and category_probs set on each item.

cc4q.predict.analyze_snippet(source_code, models=None)[source]

Run extract -> segment -> classify on a code snippet.

Parameters:
  • source_code – Python source text (or arbitrary text for fallback).

  • models – Optional model handle. When None, units are extracted and segmented but not classified.

Returns:

A (units, parse_error, fallback_used) tuple. parse_error is None on success; fallback_used is True when the input was processed line by line with raw_text_units().

cc4q.predict.raw_text_units(raw_text)[source]

Build SCCU-like units for non-Python input, line by line.

This is the fallback used by analyze_snippet() when LibCST cannot parse the snippet: each non-empty line becomes one unit with comment_type="raw".

Parameters:

raw_text – Arbitrary text (source code, logs, prose, …).

Returns:

A list of unit dictionaries with the same keys as regular SCCUs.

cc4q.predict.compact_snippet(code_snippet, comment_text, max_lines=32, max_chars=1200)[source]

Extract a concise, readable window of code around a comment.

Used by the Workbench API and the demo-data build so long function bodies do not overwhelm the UI/JSON payloads.

Parameters:
  • code_snippet – Full enclosing code context.

  • comment_text – Comment text used to locate the interesting window.

  • max_lines – Maximum number of lines to keep.

  • max_chars – Maximum number of characters to keep.

Returns:

The trimmed snippet (with # ... [truncated] ... markers when content was removed), or an empty string for empty input.

Examples

Corpus labeling:

from cc4q.predict import predict_corpus
from cc4q.models.hub import load_models

models = load_models()
labeled = predict_corpus("out/sccus_segmented.json", models=models)
quantum = [r for r in labeled if r["quantum_related"]]

Snippet inspection (also handles non-Python input through the line-by-line fallback):

from cc4q.predict import predict_snippet

units, parse_error, fallback = predict_snippet(code, models=models)
for unit in units:
    print(unit["comment_text"], unit["quantum_related"],
          unit["quantum_category"])