Spaces:
Runtime error
Runtime error
File size: 1,427 Bytes
38fe687 f2e8c83 38fe687 a90ca10 38fe687 2b9363f 38fe687 f2e8c83 38fe687 f2e8c83 38fe687 f2e8c83 38fe687 f2e8c83 |
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 |
import gradio as gr
import matplotlib
import matplotlib.pyplot as plt
from quantum_perceptron import Perceptron
matplotlib.pyplot.switch_backend('Agg')
def run_perceptron(
num_qubits: int,
input_value: int,
weight_value: int,
num_iters: int
):
p = Perceptron(num_qubits, weight_value, input_value)
counts = p.measure_circuit(num_iters)
counts.setdefault('0', 0)
counts.setdefault('1', 0)
prob_1 = counts['1'] / num_iters
freq_hist = plt.figure()
plt.bar(counts.keys(), counts.values(), width=0.5)
for i, v in enumerate(list(counts.values())):
plt.text(i, v+10, v)
plt.xlabel('Measured State')
plt.ylabel('Frequency of Measured State')
freq_hist.subplots_adjust(top=0.2)
freq_hist.tight_layout()
return prob_1, freq_hist
app_inputs = [
gr.Slider(1, 9, value=2, step=1, label="Number of Qubits"),
gr.Number(value=12, label="Input Value", precision=0),
gr.Number(value=13, label="Weight Value", precision=0),
gr.Number(value=1000,
label="Number of Measurement Iterations",
precision=0),
]
app_outputs = [
gr.Number(precision=2, label="Probability of Firing Perceptron"),
gr.Plot(label="Distribution of Measurement Frequencies")
]
demo = gr.Interface(
fn=run_perceptron,
inputs=app_inputs,
outputs=app_outputs,
title="Simulate Quantum Perceptron",
)
demo.launch()
|