'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    import math
    m=math.ceil(math.log2(L))
    qc.h(range(m))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L==1:
        return qc

    import math
    m=math.ceil(math.log2(L))
    qc.h(range(m))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1: return qc
    k = 1
    while 1 << k < L: k += 1
    for i in range(k): qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc = QuantumCircuit(n)
    for i in range(n):
        if L > (1 << i):
            qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    eff_n = int(math.log2(L)) + 1
    qc.h(0)
    for i in range(eff_n - 1):
        qc.cx(i, i + 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    eff_n = int(math.log2(L - 1)) + 1
    print(eff_n)
    qc.h(0)
    for i in range(eff_n - 1):
        qc.cx(i, i + 1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    eff_n = int(math.log2(L - 1)) + 1
    for i in range(eff_n):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    eff_n = int(math.log2(L - 1)) + 1 if L > 1 else 0
    for i in range(eff_n):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(L.bit_length()):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(str(L).bit_length()):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(L.bit_length()-int(2**n==L)):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l=L-1
    k=0
    while l>0:
        for i in range(n-1-k,-1,-1):
            if 1<<i > l:
                continue
            if 1<<i <=l and 1<<(i+1)>l:
                print(1<<(i))
                qc.u(-math.acos(math.sqrt((1<<i))/math.sqrt(l+1))*2,math.pi,0,i)
                qc.x(i)
                c3h_gate = HGate().control(1)
                for j in range(0,i):
                    qc.append(c3h_gate,[i,j])
                qc.x(i)

                k+=1
                l=l-(1<<i) # 最上位ビット削除

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
from qiskit.circuit.library import HGate, U3Gate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l=L-1
    k=0
    while l>0:
        maxi=0
        for i in range(n-1-k,-1,-1):
            if 1<<i > l:
                continue
            if 1<<i <=l and 1<<(i+1)>l:
                maxi=max(maxi,i)
                print(1<<(i))
                # if i>0:
                l2=L-1
                for k1 in range(n-1,i,-1):
                    if not (1<<k1)&l2:
                        qc.x(k1)
                clist=list(range(n-1,i,-1))+[i]
                cu3_gate = U3Gate(-math.acos(math.sqrt((1<<i))/math.sqrt(l+1))*2,math.pi,0).control(len(clist)-1)
                print(clist)
                qc.append(cu3_gate,clist)

                l2=L-1
                for k1 in range(n-1,i,-1):
                    if not (1<<k1)&l2:
                        qc.x(k1)

                qc.x(i)
                for j in range(n-1,maxi,-1):
                    qc.x(j)
                for j in range(0,i):
                    clist=list(range(n-1,i-1,-1))+[j]

                    c3h_gate = HGate().control(len(clist)-1)
                    print(clist)
                    qc.append(c3h_gate,clist)
                qc.x(i)
                for j in range(n-1,maxi,-1):
                    qc.x(j)

                k+=1
                l=l-(1<<i) # 最上位ビット削除

    return qc
'''


'''python
from qiskit import QuantumCircuit
from numpy import sqrt, log2, ceil

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Determine the required number of qubits
    num_states = min(L, 2**n)  # Ensure L does not exceed the total number of states

    # Apply Hadamard gates on a subset of qubits to create a superposition
    num_qubits_needed = ceil(log2(num_states))
    for i in range(num_qubits_needed):
        qc.h(i)

    # Use a barrier for visualization (optional)
    qc.barrier()

    # If L is not a power of 2, we need to adjust the probability amplitudes
    if num_states != 2**num_qubits_needed:
        probability_threshold = num_states / 2**num_qubits_needed
        qc.ry(2 * sqrt(probability_threshold), num_qubits_needed - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # The total Hilbert space has dimension 2^n.
    dim = 2 ** n

    # Create a state vector of length 2^n.
    # For i in 0 to L-1, set the amplitude to 1/sqrt(L), and leave all other amplitudes as 0.
    state = [0] * dim
    amplitude = 1 / math.sqrt(L)
    for i in range(L):
        state[i] = amplitude

    # Use Qiskit's initialize() method.
    # Passing list(range(n)) makes sure the state vector is interpreted in little-endian order.
    qc.initialize(state, list(range(n)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Given a k–bit prefix (interpreted in big–endian order),
    # count how many numbers in {0,1,...,L-1} have that prefix.
    def count(prefix: int, k: int, L: int, n: int) -> int:
        # The k–bit prefix p (an integer in 0,...,2^k-1)
        # corresponds to all numbers in the interval:
        #    [ p * 2^(n-k),  (p+1)*2^(n-k) - 1 ]
        low = prefix * (2 ** (n - k))
        # high = (prefix+1)*2^(n-k) - 1   (not needed explicitly)
        if L <= low:
            return 0
        return min(2 ** (n - k), L - low)

    # Recursively “build” the uniformly controlled rotations.
    # Here, k is the number of bits already fixed.
    # (We work in “logical” qubit order – that is, logical qubit 0 is the most–significant bit.
    # Later we note that physical qubit (n-1) will play the role of logical qubit 0,
    # physical qubit (n-2) that of logical qubit 1, etc. This exactly produces little–endian encoding.)
    def prepare(prefix: int, k: int):
        if k == n:
            return  # all bits fixed
        f_prefix = count(prefix, k, L, n)
        if f_prefix == 0:
            return  # no amplitude flows down this branch
        # Compute counts for the two branches:
        f0 = count(prefix * 2, k + 1, L, n)
        f1 = count(prefix * 2 + 1, k + 1, L, n)
        # The desired rotation should “split” the amplitude so that the |0⟩ branch gets weight f0/f_prefix.
        # In other words, choose theta so that:
        #   cos(theta/2) = sqrt(f0/f_prefix)
        theta = 2 * math.acos(math.sqrt(f0 / f_prefix))
        # The target qubit for level k is logical qubit k, which we now map to physical qubit (n-1-k)
        target = n - 1 - k
        if k == 0:
            # No control needed on the very first rotation.
            qc.ry(theta, target)
        else:
            # Controls come from the previously fixed (logical) qubits.
            # (They are mapped to physical qubits: logical j -> physical (n-1-j).)
            controls = [n - 1 - j for j in range(k)]
            # Because Qiskit’s controlled–rotation (cry/mcry) gates control on |1⟩,
            # we “flip” any control qubit whose bit in our prefix is 0.
            for j in range(k):
                bit = (prefix >> (k - 1 - j)) & 1  # extract the jth bit (with j=0 = most–significant)
                if bit == 0:
                    qc.x(n - 1 - j)
            if k == 1:
                qc.cry(theta, controls[0], target)
            else:
                qc.mcry(theta, controls, target, None, mode='noancilla')
            for j in range(k):
                bit = (prefix >> (k - 1 - j)) & 1
                if bit == 0:
                    qc.x(n - 1 - j)
        # Recurse to “prepare” the next bits in both branches.
        prepare(prefix * 2, k + 1)
        prepare(prefix * 2 + 1, k + 1)

    # Begin with an empty prefix.
    prepare(0, 0)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # For L == 1, the initial state |0...0> already meets the requirement.
    if L == 1:
        return qc

    # Determine the smallest k such that 2^k >= L.
    k = math.ceil(math.log2(L))

    # Apply Hadamard to the first k qubits.
    qc.h(range(k))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(int.bit_length(L)):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(int.bit_length(L-1)):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def add_rule(qc: QuantumCircuit, n: int, suffix: list[int], Lpre: int) -> None:
    k = len(suffix)
    split_idx = n - k - 1

    #print(f"Lpre={Lpre}, split_idx={split_idx}")
    num = 1 << split_idx
    denom = Lpre
    #print(f"{num}/{denom}")
    theta = math.acos(math.sqrt(num/denom)) * 2
    if k > 0:
        idx = n - 1
        for bit in suffix:
            if bit == 0:
                qc.x(idx)
            idx -= 1
        #print("ry", k, range(split_idx + 1, n))
        qc.append(RYGate(theta).control(k), list(range(split_idx + 1, n)) + [split_idx])
        idx = n - 1
        for bit in suffix:
            if bit == 0:
                qc.x(idx)
            idx -= 1
    else:
        qc.ry(theta, n - 1)

    #print(suffix)
    for i in range(0, split_idx):
        idx = n - 1
        for bit in suffix + [0]:
            if bit == 0:
                qc.x(idx)
            idx -= 1

        size = k + 1
        qubits = list(range(split_idx, n)) + [i]
        #print(size, qubits)
        qc.append(HGate().control(size), qubits)

        idx = n - 1
        for bit in suffix + [0]:
            if bit == 0:
                qc.x(idx)
            idx -= 1

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    if L == (1 << n):
        for i in range(n):
            qc.h(i)
        return qc

    cnt = 0
    suffix = []
    Lpre = L
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            add_rule(qc, n, suffix, Lpre)
            if cnt == 1:
                break
            Lpre -= (1 << i)
            suffix.append(1)
        else:
            suffix.append(0)

        cnt += 1

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def add_rule(qc: QuantumCircuit, n: int, suffix: list[int], Lpre: int) -> None:
    k = len(suffix)
    split_idx = n - k - 1

    #print(f"Lpre={Lpre}, split_idx={split_idx}")
    num = 1 << split_idx
    denom = Lpre
    #print(f"{num}/{denom}")
    theta = math.acos(math.sqrt(num/denom)) * 2
    if k > 0:
        idx = n - 1
        for bit in suffix:
            if bit == 0:
                qc.x(idx)
            idx -= 1
        #print("ry", k, range(split_idx + 1, n))
        qc.append(RYGate(theta).control(k), list(range(split_idx + 1, n)) + [split_idx])
        idx = n - 1
        for bit in suffix:
            if bit == 0:
                qc.x(idx)
            idx -= 1
    else:
        qc.ry(theta, n - 1)

    #print(suffix)
    for i in range(0, split_idx):
        idx = n - 1
        for bit in suffix + [0]:
            if bit == 0:
                qc.x(idx)
            idx -= 1

        size = k + 1
        qubits = list(range(split_idx, n)) + [i]
        #print(size, qubits)
        qc.append(HGate().control(size), qubits)

        idx = n - 1
        for bit in suffix + [0]:
            if bit == 0:
                qc.x(idx)
            idx -= 1

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    if L == (1 << n):
        for i in range(n):
            qc.h(i)
        return qc

    cnt = 0
    suffix = []
    Lpre = L
    for i in range(n - 1, -1, -1):
        if L & (1 << i):
            add_rule(qc, n, suffix, Lpre)
            Lpre -= (1 << i)
            suffix.append(1)
        else:
            suffix.append(0)

        cnt += 1

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(1, n + 1):
        if L <= 2 ** i:
            last = i
            break

    prob_amp = math.sqrt(2 ** (last - 1) / L)
    rot_ang = 2 * math.acos(prob_amp)
    qc.ry(rot_ang, last)
    for i in range(last):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(1, n + 1):
        if L <= 2 ** i:
            last = i
            break

    prob_amp = math.sqrt((L - 2 ** (last - 1)) / L)
    rot_ang = 2 * math.acos(prob_amp)
    qc.ry(rot_ang, last)
    for i in range(last):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(1, n + 1):
        if L <= 2 ** i:
            last = i
            break

    prob_amp = math.sqrt((L -2 ** (last - 1)) / L)
    rot_ang = 2 * math.acos(prob_amp)
    qc.ry(rot_ang, last)
    for i in range(last):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(1, n + 1):
        if L <= 2 ** i:
            last = i
            break

    prob_amp = math.sqrt((L -2 ** (last - 1)) / L)
    rot_ang = 2 * math.acos(prob_amp)
    qc.ry(rot_ang, last - 1)
    for i in range(last - 1):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    bits = 0
    while L:
        L >>= 1
        bits += 1

    for i in range(bits):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    L -= 1

    bits = 0
    while L:
        L >>= 1
        bits += 1

    for i in range(bits):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = 0
    for i in range(n):
        if L <= (1<<i):
            a = i
    for i in range(a):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = 0
    for i in range(n):
        if L <= (1<<i):
            a = i+1
            break
    for i in range(a):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = 0
    for i in range(n):
        if L <= (1<<i):
            a = i
            break
    for i in range(a):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = 0
    for i in range(n):
        if L-1 <= (1<<i):
            a = i+1
            break
    for i in range(a):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = 0
    for i in range(n+1):
        if L <= (1<<i):
            a = i
            break
    for i in range(a):
        qc.h(i)
    return qc
'''


'''python
import math
from qiskit import QuantumCircuit
import qiskit.circuit.library as qis

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    L -= 1

    bits = []
    ones = []
    for i in range(n-1, -1, -1):
        if L & (1<<i) != 0:
            bits += [(i, len(ones), 1)]
            ones += [i]
        else:
            bits += [(i, len(ones), 0)]

    for target, up_to_i_controls, bit in bits:
        for lsb_ctrl_i in range(up_to_i_controls, 0, -1):

            ctrl_subset = ones[0:lsb_ctrl_i]

            ctrl_state = (1<< (lsb_ctrl_i - 1)) - 1
            gate = qis.HGate().control(num_ctrl_qubits=len(ctrl_subset), ctrl_state=ctrl_state)

            qc.append(gate,  ctrl_subset + [target])

        if bit == 1:
            controls = ones[0:up_to_i_controls]
            left = 1<<target
            right = (L % left) + 1

            wanted_cos = (left/(left + right))**0.5
            theta = math.acos(wanted_cos)*2

            gate = qis.RXGate(theta=theta)
            if len(controls) != 0:
                gate = gate.control(num_ctrl_qubits=len(controls))
            qc.append(gate, controls + [target])

    return qc
'''


'''python
import math
from qiskit import QuantumCircuit
import qiskit.circuit.library as qis
import numpy as np

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    L -= 1

    ones = []
    one_count = 0

    inds = np.arange(n)
    lefts = np.vectorize(lambda x: 1<<x)(inds)
    wanted_coss = np.vectorize(lambda left: (left / (left + ((L%left) + 1))) ** 0.5)(lefts)
    thetas = np.vectorize(lambda w: math.acos(w) * 2)(wanted_coss)

    for target in range(n-1, -1, -1):
        bit = L & (1<<target) != 0

        for lsb_ctrl_i in range(len(ones), 0, -1):

            ctrl_subset = ones[0:lsb_ctrl_i]

            ctrl_state = (1<< (lsb_ctrl_i - 1)) - 1
            gate = qis.HGate().control(num_ctrl_qubits=len(ctrl_subset), ctrl_state=ctrl_state)

            qc.append(gate, ctrl_subset + [target], copy=False)

        if bit == 1:
            theta = thetas[target]

            gate = qis.RXGate(theta=theta)
            if one_count != 0:
                gate = gate.control(num_ctrl_qubits=one_count)

            qc.append(gate, ones + [target], copy=False)

            ones.append(target)
            one_count += 1

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    if (L == 1):
	    return qc
    k = math.ceil(math.log2(L))
    qc.h(range(k))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    k = 0
    while True:
        qc.h(k)
        k += 1
        if (1<<k) >= L:
            break

    return qc
'''


'''python
from qiskit import QuantumCircuit
import qiskit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    str_int = str(bin(L)).replace('0b', '')
    for i in range(n)[::-1]:
    if str_int[i] == '1':
        cx = qiskit.circuit.library.standard_gates.ZGate().control(n-i)
        for j in range(i):
            qc.append(cx, list(range(i, n)[::-1]) + [j],)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    while L * 2 <= n
        n //= 2

    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    while L * 2 <= n:
        n //= 2

    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    m = 0, mk = 1
    while mk < L:
        m += 1
        mk *= 2

    for i in range(m):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    m = 0
    mk = 1
    while mk < L:
        m += 1
        mk *= 2

    for i in range(m):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    a = n
    for i in range(n):
        if L <= 2**i:
            a = i
            break
    for i in range(a):
        qc.h(i)
    return qc
'''


'''python
def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    l = L
    for i in range(n - 1):
        mid = 2 ** (n - 1 - i)
        if l >= mid:
            theta = 2 * math.acos(math.sqrt(mid / l))
            qc.ry(theta, n - 1 - i)
            l -= mid

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    l = L
    for i in range(n - 1):
        mid = 2 ** (n - 1 - i)
        if l >= mid:
            theta = 2 * math.acos(math.sqrt(mid / l))
            qc.ry(theta, n - 1 - i)
            l -= mid

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qr = qc.qregs[0]
    # Write your code here:

    if n == 1:
        if L == 1:
            pass
        else:
            assert L == 2
            qc.h(0)
        return qc.decompose()

    mid = 2 ** (n - 1)
    if L <= mid:
        c = solve(n - 1, L)
        qc.x(n - 1)
        qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
        qc.x(n - 1)
        return qc.decompose()

    theta = 2 * math.acos(math.sqrt(mid / L))
    qc.ry(theta, n - 1)
    for i in range(n - 1):
        qc.x(i)
        qc.cx(n - 1, i)
        qc.x(i)

    c = solve(n - 1, L - mid)
    qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
    return qc.decompose()
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qr = qc.qregs[0]
    # Write your code here:

    if n == 1:
        if L == 1:
            pass
        else:
            assert L == 2
            qc.h(0)
        return qc.decompose()

    mid = 2 ** (n - 1)
    if L <= mid:
        c = solve(n - 1, L)
        qc.x(n - 1)
        qc.append(c.control(1).decompose(), qr)
        qc.x(n - 1)
        return qc.decompose()

    theta = 2 * math.acos(math.sqrt(mid / L))
    qc.ry(theta, n - 1)
    for i in range(n - 1):
        qc.x(i)
        qc.cx(n - 1, i)
        qc.x(i)

    c = solve(n - 1, L - mid)
    qc.append(c.control(1).decompose(), qr)
    return qc.decompose()
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qr = qc.qregs[0]
    # Write your code here:

    if n == 1:
        if L == 1:
            pass
        else:
            assert L == 2
            qc.h(0)
        return qc.decompose()

    mid = 2 ** (n - 1)
    if L <= mid:
        c = solve(n - 1, L)
        qc.x(n - 1)
        qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
        qc.x(n - 1)
        return qc.decompose()

    theta = 2 * math.acos(math.sqrt(mid / L))
    qc.ry(theta, n - 1)
    qc.x(n - 1)
    for i in range(n - 1):
        qc.cx(n - 1, i)
    qc.x(n - 1)

    c = solve(n - 1, L - mid)
    qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
    return qc.decompose()

import qiskit_aer

def run(circuit):
    sim = qiskit_aer.AerSimulator()
    c = circuit.copy()
    c.save_statevector()
    return sim.run(c, shots=1).result()

if __name__ == "__main__":
    n = 2
    L = 1
    c = solve(n, L)
    print(c)
    r = run(c)
    print(r)
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qr = qc.qregs[0]
    # Write your code here:

    if n == 1:
        if L == 1:
            pass
        else:
            assert L == 2
            qc.h(0)
        return qc.decompose()

    mid = 2 ** (n - 1)
    if L <= mid:
        c = solve(n - 1, L)
        qc.x(n - 1)
        qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
        qc.x(n - 1)
        return qc.decompose()

    theta = 2 * math.acos(math.sqrt(mid / L))
    qc.ry(theta, n - 1)
    qc.x(n - 1)
    for i in range(n - 1):
        qc.cx(n - 1, i)
    qc.x(n - 1)

    c = solve(n - 1, L - mid)
    qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
    return qc.decompose()
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qr = qc.qregs[0]
    # Write your code here:

    if n == 1:
        if L == 1:
            pass
        else:
            assert L == 2
            qc.h(0)
        return qc.decompose()

    mid = 2 ** (n - 1)
    if L <= mid:
        c = solve(n - 1, L)
        qc.x(n - 1)
        qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
        qc.x(n - 1)
        return qc.decompose()

    theta = 2 * math.acos(math.sqrt(mid / L))
    qc.ry(theta, n - 1)
    qc.x(n - 1)
    for i in range(n - 1):
        qc.ch(n - 1, i)
    qc.x(n - 1)

    c = solve(n - 1, L - mid)
    qc.append(c.control(1).decompose(), [qr[-1]] + qr[:-1])
    return qc.decompose()
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qr = qc.qregs[0]
    # Write your code here:

    if n == 1:
        if L == 1:
            pass
        else:
            assert L == 2
            qc.h(0)
        return qc

    mid = 2 ** (n - 1)
    if L <= mid:
        c = solve(n - 1, L)
        qc.x(n - 1)
        qc.append(c.control(1), [qr[-1]] + qr[:-1])
        qc.x(n - 1)
        return qc

    theta = 2 * math.acos(math.sqrt(mid / L))
    qc.ry(theta, n - 1)
    qc.x(n - 1)
    for i in range(n - 1):
        qc.ch(n - 1, i)
    qc.x(n - 1)

    c = solve(n - 1, L - mid)
    qc.append(c.control(1), [qr[-1]] + qr[:-1])
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 1:
        return qc

    qc.h(range(math.ceil(math.log2(L))))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    # ランダムに複素振幅を設定
    alphas = np.random.rand(2**n) + 1j * np.random.rand(2**n)

    # 複素振幅を正規化
    norm = np.linalg.norm(alphas)
    normalized_alphas = alphas / norm

    # 正規化された複素振幅を量子回路にセット
    qc.initialize(normalized_alphas, range(n))

    return qc
'''


'''python
#%%
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if 2**i < L:
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # 各状態の振幅を等しくするため、量子回路にHゲートを適用します（必要なビット数分だけ）
    for i in range(math.ceil(math.log(L, 2))):
        qc.h(i)

    # 末尾に余分な状態が含まれないようにする
    # ここではビットが少ないケースが考慮されているが、一般的な解決方法が必要です。
    if L != 2 ** n:
        # 未使用のビット位置を計算する
        unused_bits = n - math.ceil(math.log(L, 2))
        # 制御リセットゲートを未使用のビット位置に適用して、余分な状態をリセットする
        for l in range(L, 2 ** n):
            binary_state = bin(l)[2:].zfill(n)
            controls = [ii for ii, bb in enumerate(reversed(binary_state)) if bb == '1']
            if controls:
                qc.x(controls)
            qc.mct(controls, list(range(math.ceil(math.log(L, 2)), n)), None, mode='noancilla')
            if controls:
                qc.x(controls)

    return qc

# トレースを除去してコードを実行するためのテスト
if __name__ == "__main__":
    n = 3
    L = 3
    qc = solve(n, L)
    print(qc)
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # 各状態の振幅を等しくするため、量子回路にHゲートを適用します（必要なビット数分だけ）
    for i in range(math.ceil(math.log(L, 2))):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    if L == 1:
        return qc

    k = math.ceil(math.log2(L))
    qc.h(range(k))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    amplitudes = [2 / (2 ** n) for _ in range(2 ** n - 1)]
    amplitudes.append(1 - sum(amplitudes))

    # 振幅を使用して状態を作成
    qc.initialize(amplitudes, list(range(n)))
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 1:
        return qc
    k = math.ceil(math.log2(L))
    qc.h(range(L))

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 1:
        return qc
    k = math.ceil(math.log2(L))
    qc.h(range(k))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    k = math.ceil(math.log2(L))
    qc.h(range(k))
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    k = math.ceil(math.log2(L))
    qc.h(range(k))
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 1:
        return qc

    k = math.ceil(math.log2(L))
    qc.h(range(k))
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import XGate
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    p=1
    qc.h(0)
    while((1<<p)<L):
        qc.h(p)
        p+=1
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(len(bin(L))-2):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == (1<<n):
        for i in range(n):
            qc.h(i)
    else:
        for i in range(len(bin(L))-2):
            qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import ceil,log2

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ceilL = ceil(log2(L))
   qc.h(range(ceilL))

   return qc
'''


'''python
from qiskit import QuantumCircuit
from math import ceil,log2

def solve(n: int, L: int) -> QuantumCircuit:
   qc = QuantumCircuit(n)
   ceilL = ceil(log2(L))
   if ceilL != 0:
      qc.h(range(ceilL))

   return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    th = 2 * math.atan(math.sqrt(L-1))

    qc.ry(th, 0)
    for i in range(L-1):
        qc.append(RYGate(th).control(1), i, i + 1)
        qc.cx(i + 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    th = 2 * math.atan(math.sqrt(L-1))

    qc.ry(th, 0)
    for i in range(L-1):
        th = 2 * math.atan(math.sqrt(L - i - 2))
        qc.cry(th, i, i + 1)
        qc.cx(i + 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    th = 2 * math.atan(math.sqrt(L-1))
    L =  L - 1

    qc.ry(th, 0)
    for i in range(L-1):
        th = 2 * math.atan(math.sqrt(L - i - 2))
        qc.cry(th, i, i + 1)
        qc.cx(i + 1, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    if L == 1:
        return qc

    qc.h(range(math.ceil(math.log2(L))))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RYGate, HGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    target_L = 0
    x_lst = []
    for digit in range(n):
        target_R = target_L + (1 << (n - digit - 1))
        if L > target_R:
            theta = math.asin(math.sqrt((L - target_R) / (L - target_L))) * 2
            if(digit == 0):
                qc.ry(theta, n-1)
            else:
                for i in x_lst: qc.x(i)
                qc.append(RYGate(theta).control(digit), range(n - digit - 1, n)[::-1])
                for i in x_lst: qc.x(i)

            target_L += (1 << (n - digit - 1))
        else:
            x_lst.append(n - digit - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RYGate, HGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    if L == (1 << n):
        for i in range(n):
            qc.h(i)
        return qc

    target_L = 0
    x_lst = []
    for digit in range(n):
        target_R = target_L + (1 << (n - digit - 1))
        if L > target_R:
            theta = math.asin(math.sqrt((L - target_R) / (L - target_L))) * 2
            if(digit == 0):
                qc.ry(theta, n-1)
            else:
                for i in x_lst: qc.x(i)
                qc.append(RYGate(theta).control(digit), range(n - digit - 1, n)[::-1])
                for i in x_lst: qc.x(i)

            for i in x_lst: qc.x(i)
            qc.x(n - digit - 1)
            for i in range(n - digit - 1):
                qc.append(HGate().control(digit + 1), list(range(n - digit - 1, n)) + [i])
            for i in x_lst: qc.x(i)
            qc.x(n - digit - 1)

            target_L += (1 << (n - digit - 1))
        else:
            x_lst.append(n - digit - 1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        qc.h(i)

    half = n // 2
    i = 0
    while half > 0:
        if L > half:
            break
        qc.h(i)
        i += 1
        half //= 2

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        qc.h(i)

    half = 2**n // 2
    i = 0
    while half > 0:
        if L > half:
            break
        qc.h(i)
        i += 1
        half //= 2

    return qc

print(solve(3, 3))
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(n):
        qc.h(i)

    m = 2**n
    i = 0
    while m > 0:
        if L * 2 > m:
            break
        qc.h(i)
        i += 1
        m //= 2

    return qc

print(solve(3, 3))
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    m = 1
    i = n - 1
    while m < L:
        qc.h(i)
        m *= 2
        i -= 1

    return qc

n = 3
for l in range(1, 2**n + 1):
    print(solve(n, l))
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    m = 1
    i = 0
    while m < L:
        qc.h(i)
        m *= 2
        i += 1

    return qc

n = 3
for l in range(1, 2**n + 1):
    print(solve(n, l))
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    k = math.ceil(math.log2(L))
    for i in range(k):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    left_coeff = math.sqrt(0.6/L)
    right_coeff = math.sqrt(0.4/(2**n-L))

    coeffs = [left_coeff]*L + [right_coeff]*(2**n-L)
    qc.initialize(coeffs, list(range(n)))

    return qc.decompose(reps=5)
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    left_coeff = math.sqrt(0.6/L)
    right_coeff = math.sqrt(0.4/(2**n-L))

    coeffs = [left_coeff]*L + [right_coeff]*(2**n-L)
    qc.initialize(coeffs, list(range(n)))

    return qc.decompose(reps=10)
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    if L == 1:
        return qc

    th = 0
    while True:
        if 2**th >= L:
            break
        else:
            th += 1

    qc.h(range(th))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    k = math.ceil(math.log2(L))
    for i in range(k):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        return qc
    # Write your code here:
    k = math.ceil(math.log2(L))
    for i in range(k):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L == 1:
        return qc

    k = math.ceil(math.log2(L))
    for i in range(k):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    theta = 2*arcsin(1/sqrt(n))

    qc.ry(theta, 0)

    for i in range(1, n):
        qc.ch(0, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from numpy import arcsin, sqrt, pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    theta = 2*arcsin(1/sqrt(n))

    qc.ry(theta, 0)

    for i in range(1, n):
        qc.ch(0, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from numpy import arcsin, sqrt, p

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    theta = 2*arcsin(1/sqrt(L))

    qc.ry(theta, 0)

    for i in range(1, n):
        qc.ch(0, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import numpy as np

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    theta = 2*np.arcsin(1/np.sqrt(L))

    qc.ry(theta, 0)

    for i in range(1, n):
        qc.ch(0, i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))

    # Tゲートを適用して位相を導入
    for _ in range(L):
        for qubit in range(n):
            qc.append(TGate(), [qubit])

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, TGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))

    # Tゲートを適用して位相を導入
    for _ in range(L):
        for qubit in range(n):
            qc.append(TGate(), [qubit])

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L == 1:
        return qc

    k = math.ceil(math.log2(L))
    qc.h(range(k))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for qubit in range(n):
        qc.h(qubit)

    # 振幅を調整
    # 例: 最初の 'L' 状態の振幅を減少させる
    for i in range(L):
        angle = pi / L  # 回転角度は 'L' に応じて調整
        qc.ry(-angle, i)  # 'ry' はY軸回転ゲート

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import pi

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # すべての量子ビットにアダマールゲートを適用
    for qubit in range(n):
        qc.h(qubit)

    # 振幅を調整
    # 例: 最初の 'L' 状態の振幅を減少させる
    for i in range(L):
        angle = pi / L  # 回転角度は 'L' に応じて調整
        qc.ry(-angle, i)  # 'ry' はY軸回転ゲート
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.x(0)
    for i in range(L-1):
        # print(i)
        theta = 2 * math.atan(math.sqrt(n - i - 1))
        # print(math.cos(theta/2))
        qc.cry(theta, i, i+1)
        qc.cx(i+1, i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    if L == 1:
        return qc

    k = math.ceil(math.log2(L))
    qc.h(range(k))

    return qc
'''


'''python
from qiskit import QuantumCircuit, Aer, execute

def create_equal_superposition(n: int) -> QuantumCircuit:
    # n量子ビットの量子回路を作成
    qc = QuantumCircuit(n)

    # 全ての量子ビットにハダマードゲートを適用
    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def create_equal_superposition(n: int) -> QuantumCircuit:
    # n量子ビットの量子回路を作成
    qc = QuantumCircuit(n)

    # 全ての量子ビットにハダマードゲートを適用
    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate

def create_equal_superposition(n: int) -> QuantumCircuit:
    # n量子ビットの量子レジスタを作成
    qreg = QuantumRegister(n)
    qc = QuantumCircuit(qreg)

    # 各量子ビットにハダマードゲートを適用してスーパー・ポジションを作成
    for i in range(n):
        qc.append(HGate(), [qreg[i]])

    return qc
'''


'''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate, MCPhaseGate

def create_custom_superposition(n: int) -> QuantumCircuit:
    # n量子ビットの量子レジスタと回路を作成
    qr = QuantumRegister(n, 'q')
    qc = QuantumCircuit(qr)

    # 全ての量子ビットにハダマードゲートを適用
    for i in range(n):
        qc.append(HGate(), [qr[i]])

    # 位相を調整して、実数部が0.5以上になるようにする
    phase_angle = math.acos(0.5)  # cos(phase_angle) = 0.5
    for i in range(1, n):
        qc.append(MCPhaseGate(phase_angle, num_ctrl_qubits=i), list(range(i + 1)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
def solve(n: int, L: int) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  for i in range(n + 1):
    if L <= (1 << i):
      for j in range(i):
        qc.h(j)
      return qc
'''


'''python
from qiskit import QuantumCircuit
import math

"""
def solve() -> QuantumCircuit:
    qc = QuantumCircuit(2)
    qc.x(0)

    prob_amp = math.sqrt(1 / 3)
    rot_ang = 2 * math.acos(prob_amp)
    qc.cry(rot_ang, 0, 1)
    qc.cx(1, 0)

    prob_amp = math.sqrt(1 / 2)
    rot_ang = 2 * math.acos(prob_amp)
    qc.cry(rot_ang, 1, 0)

    qc.x(0)
    qc.x(1)

    return qc
"""

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(5)

    for i in range(1, n + 1):
        if L <= 2 ** i:
            iter = i - 1
            front = 2 ** iter
            back = L - front
            break

    for i in range(1, iter + 1):
        front_prob_amp = math.sqrt(front / L)
        front_rot_ang = 2 * math.acos(front_prob_amp)
        back_prob_amp = math.sqrt(back / L)
        back_rot_ang = 2 * math.acos(back_prob_amp)

    prob_amp = math.sqrt(1 / 7)
    rot_ang = 2 * math.acos(prob_amp)
    qc.cry(rot_ang, 0, 1)
    qc.cx(1, 0)

    prob_amp = math.sqrt(1 / 6)
    rot_ang = 2 * math.acos(prob_amp)
    qc.cry(rot_ang, 1, 0)
    qc.x(0)
    qc.x(1)

    return qc

prob_amp = math.sqrt(1 / 3)
rot_ang = 2 * math.acos(prob_amp)
print(rot_ang)
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if 2 ** i >= L:
            break
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if L==2**n:
        qc.h(range(int(math.log2(L))))
    else:
        qc.h(range(1+int(math.log2(L))))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    M = L
    i = n-1
    if (L>>i)&1 == 0:
        qc.x(i)
    else:
        theta = -2*math.acos(math.sqrt(2**i/M))
        qc.x(i)
        qc.ry(theta,i)
        CH = HGate().control(n-i)
        for j in range(i):
            qc.append(CH,list(range(n-1,i-1,-1))+[j])
        qc.x(i)
        M -= 2**i
    for i in range(n-2,-1,-1):
        if (L>>i)&1 == 0:
            qc.x(i)
        else:
            theta = -2*math.acos(math.sqrt(2**i/M))
            qc.x(i)
            qc.append(RYGate(theta).control(n-i-1),range(n-1,i-1,-1))
            CH = HGate().control(n-i)
            for j in range(i):
                qc.append(CH,list(range(n-1,i-1,-1))+[j])
            qc.x(i)
            M -= 2**i
        qc_copy = qc.copy()
        for j in range(n-1,i-1,-1):
            if (L>>j)&1 == 0:
                qc_copy.x(j)
    for i in range(n-1,-1,-1):
        if (L>>i)&1 == 0:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L==(1<<n):
        for i in range(n):
            qc.h(i)
    else:
        M = L
        i = n-1
        if (L>>i)&1 == 0:
            qc.x(i)
        else:
            theta = -2*math.acos(math.sqrt(2**i/M))
            qc.x(i)
            qc.ry(theta,i)
            CH = HGate().control(n-i)
            for j in range(i):
                qc.append(CH,list(range(n-1,i-1,-1))+[j])
            qc.x(i)
            M -= 2**i
        for i in range(n-2,-1,-1):
            if (L>>i)&1 == 0:
                qc.x(i)
            else:
                theta = -2*math.acos(math.sqrt(2**i/M))
                qc.x(i)
                qc.append(RYGate(theta).control(n-i-1),range(n-1,i-1,-1))
                CH = HGate().control(n-i)
                for j in range(i):
                    qc.append(CH,list(range(n-1,i-1,-1))+[j])
                qc.x(i)
                M -= 2**i
            qc_copy = qc.copy()
            for j in range(n-1,i-1,-1):
                if (L>>j)&1 == 0:
                    qc_copy.x(j)
        for i in range(n-1,-1,-1):
            if (L>>i)&1 == 0:
                qc.x(i)
        return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L==(1<<n):
        for i in range(n):
            qc.h(i)
    else:
        M = L
        i = n-1
        if (L>>i)&1 == 0:
            qc.x(i)
        else:
            theta = -2*math.acos(math.sqrt(2**i/M))
            qc.x(i)
            qc.ry(theta,i)
            CH = HGate().control(n-i)
            for j in range(i):
                qc.append(CH,list(range(n-1,i-1,-1))+[j])
            qc.x(i)
            M -= 2**i
        for i in range(n-2,-1,-1):
            if (L>>i)&1 == 0:
                qc.x(i)
            else:
                theta = -2*math.acos(math.sqrt(2**i/M))
                qc.x(i)
                qc.append(RYGate(theta).control(n-i-1),range(n-1,i-1,-1))
                CH = HGate().control(n-i)
                for j in range(i):
                    qc.append(CH,list(range(n-1,i-1,-1))+[j])
                qc.x(i)
                M -= 2**i
        for i in range(n-1,-1,-1):
            if (L>>i)&1 == 0:
                qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l = math.ceil(math.log2(L))
    for i in range(l):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l = math.ceil(math.log2(L))
    for i in range(l):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if L > 2**{n-1}
        for i in range(n):
            qc.h(i)
    else:
        qc.x(n-1)
        qc.h(n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    if L < 2**(n-1):
        qc.x(n-1)
        qc.h(n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)

    for k in range(1, n):
        print(k)
        if L <= 2**(n-k):
            qc.x(n-k)
            qc.h(n-k)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    qc.cp(-2 * np.pi / (2**n), n-1, 0)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    if L == 1:
        return qc

    while True:
        print(n)
        if L > 2 ** (n - 1):
            qc.h(range(n))
            return qc
        n -= 1
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qubit_num = 1
    while 2**qubit_num < L:
    qubit_num += 1

    qc = QuantumCircuit(n)

    qc.h(range(n-qubit_num,n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    qubit_num = 1
    while 2**qubit_num < L:
        qubit_num += 1

    qc.h(range(n-qubit_num,n))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    qubit_num = 1
    while 2**qubit_num < L:
        qubit_num += 1

    qc.h(range(qubit_num))

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    m = None
    for i in list(range(n))[::-1]:
        if L & (2 ** i):
            m = i
    for i in range(m):
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import acos, sqrt

def get_bit(x, y):
    return 1 if x & int(2 ** y) != 0 else 0

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    M = L
    # Write your code here:
    ls = [i for i in range(n) if get_bit(M, i)]
    for l in ls:
        qc.x(l)
    l0 = ls[0]
    l1 = ls[1]
    M0 = 2 ** l0
    if l0 > 0:
        for i in range(l0):
            qc.h(i)
    theta0 = -2 * acos(sqrt(M0 / M))
    qc.ry(theta0, l1)
    qc.x(l1)
    for i in range(l0, l1):
        qc.ch(l1, i)
    qc.x(l1)

    k = len(ls) - 1

    Mm = M0

    for m in range(1, k):
        theta_m = -2 * acos(sqrt(2 ** ls[m] / (M - Mm)))
        qc.x(l[m])
        qc.cry(theta_m, l[m+1], l[m])
        qc.x(l[m])
        qc.x(l[m+1])
        for i in range(l[m], l[m+1]):
            qc.ch(l[m+1], i)
        qc.x(l[m+1])
        Mm = Mm + 2 ** l[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import acos, sqrt

def get_bit(x, y):
    return 1 if x & int(2 ** y) != 0 else 0

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    M = L
    # Write your code here:
    ls = [i for i in range(n) if get_bit(M, i)]
    if len(ls) == 0:
        return qc
    for l in ls:
        qc.x(l)
    l0 = ls[0]
    if len(ls) == 1:
        for i in range(l0):
            qc.h(i)
        return qc
    l1 = ls[1]
    M0 = 2 ** l0
    if l0 > 0:
        for i in range(l0):
            qc.h(i)
    theta0 = -2 * acos(sqrt(M0 / M))
    qc.ry(theta0, l1)
    qc.x(l1)
    for i in range(l0, l1):
        qc.ch(l1, i)
    qc.x(l1)

    k = len(ls) - 1

    Mm = M0

    for m in range(1, k):
        theta_m = -2 * acos(sqrt(2 ** ls[m] / (M - Mm)))
        qc.x(l[m])
        qc.cry(theta_m, l[m+1], l[m])
        qc.x(l[m])
        qc.x(l[m+1])
        for i in range(l[m], l[m+1]):
            qc.ch(l[m+1], i)
        qc.x(l[m+1])
        Mm = Mm + 2 ** l[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import acos, sqrt

def get_bit(x, y):
    return 1 if x & int(2 ** y) != 0 else 0

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    M = L
    # Write your code here:
    ls = [i for i in range(n) if get_bit(M, i)]
    if len(ls) == 0:
        return qc
    for l in ls:
        qc.x(l)
    l0 = ls[0]
    if len(ls) == 1:
        for i in range(l0):
            qc.h(i)
        return qc
    l1 = ls[1]
    M0 = 2 ** l0
    if l0 > 0:
        for i in range(l0):
            qc.h(i)
    theta0 = -2 * acos(sqrt(M0 / M))
    qc.ry(theta0, l1)
    qc.x(l1)
    for i in range(l0, l1):
        qc.ch(l1, i)
    qc.x(l1)

    k = len(ls) - 1

    Mm = M0

    for m in range(1, k):
        theta_m = -2 * acos(sqrt(2 ** ls[m] / (M - Mm)))
        qc.x(ls[m])
        qc.cry(theta_m, ls[m], ls[m+1])
        qc.x(ls[m])
        qc.x(ls[m+1])
        for i in range(ls[m], ls[m+1]):
            qc.ch(ls[m+1], i)
        qc.x(ls[m+1])
        Mm = Mm + 2 ** ls[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import acos, sqrt

def get_bit(x, y):
    return 1 if x & int(2 ** y) != 0 else 0

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    M = L
    # Write your code here:
    ls = [i for i in range(n) if get_bit(M, i)]
    if len(ls) == 0:
        return qc
    for l in ls[1:]:
        qc.x(l)

    l0 = ls[0]
    if len(ls) == 1:
        for i in range(l0):
            qc.h(i)
        return qc

    l1 = ls[1]
    M0 = 2 ** l0
    if l0 > 0:
        for i in range(l0):
            qc.h(i)
    theta0 = -2 * acos(sqrt(M0 / M))
    qc.ry(theta0, l1)

    qc.x(l1)
    for i in range(l0, l1):
        qc.ch(l1, i)
    qc.x(l1)

    k = len(ls) - 1

    Mm = M0

    for m in range(1, k):
        theta_m = -2 * acos(sqrt(2 ** ls[m] / (M - Mm)))
        qc.x(ls[m])
        qc.cry(theta_m, ls[m], ls[m+1])
        qc.x(ls[m])
        qc.x(ls[m+1])
        for i in range(ls[m], ls[m+1]):
            qc.ch(ls[m+1], i)
        qc.x(ls[m+1])
        Mm = Mm + 2 ** ls[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
from math import acos, sqrt

def get_bit(x, y):
    return 1 if x & int(2 ** y) != 0 else 0

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    M = L
    # Write your code here:
    ls = [i for i in range(n) if get_bit(M, i)]
    if len(ls) == 0:
        for i in range(n):
            qc.h(i)
        return qc
    for l in ls[1:]:
        qc.x(l)

    l0 = ls[0]
    if len(ls) == 1:
        print(l0)
        for i in range(l0):
            qc.h(i)
        return qc

    l1 = ls[1]
    M0 = 2 ** l0
    if l0 > 0:
        for i in range(l0):
            qc.h(i)
    theta0 = -2 * acos(sqrt(M0 / M))
    qc.ry(theta0, l1)

    qc.x(l1)
    for i in range(l0, l1):
        qc.ch(l1, i)
    qc.x(l1)

    k = len(ls) - 1

    Mm = M0

    for m in range(1, k):
        theta_m = -2 * acos(sqrt(2 ** ls[m] / (M - Mm)))
        qc.x(ls[m])
        qc.cry(theta_m, ls[m], ls[m+1])
        qc.x(ls[m])
        qc.x(ls[m+1])
        for i in range(ls[m], ls[m+1]):
            qc.ch(ls[m+1], i)
        qc.x(ls[m+1])
        Mm = Mm + 2 ** ls[m]

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(math.ceil(math.log2(L))):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
from qiskit.circuit.library import ZGate,XGate,RYGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Write your code here:

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 - start), range(start,qubits))
        else:
            qc.z(start)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    from collections import defaultdict
    d=defaultdict(int)
    for i in range(L):
        s=bin(i)[2:].rjust(n,"0")
        tmp=""
        for i in s:
            tmp+=i
            d[tmp]+=1

    # 指定したbit(ゼロ状態)が0になる確率をratioにする

    # 指定したcontroll bitがすべて1の場合にbit(ゼロ状態)が0になる確率をratioにする
    def crotate(qc,bits,p0):
        theta=math.acos(p0*2-1)
        if len(bits)==1:
            qc.ry(theta,bits[0])
        else:
            qc.append(RYGate(theta).control(len(bits)-1),bits)

    import time

    def dfs(prefix,bit,qc,n):
        # print("call",prefix,bit)
        prefix0=prefix+"0"
        prefix1=prefix+"1"
        v=int(prefix.ljust(n,"0"),2)

        if d[prefix0]+d[prefix1]==0:return
        if prefix=="":
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            to_calcable(n,qc,v,0)
            crotate(qc,[bit],p0)
            to_calcable(n,qc,v,0)
            dfs(prefix0,bit-1,qc,n)
            dfs(prefix1,bit-1,qc,n)
        elif bit>=0:
            # print(prefix,d[prefix+"0"],d[prefix+"1"])
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            if p0==0.5:
                to_calcable(n,qc,v,0)
                for b in range(bit,-1,-1):
                    crotate(qc,list(range(n-1,bit,-1))+[b],p0)
                to_calcable(n,qc,v,0)
            else:
                to_calcable(n,qc,v,0)
                crotate(qc,list(range(n-1,bit-1,-1)),p0)
                to_calcable(n,qc,v,0)
                dfs(prefix0,bit-1,qc,n)
                dfs(prefix1,bit-1,qc,n)

    dfs("",n-1,qc,n)
    # depth = qc.depth()
    # print("量子回路の深さ:", depth)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
from qiskit.circuit.library import ZGate,XGate,RYGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(nn)

    # Write your code here:

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 - start), range(start,qubits))
        else:
            qc.z(start)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    from collections import defaultdict
    d=defaultdict(int)
    for i in range(L):
        s=bin(i)[2:].rjust(n,"0")
        tmp=""
        for i in s:
            tmp+=i
            d[tmp]+=1

    # 指定したbit(ゼロ状態)が0になる確率をratioにする

    # 指定したcontroll bitがすべて1の場合にbit(ゼロ状態)が0になる確率をratioにする
    def crotate(qc,bits,p0):
        theta=math.acos(p0*2-1)
        if len(bits)==1:
            qc.ry(theta,bits[0])
        else:
            qc.append(RYGate(theta).control(len(bits)-1),bits)

    def dfs(prefix,bit,qc,n):
        # print("call",prefix,bit)
        prefix0=prefix+"0"
        prefix1=prefix+"1"
        v=int(prefix.ljust(n,"0"),2)

        if d[prefix0]+d[prefix1]==0:return
        if prefix=="":
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            to_calcable(n,qc,v,0)
            crotate(qc,[bit],p0)
            to_calcable(n,qc,v,0)
            dfs(prefix0,bit-1,qc,n)
            dfs(prefix1,bit-1,qc,n)
        elif bit>=0:
            # print(prefix,d[prefix+"0"],d[prefix+"1"])
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            if p0==0.5:
                to_calcable(n,qc,v,0)
                for b in range(bit,-1,-1):
                    crotate(qc,list(range(n-1,bit,-1))+[b],p0)
                to_calcable(n,qc,v,0)
            else:
                to_calcable(n,qc,v,0)
                crotate(qc,list(range(n-1,bit-1,-1)),p0)
                to_calcable(n,qc,v,0)
                dfs(prefix0,bit-1,qc,n)
                dfs(prefix1,bit-1,qc,n)

    dfs("",n-1,qc,n)
    # depth = qc.depth()
    # print("量子回路の深さ:", depth)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math
from qiskit.circuit.library import ZGate,XGate,RYGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    # Write your code here:

    # 左n bitが1になっている物の位相を反転
    def reverse(qubits,qc,start):
        if qubits-start>1:
            qc.append(ZGate().control(qubits - 1 - start), range(start,qubits))
        else:
            qc.z(start)

    # ある値をall_1に変更する操作
    # 左何bit目から操作するか指定
    def to_calcable(qubits,qc,n,start):
        for i in range(start,qubits):
            if not (n&(1<<i)):
                qc.x(i)

    d={}
    for i in range(1<<n):
        tmp=""
        for j in bin(i)[2:].rjust(n,"0"):
            tmp+=j
            d.setdefault(tmp,0)

    for i in range(L):
        s=bin(i)[2:].rjust(n,"0")
        tmp=""
        for i in s:
            tmp+=i
            d[tmp]+=1

    # 指定したbit(ゼロ状態)が0になる確率をratioにする

    # 指定したcontroll bitがすべて1の場合にbit(ゼロ状態)が0になる確率をratioにする
    def crotate(qc,bits,p0):
        theta=math.acos(p0*2-1)
        if len(bits)==1:
            qc.ry(theta,bits[0])
        else:
            qc.append(RYGate(theta).control(len(bits)-1),bits)

    def dfs(prefix,bit,qc,n):
        # print("call",prefix,bit)
        prefix0=prefix+"0"
        prefix1=prefix+"1"
        v=int(prefix.ljust(n,"0"),2)

        if bit==-1 or d.get(prefix0)+d.get(prefix1)==0:return
        if prefix=="":
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            to_calcable(n,qc,v,0)
            crotate(qc,[bit],p0)
            to_calcable(n,qc,v,0)
            dfs(prefix0,bit-1,qc,n)
            dfs(prefix1,bit-1,qc,n)
        elif bit>=0:
            # print(prefix,d[prefix+"0"],d[prefix+"1"])
            p0=d[prefix0]/(d[prefix0]+d[prefix1])
            if p0==0.5:
                to_calcable(n,qc,v,0)
                for b in range(bit,-1,-1):
                    crotate(qc,list(range(n-1,bit,-1))+[b],p0)
                to_calcable(n,qc,v,0)
            else:
                to_calcable(n,qc,v,0)
                crotate(qc,list(range(n-1,bit-1,-1)),p0)
                to_calcable(n,qc,v,0)
                dfs(prefix0,bit-1,qc,n)
                dfs(prefix1,bit-1,qc,n)

    dfs("",n-1,qc,n)
    # depth = qc.depth()
    # print("量子回路の深さ:", depth)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.x(i)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        qc.h(i)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    c=0
    while 2**c<L:
        c+=1

    for i in range(c):
        qc.h(i)
    # Write your code here:

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if (1 << i) < L:
            qc.h(i)
    return qc

if False && __name__ == "__main__":
    from qiskit.quantum_info import Statevector
    import numpy as np

    qc = solve(3, 3)
    sv = Statevector(qc)
    print(sv)
    prob = np.abs(sv) ** 2
    print(sum(prob[i] for i in range(3)))
    # sv = Statevector.from_label('+++')
    # print(sv.evolve(qc))
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if (1 << i) < L:
            qc.h(i)
    return qc

# if __name__ == "__main__":
#     from qiskit.quantum_info import Statevector
#     import numpy as np
#     qc = solve(3, 3)
#     sv = Statevector(qc)
#     print(sv)
#     prob = np.abs(sv) ** 2
#     print(sum(prob[i] for i in range(3)))
#     # sv = Statevector.from_label('+++')
#     # print(sv.evolve(qc))
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    k=1
    while 2**k<L:
        k+=1

    for i in range(k):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.mcx([0, 1, 2], 3)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

def solve(n: int, L) -> QuantumCircuit:
    qc = QuantumCircuit(n)

    for i in range(0, int(math.log(L))+1):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit import Aer, execute

from qiskit.circuit.library import RYGate, HGate
from math import acos, sqrt, log2
import math

def f(n, L):
    # 量子レジスタと量子回路の作成
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)

    if L == 1:
        qc.h(qr[0])
        return qc

    # Lで1が立っている最上位ビットをkとする
    k = int(log2(L))

    # kビット目にRYゲートを適用
    theta = acos(sqrt((L - 2**int(log2(L)))/L)) * 2
    qc.append(RYGate(theta), [qr[k]])

    # kビットを制御ビットとして残りのビットにHadamardゲートを適用
    ch_gate = HGate().control()
    for i in range(k):
        qc.x(qr[k])
        qc.append(ch_gate, [qr[k], qr[i]])
        qc.x(qr[k])

    # kビットが1の場合、再帰的に関数を呼び出す
    if L > 2 ** k:
        sub_qc = f(n, L - 2**k)
        qc.compose(sub_qc, qr, inplace=True)

    return qc

def solve(n: int, L) -> QuantumCircuit:
    return f(n, L)
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import RYGate, HGate
from math import acos, sqrt, log2
import math

def f(n, L):
    # 量子レジスタと量子回路の作成
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)

    if L == 1:
        qc.h(qr[0])
        return qc

    # Lで1が立っている最上位ビットをkとする
    k = int(log2(L))

    # kビット目にRYゲートを適用
    theta = acos(sqrt((L - 2**int(log2(L)))/L)) * 2
    qc.append(RYGate(theta), [qr[k]])

    # kビットを制御ビットとして残りのビットにHadamardゲートを適用
    ch_gate = HGate().control()
    for i in range(k):
        qc.x(qr[k])
        qc.append(ch_gate, [qr[k], qr[i]])
        qc.x(qr[k])

    # kビットが1の場合、再帰的に関数を呼び出す
    if L > 2 ** k:
        sub_qc = f(n, L - 2**k)
        qc.compose(sub_qc, qr, inplace=True)

    return qc

def solve(n: int, L) -> QuantumCircuit:
    return f(n, L)
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit import Aer, execute

from qiskit.circuit.library import RYGate, HGate
from math import acos, sqrt, log2
import math

def f(n, L):
    # 量子レジスタと量子回路の作成
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)

    if L == 1:
        return qc

    # Lで1が立っている最上位ビットをkとする
    k = int(log2(L))

    # kビット目にRYゲートを適用
    theta = acos(sqrt(2**int(log2(L))/L)) * 2
    qc.append(RYGate(theta), [qr[k]])

    ch_gate = HGate().control()
    for i in range(k):
        qc.x(qr[k])
        qc.append(ch_gate, [qr[k], qr[i]])
        qc.x(qr[k])

    if L > 2 ** k:
        sub_qc = f(k, L - 2**k)
        sub_inst = sub_qc.control()
        qc.append(sub_inst, [qr[k]] + qr[:k])

    print(qc)
    return qc

def solve(n: int, L) -> QuantumCircuit:
    return f(n, L)
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit.circuit.library import RYGate, HGate
from math import acos, sqrt, log2
import math

def f(n, L):
    # 量子レジスタと量子回路の作成
    qr = QuantumRegister(n)
    qc = QuantumCircuit(qr)

    if L == 1:
        return qc

    # Lで1が立っている最上位ビットをkとする
    k = int(log2(L))

    # kビット目にRYゲートを適用
    theta = acos(sqrt(2**int(log2(L))/L)) * 2
    qc.append(RYGate(theta), [qr[k]])

    ch_gate = HGate().control()
    for i in range(k):
        qc.x(qr[k])
        qc.append(ch_gate, [qr[k], qr[i]])
        qc.x(qr[k])

    if L > 2 ** k:
        sub_qc = f(k, L - 2**k)
        sub_inst = sub_qc.control()
        qc.append(sub_inst, [qr[k]] + qr[:k])

    print(qc)
    return qc

def solve(n: int, L) -> QuantumCircuit:
    return f(n, L)
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit.circuit.library import RYGate, HGate, XGate
from math import acos, sqrt, log2
import math

def f(n, L):
    # 量子レジスタと量子回路の作成
    # qr = QuantumRegister(n)
    # qc = QuantumCircuit(qr)
    gates = []

    if L == 1:
        # return qc
        return gates

    # Lで1が立っている最上位ビットをkとする
    k = int(log2(L))

    # kビット目にRYゲートを適用
    theta = acos(sqrt(2**int(log2(L))/L)) * 2
    gates.append((RYGate(theta), [k]))
    # qc.append(RYGate(theta), [qr[k]])

    ch_gate = HGate().control()
    for i in range(k):
        # qc.x(qr[k])
        # qc.append(ch_gate, [qr[k], qr[i]])
        # qc.x(qr[k])
        gates.append([XGate(), [k]])
        gates.append([ch_gate, [k, i]])
        gates.append([XGate(), [k]])

    if L - 1 > 2 ** k:
        sub_gates = f(k, L - 2**k)
        # sub_inst = sub_qc.control()
        # qc.append(sub_inst, [qr[k]] + qr[:k])
        # gates.append([(gate[0].control() for gate in sub_gates, [k] + [i for i in range(k)])])
        gates.extend([(gate[0].control(), [k] + gate[1]) for gate in sub_gates])

    return gates

def solve(n: int, L) -> QuantumCircuit:
    gates = f(n, L)
    qc = QuantumCircuit(n)
    print("\n")
    print(gates)
    for gate in gates:
        print("\n")
        print(gate)
        qc.append(gate[0], gate[1])

    #swap endian
    for i in range(n//2):
        qc.swap(i, n-1-i)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister

from qiskit.circuit.library import RYGate, HGate, XGate
from math import acos, sqrt, log2
import math

def f(n, L):
    # 量子レジスタと量子回路の作成
    # qr = QuantumRegister(n)
    # qc = QuantumCircuit(qr)
    gates = []

    if L == 1:
        # return qc
        return gates

    # Lで1が立っている最上位ビットをkとする
    k = int(log2(L))

    # kビット目にRYゲートを適用
    theta = acos(sqrt(2**int(log2(L))/L)) * 2
    gates.append((RYGate(theta), [k]))
    # qc.append(RYGate(theta), [qr[k]])

    ch_gate = HGate().control()
    for i in range(k):
        gates.append([XGate(), [k]])
        gates.append([ch_gate, [k, i]])
        gates.append([XGate(), [k]])

    if L - 1 > 2 ** k:
        sub_gates = f(k, L - 2**k)
        # sub_inst = sub_qc.control()
        # qc.append(sub_inst, [qr[k]] + qr[:k])
        # gates.append([(gate[0].control() for gate in sub_gates, [k] + [i for i in range(k)])])
        gates.extend([(gate[0].control(), [k] + gate[1]) for gate in sub_gates])

    return gates

def solve(n: int, L) -> QuantumCircuit:
    gates = f(n, L)
    qc = QuantumCircuit(n)
    for gate in gates:
        qc.append(gate[0], gate[1])

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if (1 << i) < L:
            qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
      if (1 << i) < L:
        qc.h(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        if 2**n > L:
            break
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:

    for i in range(n):
        if 2**i > L:
            break
        qc.h(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    i = 0
    while 2**i < L:
        qc.h(i)
        i += 1
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    # 必要な量子ビットの数を計算
    num_qubits = max(n, math.ceil(math.log2(L+1)))
    qc = QuantumCircuit(num_qubits)

    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            # 1量子ビットの場合、通常のZゲートを使用
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

    qc.rz(math.pi, range(num_qubits))
    qc.x(num_qubits-1)
    # すべての量子ビットが0の場合にのみ作用する制御Zゲートを作成
    controls = list(range(num_qubits-1))
    target = num_qubits-1
    qc.append(ZGate().control(len(controls)), controls + [target])

    # 最後の量子ビットに再度Xゲートを適用
    qc.x(num_qubits-1)

    # すべての量子ビットにアダマールゲートを適用
    qc.h(range(num_qubits))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def grover_search(n, L):
    num_qubits = max(n, math.ceil(math.log2(L+1)))
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))

    # オラクルと拡散演算子の適用
    for _ in range(int(math.sqrt(2**num_qubits))):
        oracle(qc, num_qubits, L)
        diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = max(n, math.ceil(math.log2(L+1)))
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))

    # オラクルと拡散演算子の適用
    for _ in range(int(math.sqrt(2**num_qubits))):
        oracle(qc, num_qubits, L)
        diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))

    # オラクルと拡散演算子の適用
    for _ in range(10):
        oracle(qc, num_qubits, L)
        diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))

    # オラクルと拡散演算子の適用
    for _ in range(int(math.sqrt(num_qubits))):
        oracle(qc, num_qubits, L)
        diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        #qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        #qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))

    # オラクルと拡散演算子の適用
    for _ in range(int(math.sqrt(num_qubits))):
        oracle(qc, num_qubits, L)
        diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        #qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        #qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))
    if L!=n**2:
        # オラクルと拡散演算子の適用
        for _ in range(int(math.sqrt(num_qubits))):
            oracle(qc, num_qubits, L)
            diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        #qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
        qc.x([qubit for qubit, bit in enumerate(state) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        #qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
        qc.x([qubit for qubit, bit in enumerate(state) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))
    if L!=2**n:
        # オラクルと拡散演算子の適用
        for _ in range(int(math.sqrt(num_qubits))):
            oracle(qc, num_qubits, L)
            diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')
        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
        #qc.x([qubit for qubit, bit in enumerate(state) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state[::-1]) if bit == '0'])
        #qc.x([qubit for qubit, bit in enumerate(state) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))
    if L!=2**n:
        # オラクルと拡散演算子の適用
        for _ in range(num_qubits):
            oracle(qc, num_qubits, L)
            diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(L):
        state = format(i, '0' + str(num_qubits) + 'b')[::-1]
        qc.x([qubit for qubit, bit in enumerate(state) if bit == '0'])

        if num_qubits > 1:
            qc.append(ZGate().control(num_qubits-1), qc.qubits)
        else:
            qc.z(0)

        qc.x([qubit for qubit, bit in enumerate(state) if bit == '0'])

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))
    if L!=2**n:
        # オラクルと拡散演算子の適用
        for _ in range(num_qubits):
            oracle(qc, num_qubits, L)
            diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate, HGate, XGate
import math

def oracle(qc, num_qubits, L):
    for i in range(n):
        if not (L >> i) & 1:
            continue
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)
        qc.x(i)
        if i == n - 1:
            qc.z(i)
        else:
            qc.append(ZGate().control(n - i - 1), range(i, n))
        qc.x(i)
        for j in range(i + 1, n):
            if not (L >> j) & 1:
                qc.x(j)

def diffusion_operator(qc, num_qubits):
    qc.h(range(num_qubits))
    qc.x(range(num_qubits))

    if num_qubits > 1:
        qc.append(ZGate().control(num_qubits-1), range(num_qubits))
    else:
        qc.z(0)

    qc.x(range(num_qubits))
    qc.h(range(num_qubits))

def solve(n, L):
    num_qubits = n
    qc = QuantumCircuit(num_qubits)

    # 初期化
    qc.h(range(num_qubits))
    if L!=2**n:
        # オラクルと拡散演算子の適用
        for _ in range(num_qubits):
            oracle(qc, num_qubits, L)
            diffusion_operator(qc, num_qubits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    same = ''
    for i in range(n-1, -1, -1):
        nxt = L >> i & 1
        if nxt == 1:
            if i == n-1:
                qc.x(i)
                qc.z(i)
                qc.x(i)
            else:
                qc.x(i)
                qc.append(ZGate().control(n-i-1, ctrl_state=same), range(n-1, i-1, -1))
                qc.x(i)
        same = str(nxt) + same

    return qc

print(solve(3, 5))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    same = ''
    for i in range(n-1, -1, -1):
        nxt = L >> i & 1
        if nxt == 0:
            if i == n-1:
                qc.h(i)
            else:
                qc.x(i)
                qc.append(HGate().control(n-i-1, ctrl_state=same), range(n-1, i-1, -1))
                qc.x(i)
        same = str(nxt) + same

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        qc.h(i)
    same = ''
    for i in range(n-1, -1, -1):
        nxt = L >> i & 1
        if nxt == 0:
            if i == n-1:
                qc.h(i)
            else:
                qc.append(HGate().control(n-i-1, ctrl_state=same), range(n-1, i-1, -1))
        same = str(nxt) + same

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        lower_mask = (1 << (i+1)) - 1
        cnt = [0] * (1 << (i+1))
        for j in range(L):
            cnt[j & lower_mask] += 1
        for j in range(1 << i):
            cnt_0 = cnt[j << 1]
            cnt_1 = cnt[(j << 1) + 1]
            theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
            if i == 0:
                qc.append(RYGate(theta), [i])
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=(bin(j)[2:].zfill(i))[::-1]), list(range(i+1)))

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        lower_mask = (1 << (i+1)) - 1
        cnt = [0] * (1 << (i+1))
        for j in range(L):
            cnt[j & lower_mask] += 1
        for j in range(1 << i):
            cnt_0 = cnt[j << 1]
            cnt_1 = cnt[(j << 1) + 1]
            if cnt_0 + cnt_1 == 0:
                continue
            theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
            if i == 0:
                qc.append(RYGate(theta), [i])
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=(bin(j)[2:].zfill(i))[::-1]), list(range(i+1)))

    return qc

print(solve(10, 911))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        lower_mask = (1 << (i+1)) - 1
        l_low = L & lower_mask
        same = ''
        for j in range(i-1, -1, -1):
            nxt = l_low >> j & 1
            if nxt == 1:
                if j == i-1:
                    qc.h(j)
                else:
                    qc.append(HGate().control(i-j-1, ctrl_state=same), range(i-1, j-1, -1))
            same = str(nxt) + same
        cnt_0 = L >> i
        cnt_1 = cnt_0 + 1
        theta = math.asin(-math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        same = ''
        for j in range(i-1, -1, -1):
            nxt = l_low >> j & 1
            if nxt == 0:
                if j == i-1:
                    qc.x(j)
                    qc.h(j)
                    qc.x(j)
                else:
                    qc.append(RYGate(theta).control(i-j-1, ctrl_state=same), range(i-1, j-1, -1))
            same = str(nxt) + same

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L & ((1 << i) - 1) == 0:
            qc.h(i)
            continue
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 1:
                qc.append(HGate().control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        cnt_0 = L >> i
        cnt_1 = cnt_0 + 1
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 0:
                qc.append(RYGate(theta).control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        qc.append(RYGate(theta).control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])

    return qc

print(solve(3, 6))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 1:
                qc.append(HGate().control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        cnt_0 = L >> i
        cnt_1 = cnt_0 + 1
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 0:
                qc.append(RYGate(theta).control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        if i == 0:
            qc.ry(theta, 0)
        else:
            qc.append(RYGate(theta).control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])

    return qc

print(solve(3, 5))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 1:
                qc.append(HGate().control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        cnt_0 = L >> (i+1) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 0:
                qc.append(RYGate(theta).control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        if i == 0:
            qc.ry(theta, 0)
        else:
            qc.append(RYGate(theta).control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])

    return qc

print(solve(3, 5))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 1:
                qc.append(HGate().control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 0:
                qc.append(RYGate(theta).control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        if i == 0:
            qc.ry(theta, 0)
        else:
            qc.append(RYGate(theta).control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])

    return qc

print(solve(3, 5))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        cnt_0 = (L >> (i+1)) + (L >> i & 1 ^ 1)
        cnt_1 = L >> (i+1)
        if cnt_0 + cnt_1 != 0:
            theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 1:
                    qc.append(RYGate(theta).control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
                same = str(nxt) + same
        cnt_0 = (L >> (i+1)) + (L >> i & 1)
        cnt_1 = L >> (i+1)
        if cnt_0 + cnt_1 != 0:
            theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
            same = ''
            for j in range(i-1, -1, -1):
                nxt = L >> j & 1
                if nxt == 0:
                    qc.append(RYGate(theta).control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
                same = str(nxt) + same
            if i == 0:
                qc.ry(theta, 0)
            else:
                qc.append(RYGate(theta).control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])

    return qc

print(solve(3, 5))
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import HGate, RYGate
import math

def solve(n: int, L: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L >> i & 1 == 1:
            cnt_0 = (L >> (i+1)) + 1
            cnt_1 = L >> (i+1) + 1
            theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
            qc.ry(theta, i)
            continue
        cnt_0 = (L >> (i+1)) + 1
        cnt_1 = L >> (i+1)
        theta = math.acos(math.sqrt(cnt_0 / (cnt_0 + cnt_1))) * 2
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 1:
                qc.append(RYGate(theta).control(i-j, ctrl_state='0'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        same = ''
        for j in range(i-1, -1, -1):
            nxt = L >> j & 1
            if nxt == 0:
                qc.append(HGate().control(i-j, ctrl_state='1'+same), list(range(i-1, j-1, -1)) + [i])
            same = str(nxt) + same
        if i == 0:
            qc.h(0)
        else:
            qc.append(HGate().control(i, ctrl_state=same), list(range(i-1, -1, -1)) + [i])

    return qc

print(solve(3, 5))
'''