|
import streamlit as st |
|
from qiskit import QuantumCircuit, transpile |
|
from qiskit_aer import AerSimulator |
|
import io |
|
import sys |
|
|
|
|
|
st.title("Quantum Circuit Emulator") |
|
st.write("Paste your Qiskit code below and press 'Run' to execute.") |
|
|
|
|
|
qiskit_code = st.text_area("Qiskit Code", height=300, value=""" |
|
# Example Qiskit Code |
|
from qiskit import QuantumCircuit, transpile |
|
from qiskit_aer import AerSimulator |
|
# Create a Quantum Circuit |
|
qc = QuantumCircuit(2, 2) |
|
# Apply Hadamard and CNOT gates |
|
qc.h(0) |
|
qc.cx(0, 1) |
|
# Measure the qubits |
|
qc.measure([0, 1], [0, 1]) |
|
# Use AerSimulator as the backend |
|
simulator = AerSimulator() |
|
# Transpile the circuit for the simulator |
|
compiled_circuit = transpile(qc, simulator) |
|
# Execute the circuit |
|
result = simulator.run(compiled_circuit, shots=1024).result() |
|
# Get the measurement results |
|
counts = result.get_counts() |
|
print(counts) |
|
""") |
|
|
|
|
|
if st.button("Run"): |
|
try: |
|
|
|
old_stdout = sys.stdout |
|
redirected_output = io.StringIO() |
|
sys.stdout = redirected_output |
|
|
|
|
|
exec(qiskit_code) |
|
|
|
|
|
output = redirected_output.getvalue() |
|
st.success("Execution successful!") |
|
st.text_area("Output", output, height=200) |
|
except Exception as e: |
|
st.error(f"An error occurred: {e}") |
|
finally: |
|
|
|
sys.stdout = old_stdout |