Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from qiskit import QuantumCircuit, transpile, Aer
|
| 3 |
+
from qiskit.visualization import plot_histogram
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
def apply_cnot(initial_state):
|
| 7 |
+
qc = QuantumCircuit(2, 2)
|
| 8 |
+
qc.initialize(initial_state, [0, 1])
|
| 9 |
+
qc.cx(0, 1)
|
| 10 |
+
qc.measure([0, 1], [0, 1])
|
| 11 |
+
|
| 12 |
+
simulator = Aer.get_backend('qasm_simulator')
|
| 13 |
+
transpiled_circuit = transpile(qc, simulator)
|
| 14 |
+
result = simulator.run(transpiled_circuit, shots=1024).result()
|
| 15 |
+
counts = result.get_counts()
|
| 16 |
+
|
| 17 |
+
# Plotting results
|
| 18 |
+
fig, ax = plt.subplots()
|
| 19 |
+
plot_histogram(counts, ax=ax)
|
| 20 |
+
return fig
|
| 21 |
+
|
| 22 |
+
interface = gr.Interface(
|
| 23 |
+
fn=apply_cnot,
|
| 24 |
+
inputs=gr.Radio(["[1,0,0,0]", "[0,1,0,0]", "[0,0,1,0]", "[0,0,0,1]"], label="Initial State"),
|
| 25 |
+
outputs=gr.Plot(),
|
| 26 |
+
title="Quantum CNOT Gate Simulation",
|
| 27 |
+
description="Select an initial state to apply a CNOT gate in a quantum circuit and view the measurement results."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
interface.launch()
|