Spaces:
Runtime error
Runtime error
BoltzmannEntropy
commited on
Commit
·
334036d
1
Parent(s):
4cf69d5
Co
Browse files
app.py
CHANGED
|
@@ -9,31 +9,35 @@ import PIL
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from PIL import Image
|
| 11 |
import pennylane as qml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Define a device
|
| 14 |
dev = qml.device('default.qubit', wires=10)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
| 19 |
def store_in_hf_dataset(data):
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
'id': [item[0] for item in data],
|
| 23 |
-
'hamiltonian': [item[2] for item in data],
|
| 24 |
-
'qasm_code': [item[3] for item in data],
|
| 25 |
-
'trotter_code': [item[4] for item in data],
|
| 26 |
-
'num_qubits': [item[5] for item in data],
|
| 27 |
-
'trotter_order': [item[6] for item in data],
|
| 28 |
-
'timestamp': [str(item[7]) for item in data],
|
| 29 |
-
})
|
| 30 |
-
# Push to Hugging Face dataset hub (replace with your dataset path)
|
| 31 |
-
dataset.push_to_hub("your-username/BoltzmannEntropy-QuantumLLMInstruct")
|
| 32 |
|
| 33 |
def load_from_hf_dataset():
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
return dataset
|
| 37 |
|
| 38 |
# Function to buffer the plot and return as PIL image
|
| 39 |
def buffer_plot_and_get(fig):
|
|
@@ -116,12 +120,58 @@ def store_in_duckdb(data, db_file='quantum_hamiltonians.duckdb'):
|
|
| 116 |
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", data)
|
| 117 |
conn.close()
|
| 118 |
|
| 119 |
-
# Load results from DuckDB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
def load_from_duckdb(db_file='quantum_hamiltonians.duckdb'):
|
| 121 |
conn = duckdb.connect(database=db_file)
|
| 122 |
df = conn.execute("SELECT * FROM hamiltonians").df()
|
| 123 |
conn.close()
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# Function to generate Hamiltonians
|
| 127 |
def generate_hamiltonians(num_hamiltonians, selected_qubits, selected_order, write_to_hf, write_to_duckdb):
|
|
@@ -153,11 +203,11 @@ def generate_hamiltonians(num_hamiltonians, selected_qubits, selected_order, wri
|
|
| 153 |
store_in_duckdb(results_table)
|
| 154 |
|
| 155 |
# Function to load results from either DuckDB or Hugging Face dataset
|
| 156 |
-
def load_results(
|
| 157 |
-
if
|
| 158 |
-
return load_from_hf_dataset()
|
| 159 |
-
if
|
| 160 |
-
return load_from_duckdb()
|
| 161 |
|
| 162 |
# Gradio app
|
| 163 |
with gr.Blocks() as app:
|
|
@@ -170,39 +220,26 @@ with gr.Blocks() as app:
|
|
| 170 |
order_choices = [1, 2, 3, 4, 5]
|
| 171 |
selected_order = gr.Dropdown(label="Select Trotter order", choices=order_choices, value=1)
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
value="Write to Hugging Face dataset")
|
| 177 |
|
| 178 |
generate_button = gr.Button("Generate Hamiltonians")
|
| 179 |
status = gr.Markdown("Click 'Generate Hamiltonians' to start the process.")
|
| 180 |
|
| 181 |
-
def update_status(num, qubits, order,
|
| 182 |
-
|
| 183 |
-
# Call function to write to Hugging Face dataset
|
| 184 |
-
generate_hamiltonians(num, qubits, order, write_to_hf=True, write_to_duckdb=False)
|
| 185 |
-
else:
|
| 186 |
-
# Call function to write to DuckDB
|
| 187 |
-
generate_hamiltonians(num, qubits, order, write_to_hf=False, write_to_duckdb=True)
|
| 188 |
return "Data stored as per selection."
|
| 189 |
|
| 190 |
-
generate_button.click(update_status, inputs=[num_hamiltonians, selected_qubits, selected_order,
|
| 191 |
|
| 192 |
with gr.Tab("View Results"):
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
value="Load from DuckDB")
|
| 196 |
|
| 197 |
load_button = gr.Button("Load Results")
|
| 198 |
output_display = gr.HTML()
|
| 199 |
|
| 200 |
-
|
| 201 |
-
if load_option == "Load from Hugging Face dataset":
|
| 202 |
-
return load_from_hf_dataset()
|
| 203 |
-
else:
|
| 204 |
-
return load_from_duckdb()
|
| 205 |
-
|
| 206 |
-
load_button.click(load_results, inputs=[load_option], outputs=output_display)
|
| 207 |
|
| 208 |
-
app.launch(
|
|
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
from PIL import Image
|
| 11 |
import pennylane as qml
|
| 12 |
+
import base64
|
| 13 |
+
|
| 14 |
+
from numpy import pi
|
| 15 |
+
import numpy as np
|
| 16 |
+
from qutip import *
|
| 17 |
+
from qutip.qip.operations import *
|
| 18 |
+
from qutip.qip.circuit import QubitCircuit, Gate
|
| 19 |
|
| 20 |
# Define a device
|
| 21 |
dev = qml.device('default.qubit', wires=10)
|
| 22 |
|
| 23 |
+
def plot_qutip_circuit():
|
| 24 |
+
q = QubitCircuit(2, reverse_states=False)
|
| 25 |
+
q.add_gate("CNOT", controls=[0], targets=[1])
|
| 26 |
+
|
| 27 |
+
# Display the circuit as an image
|
| 28 |
+
q.png # Generates and renders the circuit diagram
|
| 29 |
+
|
| 30 |
+
return q
|
| 31 |
|
| 32 |
+
|
| 33 |
+
# Hugging Face and DuckDB function placeholders
|
| 34 |
def store_in_hf_dataset(data):
|
| 35 |
+
# Implement storing data in the Hugging Face dataset
|
| 36 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def load_from_hf_dataset():
|
| 39 |
+
# Implement loading data from the Hugging Face dataset
|
| 40 |
+
return []
|
|
|
|
| 41 |
|
| 42 |
# Function to buffer the plot and return as PIL image
|
| 43 |
def buffer_plot_and_get(fig):
|
|
|
|
| 120 |
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", data)
|
| 121 |
conn.close()
|
| 122 |
|
| 123 |
+
# Load results from DuckDB and encode images to base64
|
| 124 |
+
def encode_image_from_blob(blob):
|
| 125 |
+
img_buffer = io.BytesIO(blob)
|
| 126 |
+
image = Image.open(img_buffer)
|
| 127 |
+
img_str = base64.b64encode(img_buffer.getvalue()).decode("utf-8")
|
| 128 |
+
return f'<img src="data:image/png;base64,{img_str}" style="max-width:500px;"/>'
|
| 129 |
+
|
| 130 |
def load_from_duckdb(db_file='quantum_hamiltonians.duckdb'):
|
| 131 |
conn = duckdb.connect(database=db_file)
|
| 132 |
df = conn.execute("SELECT * FROM hamiltonians").df()
|
| 133 |
conn.close()
|
| 134 |
+
|
| 135 |
+
# Convert results to HTML with images
|
| 136 |
+
html_content = []
|
| 137 |
+
for index, row in df.iterrows():
|
| 138 |
+
plot_blob = row['plot']
|
| 139 |
+
encoded_img = encode_image_from_blob(plot_blob)
|
| 140 |
+
|
| 141 |
+
html_content.append(f"""
|
| 142 |
+
<table style='width: 100%; border-collapse: collapse; margin: 10px;'>
|
| 143 |
+
<tr>
|
| 144 |
+
<td style='width: 30%; text-align: center;'>
|
| 145 |
+
<h3>Circuit {index + 1}</h3>
|
| 146 |
+
{encoded_img} <!-- Display the image -->
|
| 147 |
+
</td>
|
| 148 |
+
<td style='padding: 10px;'>
|
| 149 |
+
<table style='width: 100%; border-collapse: collapse;'>
|
| 150 |
+
<tr>
|
| 151 |
+
<td><strong>Hamiltonian:</strong></td><td>{row['hamiltonian']}</td>
|
| 152 |
+
</tr>
|
| 153 |
+
<tr>
|
| 154 |
+
<td><strong>QASM Representation:</strong></td><td>{row['qasm_code']}</td>
|
| 155 |
+
</tr>
|
| 156 |
+
<tr>
|
| 157 |
+
<td><strong>Trotter Decomposition:</strong></td><td>{row['trotter_code']}</td>
|
| 158 |
+
</tr>
|
| 159 |
+
<tr>
|
| 160 |
+
<td><strong>Number of Qubits:</strong></td><td>{row['num_qubits']}</td>
|
| 161 |
+
</tr>
|
| 162 |
+
<tr>
|
| 163 |
+
<td><strong>Trotter Order:</strong></td><td>{row['trotter_order']}</td>
|
| 164 |
+
</tr>
|
| 165 |
+
<tr>
|
| 166 |
+
<td><strong>Timestamp:</strong></td><td>{row['timestamp']}</td>
|
| 167 |
+
</tr>
|
| 168 |
+
</table>
|
| 169 |
+
</td>
|
| 170 |
+
</tr>
|
| 171 |
+
</table>
|
| 172 |
+
""")
|
| 173 |
+
|
| 174 |
+
return "".join(html_content)
|
| 175 |
|
| 176 |
# Function to generate Hamiltonians
|
| 177 |
def generate_hamiltonians(num_hamiltonians, selected_qubits, selected_order, write_to_hf, write_to_duckdb):
|
|
|
|
| 203 |
store_in_duckdb(results_table)
|
| 204 |
|
| 205 |
# Function to load results from either DuckDB or Hugging Face dataset
|
| 206 |
+
def load_results(load_from_hf, load_from_duckdb1):
|
| 207 |
+
if load_from_hf:
|
| 208 |
+
return load_from_hf_dataset()
|
| 209 |
+
if load_from_duckdb1:
|
| 210 |
+
return load_from_duckdb()
|
| 211 |
|
| 212 |
# Gradio app
|
| 213 |
with gr.Blocks() as app:
|
|
|
|
| 220 |
order_choices = [1, 2, 3, 4, 5]
|
| 221 |
selected_order = gr.Dropdown(label="Select Trotter order", choices=order_choices, value=1)
|
| 222 |
|
| 223 |
+
# Checkboxes for writing to HF dataset and DuckDB
|
| 224 |
+
write_to_hf = gr.Checkbox(label="Write to Hugging Face dataset", value=False)
|
| 225 |
+
write_to_duckdb = gr.Checkbox(label="Write to DuckDB", value=True)
|
|
|
|
| 226 |
|
| 227 |
generate_button = gr.Button("Generate Hamiltonians")
|
| 228 |
status = gr.Markdown("Click 'Generate Hamiltonians' to start the process.")
|
| 229 |
|
| 230 |
+
def update_status(num, qubits, order, write_hf, write_duckdb):
|
| 231 |
+
generate_hamiltonians(num, qubits, order, write_hf, write_duckdb)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
return "Data stored as per selection."
|
| 233 |
|
| 234 |
+
generate_button.click(update_status, inputs=[num_hamiltonians, selected_qubits, selected_order, write_to_hf, write_to_duckdb], outputs=status)
|
| 235 |
|
| 236 |
with gr.Tab("View Results"):
|
| 237 |
+
load_from_hf = gr.Checkbox(label="Load from Hugging Face dataset", value=False)
|
| 238 |
+
load_from_duckdb1 = gr.Checkbox(label="Load from DuckDB", value=True)
|
|
|
|
| 239 |
|
| 240 |
load_button = gr.Button("Load Results")
|
| 241 |
output_display = gr.HTML()
|
| 242 |
|
| 243 |
+
load_button.click(load_results, inputs=[load_from_hf, load_from_duckdb1], outputs=output_display)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
+
app.launch()
|