Instructions to use itsalissonsilva/QuantumCoder-9B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use itsalissonsilva/QuantumCoder-9B with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3.5-9B") model = PeftModel.from_pretrained(base_model, "itsalissonsilva/QuantumCoder-9B") - Notebooks
- Google Colab
- Kaggle
Preserve all-framework quantum adapter and paired baseline before final generation; no release claim
Browse files
curriculum_v2_run/adapter/README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
base_model: Qwen/Qwen3.5-9B
|
| 3 |
+
library_name: peft
|
| 4 |
+
pipeline_tag: text-generation
|
| 5 |
+
tags:
|
| 6 |
+
- base_model:adapter:Qwen/Qwen3.5-9B
|
| 7 |
+
- lora
|
| 8 |
+
- transformers
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Model Card for Model ID
|
| 12 |
+
|
| 13 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
## Model Details
|
| 18 |
+
|
| 19 |
+
### Model Description
|
| 20 |
+
|
| 21 |
+
<!-- Provide a longer summary of what this model is. -->
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
- **Developed by:** [More Information Needed]
|
| 26 |
+
- **Funded by [optional]:** [More Information Needed]
|
| 27 |
+
- **Shared by [optional]:** [More Information Needed]
|
| 28 |
+
- **Model type:** [More Information Needed]
|
| 29 |
+
- **Language(s) (NLP):** [More Information Needed]
|
| 30 |
+
- **License:** [More Information Needed]
|
| 31 |
+
- **Finetuned from model [optional]:** [More Information Needed]
|
| 32 |
+
|
| 33 |
+
### Model Sources [optional]
|
| 34 |
+
|
| 35 |
+
<!-- Provide the basic links for the model. -->
|
| 36 |
+
|
| 37 |
+
- **Repository:** [More Information Needed]
|
| 38 |
+
- **Paper [optional]:** [More Information Needed]
|
| 39 |
+
- **Demo [optional]:** [More Information Needed]
|
| 40 |
+
|
| 41 |
+
## Uses
|
| 42 |
+
|
| 43 |
+
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
| 44 |
+
|
| 45 |
+
### Direct Use
|
| 46 |
+
|
| 47 |
+
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 48 |
+
|
| 49 |
+
[More Information Needed]
|
| 50 |
+
|
| 51 |
+
### Downstream Use [optional]
|
| 52 |
+
|
| 53 |
+
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
| 54 |
+
|
| 55 |
+
[More Information Needed]
|
| 56 |
+
|
| 57 |
+
### Out-of-Scope Use
|
| 58 |
+
|
| 59 |
+
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
| 60 |
+
|
| 61 |
+
[More Information Needed]
|
| 62 |
+
|
| 63 |
+
## Bias, Risks, and Limitations
|
| 64 |
+
|
| 65 |
+
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
| 66 |
+
|
| 67 |
+
[More Information Needed]
|
| 68 |
+
|
| 69 |
+
### Recommendations
|
| 70 |
+
|
| 71 |
+
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
| 72 |
+
|
| 73 |
+
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
| 74 |
+
|
| 75 |
+
## How to Get Started with the Model
|
| 76 |
+
|
| 77 |
+
Use the code below to get started with the model.
|
| 78 |
+
|
| 79 |
+
[More Information Needed]
|
| 80 |
+
|
| 81 |
+
## Training Details
|
| 82 |
+
|
| 83 |
+
### Training Data
|
| 84 |
+
|
| 85 |
+
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
| 86 |
+
|
| 87 |
+
[More Information Needed]
|
| 88 |
+
|
| 89 |
+
### Training Procedure
|
| 90 |
+
|
| 91 |
+
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
| 92 |
+
|
| 93 |
+
#### Preprocessing [optional]
|
| 94 |
+
|
| 95 |
+
[More Information Needed]
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
#### Training Hyperparameters
|
| 99 |
+
|
| 100 |
+
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
| 101 |
+
|
| 102 |
+
#### Speeds, Sizes, Times [optional]
|
| 103 |
+
|
| 104 |
+
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
| 105 |
+
|
| 106 |
+
[More Information Needed]
|
| 107 |
+
|
| 108 |
+
## Evaluation
|
| 109 |
+
|
| 110 |
+
<!-- This section describes the evaluation protocols and provides the results. -->
|
| 111 |
+
|
| 112 |
+
### Testing Data, Factors & Metrics
|
| 113 |
+
|
| 114 |
+
#### Testing Data
|
| 115 |
+
|
| 116 |
+
<!-- This should link to a Dataset Card if possible. -->
|
| 117 |
+
|
| 118 |
+
[More Information Needed]
|
| 119 |
+
|
| 120 |
+
#### Factors
|
| 121 |
+
|
| 122 |
+
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
| 123 |
+
|
| 124 |
+
[More Information Needed]
|
| 125 |
+
|
| 126 |
+
#### Metrics
|
| 127 |
+
|
| 128 |
+
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
| 129 |
+
|
| 130 |
+
[More Information Needed]
|
| 131 |
+
|
| 132 |
+
### Results
|
| 133 |
+
|
| 134 |
+
[More Information Needed]
|
| 135 |
+
|
| 136 |
+
#### Summary
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
## Model Examination [optional]
|
| 141 |
+
|
| 142 |
+
<!-- Relevant interpretability work for the model goes here -->
|
| 143 |
+
|
| 144 |
+
[More Information Needed]
|
| 145 |
+
|
| 146 |
+
## Environmental Impact
|
| 147 |
+
|
| 148 |
+
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
| 149 |
+
|
| 150 |
+
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
| 151 |
+
|
| 152 |
+
- **Hardware Type:** [More Information Needed]
|
| 153 |
+
- **Hours used:** [More Information Needed]
|
| 154 |
+
- **Cloud Provider:** [More Information Needed]
|
| 155 |
+
- **Compute Region:** [More Information Needed]
|
| 156 |
+
- **Carbon Emitted:** [More Information Needed]
|
| 157 |
+
|
| 158 |
+
## Technical Specifications [optional]
|
| 159 |
+
|
| 160 |
+
### Model Architecture and Objective
|
| 161 |
+
|
| 162 |
+
[More Information Needed]
|
| 163 |
+
|
| 164 |
+
### Compute Infrastructure
|
| 165 |
+
|
| 166 |
+
[More Information Needed]
|
| 167 |
+
|
| 168 |
+
#### Hardware
|
| 169 |
+
|
| 170 |
+
[More Information Needed]
|
| 171 |
+
|
| 172 |
+
#### Software
|
| 173 |
+
|
| 174 |
+
[More Information Needed]
|
| 175 |
+
|
| 176 |
+
## Citation [optional]
|
| 177 |
+
|
| 178 |
+
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 179 |
+
|
| 180 |
+
**BibTeX:**
|
| 181 |
+
|
| 182 |
+
[More Information Needed]
|
| 183 |
+
|
| 184 |
+
**APA:**
|
| 185 |
+
|
| 186 |
+
[More Information Needed]
|
| 187 |
+
|
| 188 |
+
## Glossary [optional]
|
| 189 |
+
|
| 190 |
+
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 191 |
+
|
| 192 |
+
[More Information Needed]
|
| 193 |
+
|
| 194 |
+
## More Information [optional]
|
| 195 |
+
|
| 196 |
+
[More Information Needed]
|
| 197 |
+
|
| 198 |
+
## Model Card Authors [optional]
|
| 199 |
+
|
| 200 |
+
[More Information Needed]
|
| 201 |
+
|
| 202 |
+
## Model Card Contact
|
| 203 |
+
|
| 204 |
+
[More Information Needed]
|
| 205 |
+
### Framework versions
|
| 206 |
+
|
| 207 |
+
- PEFT 0.18.1
|
curriculum_v2_run/adapter/adapter_config.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"alora_invocation_tokens": null,
|
| 3 |
+
"alpha_pattern": {},
|
| 4 |
+
"arrow_config": null,
|
| 5 |
+
"auto_mapping": null,
|
| 6 |
+
"base_model_name_or_path": "Qwen/Qwen3.5-9B",
|
| 7 |
+
"bias": "none",
|
| 8 |
+
"corda_config": null,
|
| 9 |
+
"ensure_weight_tying": false,
|
| 10 |
+
"eva_config": null,
|
| 11 |
+
"exclude_modules": null,
|
| 12 |
+
"fan_in_fan_out": false,
|
| 13 |
+
"inference_mode": true,
|
| 14 |
+
"init_lora_weights": true,
|
| 15 |
+
"layer_replication": null,
|
| 16 |
+
"layers_pattern": null,
|
| 17 |
+
"layers_to_transform": null,
|
| 18 |
+
"loftq_config": {},
|
| 19 |
+
"lora_alpha": 32,
|
| 20 |
+
"lora_bias": false,
|
| 21 |
+
"lora_dropout": 0.0,
|
| 22 |
+
"megatron_config": null,
|
| 23 |
+
"megatron_core": "megatron.core",
|
| 24 |
+
"modules_to_save": null,
|
| 25 |
+
"peft_type": "LORA",
|
| 26 |
+
"peft_version": "0.18.1",
|
| 27 |
+
"qalora_group_size": 16,
|
| 28 |
+
"r": 16,
|
| 29 |
+
"rank_pattern": {},
|
| 30 |
+
"revision": null,
|
| 31 |
+
"target_modules": [
|
| 32 |
+
"in_proj_qkv",
|
| 33 |
+
"q_proj",
|
| 34 |
+
"v_proj"
|
| 35 |
+
],
|
| 36 |
+
"target_parameters": null,
|
| 37 |
+
"task_type": "CAUSAL_LM",
|
| 38 |
+
"trainable_token_indices": null,
|
| 39 |
+
"use_dora": false,
|
| 40 |
+
"use_qalora": false,
|
| 41 |
+
"use_rslora": false
|
| 42 |
+
}
|
curriculum_v2_run/adapter/adapter_model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0261aa5f75e7966692383136ec7d5ac7fc9ab9caaeb94483ceba5bcbda932823
|
| 3 |
+
size 27799552
|
curriculum_v2_run/generation_report.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
curriculum_v2_run/train_quantum_v2_pilot.py
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Fresh all-framework quantum-program SFT with separate circuit/size stress holdouts."""
|
| 2 |
+
|
| 3 |
+
import hashlib
|
| 4 |
+
import json
|
| 5 |
+
import random
|
| 6 |
+
import time
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import torch
|
| 9 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 10 |
+
from peft import LoraConfig, get_peft_model
|
| 11 |
+
from transformers import AutoTokenizer, Qwen3_5ForConditionalGeneration
|
| 12 |
+
|
| 13 |
+
REPO = "itsalissonsilva/QuantumCoder-9B"
|
| 14 |
+
BASE = "Qwen/Qwen3.5-9B"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def main():
|
| 18 |
+
start = time.monotonic()
|
| 19 |
+
torch.manual_seed(20261101)
|
| 20 |
+
torch.set_num_threads(4)
|
| 21 |
+
api = HfApi()
|
| 22 |
+
data_rev = api.model_info(REPO).sha
|
| 23 |
+
assert api.model_info(REPO).private
|
| 24 |
+
file = Path(hf_hub_download(REPO, "curriculum_v2/rows.json", revision=data_rev))
|
| 25 |
+
manifest = json.loads(
|
| 26 |
+
Path(hf_hub_download(REPO, "curriculum_v2/manifest.json", revision=data_rev)).read_text()
|
| 27 |
+
)
|
| 28 |
+
assert hashlib.sha256(file.read_bytes()).hexdigest() == manifest["rows_sha256"]
|
| 29 |
+
all_rows = json.loads(file.read_text())
|
| 30 |
+
rows = {
|
| 31 |
+
s: [r for r in all_rows if r["split"] == s]
|
| 32 |
+
for s in ["train", "validation", "test_iid", "test_stress"]
|
| 33 |
+
}
|
| 34 |
+
groups = {s: {r["group_id"] for r in rs} for s, rs in rows.items()}
|
| 35 |
+
assert all(
|
| 36 |
+
not groups[a] & groups[b] for i, a in enumerate(groups) for b in list(groups)[i + 1 :]
|
| 37 |
+
)
|
| 38 |
+
original = json.loads(
|
| 39 |
+
Path(
|
| 40 |
+
hf_hub_download(REPO, "hf_pilot/generation_report.json", revision=data_rev)
|
| 41 |
+
).read_text()
|
| 42 |
+
)
|
| 43 |
+
revision = original["base_revision"]
|
| 44 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE, revision=revision)
|
| 45 |
+
tokenizer.padding_side = "left"
|
| 46 |
+
model = Qwen3_5ForConditionalGeneration.from_pretrained(
|
| 47 |
+
BASE, revision=revision, dtype=torch.bfloat16, device_map="cuda", attn_implementation="sdpa"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
def messages(row):
|
| 51 |
+
return [
|
| 52 |
+
dict(
|
| 53 |
+
role="system",
|
| 54 |
+
content="Return only Python source code using the requested quantum framework. No prose or markdown.",
|
| 55 |
+
),
|
| 56 |
+
dict(
|
| 57 |
+
role="user",
|
| 58 |
+
content=f"Write a {row['framework']} program preparing this circuit from zero on {row['qubits']} qubits. Operations [gate,qubits,angle]: {row['operations_json']}. Angles are radians. Store the final statevector in a variable named state. Use little-endian qubit order.",
|
| 59 |
+
),
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
def prompt(row):
|
| 63 |
+
return tokenizer.apply_chat_template(
|
| 64 |
+
messages(row), tokenize=False, add_generation_prompt=True, enable_thinking=False
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
report = dict(
|
| 68 |
+
base_model=BASE,
|
| 69 |
+
base_revision=revision,
|
| 70 |
+
data_revision=data_rev,
|
| 71 |
+
manifest=manifest,
|
| 72 |
+
seed=20261101,
|
| 73 |
+
conditions={},
|
| 74 |
+
protocol="Distinct follow-up to the failed zero-shot-Cirq experiment. Fresh circuit groups, all three frameworks trained, RZ included; unseen angles and separate five/six-qubit longer/nonlocal stress. One fixed epoch from the base model, no original-test reuse or test checkpoint selection.",
|
| 75 |
+
training=dict(epochs=1, rank=16, alpha=32, learning_rate=2e-5, gradient_accumulation=8),
|
| 76 |
+
generation=dict(do_sample=False, max_new_tokens=768, batch_size=8, thinking=False),
|
| 77 |
+
)
|
| 78 |
+
eos = model.generation_config.eos_token_id
|
| 79 |
+
eos = set(eos if isinstance(eos, list) else [eos])
|
| 80 |
+
|
| 81 |
+
@torch.no_grad()
|
| 82 |
+
def evaluate(condition):
|
| 83 |
+
model.eval()
|
| 84 |
+
outputs = []
|
| 85 |
+
for split in ["validation", "test_iid", "test_stress"]:
|
| 86 |
+
for begin in range(0, len(rows[split]), 8):
|
| 87 |
+
batch = rows[split][begin : begin + 8]
|
| 88 |
+
inputs = tokenizer(
|
| 89 |
+
[prompt(r) for r in batch], padding=True, return_tensors="pt"
|
| 90 |
+
).to("cuda")
|
| 91 |
+
generated = model.generate(
|
| 92 |
+
**inputs,
|
| 93 |
+
max_new_tokens=768,
|
| 94 |
+
do_sample=False,
|
| 95 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 96 |
+
)
|
| 97 |
+
for row, tokens in zip(
|
| 98 |
+
batch, generated[:, inputs["input_ids"].shape[1] :], strict=True
|
| 99 |
+
):
|
| 100 |
+
ids = tokens.tolist()
|
| 101 |
+
stop = next((i for i, t in enumerate(ids) if t in eos), None)
|
| 102 |
+
used = ids if stop is None else ids[: stop + 1]
|
| 103 |
+
outputs.append(
|
| 104 |
+
dict(
|
| 105 |
+
id=row["id"],
|
| 106 |
+
group_id=row["group_id"],
|
| 107 |
+
split=split,
|
| 108 |
+
framework=row["framework"],
|
| 109 |
+
qubits=row["qubits"],
|
| 110 |
+
program=tokenizer.decode(used, skip_special_tokens=True),
|
| 111 |
+
generated_tokens=len(used),
|
| 112 |
+
eos_observed=stop is not None,
|
| 113 |
+
at_token_cap=stop is None and len(used) >= 768,
|
| 114 |
+
)
|
| 115 |
+
)
|
| 116 |
+
print(
|
| 117 |
+
json.dumps(
|
| 118 |
+
dict(stage="generated", condition=condition, split=split, n=len(rows[split]))
|
| 119 |
+
),
|
| 120 |
+
flush=True,
|
| 121 |
+
)
|
| 122 |
+
return outputs
|
| 123 |
+
|
| 124 |
+
report["conditions"]["base"] = evaluate("base")
|
| 125 |
+
model = get_peft_model(
|
| 126 |
+
model,
|
| 127 |
+
LoraConfig(
|
| 128 |
+
r=16,
|
| 129 |
+
lora_alpha=32,
|
| 130 |
+
lora_dropout=0.0,
|
| 131 |
+
target_modules=["q_proj", "v_proj", "in_proj_qkv"],
|
| 132 |
+
bias="none",
|
| 133 |
+
task_type="CAUSAL_LM",
|
| 134 |
+
),
|
| 135 |
+
)
|
| 136 |
+
model.gradient_checkpointing_enable()
|
| 137 |
+
model.enable_input_require_grads()
|
| 138 |
+
model.config.use_cache = False
|
| 139 |
+
optimizer = torch.optim.AdamW([p for p in model.parameters() if p.requires_grad], lr=2e-5)
|
| 140 |
+
train = list(rows["train"])
|
| 141 |
+
random.Random(20261101).shuffle(train)
|
| 142 |
+
optimizer.zero_grad()
|
| 143 |
+
losses = []
|
| 144 |
+
model.train()
|
| 145 |
+
for i, row in enumerate(train):
|
| 146 |
+
prefix = tokenizer(prompt(row), add_special_tokens=False)["input_ids"]
|
| 147 |
+
answer = tokenizer(row["program"] + tokenizer.eos_token, add_special_tokens=False)[
|
| 148 |
+
"input_ids"
|
| 149 |
+
]
|
| 150 |
+
assert len(prefix) + len(answer) <= 2048
|
| 151 |
+
ids = torch.tensor([prefix + answer], device="cuda")
|
| 152 |
+
labels = torch.tensor([[-100] * len(prefix) + answer], device="cuda")
|
| 153 |
+
loss = model(input_ids=ids, attention_mask=torch.ones_like(ids), labels=labels).loss
|
| 154 |
+
assert torch.isfinite(loss)
|
| 155 |
+
(loss / 8).backward()
|
| 156 |
+
losses.append(float(loss.detach()))
|
| 157 |
+
if (i + 1) % 8 == 0 or i + 1 == len(train):
|
| 158 |
+
torch.nn.utils.clip_grad_norm_([p for p in model.parameters() if p.requires_grad], 1)
|
| 159 |
+
optimizer.step()
|
| 160 |
+
optimizer.zero_grad()
|
| 161 |
+
if (i + 1) % 64 == 0:
|
| 162 |
+
print(
|
| 163 |
+
json.dumps(
|
| 164 |
+
dict(
|
| 165 |
+
stage="training",
|
| 166 |
+
examples=i + 1,
|
| 167 |
+
mean_last_loss=sum(losses[-64:]) / len(losses[-64:]),
|
| 168 |
+
)
|
| 169 |
+
),
|
| 170 |
+
flush=True,
|
| 171 |
+
)
|
| 172 |
+
folder = Path("quantum_v2_run")
|
| 173 |
+
folder.mkdir(exist_ok=True)
|
| 174 |
+
model.save_pretrained(folder / "adapter")
|
| 175 |
+
report["train_loss"] = losses
|
| 176 |
+
(folder / "generation_report.json").write_text(json.dumps(report, indent=2))
|
| 177 |
+
(folder / "train_quantum_v2_pilot.py").write_bytes(Path(__file__).read_bytes())
|
| 178 |
+
api.upload_folder(
|
| 179 |
+
repo_id=REPO,
|
| 180 |
+
folder_path=str(folder),
|
| 181 |
+
path_in_repo="curriculum_v2_run",
|
| 182 |
+
commit_message="Preserve all-framework quantum adapter and paired baseline before final generation; no release claim",
|
| 183 |
+
)
|
| 184 |
+
model.config.use_cache = True
|
| 185 |
+
model.gradient_checkpointing_disable()
|
| 186 |
+
report["conditions"]["adapted"] = evaluate("adapted")
|
| 187 |
+
report["elapsed_seconds"] = time.monotonic() - start
|
| 188 |
+
report["gate"] = (
|
| 189 |
+
"Independent reference-validated execution, held-out circuit/state fidelity and base comparison are required before publication. A perfect deterministic renderer exists for this structured input; no quantum algorithm-discovery claim."
|
| 190 |
+
)
|
| 191 |
+
(folder / "generation_report.json").write_text(json.dumps(report, indent=2))
|
| 192 |
+
api.upload_folder(
|
| 193 |
+
repo_id=REPO,
|
| 194 |
+
folder_path=str(folder),
|
| 195 |
+
path_in_repo="curriculum_v2_run",
|
| 196 |
+
commit_message="Freeze base/adapted quantum programs on fresh circuit and larger/nonlocal stress holdouts for independent execution",
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
if __name__ == "__main__":
|
| 201 |
+
main()
|