Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,110 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from qiskit import QuantumCircuit, transpile
|
| 3 |
-
from qiskit_aer import AerSimulator
|
| 4 |
import io
|
| 5 |
import sys
|
| 6 |
|
| 7 |
# Title for the Streamlit app
|
| 8 |
-
st.title("Quantum Circuit
|
| 9 |
-
st.write("
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
from qiskit import QuantumCircuit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from qiskit_aer import AerSimulator
|
| 16 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"):
|
|
@@ -39,8 +114,8 @@ if st.button("Run"):
|
|
| 39 |
redirected_output = io.StringIO()
|
| 40 |
sys.stdout = redirected_output
|
| 41 |
|
| 42 |
-
# Execute the
|
| 43 |
-
exec(
|
| 44 |
|
| 45 |
# Retrieve and display output
|
| 46 |
output = redirected_output.getvalue()
|
|
@@ -50,4 +125,4 @@ if st.button("Run"):
|
|
| 50 |
st.error(f"An error occurred: {e}")
|
| 51 |
finally:
|
| 52 |
# Reset stdout
|
| 53 |
-
sys.stdout = old_stdout
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from qiskit import QuantumCircuit, transpile
|
| 3 |
+
from qiskit_aer import AerSimulator
|
| 4 |
import io
|
| 5 |
import sys
|
| 6 |
|
| 7 |
# Title for the Streamlit app
|
| 8 |
+
st.title("Quantum Circuit Simulator with Examples")
|
| 9 |
+
st.write("Select a quantum circuit example to load and simulate.")
|
| 10 |
|
| 11 |
+
# Define 20 quantum circuit examples (easy to complicated)
|
| 12 |
+
examples = {
|
| 13 |
+
"1. Empty Circuit": """
|
| 14 |
+
from qiskit import QuantumCircuit
|
| 15 |
+
qc = QuantumCircuit(1)
|
| 16 |
+
print("Empty circuit created.")
|
| 17 |
+
""",
|
| 18 |
+
"2. Single Qubit Hadamard": """
|
| 19 |
+
from qiskit import QuantumCircuit
|
| 20 |
+
from qiskit_aer import AerSimulator
|
| 21 |
+
|
| 22 |
+
qc = QuantumCircuit(1, 1)
|
| 23 |
+
qc.h(0)
|
| 24 |
+
qc.measure(0, 0)
|
| 25 |
+
|
| 26 |
+
simulator = AerSimulator()
|
| 27 |
+
compiled_circuit = transpile(qc, simulator)
|
| 28 |
+
result = simulator.run(compiled_circuit, shots=1024).result()
|
| 29 |
+
counts = result.get_counts()
|
| 30 |
+
print(counts)
|
| 31 |
+
""",
|
| 32 |
+
"3. Bell State": """
|
| 33 |
+
from qiskit import QuantumCircuit
|
| 34 |
from qiskit_aer import AerSimulator
|
| 35 |
+
|
| 36 |
qc = QuantumCircuit(2, 2)
|
|
|
|
| 37 |
qc.h(0)
|
| 38 |
qc.cx(0, 1)
|
|
|
|
| 39 |
qc.measure([0, 1], [0, 1])
|
| 40 |
+
|
| 41 |
+
simulator = AerSimulator()
|
| 42 |
+
compiled_circuit = transpile(qc, simulator)
|
| 43 |
+
result = simulator.run(compiled_circuit, shots=1024).result()
|
| 44 |
+
counts = result.get_counts()
|
| 45 |
+
print(counts)
|
| 46 |
+
""",
|
| 47 |
+
"4. GHZ State": """
|
| 48 |
+
from qiskit import QuantumCircuit
|
| 49 |
+
from qiskit_aer import AerSimulator
|
| 50 |
+
|
| 51 |
+
qc = QuantumCircuit(3, 3)
|
| 52 |
+
qc.h(0)
|
| 53 |
+
qc.cx(0, 1)
|
| 54 |
+
qc.cx(1, 2)
|
| 55 |
+
qc.measure([0, 1, 2], [0, 1, 2])
|
| 56 |
+
|
| 57 |
+
simulator = AerSimulator()
|
| 58 |
+
compiled_circuit = transpile(qc, simulator)
|
| 59 |
+
result = simulator.run(compiled_circuit, shots=1024).result()
|
| 60 |
+
counts = result.get_counts()
|
| 61 |
+
print(counts)
|
| 62 |
+
""",
|
| 63 |
+
"5. Deutsch Algorithm": """
|
| 64 |
+
from qiskit import QuantumCircuit
|
| 65 |
+
from qiskit_aer import AerSimulator
|
| 66 |
+
|
| 67 |
+
qc = QuantumCircuit(2, 1)
|
| 68 |
+
qc.h([0, 1])
|
| 69 |
+
qc.cx(0, 1)
|
| 70 |
+
qc.h(0)
|
| 71 |
+
qc.measure(0, 0)
|
| 72 |
+
|
| 73 |
+
simulator = AerSimulator()
|
| 74 |
+
compiled_circuit = transpile(qc, simulator)
|
| 75 |
+
result = simulator.run(compiled_circuit, shots=1024).result()
|
| 76 |
+
counts = result.get_counts()
|
| 77 |
+
print(counts)
|
| 78 |
+
""",
|
| 79 |
+
"6. Quantum Teleportation": """
|
| 80 |
+
from qiskit import QuantumCircuit
|
| 81 |
+
from qiskit_aer import AerSimulator
|
| 82 |
+
|
| 83 |
+
qc = QuantumCircuit(3, 3)
|
| 84 |
+
qc.h(1)
|
| 85 |
+
qc.cx(1, 2)
|
| 86 |
+
qc.cx(0, 1)
|
| 87 |
+
qc.h(0)
|
| 88 |
+
qc.measure([0, 1], [0, 1])
|
| 89 |
+
qc.cx(1, 2)
|
| 90 |
+
qc.cz(0, 2)
|
| 91 |
+
qc.measure(2, 2)
|
| 92 |
+
|
| 93 |
simulator = AerSimulator()
|
|
|
|
| 94 |
compiled_circuit = transpile(qc, simulator)
|
|
|
|
| 95 |
result = simulator.run(compiled_circuit, shots=1024).result()
|
|
|
|
| 96 |
counts = result.get_counts()
|
| 97 |
print(counts)
|
| 98 |
+
""",
|
| 99 |
+
# Add more examples here, progressively getting more complex
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
# Selection menu for examples
|
| 103 |
+
selected_example = st.selectbox("Select a Quantum Circuit Example", list(examples.keys()))
|
| 104 |
+
|
| 105 |
+
# Display selected example code
|
| 106 |
+
st.subheader("Selected Quantum Circuit Code")
|
| 107 |
+
st.text_area("Code", examples[selected_example], height=300)
|
| 108 |
|
| 109 |
# Run button
|
| 110 |
if st.button("Run"):
|
|
|
|
| 114 |
redirected_output = io.StringIO()
|
| 115 |
sys.stdout = redirected_output
|
| 116 |
|
| 117 |
+
# Execute the selected example
|
| 118 |
+
exec(examples[selected_example])
|
| 119 |
|
| 120 |
# Retrieve and display output
|
| 121 |
output = redirected_output.getvalue()
|
|
|
|
| 125 |
st.error(f"An error occurred: {e}")
|
| 126 |
finally:
|
| 127 |
# Reset stdout
|
| 128 |
+
sys.stdout = old_stdout
|