import qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer

import numpy as np
from math import floor, ceil

%matplotlib inline

import gate_extensions

for n in range(5, 7):
    print('{}-bit controls'.format(n))
    for inp in range(2**n):
        qr = QuantumRegister(n, 'qr')
        anc = QuantumRegister(max(ceil((n-2)/2), 1), 'anc')
        target = QuantumRegister(1)
        cr = ClassicalRegister(1, 'cr')
        qc = QuantumCircuit(qr, anc, target, cr)
        
        for i in range(n):
            if (inp & (1<<i)) > 0:
                qc.x(qr[i])
        
        qc.mct(qr, target, anc, mode='clean-ancilla')
        qc.barrier()
        qc.measure(target, cr[0])
        
        backend = Aer.get_backend('qasm_simulator')
        job = qiskit.execute(qc, backend, shots=10)
        result = job.result()
        counts = result.get_counts()
#         print(inp)
        if '1' in counts:
            print('{} got 1'.format(inp))

