BoltzmannEntropy
commited on
Commit
·
b8bd9c0
1
Parent(s):
9a89425
Co
Browse files
app.py
CHANGED
@@ -10,6 +10,9 @@ import matplotlib.pyplot as plt
|
|
10 |
from PIL import Image
|
11 |
import pennylane as qml
|
12 |
|
|
|
|
|
|
|
13 |
# Hugging Face and DuckDB function placeholders
|
14 |
def store_in_hf_dataset(data):
|
15 |
# Implement storing data in the Hugging Face dataset
|
@@ -19,6 +22,13 @@ def load_from_hf_dataset():
|
|
19 |
# Implement loading data from the Hugging Face dataset
|
20 |
return []
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Store image in bytes for DuckDB
|
23 |
def pil_image_to_bytes(image):
|
24 |
img_byte_arr = io.BytesIO()
|
@@ -35,6 +45,47 @@ def generate_random_hamiltonian(num_qubits):
|
|
35 |
terms.append(term)
|
36 |
return " + ".join(terms)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Store data in DuckDB
|
39 |
def store_in_duckdb(data, db_file='quantum_hamiltonians.duckdb'):
|
40 |
conn = duckdb.connect(database=db_file)
|
@@ -91,9 +142,9 @@ def generate_hamiltonians(num_hamiltonians, selected_qubits, selected_order, wri
|
|
91 |
# Function to load results from either DuckDB or Hugging Face dataset
|
92 |
def load_results(load_from_hf, load_from_duckdb):
|
93 |
if load_from_hf:
|
94 |
-
return load_from_hf_dataset()
|
95 |
if load_from_duckdb:
|
96 |
-
return load_from_duckdb()
|
97 |
|
98 |
# Gradio app
|
99 |
with gr.Blocks() as app:
|
|
|
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 |
# Hugging Face and DuckDB function placeholders
|
17 |
def store_in_hf_dataset(data):
|
18 |
# Implement storing data in the Hugging Face dataset
|
|
|
22 |
# Implement loading data from the Hugging Face dataset
|
23 |
return []
|
24 |
|
25 |
+
# Function to buffer the plot and return as PIL image
|
26 |
+
def buffer_plot_and_get(fig):
|
27 |
+
buf = io.BytesIO()
|
28 |
+
fig.savefig(buf, format='png')
|
29 |
+
buf.seek(0)
|
30 |
+
return PIL.Image.open(buf)
|
31 |
+
|
32 |
# Store image in bytes for DuckDB
|
33 |
def pil_image_to_bytes(image):
|
34 |
img_byte_arr = io.BytesIO()
|
|
|
45 |
terms.append(term)
|
46 |
return " + ".join(terms)
|
47 |
|
48 |
+
# Function to convert Hamiltonian to QASM code
|
49 |
+
def hamiltonian_to_qasm(hamiltonian, num_qubits):
|
50 |
+
qasm_code = f"OPENQASM 2.0;\ninclude \"qelib1.inc\";\nqreg q[{num_qubits}];\n"
|
51 |
+
rotations = {i: 0.0 for i in range(num_qubits)}
|
52 |
+
terms = hamiltonian.split(" + ")
|
53 |
+
|
54 |
+
for term in terms:
|
55 |
+
coeff, paulis = term.split(" * ")
|
56 |
+
paulis = paulis.split()
|
57 |
+
coeff = float(coeff)
|
58 |
+
|
59 |
+
for i, pauli in enumerate(paulis):
|
60 |
+
if pauli == "X":
|
61 |
+
qasm_code += f"x q[{i}];\n"
|
62 |
+
elif pauli == "Y":
|
63 |
+
qasm_code += f"ry(pi/2) q[{i}];\n"
|
64 |
+
elif pauli == "Z":
|
65 |
+
rotations[i] += coeff
|
66 |
+
|
67 |
+
for i, angle in rotations.items():
|
68 |
+
if angle != 0:
|
69 |
+
angle_degrees = round(angle * 180 / math.pi, 2)
|
70 |
+
qasm_code += f"rz({angle_degrees}) q[{i}];\n"
|
71 |
+
|
72 |
+
return qasm_code
|
73 |
+
|
74 |
+
# Function for Trotter decomposition
|
75 |
+
def trotter_decomposition(hamiltonian, order):
|
76 |
+
terms = hamiltonian.split(" + ")
|
77 |
+
trotter_steps = []
|
78 |
+
|
79 |
+
for term in terms:
|
80 |
+
coeff, *pauli_ops = term.split(" * ")
|
81 |
+
coeff = float(coeff)
|
82 |
+
for _ in range(order):
|
83 |
+
trotter_steps.append(f"exp({coeff / order}) * ({' * '.join(pauli_ops)})")
|
84 |
+
for _ in range(order):
|
85 |
+
trotter_steps.append(f"exp({-coeff / order}) * ({' * '.join(pauli_ops)})")
|
86 |
+
|
87 |
+
return " + ".join(trotter_steps)
|
88 |
+
|
89 |
# Store data in DuckDB
|
90 |
def store_in_duckdb(data, db_file='quantum_hamiltonians.duckdb'):
|
91 |
conn = duckdb.connect(database=db_file)
|
|
|
142 |
# Function to load results from either DuckDB or Hugging Face dataset
|
143 |
def load_results(load_from_hf, load_from_duckdb):
|
144 |
if load_from_hf:
|
145 |
+
return load_from_hf_dataset()
|
146 |
if load_from_duckdb:
|
147 |
+
return load_from_duckdb()
|
148 |
|
149 |
# Gradio app
|
150 |
with gr.Blocks() as app:
|