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 |
|---|---|
|
|
|
Probability that the unit is quantum-specific. |
|
One of the six category names, or |
|
Probability of the predicted category, or |
|
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:
texts – Comment/unit strings to classify.
models – A
cc4q.models.hub.ModelHandle.batch – Inference batch size.
- Returns:
One dict per text with these keys –
quantum_relatedTruewhen the unit is quantum-specific.quantum_confidenceProbability of the predicted binary class.
quantum_probProbability that the unit is quantum-specific.
quantum_categoryCategory name, or
Nonefor non-quantum units.category_confidenceProbability of the predicted category, or
None.category_probsMapping from category name to probability, or
None.
- cc4q.predict.predict_labels(texts, models, batch=32)[source]¶
Run the quantum classifiers over raw texts.
- Parameters:
texts – Comment/unit strings to classify.
models – A
cc4q.models.hub.ModelHandle.batch – Inference batch size.
- Returns:
A
(quantum_related, quantum_category)pair of lists, wherequantum_relatedcontains booleans andquantum_categorycontains category names orNonefor 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_textortextfield.models – Optional model handle; loaded lazily when
None.batch – Inference batch size.
auto_download – Download checkpoints when
modelsisNoneand the released checkpoints are missing.
- Returns:
The labeled records. Each record gains
quantum_related(0/1),quantum_prob,quantum_category, andcategory_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
modelsisNoneand the released checkpoints are missing.
- Returns:
The
(units, parse_error, fallback_used)tuple fromanalyze_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,unitsis empty anderroris a human-readable message; callers can fall back toraw_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
textorcomment_text).models – A
cc4q.models.hub.ModelHandle.batch – Inference batch size.
- Returns:
The same
unitslist, withquantum_related,quantum_confidence,quantum_prob,quantum_category,category_confidence, andcategory_probsset 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_errorisNoneon success;fallback_usedisTruewhen the input was processed line by line withraw_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 withcomment_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"])