Team Ai
Apppublic

saramalik/Designing_Phase_Oracle_of_Boolean_Functions

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
streamlit_app.py64 linesDownload Raw Back to src
1import streamlit as st2from qiskit import QuantumCircuit3import numpy as np4 5st.title("🔮 Phase Oracle Designer")6st.markdown("**Design Boolean function → Build Grover Oracle**")7 8# User designs truth table9n = 210inputs = [f"{i:02b}" for i in range(4)]11truth_table = []12 13st.subheader("1. Design Boolean Function")14col1, col2, col3, col4 = st.columns(4)15 16with col1: truth_table.append(st.selectbox("f(00):", [0, 1], key="f00"))17with col2: truth_table.append(st.selectbox("f(01):", [0, 1], key="f01"))18with col3: truth_table.append(st.selectbox("f(10):", [0, 1], key="f10"))19with col4: truth_table.append(st.selectbox("f(11):", [0, 1], key="f11"))20 21# Find solutions22solutions = [inputs[i] for i, val in enumerate(truth_table) if val == 1]23 24if solutions:25    st.success(f"✅ Solutions: {solutions}")26    27    st.subheader("2. Auto-Built Phase Oracle")28    qc = QuantumCircuit(n+1, n)  # +1 ancilla29    30    # Superposition31    qc.h(range(n))32    33    # Phase oracle for each solution34    for sol in solutions:35        # Flip qubits to match solution36        for i, bit in enumerate(sol):37            if bit == '0': qc.x(i)38        39        # Multi-controlled Z (phase flip)40        qc.h(n)41        qc.mcx(list(range(n)), n)42        qc.h(n)43        44        # Uncompute45        for i, bit in enumerate(sol):46            if bit == '0': qc.x(i)47    48    qc.measure(range(n), range(n))49    50    st.code(qc.draw(output='text'))51    52    # Run Grover (simplified)53    from qiskit_aer import AerSimulator54    sim = AerSimulator()55    result = sim.run(qc, shots=1024).result()56    counts = result.get_counts()57    58    st.subheader("3. Grover Finds Solutions!")59    st.json(counts)60    61    best = max(counts, key=counts.get)62    st.balloons()63else:64    st.warning("No solutions! Set at least one f(x)=1")