%matplotlib inline
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
import math
# Loading your IBM Q account(s)
IBMQ.load_accounts()

backend = Aer.get_backend("qasm_simulator")

def bin_expansion(circ, q, N):
    """Places q of |0> into binary expansion of N"""
    for i in range(N + 1):
#     while N >= 0:
        if N & (1 << i):
            circ.x(q[i])

q = QuantumRegister(5, 'x')
c = ClassicalRegister(5, 'y')
qc = QuantumCircuit(q, c)

bin_expansion(qc, q, 5)
for i in range(5):
    qc.measure(q[i], c[i])

qc.draw()

simulate = execute(qc, backend=backend, shots=1024).result()

def raw_bin_exp(N):
    place_mask = 1
    bins = []
#     more_digits = True
    while place_mask <= N:
#     while N >= 0:
        if N & place_mask:
            bins = [1] + bins
        else:
            bins = [0] + bins
#         place += 1
        place_mask = place_mask * 2
#         more_digits
    if bins == []:
        bins = [0]
    return bins

simulate.get_counts()

