Spaces:
Sleeping
Sleeping
File size: 1,196 Bytes
cb1794e 56e6fc1 cb1794e c5b8c8c cb1794e c5b8c8c cb1794e c5b8c8c cb1794e c5b8c8c cb1794e c5b8c8c cb1794e | 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 | 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()
|