from qiskit import QuantumCircuit
import numpy as np
from qiskit import QuantumCircuit, assemble, Aer
def deutsch_problem(seed=None):
    """Returns a circuit that carries out the function
    from Deutsch's problem.
    Args:
        seed (int): If set, then returned circuit will
            always be the same for the same seed.
    Returns: QuantumCircuit
    """
    np.random.seed(seed)
    problem = QuantumCircuit(2)
    if np.random.randint(2):
        print("Function is balanced.")
        problem.cx(0, 1)
    else:
        print("Function is constant.")
        if np.random.randint(2):
            problem.x(1)
    return problem


def deutsch(problem):
    """Implements Deutsch's algorithm.

    Args:
        function (QuantumCircuit): Deutsch function to solve.
            Must be a 2-qubit circuit, and either balanced,
            or constant.
    Returns:
        bool: True if the circuit is balanced, otherwise False.
    """
    qc=QuantumCircuit(2,1)
    qc.x(1)
    qc.h([0,1])
    qc.draw()
    qc.append(problem.to_gate(label='f'),[0,1])
    qc.h(0)
    qc.measure(0,0)
    qc = qc.decompose('f')
    display(qc.draw())
    sim = Aer.get_backend('aer_simulator')
    state = sim.run(qc).result().get_counts() # Execute the circuit and get the count
    state=list(state.keys())[0] ## fetch the bit from the Dictionary  
    if state=='1':
        return('balanced')
    if state=='0':
        return('constant')


    
    


problem=deutsch_problem()
print(deutsch(problem))