import streamlit as st from qiskit import QuantumCircuit, transpile from qiskit_aer import AerSimulator # Correct import for AerSimulator import io import sys # Title for the Streamlit app st.title("Quantum Circuit Emulator") st.write("Paste your Qiskit code below and press 'Run' to execute.") # Input box for Qiskit code 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) """) # Run button if st.button("Run"): try: # Redirect stdout to capture print output old_stdout = sys.stdout redirected_output = io.StringIO() sys.stdout = redirected_output # Execute the pasted Qiskit code exec(qiskit_code) # Retrieve and display output 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: # Reset stdout sys.stdout = old_stdout