eaglelandsonce
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1 +1,53 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from qiskit import QuantumCircuit, transpile
|
3 |
+
from qiskit_aer import AerSimulator # Correct import for AerSimulator
|
4 |
+
import io
|
5 |
+
import sys
|
6 |
+
|
7 |
+
# Title for the Streamlit app
|
8 |
+
st.title("Quantum Circuit Emulator")
|
9 |
+
st.write("Paste your Qiskit code below and press 'Run' to execute.")
|
10 |
+
|
11 |
+
# Input box for Qiskit code
|
12 |
+
qiskit_code = st.text_area("Qiskit Code", height=300, value="""
|
13 |
+
# Example Qiskit Code
|
14 |
+
from qiskit import QuantumCircuit, transpile
|
15 |
+
from qiskit_aer import AerSimulator
|
16 |
+
# Create a Quantum Circuit
|
17 |
+
qc = QuantumCircuit(2, 2)
|
18 |
+
# Apply Hadamard and CNOT gates
|
19 |
+
qc.h(0)
|
20 |
+
qc.cx(0, 1)
|
21 |
+
# Measure the qubits
|
22 |
+
qc.measure([0, 1], [0, 1])
|
23 |
+
# Use AerSimulator as the backend
|
24 |
+
simulator = AerSimulator()
|
25 |
+
# Transpile the circuit for the simulator
|
26 |
+
compiled_circuit = transpile(qc, simulator)
|
27 |
+
# Execute the circuit
|
28 |
+
result = simulator.run(compiled_circuit, shots=1024).result()
|
29 |
+
# Get the measurement results
|
30 |
+
counts = result.get_counts()
|
31 |
+
print(counts)
|
32 |
+
""")
|
33 |
+
|
34 |
+
# Run button
|
35 |
+
if st.button("Run"):
|
36 |
+
try:
|
37 |
+
# Redirect stdout to capture print output
|
38 |
+
old_stdout = sys.stdout
|
39 |
+
redirected_output = io.StringIO()
|
40 |
+
sys.stdout = redirected_output
|
41 |
+
|
42 |
+
# Execute the pasted Qiskit code
|
43 |
+
exec(qiskit_code)
|
44 |
+
|
45 |
+
# Retrieve and display output
|
46 |
+
output = redirected_output.getvalue()
|
47 |
+
st.success("Execution successful!")
|
48 |
+
st.text_area("Output", output, height=200)
|
49 |
+
except Exception as e:
|
50 |
+
st.error(f"An error occurred: {e}")
|
51 |
+
finally:
|
52 |
+
# Reset stdout
|
53 |
+
sys.stdout = old_stdout
|