stevafernandes commited on
Commit
185345b
·
verified ·
1 Parent(s): 2c44442

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from qiskit import QuantumCircuit, transpile
3
+ from qiskit_aer import Aer
4
+ from qiskit.visualization import plot_histogram
5
+ import matplotlib.pyplot as plt
6
+
7
+ def run_quantum_circuit(apply_x_gate: bool):
8
+ # Setup Quantum Circuit
9
+ qc = QuantumCircuit(2)
10
+
11
+ if apply_x_gate:
12
+ qc.x(0) # Apply X gate if checkbox is checked
13
+
14
+ qc.h(0)
15
+ qc.cx(0, 1)
16
+ qc.measure_all()
17
+
18
+ # Simulation
19
+ simulator = Aer.get_backend('qasm_simulator')
20
+ transpiled_circuit = transpile(qc, simulator)
21
+ result = simulator.run(transpiled_circuit, shots=1024).result()
22
+
23
+ # Get results and plot histogram
24
+ counts = result.get_counts(qc)
25
+ fig, ax = plt.subplots()
26
+ plot_histogram(counts, ax=ax)
27
+
28
+ return fig
29
+
30
+ # Gradio Interface
31
+ interface = gr.Interface(
32
+ fn=run_quantum_circuit,
33
+ inputs=gr.Checkbox(label="Apply X Gate to Qubit 0 (Set to |1⟩)"),
34
+ outputs=gr.Plot(),
35
+ title="Quantum Circuit Simulator",
36
+ description="Simulate a simple quantum circuit. Optionally apply an X gate before the Hadamard and CNOT gates."
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ interface.launch()