class QuantComp:
    def __init__(qboard, classical):
        qboard.classical = classical
        
    def convert(cboard):
        ret = []
        for i in range(len(cboard)):
            if cboard[i] == 1:
                hold = str(bin(i))
                while(len(hold) < 7):
                    hold = '0' + hold
                ret.append(hold)
        return ret
    
    def setup(array):
        tot = len(array)
        n = 7
        s = []
        for i in range(n-1):
            s.append(i)
        if tot > 8:
            rep = 2
        elif tot > 4:
            rep = 3
        elif tot == 4:
            rep = 4
        elif tot == 3:
            rep = 5
        elif tot == 2:
            rep = 6
        else:
            rep = 8
        return rep, n
        
    def build_oracle(circuit, solutions, n):
        for i in range(tot):
            temp = solutions[i]
            for j, yesno in enumerate(reversed(temp)):
                if yesno == '0':
                    circuit.x(j)
            circuit.h(n-1)
            circuit.mct(s, n-1)
            circuit.h(n-1)
            for k, noyes in enumerate(reversed(temp)):
                if noyes == '0':
                    circuit.x(k)
                    
    def amplify(circuit, n):
        circuit.h(range(n))
        circuit.x(range(n))
        circuit.h(n-1)
        circuit.mct(s, n-1)
        circuit.h(n-1)
        circuit.x(range(n))
        circuit.h(range(n))
        
    def guess():
        sol = convert(qboard.classical)
        rep, n = setup(sol)
        qc = QuantumCircuit(n)
        qc.h(range(n))
        for i in range(rep):
            build_oracle(qc, sol, n)
            amplify(qc, n)
        qc.measure_all()
        simulator = Aer.get_backend('qasm_simulator')
        return execute(qc, backend = simulator, shots = 1).result().get_counts()

