"""Generate bounded circuit-to-Python candidates. This module never executes them.""" import json import math import torch from peft import PeftModel from transformers import AutoTokenizer,Qwen3_5ForConditionalGeneration BASE="Qwen/Qwen3.5-9B" BASE_REVISION="c202236235762e1c871ad0ccb60c8ee5ba337b9a" ADAPTER="itsalissonsilva/QuantumCoder-9B" def circuit_prompt(framework,qubits,operations): if framework not in {"qiskit","pennylane","cirq"}: raise ValueError("framework must be qiskit, pennylane or cirq") if type(qubits) is not int or not 2<=qubits<=6: raise ValueError("The measured scope is two through six qubits") if not isinstance(operations,(list,tuple)) or not 1<=len(operations)<=24: raise ValueError("Provide one through24 operations") normalized=[] for operation in operations: if not isinstance(operation,(list,tuple)) or len(operation)!=3: raise ValueError("Each operation must be [gate,qubit_indices,angle]") gate,indices,angle=operation if gate not in {"h","x","z","ry","rz","cx"}: raise ValueError("Supported gates: h, x, z, ry, rz, cx") if not isinstance(indices,(list,tuple)) or len(indices)!=(2 if gate=="cx" else 1): raise ValueError("Gate arity mismatch") if any(type(k) is not int or not 0<=k2048: raise ValueError("Prompt exceeds the measured2048-token bound; no truncation applied") prompts.append(text) results=[] eos=self.model.generation_config.eos_token_id eos=set(eos if isinstance(eos,list) else [eos]) for begin in range(0,len(prompts),8): inputs=self.tokenizer(prompts[begin:begin+8],padding=True,return_tensors="pt").to("cuda") generated=self.model.generate(**inputs,max_new_tokens=768,do_sample=False, pad_token_id=self.tokenizer.pad_token_id) for tokens in generated[:,inputs["input_ids"].shape[1]:]: ids=tokens.tolist();stop=next((i for i,t in enumerate(ids) if t in eos),None) used=ids if stop is None else ids[:stop+1] results.append(dict(program=self.tokenizer.decode(used,skip_special_tokens=True),generated_tokens=len(used), eos_observed=stop is not None,at_token_cap=stop is None and len(used)>=768)) return results