Spaces:
Runtime error
Runtime error
Download src/models/llm_prompt_utils.py from QuantumGenAI/ErrorMitigation: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/models/llm_prompt_utils.py
- Command line
-
hf download hf://spaces/QuantumGenAI/ErrorMitigation/src/models/llm_prompt_utils.py
-
curl -L -o llm_prompt_utils.py https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/models/llm_prompt_utils.py
2.42 kB
| from typing import List | |
| from pathlib import Path | |
| import logging | |
| from jinja2 import Environment, FileSystemLoader | |
| from quantum_circuits.noisy_qasm_runner import NoisyQASMRunner | |
| from models.llm_interface import chat_completion | |
| from utils.qasm_utils import extract_qasm_from_llm_output | |
| logging.basicConfig( | |
| level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" | |
| ) | |
| logger = logging.getLogger(__name__) | |
| def create_prompt(data_dict, refine=False) -> str: | |
| """ | |
| Generates a QASM-related prompt using a Jinja2 template. | |
| Args: | |
| data_dict (dict): A dictionary containing the data to be rendered in the template. | |
| refine (bool, optional): If True, uses the "refine_qasm.j2" template | |
| instead of the default "qasm.j2". Defaults to False. | |
| Returns: | |
| str: The rendered template as a string. | |
| """ | |
| template_name = "qasm.j2" | |
| if refine: | |
| template_name = "refine_qasm.j2" | |
| env = Environment( | |
| loader=FileSystemLoader(Path(__file__).parent / "jinja_templates") | |
| ) | |
| template = env.get_template(template_name) | |
| rendered_template = template.render(data_dict) | |
| return rendered_template | |
| def generate_qasm_with_llm(circuit_data, is_refine=False, model="phi4") -> str: | |
| """ | |
| Generates a QASM circuit using a language model. | |
| Args: | |
| circuit_data (dict): The input data required for generating the QASM circuit. | |
| is_refine (bool, optional): If True, uses a refined QASM template. Defaults to False. | |
| model (str, optional): The name of the language model to use. Defaults to "phi4". | |
| Returns: | |
| str: The generated QASM circuit as a string. | |
| """ | |
| prompt = create_prompt(circuit_data, is_refine) | |
| llm_result = chat_completion(prompt, model_name=model) | |
| generated_qasm = extract_qasm_from_llm_output(llm_result) | |
| return generated_qasm | |
| def run_noisy_qasm(qasm_circuit, noise_data) -> List[float]: | |
| """ | |
| Simulates a noisy QASM circuit and returns expectation values. | |
| Args: | |
| qasm_circuit (str): The QASM representation of the quantum circuit. | |
| noise_data (list): The noise model data for simulation. | |
| Returns: | |
| List[float]: A list of expectation values obtained from the noisy simulation. | |
| """ | |
| runner = NoisyQASMRunner(qasm_circuit, noise_data) | |
| expectation_values = runner.run() | |
| return [float(val) for val in expectation_values] | |