import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

from qiskit import *

from qiskit.tools.visualization import plot_histogram
from IPython.display import display, Math, Latex

def Decrement(n):
    U = QuantumCircuit(n)
    control = [x for x in range(n-1)]
    for k in range(n-1):
        U.x(control)
        U.mcx(control, control[-1]+1)
        U.x(control)
        control.pop()
    U.x(0)
    
    U = U.to_gate()
    U.name = 'Decrement'
    #ctl_U = U.control() make it a controlled gate
    
    return U

n = 3
mc = QuantumCircuit(n, n)
U = Decrement(n)
mc.append(U, range(n))

mc.measure(range(n), range(n))

backend = BasicAer.get_backend('qasm_simulator')
atp = 1024
res = execute(mc, backend=backend, shots=atp).result()
ans = res.get_counts()

mc.draw('mpl')

plot_histogram(ans)