Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from qiskit import QuantumCircuit, transpile | |
| from qiskit_aer import Aer | |
| from qiskit.visualization import plot_histogram | |
| import matplotlib.pyplot as plt | |
| import ast | |
| def apply_cnot(initial_state_str): | |
| # Convert input string to actual Python list | |
| initial_state = ast.literal_eval(initial_state_str) | |
| # Create Quantum Circuit | |
| qc = QuantumCircuit(2, 2) | |
| qc.initialize(initial_state, [0, 1]) | |
| qc.cx(0, 1) | |
| qc.measure([0, 1], [0, 1]) | |
| # Simulation | |
| simulator = Aer.get_backend('qasm_simulator') | |
| transpiled_circuit = transpile(qc, simulator) | |
| result = simulator.run(transpiled_circuit, shots=1024).result() | |
| counts = result.get_counts() | |
| # Plot results | |
| fig, ax = plt.subplots() | |
| plot_histogram(counts, ax=ax) | |
| return fig | |
| # Gradio Interface | |
| interface = gr.Interface( | |
| fn=apply_cnot, | |
| inputs=gr.Radio(["[1,0,0,0]", "[0,1,0,0]", "[0,0,1,0]", "[0,0,0,1]"], label="Initial State"), | |
| outputs=gr.Plot(), | |
| title="Quantum CNOT Gate Simulation", | |
| description="Select an initial state to apply a CNOT gate in a quantum circuit and view the measurement results." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |