Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from qiskit import QuantumCircuit, transpile, Aer
|
| 3 |
+
from qiskit.visualization import plot_histogram, plot_bloch_multivector
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
def apply_cnot_gradio(initial_state_str, visualize_statevector):
|
| 7 |
+
initial_states = {
|
| 8 |
+
"|00⟩": [1, 0, 0, 0],
|
| 9 |
+
"|01⟩": [0, 1, 0, 0],
|
| 10 |
+
"|10⟩": [0, 0, 1, 0],
|
| 11 |
+
"|11⟩": [0, 0, 0, 1]
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
initial_state = initial_states[initial_state_str]
|
| 15 |
+
|
| 16 |
+
qc = QuantumCircuit(2, 2)
|
| 17 |
+
qc.initialize(initial_state, [0, 1])
|
| 18 |
+
qc.cx(0, 1)
|
| 19 |
+
|
| 20 |
+
if visualize_statevector:
|
| 21 |
+
simulator = Aer.get_backend('statevector_simulator')
|
| 22 |
+
transpiled_circuit = transpile(qc, simulator)
|
| 23 |
+
result = simulator.run(transpiled_circuit).result()
|
| 24 |
+
statevector = result.get_statevector()
|
| 25 |
+
fig = plot_bloch_multivector(statevector, title=f"Statevector for {initial_state_str}")
|
| 26 |
+
else:
|
| 27 |
+
qc.measure([0, 1], [0, 1])
|
| 28 |
+
simulator = Aer.get_backend('qasm_simulator')
|
| 29 |
+
transpiled_circuit = transpile(qc, simulator)
|
| 30 |
+
result = simulator.run(transpiled_circuit, shots=1024).result()
|
| 31 |
+
counts = result.get_counts()
|
| 32 |
+
fig, ax = plt.subplots()
|
| 33 |
+
plot_histogram(counts, ax=ax, title=f"Measurement Counts for {initial_state_str}")
|
| 34 |
+
|
| 35 |
+
return fig
|
| 36 |
+
|
| 37 |
+
interface = gr.Interface(
|
| 38 |
+
fn=apply_cnot_gradio,
|
| 39 |
+
inputs=[
|
| 40 |
+
gr.Radio(["|00⟩", "|01⟩", "|10⟩", "|11⟩"], label="Initial State"),
|
| 41 |
+
gr.Checkbox(label="Visualize Statevector (Bloch Spheres)")
|
| 42 |
+
],
|
| 43 |
+
outputs=gr.Plot(),
|
| 44 |
+
title="Quantum CNOT Operation and Visualization",
|
| 45 |
+
description="Apply a CNOT gate on selected initial state and visualize results either as measurement counts or as statevector on Bloch spheres."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
interface.launch()
|