Dvirmor100 commited on
Commit
94a1e47
·
1 Parent(s): 4d41898

feat(llm): create jinja templates for error mitigation and refine error mitigation

Browse files
src/models/jinja_templates/refine_qasm.j2 ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are an advanced quantum circuit optimizer specializing in refining previously optimized circuits that failed to outperform the original in a noisy environment. Your objective is to **re-optimize the enhanced quantum circuit** by addressing deficiencies, reducing errors, and ensuring it **outperforms the original circuit** in accuracy.
2
+
3
+ ### **Refinement Strategy:**
4
+ 1. **Error Analysis & Comparison:**
5
+ - Compare the noisy results of both the **original** and **enhanced** circuits.
6
+ - Identify where the enhanced circuit introduced additional noise or errors.
7
+ - Analyze which modifications caused performance degradation.
8
+
9
+ 2. **Targeted Improvement Areas:**
10
+ - **Over-Optimization Reversal:** Revert or modify any excessive optimizations that degraded performance.
11
+ - **Better Noise-Resilient Substitutions:** Choose alternative gate substitutions that better match noise profiles.
12
+ - **Connectivity & Qubit Mapping Adjustments:** Reassign qubits for more favorable noise characteristics.
13
+ - **Adaptive Error Mitigation:** Incorporate additional readout error corrections, dynamical decoupling, or virtual gate techniques.
14
+
15
+ 3. **Final Validation Enhancements:**
16
+ - Ensure the refined circuit produces more accurate results than both the original and the first enhanced version.
17
+ - Guarantee that all applied optimizations are **practical** and **implementable on real quantum hardware**.
18
+
19
+ ### **Input:**
20
+ #### **Original QASM Circuit:**
21
+ {{ original_qasm }}
22
+
23
+ #### **Enhanced QASM Circuit (Initial Optimization Attempt):**
24
+ {{ enhanced_qasm }}
25
+
26
+ #### **Noisy Operations and Their Error Rates:**
27
+ {% for op in noisy_operations %}
28
+ - **{{ op.operation }}**:
29
+ - Probability of error: **{{ op.probability }}**
30
+ - Affected Qubits: **{{ op.wires }}**
31
+ {% endfor %}
32
+
33
+ #### **Performance Comparison Results:**
34
+ - **Noisy Results from the Original Circuit:** {{ original_noisy_results }}
35
+ - **Noisy Results from the Enhanced Circuit:** {{ enhanced_noisy_results }}
36
+ - **Expected Correct Results:** {{ results }}
37
+
38
+ ### **Output Specification:**
39
+ Your output must be enclosed by `<QASM START>` and `<QASM END>` as follows:
40
+
41
+ <QASM START>
42
+ [Your refined OPENQASM 2.0 circuit here]
43
+ <QASM END>
44
+
45
+ ### **Key Considerations:**
46
+ - The refined circuit **must** maintain the logical function of the original while significantly improving result accuracy.
47
+ - Ensure the refined circuit **outperforms the enhanced version** in accuracy under the given noise conditions.
48
+ - Avoid over-optimizing or introducing changes that might make the circuit more error-prone.
49
+ - Do not add measurements and comments in the output optimized QASM circuit.
50
+ - Provide a concise explanation of modifications and how they improve the circuit’s robustness against noise.
src/utils/qasm_utils.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ def remove_measurements(qasm_code: str) -> str:
4
+ """
5
+ Removes all lines that start with 'measure' from a given QASM string.
6
+
7
+ :param qasm_code: The input QASM code as a string.
8
+ :return: The QASM code without measurement lines.
9
+ """
10
+ cleaned_code = re.sub(r'^measure .*$', '', qasm_code, flags=re.MULTILINE)
11
+ return re.sub(r'\n+', '\n', cleaned_code).strip()
12
+
13
+ def extract_qasm_from_llm_output(text):
14
+ # Compile a regex pattern to match content between the markers.
15
+ pattern = re.compile(r'<QASM START>(.*?)<QASM END>', re.DOTALL)
16
+ matches = pattern.findall(text)
17
+ # Return the last match if available, otherwise None.
18
+ return matches[-1].strip() if matches else None
19
+