Spaces:
Runtime error
Runtime error
File size: 2,415 Bytes
8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 8f5c845 e0d7f28 0644cee e0d7f28 8f5c845 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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]
|