ashutosh1919 commited on
Commit
38fe687
1 Parent(s): 91d3e9c

Adding Gradio App

Browse files
Files changed (3) hide show
  1. PerceptronSimulator.ipynb +0 -0
  2. gradio_app.py +43 -0
  3. requirements.txt +2 -1
PerceptronSimulator.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
gradio_app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib
3
+ import matplotlib.pyplot as plt
4
+ from quantum_perceptron import Perceptron
5
+
6
+ matplotlib.pyplot.switch_backend('Agg')
7
+
8
+ def run_perceptron(
9
+ num_qubits: int,
10
+ input_value: int,
11
+ weight_value: int,
12
+ num_iters: int
13
+ ):
14
+ p = Perceptron(num_qubits, weight_value, input_value)
15
+ counts = p.measure_circuit(num_iters)
16
+ prob_1 = counts.get('1', 0) / num_iters
17
+ freq_hist = plt.figure()
18
+ plt.bar(counts.keys(), counts.values(), width=0.5)
19
+ for i, v in enumerate(list(counts.values())):
20
+ plt.text(i, v+10, v)
21
+ plt.xlabel('Measured State')
22
+ plt.ylabel('Frequency of Measured State')
23
+ plt.tight_layout()
24
+ return prob_1, freq_hist
25
+
26
+ app_inputs = [
27
+ gr.Slider(1, 9, value=2, step=1, label="Number of Qubits"),
28
+ gr.Number(value=12, label="Input Value", precision=0),
29
+ gr.Number(value=13, label="Weight Value", precision=0),
30
+ gr.Number(value=1000, label="Number of Measurement Iterations", precision=0),
31
+ ]
32
+
33
+ app_outputs = [
34
+ gr.Number(precision=2, label="Probability of Firing Perceptron"),
35
+ gr.Plot(label="Distribution of Measurement Frequencies")
36
+ ]
37
+
38
+ demo = gr.Interface(
39
+ fn=run_perceptron,
40
+ inputs=app_inputs,
41
+ outputs=app_outputs,
42
+ )
43
+ demo.launch(share=True)
requirements.txt CHANGED
@@ -2,4 +2,5 @@ numpy
2
  qiskit
3
  pycodestyle
4
  pytest
5
- mypy
 
 
2
  qiskit
3
  pycodestyle
4
  pytest
5
+ mypy
6
+ gradio