Spaces:
Runtime error
Runtime error
Commit ·
b925346
1
Parent(s): a5fcd60
feat(script): Created script directory with the relevant operation we enable in this project
Browse files
src/scripts/error_mitigation_eval.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
|
| 4 |
+
# Load the JSONL data
|
| 5 |
+
json_path = "output.jsonl"
|
| 6 |
+
|
| 7 |
+
original_distances = []
|
| 8 |
+
qwen_optimized_distances = []
|
| 9 |
+
phi_optimized_distances = []
|
| 10 |
+
|
| 11 |
+
better_count = 0
|
| 12 |
+
worse_count = 0
|
| 13 |
+
|
| 14 |
+
qwen_better_count = 0
|
| 15 |
+
phi_better_count = 0
|
| 16 |
+
|
| 17 |
+
qwen_worse_count = 0
|
| 18 |
+
phi_worse_count = 0
|
| 19 |
+
|
| 20 |
+
with open(json_path, "r", encoding="utf-8") as f:
|
| 21 |
+
json_list = [json.loads(line.strip()) for line in f]
|
| 22 |
+
|
| 23 |
+
for res in json_list:
|
| 24 |
+
if "error" in res:
|
| 25 |
+
continue
|
| 26 |
+
|
| 27 |
+
original = res["qwen"]["original_distance"] # Original distance
|
| 28 |
+
qwen_optimized = res["qwen"]["optimized_distance"]
|
| 29 |
+
phi_optimized = res["phi"]["optimized_distance"]
|
| 30 |
+
|
| 31 |
+
original_distances.append(original)
|
| 32 |
+
qwen_optimized_distances.append(qwen_optimized)
|
| 33 |
+
phi_optimized_distances.append(phi_optimized)
|
| 34 |
+
|
| 35 |
+
best_optimized = min(qwen_optimized, phi_optimized)
|
| 36 |
+
|
| 37 |
+
if best_optimized < original:
|
| 38 |
+
better_count += 1
|
| 39 |
+
elif best_optimized > original:
|
| 40 |
+
worse_count += 1
|
| 41 |
+
|
| 42 |
+
if qwen_optimized < original:
|
| 43 |
+
qwen_better_count += 1
|
| 44 |
+
elif qwen_optimized > original:
|
| 45 |
+
qwen_worse_count += 1
|
| 46 |
+
|
| 47 |
+
if phi_optimized < original:
|
| 48 |
+
phi_better_count += 1
|
| 49 |
+
elif phi_optimized > original:
|
| 50 |
+
phi_worse_count += 1
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# --- FUNCTIONS FOR PLOTTING ---
|
| 54 |
+
|
| 55 |
+
def plot_scatter_overall():
|
| 56 |
+
"""Scatter Plot: Overall Improvement vs Worse"""
|
| 57 |
+
plt.figure(figsize=(8, 6))
|
| 58 |
+
|
| 59 |
+
for orig, opt in zip(original_distances,
|
| 60 |
+
[min(q, p) for q, p in zip(qwen_optimized_distances, phi_optimized_distances)]):
|
| 61 |
+
color = "green" if opt < orig else "red"
|
| 62 |
+
plt.scatter(orig, opt, color=color, alpha=0.7)
|
| 63 |
+
|
| 64 |
+
# No improvement reference line
|
| 65 |
+
plt.plot([min(original_distances), max(original_distances)],
|
| 66 |
+
[min(original_distances), max(original_distances)], "k--", label="No Improvement Line (y = x)")
|
| 67 |
+
|
| 68 |
+
# Position text in the top-right with some spacing
|
| 69 |
+
max_x = max(original_distances)
|
| 70 |
+
max_y = max([q if q < p else p for (q, p) in zip(qwen_optimized_distances, phi_optimized_distances)])
|
| 71 |
+
print(max_x, max_y)
|
| 72 |
+
plt.text(max_x * 0.8, max_y * 0.9, f"↓ Better: {better_count}", color="green",
|
| 73 |
+
fontsize=12, fontweight="bold")
|
| 74 |
+
plt.text(max_x * 0.8, max_y * 0.8, f"↑ Worse: {worse_count}", color="red",
|
| 75 |
+
fontsize=12, fontweight="bold")
|
| 76 |
+
|
| 77 |
+
plt.xlabel("Original Distance")
|
| 78 |
+
plt.ylabel("Optimized Distance")
|
| 79 |
+
plt.title("Original vs Optimized Distances\n(Green Below = Better, Red Above = Worse)")
|
| 80 |
+
plt.legend()
|
| 81 |
+
plt.grid(True, linestyle="--", alpha=0.7)
|
| 82 |
+
plt.show()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def plot_bar_improvement():
|
| 86 |
+
"""Bar Chart: Overall Improvement Breakdown"""
|
| 87 |
+
plt.figure(figsize=(8, 6))
|
| 88 |
+
plt.bar(["Improved", "Worsened"], [better_count, worse_count], color=["green", "red"])
|
| 89 |
+
plt.ylabel("Count")
|
| 90 |
+
plt.title("Overall Improvement Breakdown")
|
| 91 |
+
plt.grid(axis="y", linestyle="--", alpha=0.7)
|
| 92 |
+
plt.show()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def plot_histogram(original, optimized, title, color):
|
| 96 |
+
"""Histogram: Original vs Optimized Distances with Original on Top"""
|
| 97 |
+
plt.figure(figsize=(8, 6))
|
| 98 |
+
plt.hist(optimized, bins=30, alpha=0.7, label="Optimized", color=color, edgecolor="black", stacked=True)
|
| 99 |
+
plt.hist(original, bins=30, alpha=0.4, label="Original", color="blue", edgecolor="black", stacked=True)
|
| 100 |
+
|
| 101 |
+
plt.xlabel("Distance")
|
| 102 |
+
plt.ylabel("Frequency")
|
| 103 |
+
plt.title(title)
|
| 104 |
+
plt.legend()
|
| 105 |
+
plt.grid(axis="y", linestyle="--", alpha=0.7)
|
| 106 |
+
plt.show()
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def plot_boxplot():
|
| 110 |
+
"""Box Plot: Distance Distributions"""
|
| 111 |
+
plt.figure(figsize=(8, 6))
|
| 112 |
+
plt.boxplot([original_distances, qwen_optimized_distances, phi_optimized_distances],
|
| 113 |
+
labels=["Original", "Qwen Optimized", "Phi Optimized"], patch_artist=True)
|
| 114 |
+
|
| 115 |
+
plt.ylabel("Distance")
|
| 116 |
+
plt.title("Distance Distributions (Original vs Optimized)")
|
| 117 |
+
plt.grid(axis="y", linestyle="--", alpha=0.7)
|
| 118 |
+
plt.show()
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def plot_scatter_single_model(optimized_distances, model_name, color):
|
| 122 |
+
"""Scatter Plot: Single Model Optimization"""
|
| 123 |
+
plt.figure(figsize=(8, 6))
|
| 124 |
+
|
| 125 |
+
for orig, opt in zip(original_distances, optimized_distances):
|
| 126 |
+
dot_color = "green" if opt < orig else "red"
|
| 127 |
+
plt.scatter(orig, opt, color=dot_color, alpha=0.7)
|
| 128 |
+
|
| 129 |
+
plt.plot([min(original_distances), max(original_distances)],
|
| 130 |
+
[min(original_distances), max(original_distances)], "k--", label="No Improvement Line (y = x)")
|
| 131 |
+
|
| 132 |
+
plt.xlabel("Original Distance")
|
| 133 |
+
plt.ylabel(f"{model_name} Optimized Distance")
|
| 134 |
+
plt.title(f"Original vs {model_name} Optimized Distances\n(Green Below = Better, Red Above = Worse)")
|
| 135 |
+
plt.legend()
|
| 136 |
+
plt.grid(True, linestyle="--", alpha=0.7)
|
| 137 |
+
plt.show()
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
# # --- RUN PLOTS ---
|
| 141 |
+
plot_scatter_overall()
|
| 142 |
+
# plot_bar_improvement()
|
| 143 |
+
# plot_histogram(original_distances, qwen_optimized_distances, "Qwen: Distribution of Distances (Original on Top)",
|
| 144 |
+
# "green")
|
| 145 |
+
# plot_histogram(original_distances, phi_optimized_distances, "Phi: Distribution of Distances (Original on Top)", "red")
|
| 146 |
+
# plot_boxplot()
|
| 147 |
+
# plot_scatter_single_model(qwen_optimized_distances, "Qwen", "green")
|
| 148 |
+
# plot_scatter_single_model(phi_optimized_distances, "Phi", "red")
|
src/scripts/generate_random_circuits.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from quantum_circuits import CircuitManager
|
| 2 |
+
from pennylane import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import pennylane as qml
|
| 5 |
+
from typing import Callable, Optional, Any
|
| 6 |
+
|
| 7 |
+
##############################################################################
|
| 8 |
+
# Static and Dynamic Circuit Definitions
|
| 9 |
+
##############################################################################
|
| 10 |
+
def circuit_1(params: Any, n_qubits: int = 3) -> None:
|
| 11 |
+
"""RX, RZ, RY rotations on 3 qubits."""
|
| 12 |
+
if n_qubits != 3:
|
| 13 |
+
raise ValueError("circuit_1 requires exactly 3 qubits")
|
| 14 |
+
qml.RX(params[0], wires=0)
|
| 15 |
+
qml.RZ(params[1], wires=1)
|
| 16 |
+
qml.RY(params[2], wires=2)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def circuit_2(params: Any, n_qubits: int = 2) -> None:
|
| 20 |
+
"""Bell state circuit for 2 qubits with an extra rotation for nontrivial output."""
|
| 21 |
+
if n_qubits != 2:
|
| 22 |
+
raise ValueError("circuit_2 requires exactly 2 qubits")
|
| 23 |
+
qml.Hadamard(wires=0)
|
| 24 |
+
qml.CNOT(wires=[0, 1])
|
| 25 |
+
qml.RZ(0.15, wires=0)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def circuit_3(params: Any, n_qubits: int = 2) -> None:
|
| 29 |
+
"""Entangled state with rotation on qubit 0 for 2 qubits."""
|
| 30 |
+
if n_qubits != 2:
|
| 31 |
+
raise ValueError("circuit_3 requires exactly 2 qubits")
|
| 32 |
+
qml.Hadamard(wires=0)
|
| 33 |
+
qml.CNOT(wires=[0, 1])
|
| 34 |
+
qml.RX(params[0] + 0.2, wires=0)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def circuit_4(params: Any, n_qubits: int = 1) -> None:
|
| 38 |
+
"""Arbitrary single-qubit rotations for 1 qubit."""
|
| 39 |
+
if n_qubits != 1:
|
| 40 |
+
raise ValueError("circuit_4 requires exactly 1 qubit")
|
| 41 |
+
qml.Rot(params[0], params[1], params[2], wires=0).decomposition()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def circuit_5(params: Any, n_qubits: int) -> None:
|
| 45 |
+
"""Generalized GHZ state for n_qubits (n_qubits>=2)."""
|
| 46 |
+
if n_qubits < 2:
|
| 47 |
+
raise ValueError("GHZ state requires at least 2 qubits")
|
| 48 |
+
qml.Hadamard(wires=0)
|
| 49 |
+
for i in range(1, n_qubits):
|
| 50 |
+
qml.CNOT(wires=[0, i])
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def circuit_6(params: Any, n_qubits: int = 3) -> None:
|
| 54 |
+
"""Variational Ansatz using BasicEntanglerLayers on 3 qubits."""
|
| 55 |
+
params = [np.random.rand(n_qubits, n_qubits)]
|
| 56 |
+
wires = list(range(n_qubits))
|
| 57 |
+
qml.templates.BasicEntanglerLayers(params, wires=wires).decomposition()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def circuit_7(params: Any, n_qubits: int = 3) -> None:
|
| 61 |
+
"""Quantum Fourier Transform (QFT) on 3 qubits."""
|
| 62 |
+
wires = list(range(n_qubits))
|
| 63 |
+
qml.QFT(wires=wires).decomposition()
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def circuit_8(params: Any, n_qubits: int = 2) -> None:
|
| 67 |
+
"""Amplitude Embedding on 2 qubits."""
|
| 68 |
+
wires = list(range(n_qubits))
|
| 69 |
+
qml.templates.AmplitudeEmbedding(params, wires=wires, normalize=True).decomposition()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def circuit_9(params: Any, n_qubits: int = 3) -> None:
|
| 73 |
+
"""Angle Embedding on 3 qubits."""
|
| 74 |
+
wires = list(range(n_qubits))
|
| 75 |
+
qml.templates.AngleEmbedding(params, wires=wires).decomposition()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def circuit_10(params: Any, n_qubits: int = 3) -> None:
|
| 79 |
+
"""Strongly Entangling Layers on 3 qubits."""
|
| 80 |
+
wires = list(range(n_qubits))
|
| 81 |
+
qml.templates.StronglyEntanglingLayers(params, wires=wires).decomposition()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def create_dynamic_temp_circuit(num_qubits: int) -> Callable:
|
| 85 |
+
"""
|
| 86 |
+
Returns a dynamic circuit function for a given number of qubits.
|
| 87 |
+
|
| 88 |
+
The circuit consists of four layers:
|
| 89 |
+
1. Parameterized single-qubit rotations (RX, RY, RZ) on each qubit.
|
| 90 |
+
2. An entangling layer: a ring of CNOT gates connecting neighboring qubits.
|
| 91 |
+
3. Additional single-qubit rotations for variability.
|
| 92 |
+
4. An entangling layer using CZ gates on alternating pairs.
|
| 93 |
+
|
| 94 |
+
Args:
|
| 95 |
+
num_qubits: Number of qubits for the circuit.
|
| 96 |
+
|
| 97 |
+
Returns:
|
| 98 |
+
A circuit function that accepts parameters and an optional n_qubits argument.
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
def dynamic_circuit(params: Any, n_qubits: Optional[int] = None) -> None:
|
| 102 |
+
n = num_qubits if n_qubits is None else n_qubits
|
| 103 |
+
|
| 104 |
+
# Layer 1: Parameterized rotations.
|
| 105 |
+
for i in range(n):
|
| 106 |
+
rx_angle = params[3 * i] if len(params) > 3 * i else random.uniform(0, 2 * np.pi)
|
| 107 |
+
ry_angle = params[3 * i + 1] if len(params) > 3 * i + 1 else random.uniform(0, 2 * np.pi)
|
| 108 |
+
rz_angle = params[3 * i + 2] if len(params) > 3 * i + 2 else random.uniform(0, 2 * np.pi)
|
| 109 |
+
qml.RX(rx_angle, wires=i)
|
| 110 |
+
qml.RY(ry_angle, wires=i)
|
| 111 |
+
qml.RZ(rz_angle, wires=i)
|
| 112 |
+
|
| 113 |
+
# Layer 2: Ring of CNOT gates.
|
| 114 |
+
for i in range(n):
|
| 115 |
+
if random.random() > 0.5:
|
| 116 |
+
qml.CNOT(wires=[i, (i + 1) % n])
|
| 117 |
+
|
| 118 |
+
# Layer 3: Additional single-qubit rotations.
|
| 119 |
+
for i in range(n):
|
| 120 |
+
qml.RZ(random.uniform(0, 2 * np.pi), wires=i)
|
| 121 |
+
qml.RX(random.uniform(0, 2 * np.pi), wires=i)
|
| 122 |
+
|
| 123 |
+
# Layer 4: CZ gates on alternating pairs.
|
| 124 |
+
if n >= 2:
|
| 125 |
+
for i in range(0, n - 1, 2):
|
| 126 |
+
qml.CZ(wires=[i, i + 1])
|
| 127 |
+
|
| 128 |
+
return dynamic_circuit
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
##############################################################################
|
| 132 |
+
# Build a Pool of Circuits
|
| 133 |
+
##############################################################################
|
| 134 |
+
# Static circuits (predefined with fixed qubit counts)
|
| 135 |
+
static_circuits = [
|
| 136 |
+
(circuit_1, [np.array([0.1, 0.2, 0.3])], "Circuit_1 (Static, 3 qubits)", [0, 1, 2]),
|
| 137 |
+
(circuit_2, [np.array([])], "Circuit_2 (Static, 2 qubits)", [0, 1]),
|
| 138 |
+
(circuit_3, [np.array([0.5])], "Circuit_3 (Static, 2 qubits)", [0, 1]),
|
| 139 |
+
(circuit_4, [np.random.rand(3)], "Circuit_4 (Static, 1 qubit)", [0]),
|
| 140 |
+
(circuit_8, [np.random.rand(2 ** 2)], "Circuit_8 (Static, Amplitude Embedding, 2 qubits)", [0, 1]),
|
| 141 |
+
(circuit_8, [np.random.rand(2 ** 3)], "Circuit_8 (Static, Amplitude Embedding, 3 qubits)", [0, 1, 2]),
|
| 142 |
+
(circuit_10, [np.random.rand(2, 3, 3)], "Circuit_10 (Static, Strongly Entangling, 3 qubits)", [0, 1, 2]),
|
| 143 |
+
(circuit_10, [np.random.rand(2, 4, 3)], "Circuit_10 (Static, Strongly Entangling, 4 qubits)", [0, 1, 2, 3]),
|
| 144 |
+
(circuit_10, [np.random.rand(2, 5, 3)], "Circuit_10 (Static, Strongly Entangling, 5 qubits)", [0, 1, 2, 3, 4]),
|
| 145 |
+
]
|
| 146 |
+
|
| 147 |
+
# Useful circuits that are applicable for dynamic qubit counts.
|
| 148 |
+
useful_circuits = [circuit_5, circuit_6, circuit_7, circuit_9]
|
| 149 |
+
|
| 150 |
+
# Build the complete circuits list.
|
| 151 |
+
circuits = []
|
| 152 |
+
circuits.extend(static_circuits)
|
| 153 |
+
|
| 154 |
+
# Add dynamic circuits and apply useful circuits for wire counts from 2 to 12.
|
| 155 |
+
for n in range(2, 13):
|
| 156 |
+
wires = list(range(n))
|
| 157 |
+
dynamic_circ = create_dynamic_temp_circuit(n)
|
| 158 |
+
dynamic_params = np.random.rand(n)
|
| 159 |
+
circuits.append((dynamic_circ, [dynamic_params], f"Dynamic_Circuit_{n}_qubits", wires))
|
| 160 |
+
|
| 161 |
+
# For each useful circuit, use the same parameter vector.
|
| 162 |
+
for circuit in useful_circuits:
|
| 163 |
+
circuits.append((circuit, [dynamic_params], circuit.__name__, wires))
|
| 164 |
+
|
| 165 |
+
def main():
|
| 166 |
+
device_name = "default.mixed"
|
| 167 |
+
use_qasm = True
|
| 168 |
+
|
| 169 |
+
manager = CircuitManager(
|
| 170 |
+
db_path="quantum_circuits.duckdb",
|
| 171 |
+
use_qasm=use_qasm,
|
| 172 |
+
device_name=device_name,
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
# Run each circuit with 10 noisy variations.
|
| 176 |
+
for circuit_func, params, name, wires in circuits:
|
| 177 |
+
manager.run_circuit(circuit_func, params, name, wires, num_variations=10)
|
src/scripts/run_error_mitigation.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import duckdb
|
| 2 |
+
from quantum_circuits.circuit_error_mitigator import process_circuit
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import json
|
| 5 |
+
def main():
|
| 6 |
+
# Connect to the database file
|
| 7 |
+
database_location = Path(__file__).parent.parent / "database" / "quantum_circuits.duckdb"
|
| 8 |
+
con = duckdb.connect(database_location)
|
| 9 |
+
|
| 10 |
+
# Read data from a table (replace 'your_table' with the actual table name)
|
| 11 |
+
df = con.execute("SELECT * FROM circuits LIMIT 1000").fetchdf()
|
| 12 |
+
|
| 13 |
+
for i, row in enumerate(df.itertuples()):
|
| 14 |
+
try:
|
| 15 |
+
if i == 83:
|
| 16 |
+
res_qwen = process_circuit(i, df, model="qwen2.5:14b")
|
| 17 |
+
res_phi = process_circuit(i, df, model="phi4")
|
| 18 |
+
result = {"qwen": res_qwen.model_dump(), "phi": res_phi.model_dump(), "db_index": i}
|
| 19 |
+
with open("output.jsonl", "a") as f:
|
| 20 |
+
f.write(json.dumps(result) + "\n")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
with open("output.jsonl", "a") as f:
|
| 23 |
+
f.write(json.dumps({"error": str(e), "db_index": i}) + "\n")
|
| 24 |
+
|
| 25 |
+
con.close()
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
main()
|