eaglelandsonce's picture
Update app.py
ac52b00 verified
import streamlit as st
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
import io
import sys
# Title for the Streamlit app
st.title("Quantum Circuit Simulator with Examples")
st.write("Select a quantum circuit example to load and simulate.")
# Define 20 quantum circuit examples (easy to complicated)
examples = {
"1. Empty Circuit": """
from qiskit import QuantumCircuit
qc = QuantumCircuit(1)
print("Empty circuit created.")
""",
"2. Single Qubit Hadamard": """
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0)
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit, shots=1024).result()
counts = result.get_counts()
print(counts)
""",
"3. Bell State": """
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit, shots=1024).result()
counts = result.get_counts()
print(counts)
""",
"4. GHZ State": """
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(3, 3)
qc.h(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.measure([0, 1, 2], [0, 1, 2])
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit, shots=1024).result()
counts = result.get_counts()
print(counts)
""",
"5. Deutsch Algorithm": """
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(2, 1)
qc.h([0, 1])
qc.cx(0, 1)
qc.h(0)
qc.measure(0, 0)
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit, shots=1024).result()
counts = result.get_counts()
print(counts)
""",
"6. Quantum Teleportation": """
from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
qc = QuantumCircuit(3, 3)
qc.h(1)
qc.cx(1, 2)
qc.cx(0, 1)
qc.h(0)
qc.measure([0, 1], [0, 1])
qc.cx(1, 2)
qc.cz(0, 2)
qc.measure(2, 2)
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit, shots=1024).result()
counts = result.get_counts()
print(counts)
""",
"7. DNA Base Pair Encoding": """
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
# DNA Base Pair Encoding: A -> 00, T -> 01, G -> 10, C -> 11
qc.x(0) # Example encoding for T (01)
qc.measure([0, 1], [0, 1])
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit, shots=1024).result()
counts = result.get_counts()
print(counts)
""",
"8. QUBO": """
import numpy as np
# Qiskit / Qiskit Optimization imports
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit.algorithms import QAOA
from qiskit.algorithms.optimizers import SPSA
from qiskit_optimization import QuadraticProgram
from qiskit_optimization.algorithms import MinimumEigenOptimizer
# 1) Define a small QUBO with two binary variables
problem = QuadraticProgram("my_qubo")
problem.binary_var("x0")
problem.binary_var("x1")
# Objective: minimize: H = x0 + 2*x1 + x0*x1
problem.minimize(
linear={"x0": 1, "x1": 2},
quadratic={("x0", "x1"): 1}
)
# Print to confirm the problem is not empty
print("--- Quadratic Program ---")
print(problem.export_as_lp_string())
# 2) Set up QAOA solver on the qasm_simulator
backend = Aer.get_backend('qasm_simulator')
quantum_instance = QuantumInstance(
backend=backend,
shots=512,
seed_simulator=42,
seed_transpiler=42
)
qaoa = QAOA(
optimizer=SPSA(maxiter=50), reps=2, quantum_instance=quantum_instance
)
solver = MinimumEigenOptimizer(qaoa)
# 3) Solve the QUBO
result = solver.solve(problem)
print("--- QAOA Results ---")
print("Optimal solution:", result.x)
print("Objective value:", result.fval)
""",
"9. DNA Sequence Matching with Grover's Algorithm": """
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.circuit.library import GroverOperator
# Define the oracle circuit to mark the solution state |01>
def create_oracle():
oracle_circuit = QuantumCircuit(2)
oracle_circuit.cz(0, 1) # Mark |01> as the solution
return oracle_circuit
# Define the full Grover search circuit
def create_grover_circuit():
oracle = create_oracle()
grover_circuit = QuantumCircuit(2, 2)
# Apply Hadamard to all qubits
grover_circuit.h([0, 1])
# Apply the oracle
grover_circuit.append(oracle.to_gate(), [0, 1])
# Apply Grover diffusion operator
grover_circuit.h([0, 1])
grover_circuit.x([0, 1])
grover_circuit.h(1)
grover_circuit.cz(0, 1)
grover_circuit.h(1)
grover_circuit.x([0, 1])
grover_circuit.h([0, 1])
# Measure the qubits
grover_circuit.measure([0, 1], [0, 1])
return grover_circuit
# Create the Grover circuit
grover_circuit = create_grover_circuit()
# Use AerSimulator as the backend
simulator = AerSimulator()
# Transpile and execute the circuit
compiled_circuit = transpile(grover_circuit, simulator)
result = simulator.run(compiled_circuit, shots=1024).result()
# Retrieve and print the counts
counts = result.get_counts()
print(counts)
"""
}
# Selection menu for examples
selected_example = st.selectbox("Select a Quantum Circuit Example", list(examples.keys()))
# Display selected example code
st.subheader("Selected Quantum Circuit Code")
st.text_area("Code", examples[selected_example], height=300)
# 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 selected example
exec(examples[selected_example])
# 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