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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not ((L >> i) & 1) and n!=1:
            qc.x(i)
    if n==1:
        qc.rz(-2*theta, 0)
    else:
        qc.append(RZGate(-2*theta).control(n - 1), range(n))

    for i in range(n):
        if not ((L >> i) & 1) and n!=1:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (1<<n)&L==0:
            qc.x(i)
    if n==1:
        qc.rz(-2*theta, 0)
    else:
        qc.append(RZGate(-2*theta).control(n - 1), range(n))

    for i in range(n):
        if (1<<n)&L!=0:
            qc.x(i)
    return qc

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (1<<n)&L==0:
            qc.x(i)
    if n==1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)

    for i in range(n):
        if (1<<n)&L!=0:
            qc.x(i)
    return qc

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (1<<i)&L==0:
            qc.x(i)
    if n==1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)

    for i in range(n):
        if (1<<i)&L!=0:
            qc.x(i)
    return qc

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (1<<i)&L==0:
            qc.x(i)
    if n==1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (1<<i)&L==0:
            qc.x(i)
    if n==1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)

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

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates  import PhaseGate
def solve(n: int, L: int, theta: float) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  for i in range(n - 1, -1, -1):
    if ((L >> i) & 1) == 0:
      qc.x(i)
  qc.append(PhaseGate(theta).control(n - 1), list(range(n)))
  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.standard_gates  import PhaseGate
def solve(n: int, L: int, theta: float) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  for i in range(n - 1, -1, -1):
    if ((L >> i) & 1) == 0:
      qc.x(i)
  if n == 1:
    qc.append(PhaseGate(theta))
  else:
    qc.append(PhaseGate(theta).control(n - 1), list(range(n)))
  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.standard_gates  import PhaseGate
def solve(n: int, L: int, theta: float) -> QuantumCircuit:
  qc = QuantumCircuit(n)
  for i in range(n):
    if ((L >> i) & 1) == 0:
      qc.x(i)
  if n == 1:
    qc.p(theta, 0)
  else:
    qc.append(PhaseGate(theta).control(n - 1), list(range(n)))
  for i in range(n):
    if ((L >> i) & 1) == 0:
      qc.x(i)
  return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import PhaseGate as PGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_code = format(L, f'0{n}b')[::-1]
    print(L_code)
    for i in range(n):
        if L_code[i] == "0":
            qc.x(i)
    p_gate = PGate(2*theta)
    mc_p = p_gate.control(n - 1)
    qc.append(mc_p, qargs=range(n))
    for i in range(n):
        if L_code[i] == "0":
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import PhaseGate as PGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_code = format(L, f'0{n}b')[::-1]
    print(L_code)
    for i in range(n):
        if L_code[i] == "0":
            qc.x(i)
    p_gate = PGate(theta)
    mc_p = p_gate.control(n - 1)
    qc.append(mc_p, qargs=range(n))
    for i in range(n):
        if L_code[i] == "0":
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import PhaseGate as PGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_code = format(L, f'0{n}b')[::-1]
    print(L_code)
    for i in range(n):
        if L_code[i] == "0":
            qc.x(i)
    if n >= 2:
      p_gate = PGate(theta)
      mc_p = p_gate.control(n - 1)
      qc.append(mc_p, qargs=range(n))
    else:
      qc.p(theta, 0)
    for i in range(n):
        if L_code[i] == "0":
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import PhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if ~L >> i & 1: qc.x(i)
    qc.append(PhaseGate(theta).control(n - 1), range(n))
    for i in range(n):
        if ~L >> i & 1: qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

from qiskit.circuit.library import PhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if ~L >> i & 1: qc.x(i)
    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(PhaseGate(theta).control(n - 1), range(n))
    for i in range(n):
        if ~L >> i & 1: qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n > 1:
        controls = list(range(n - 1))
        cz_gate = ZGate().control(len(controls))
        qc.append(cz_gate, controls + [n - 1])
    else:
        qc.z(0)

    # Restaurar los qubits a su estado original
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    qc.h(range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n > 1:
        controls = list(range(n - 1))
        cz_gate = ZGate().control(len(controls))
        qc.append(cz_gate, controls + [n - 1])
    else:
        qc.z(0)

    # Restaurar los qubits a su estado original
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(range(n))

    # Write your code here:
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    # Aplicar la compuerta P controlada (Phase) al estado L
    if n > 1:
        controls = list(range(n - 1))
        p_gate = PhaseGate(theta).control(len(controls))
        qc.append(p_gate, controls + [n - 1])
    else:
        qc.p(theta, 0)

    # Restaurar los qubits a su estado original
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

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

    # Write your code here:
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    # Aplicar la compuerta P controlada (Phase) al estado L
    if n > 1:
        controls = list(range(n - 1))
        p_gate = PhaseGate(theta).control(len(controls))
        qc.append(p_gate, controls + [n - 1])
    else:
        qc.p(theta, 0)

    # Restaurar los qubits a su estado original
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    qc.cp(math.pi/2, range(n-1), n-1)

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(0)
    else:
        qc.mcp(math.pi/2, [range(n-1)], n-1)

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, [range(n-1)], n-1)

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(L):
        if (L>>i)&1 == 0:
            qc.x(i)
    if n==1:
        qc.p(theta)
    else:
        qc.append(PhaseGate(theta).control(n-1),range(n))
    for i in range(L):
        if (L>>i)&1 == 0:
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (L>>i)&1 == 0:
            qc.x(i)
    if n==1:
        qc.p(theta)
    else:
        qc.append(PhaseGate(theta).control(n-1),range(n))
    for i in range(n):
        if (L>>i)&1 == 0:
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (L>>i)&1 == 0:
            qc.x(i)
    if n==1:
        qc.p(theta,0)
    else:
        qc.append(PhaseGate(theta).control(n-1),range(n))
    for i in range():
        if (L>>i)&1 == 0:
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (L>>i)&1 == 0:
            qc.x(i)
    if n==1:
        qc.p(theta,0)
    else:
        qc.append(PhaseGate(theta).control(n-1),range(n))
    for i in range(n):
        if (L>>i)&1 == 0:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    list = []
    for i in range(n):
        if L & (1 << i):
            list.append(i)
    list.append(0)
    qc.append(RZGate(theta).control(len(list)-1), list)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    list = []
    for i in range(n):
        if L & (1 << i):
            list.append(i)
    list.append(0)
    qc.append(RZGate(theta).control(len(list)-1), list)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    l = []
    for i in range(n):
        if L & (1 << i):
            l.append(i)
    list.append(0)
    qc.append(RZGate(theta).control(len(l)-1), l)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for l in range(L):
        for i in range(n):
            if  not((l>>i) & 1):
                qc.x(i)
        if n==l:
            qc.rz(-2*theta,l)
        for i in range(n):
            if not ((l>>i)&1):
                qc.x(i)

    return qc
'''


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

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

    l = L
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((l >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.p(theta, 0)
    else:
        # apply multiple controlled Phase gate
        qc.append(PhaseGate().control(n - 1), range(n))
    for i in range(n):
        if not ((l >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    l = L
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((l >> i) & 1):
            qc.x(i)
    qc.p(theta, n - 1)
    for i in range(n):
        if not ((l >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    l = L
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((l >> i) & 1):
            qc.x(i)
    qc.p(theta, n)
    for i in range(n):
        if not ((l >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta, 0)
    else:
        qc.mcp(theta, control_qubits=list(range(1, n)), target_qubit=0)

    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta, 0)
    else:
        qc.mcp(theta, control_qubits=list(range(1, n)), target_qubit=0)

    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, control_qubits=list(range(1, n)), target_qubit=0)

    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not((1 << i) & l):
            qc.x(i)
    if n == 1:
        qc.p(theta,n - 1)
    else:
        qc.mcp(theta, list(range(n - 1)), n - 1)
    for i in range(n):
            if not((1 << i) & l):
                qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not((1 << i) & l):
            qc.x(i)
    if n == 1:
        qc.p(theta,n - 1)
    else:
        qc.mcp(theta, list(range(n - 1)), n - 1)
    for i in range(n):
        if not((1 << i) & l):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not((1 << i) & L):
            qc.x(i)
    if n == 1:
        qc.p(theta,n - 1)
    else:
        qc.mcp(theta, list(range(n - 1)), n - 1)
    for i in range(n):
        if not((1 << i) & L):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    q = 0
    p = L % 2
    L //= 2
    for i in range(n-1):
      q *= 2
      q += L % 2
      L //= 2
    print(q, p)
    if p == 0:
      qc.x(q)
    qc.p(theta,q)
    if p == 0:
      qc.x(q)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    q = 0
    p = L % 2
    L //= 2
    for i in range(n-1):
      q *= 2
      q += L % 2
      L //= 2
    if p == 0:
      qc.x(q)
    qc.p(theta,q)
    if p == 0:
      qc.x(q)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    q = 0
    p = L % 2
    L //= 2
    for i in range(n-1):
      q *= 2
      q += L % 2
      L //= 2
    print(q, p)
    if p == 0:
      qc.x(q)
    qc.p(theta,q)
    if p == 0:
      qc.x(q)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    q = 0
    p = L % 2
    L //= 2
    for i in range(n-1):
      q *= 2
      q += L % 2
      L //= 2
    if p == 0:
      qc.x(q)
    qc.p(theta,q)
    if p == 0:
      qc.x(q)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    qc.append(PhaseGate(theta).control(n-1), range(n))
    if not (L >> 0) & 1:
        qc.x(0)
    if not (L >> 1) & 1:
        qc.x(1)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(range(n))
    qc.append(PhaseGate(theta).control(n-1), range(n))
    for i in range(n):
        if not (L >> i) & 1:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not (1 << i) & L:
            qc.x(i)
    if n == 1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, range(n-1), n-1)
    for i in range(n):
        if not (1 << i) & L:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not (1 << i) & L:
            qc.x(i)
    if n == 1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)
    for i in range(n):
        if not (1 << i) & L:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)
    qc.mcp(theta,range(n))
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)
    qc.mcp(theta,list(range(n)))
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)
    # qc.mcp(theta,list(range(n)))
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)
    qc.mcp(theta,list(range(n-1)), n-1)
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)
    if n == 1:
        qc.p(theta, n-1)
    elif n == 2:
        qc.cp(theta, n-2, n-1)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)
    for i in range(n):
        if L&(1<<i) == 0:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    if n == 1:
        qc.p(0)
    else:
        qc.append(PGate().control(n - 1), range(n))

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
#from qiskit.circuit.library import PGate

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

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    if n == 1:
        qc.p(0)
    else:
        #qc.append(PGate().control(n - 1), range(n))

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
#from qiskit.circuit.library import PGate

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

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    if n == 1:
        qc.p(0)
    #else:
        #qc.append(PGate().control(n - 1), range(n))

    for i in range(n):
        if not (L >> i & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
#from qiskit.circuit.library import PGate

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(0)
    #else:
        #qc.append(PGate().control(n - 1), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
#from qiskit.circuit.library import PGate

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    qc.p(theta, 0)
    #else:
        #qc.append(PGate().control(n - 1), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(PGate().control(n - 1), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(PhazeGate().control(n - 1), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(PhaseGate().control(n - 1), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(CPHASEGate, range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    qc.p(theta, 0)

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import PhaseGate, CPHASEGate

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(CPHASEGate(theta), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import PhaseGate, ControlledPhaseGate

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(ControlledPhaseGate(theta), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.append(MCPhaseGate(theta, n - 1), range(n))

    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n > 1:
        bit_L = list(bin(L)[2:].zfill(n))[::-1]
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.append(RZGate(- theta).control(n - 1), range(n))
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.rz(- theta, n - 1)
    else:
        if L % 2 == 1:
            qc.rz(-2 * theta, n - 1)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n > 1:
        bit_L = list(bin(L)[2:].zfill(n))[::-1]
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.append(RZGate(2 * theta).control(n - 1), range(n))
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
    else:
        if L % 2 == 1:
            qc.rz(-2 * theta, n - 1)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n > 1:
        bit_L = list(bin(L)[2:].zfill(n))[::-1]
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.append(RZGate(- 2 * theta).control(n - 1), range(n))
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
    else:
        if L % 2 == 1:
            qc.rz(-2 * theta, n - 1)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n > 1:
        bit_L = list(bin(L)[2:].zfill(n))[::-1]
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.append(RZGate(-2 * theta).control(n - 1), range(n))
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
    else:
        if L % 2 == 1:
            qc.rz(-2 * theta, n - 1)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n > 1:
        bit_L = list(bin(L)[2:].zfill(n))[::-1]
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.mcp(theta, list(range(n - 1)), n - 1)
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
    else:
        if L % 2 == 1:
            qc.rz(-2 * theta, n - 1)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n > 1:
        bit_L = list(bin(L)[2:].zfill(n))[::-1]
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.mcp(theta, list(range(n - 1)), n - 1)
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
    else:
        if L % 2 == 1:
            qc.p(theta, n - 1)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    if n > 1:
        bit_L = list(bin(L)[2:].zfill(n))[::-1]
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
        qc.mcp(theta, list(range(n - 1)), n - 1)
        for j, b in enumerate(bit_L):
            if b == "0":
                qc.x(j)
    else:
        if L == 0:
            qc.x(0)
        qc.p(theta, n - 1)
        if L == 0:
            qc.x(0)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import MCMT, PhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bitstring = (f'{L:b}'.zfill(n))[::-1]
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    qc.compose(MCMT(PhaseGate(theta), num_ctrl_qubits=n - 1, num_target_qubits=1), range(n), inplace=True)
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import MCPhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    qc.h(0)
    qc.h(1)
    # Write your code here:
    bitstring = (f'{L:b}'.zfill(n))[::-1]
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    qc.compose(MCPhaseGate(theta, num_ctrl_qubits=n - 1), range(n), inplace=True)
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import MCPhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bitstring = (f'{L:b}'.zfill(n))[::-1]
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    qc.compose(MCPhaseGate(theta, num_ctrl_qubits=n - 1), range(n), inplace=True)
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import MCPhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    bitstring = (f'{L:b}'.zfill(n))[::-1]
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    if n == 1:
        qc.p(theta, 0)
    else:
        qc.compose(MCPhaseGate(theta, num_ctrl_qubits=n - 1), range(n), inplace=True)
    for i, x in enumerate(bitstring):
        if x == '0':
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import PhaseGate
import math

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if ((L>>i)&1)==0:
            qc.x(i)
    qc.append(PhaseGate(theta).control(n-1), range(n))
    for i in range(n):
        if ((L>>i)&1)==0:
            qc.x(i)
    return qc

# if __name__ == "__main__":
#     qc = solve(3, 0, math.pi)
#     print(Statevector(qc))
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import PhaseGate
import math

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if ((L>>i)&1)==0:
            qc.x(i)
    if n==1:
        qc.p(theta, 0)
    else:
        qc.append(PhaseGate(theta).control(n-1), range(n))
    for i in range(n):
        if ((L>>i)&1)==0:
            qc.x(i)
    # print(qc.depth())
    return qc

# if __name__ == "__main__":
#     qc = solve(1, 0, math.pi)
#     print(Statevector(qc))
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (L >> i) & 1:
            qc.x(i)

    qc.mcrz(theta, list(range(n)), None)

    for i in range(n):
        if not (L >> i) & 1:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    qc.mcrz(theta, list(range(n)), None)

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    qc.mcrz(theta, list(range(n)),0)

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    qc.mcrz(theta, list(range(n)),None)

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

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

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

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

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

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

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    if n == 1:
        qc.z(0)
    else:
        qc.append(RZGate().control(n - 1), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    if n == 1:
        qc.rz(theta,0)
    else:
        qc.append(theta,RZGate().control(n - 1), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    if n == 1:
        qc.rz(theta,0)
    else:
        qc.append(RZGate(theta).control(n - 1), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    if n == 1:
        qc.rz(theta,0)
    else:
        qc.append(RZGate(theta).control(n), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    if n == 1:
        qc.rz(theta,0)
    else:
        qc.append(RZGate(theta).control(list(range(n))), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    for i in range(n):
        qc.append(RZGate(theta).control(i)), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    for i in range(n):
        qc.append(RZGate(theta).control(i), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    if n == 1:
        qc.rz(theta,0)
    else:
        for i in range(n):
            qc.append(RZGate(theta).control(i), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    if n == 1:
        qc.rz(theta,0)
    else:
        qc.append(PGate(theta).control(n - 1), range(n))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    for i in range(n):
        qc.rz(theta,i)

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    qc.append(RZGate(theta).control(n-1), list(range(n)))

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    for i in range(n):
        qc.rz(theta/n,i)

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    A=[]
    for i in range(n):
        A.append(L%2)
        L//=2

    for i in range(n):
        if not A[i]:
            qc.x(i)

    for i in range(n):
        qc.p(theta, i)

    for i in range(n):
        if not A[i]:
            qc.x(i)

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    X=[]
    for i in range(n):
        X.append(L%2)
        L//=2
    for i in range(n):
        if X[i]==0:
            qc.x(i)
    if n==1:
        qc.rz(theta,0)
    else:
        z = RZGate(theta)
        cz = z.control(n-1)
        qc.append(cz, range(n))
    for i in range(n):
        if X[i]==0:
            qc.x(i)
    # Write your code here:

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    X=[]
    for i in range(n):
        X.append(L%2)
        L//=2
    for i in range(n):
        if X[i]==0:
            qc.x(i)
    if n==1:
        qc.rz(theta,0)
    else:
        assert False
        z = RZGate(theta)
        cz = z.control(n-1)
        qc.append(cz, range(n))
    for i in range(n):
        if X[i]==0:
            qc.x(i)
    # Write your code here:

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    X=[]
    for i in range(n):
        X.append(L%2)
        L//=2
    for i in range(n):
        if X[i]==1:
            qc.x(i)
    if n==1:
        qc.rz(theta,0)
    else:
        z = RZGate(theta)
        cz = z.control(n-1)
        qc.append(cz, list(range(1,n))+[0])
    for i in range(n):
        if X[i]==1:
            qc.x(i)
    # Write your code here:

    return qc

solve(4,2,1).draw()
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    X=[]
    for i in range(n):
        X.append(L%2)
        L//=2
    for i in range(n):
        if X[i]==0:
            qc.x(i)
    if n==1:
        qc.p(theta,0)
    else:
        p = PhaseGate(theta)
        cp = p.control(n-1)
        qc.append(cp, list(range(1,n))+[0])
    for i in range(n):
        if X[i]==0:
            qc.x(i)
    # Write your code here:

    return qc

solve(4,2,10).draw()
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc = QuantumCircuit(n)
    for i in range(n):
        if (1 << i) & L == 0:
            qc.x(i)
    if n == 1:
        qc.p(theta, n - 1)
    else:
        qc.mcp(theta, list(range(n - 1)), n - 1)
    for i in range(n):
        if (1 << i) & L == 0:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import GlobalPhaseGate
import numpy as np
import math

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

    bits = []
    for idx in range(0, n):
        bits.append(bool(L % 2))
        L = L // 2

    for idx in range(0, n):
        if not bits[idx]:
            qc.x(n - 1 - idx)

    if n == 1:
        qc.p(theta, n - 1)
    else:
        qc.mcp(theta, list(range(0, n - 1)), n - 1)

    for idx in range(0, n):
        if not bits[idx]:
            qc.x(n - 1 - idx)

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import GlobalPhaseGate
import numpy as np
import math

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

    bits = []
    for idx in range(0, n):
        bits.append(bool(L % 2))
        L = L // 2

    for idx in range(0, n):
        if not bits[idx]:
            qc.x(idx)

    if n == 1:
        qc.p(theta, n - 1)
    else:
        qc.mcp(theta, list(range(0, n - 1)), n - 1)

    for idx in range(0, n):
        if not bits[idx]:
            qc.x(idx)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standardgates import MCPhaseGate

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

    # ステップ1: 整数Lをリトルエンディアン形式のバイナリ表現に変換
    L_bits = []
    temp_L = L
    for i in range(n):
        L_bits.append(temp_L % 2)
        temp_L //= 2

    # ステップ2: Lのバイナリ表現で0の位置の量子ビットをXゲートで反転
    for i in range(n):
        if L_bits[i] == 0:
            qc.x(i)

    # ステップ3: Multi-Controlled Phase Gateで位相θを適用
    # すべての量子ビットが制御ビットとなる
    control_qubits = list(range(n))
    if n == 1:
        qc.p(theta, 0)  # 1量子ビットの場合は通常のPhaseゲート
    else:
        qc.append(MCPhaseGate(theta, n-1), control_qubits)

    # ステップ4: Xゲートで反転したビットを元に戻す
    for i in range(n):
        if L_bits[i] == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    # 整数Lをリトルエンディアン形式のバイナリ表現に変換
    L_bits = []
    temp_L = L
    for i in range(n):
        L_bits.append(temp_L % 2)
        temp_L //= 2

    # Lのバイナリ表現で0の位置の量子ビットを反転
    for i in range(n):
        if L_bits[i] == 0:
            qc.x(i)

    # n量子ビットすべてが|1⟩の時のみ位相θを適用
    # 制御量子ビットとターゲット量子ビットを設定
    if n == 1:
        qc.rz(theta, 0)
    elif n == 2:
        qc.crz(theta, 0, 1)
    else:
        # 複数制御の場合は段階的に構築
        # 最後の量子ビットをターゲットとして他すべてで制御
        controls = list(range(n-1))
        target = n-1
        qc.mcx(controls, target)  # Multi-controlled X
        qc.rz(theta, target)
        qc.mcx(controls, target)  # 元に戻す

    # 反転したビットを元に戻す
    for i in range(n):
        if L_bits[i] == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import MCPhaseGate

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

    # ステップ1: 整数Lをリトルエンディアン形式のバイナリ表現に変換
    L_bits = []
    temp_L = L
    for i in range(n):
        L_bits.append(temp_L % 2)
        temp_L //= 2

    # ステップ2: Lのバイナリ表現で0の位置の量子ビットをXゲートで反転
    for i in range(n):
        if L_bits[i] == 0:
            qc.x(i)

    # ステップ3: Multi-Controlled Phase Gateで位相θを適用
    control_qubits = list(range(n))
    if n == 1:
        # 1量子ビットの場合はPhaseGateを直接使用
        qc.rz(theta, 0)  # RZゲートでPhaseを実現
    else:
        # n≥2の場合はMCPhaseGateを使用
        mc_phase = MCPhaseGate(theta, n-1)
        qc.append(mc_phase, control_qubits)

    # ステップ4: Xゲートで反転したビットを元に戻す
    for i in range(n):
        if L_bits[i] == 0:
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not ((l >> i) & 1):
            qc.x(i)
    if n == 1
        qc.rz(-2*theta)
    else:
        qc.append(RZGate().control(n-1), range(n))
    for i in range(n):
        if not ((l >> i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta)
    else:
        qc.append(RZGate().control(n-1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta)
    else:
        qc.append(RZGate().control(n-1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta,0)
    else:
        qc.append(RZGate().control(n-1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta,0)
    else:
        qc.append(RZGate(-2*theta).control(n-1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
        return qc
    for i in range(n):
        if (L&(1<<i)) <= 0:
            qc.x(i)
    qc.mcp(theta, list(range(1,n)), 0)
    for i in range(n):
        if (L&(1<<i)) <= 0:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    if n == 1:
        qc.x(0)
        qc.p(theta, 0)
        qc.x(0)
        return qc
    for i in range(n):
        if (L&(1<<i)) <= 0:
            qc.x(i)
    qc.mcp(theta, list(range(1,n)), 0)
    for i in range(n):
        if (L&(1<<i)) <= 0:
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.p(theta,0)
    else:
        qc.append(CPhaseGate().control(theta,n - 1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.p(theta,0)
    else:
        qc.append(CPhaseGate(theta).control(n - 1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.p(theta,0)
    else:
        qc.append(CPhaseGate(theta=theta).control(n - 1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.p(theta,0)
    elif n == 2:
        qc.cp(theta,0,1)
    else:
        qc.append(PhaseGate(theta=theta).control(n - 1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        # check if i-th bit of l is 0 or 1
        if not ((L >> i) & 1):
            qc.x(i)
    if n == 1:
        qc.p(theta,0)
    elif n == 2:
        qc.cp(theta,0,1)
    else:
        qc.append(PhaseGate(theta=theta).control(n - 1), range(n))
    for i in range(n):
        if not ((L >> i) & 1):
            qc.x(i)
    return qc
'''


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

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

  for i in range(n):
    if (L >> i) & 1 == 0:
      qc.x(i)

  qc.append(PhaseGate(theta).control(n-1), range(n))

  for i in range(n):
    if (L >> i) & 1 == 0:
      qc.x(i)

  return qc
'''


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

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

  for i in range(n):
    if (L >> i) & 1 == 0:
      qc.x(i)

  if (n > 1):
    qc.append(PhaseGate(theta).control(n-1), range(n))
  else:
    qc.p(theta, 0)

  for i in range(n):
    if (L >> i) & 1 == 0:
      qc.x(i)

  return qc
'''


'''python
from qiskit import QuantumCircuit

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

    binary_L = bin(L)[2:].zfill(n)

    for i in range(n):
        if binary_L[i] == '1':
            qc.cu1(theta, i, n-1)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    qc.append(RZGate(theta).control(n))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    qc.append(RZGate(theta).control(n-1))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)

    qc.append(RZGate(theta).control(len(array) - 1), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta, array[0])
    else:
        qc.append(RZGate(theta).control(len(array) - 1), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(len(array) - 1), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(n))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(len(array)-1), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(-theta).control(len(array)-1), arra)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(len(array)-1), arra)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(len(array)-1), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(n-j-)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(len(array)-1), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(len(array)), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(-j)

    if len(array) == 1:
        qc.rz(theta,array[0])
    else:
        qc.append(RZGate(theta).control(len(array)-1), array)

    #for j in range(n):
    #    if L & (1 << j) == 0:
    #        qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta, array[0])
    else:
        qc.append(RZGate(theta).control(len(array)-1), array)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n):
        array.append(j)
        if L & (1 << j) != 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta, array[0])
    else:
        qc.append(RZGate(theta).control(len(array)-1), array)

    for j in range(n):
        if L & (1 << j) != 0:
            qc.x(j)

    return qc
'''


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

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    if len(array) == 1:
        qc.rz(theta, 0)
    else:
        qc.append(RZGate(theta).control(n-1), range(n))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    if n == 1:
        qc.rz(theta, 0)
    else:
        qc.append(RZGate(theta).control(n-1), range(n))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    #if n == 1:
    #    qc.rz(theta, 0)
    #else:
    #    qc.append(RZGate(theta).control(n-1), range(n))
    qc.GRZ(n, theta)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    #if n == 1:
    #    qc.rz(theta, 0)
    #else:
    #    qc.append(RZGate(theta).control(n-1), range(n))
    qc.grz(n, theta)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    #if n == 1:
    #    qc.rz(theta, 0)
    #else:
    #    qc.append(RZGate(theta).control(n-1), range(n))
    qc.grz(theta)

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit.library import RZGate, GRZ

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    #if n == 1:
    #    qc.rz(theta, 0)
    #else:
    #    qc.append(RZGate(theta).control(n-1), range(n))
    qc.append(GRZ(n, theta))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    if n == 1:
        qc.rz(theta, 0)
    else:
        qc.append(RZGate(theta).control(n-1), range(n))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(n-j-1)

    return qc
'''


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

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

    for j in range(n-1):
        if L & (1 << j) == 0:
            qc.x(j)

    if n == 1:
        qc.rz(theta, 0)
    else:
        qc.append(RZGate(theta).control(n-1), range(n))

    for j in range(n-1):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    array = []
    for j in range(n-1):
        array.append(j)
        if L & (1 << j) == 0:
            qc.x(j)
    array.append(n-1)

    if n == 1:
        qc.rz(theta, 0)
    else:
        qc.append(RZGate(theta).control(n-1), array)

    for j in range(n-1):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


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

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

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    if n == 1:
        qc.rz(theta, 0)
    else:
        qc.append(RZGate(theta).control(n-2), range(n))

    for j in range(n):
        if L & (1 << j) == 0:
            qc.x(j)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(list(range(n)))
    L_s = format(L, '010b')
    bits = []
    for i, bit in enumerate(L_s[10-n:]):
        if bit == '0':
            bits.append(i)
    if bits != []:
        qc.x(bits)
    qc.mcp(theta, list(range(n-1)), n-1)
    if bits != []:
        qc.x(bits)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    qc.h(list(range(n)))
    L_s = format(L, '010b')
    bits = []
    for i, bit in enumerate(L_s[10-n:][::-1]):
        if bit == '0':
            bits.append(i)
    if bits != []:
        qc.x(bits)
    qc.mcp(theta, list(range(n-1)), n-1)
    if bits != []:
        qc.x(bits)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_s = format(L, '010b')
    bits = []
    for i, bit in enumerate(L_s[10-n:][::-1]):
        if bit == '0':
            bits.append(i)
    if bits != []:
        qc.x(bits)
    qc.mcp(theta, list(range(n-1)), n-1)
    if bits != []:
        qc.x(bits)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_s = format(L, '010b')
    bits = []
    for i, bit in enumerate(L_s[10-n:]):
        if bit == '0':
            bits.append(i)
    if bits != []:
        qc.x(bits)
    qc.mcp(theta, list(range(n-1)), n-1)
    if bits != []:
        qc.x(bits)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    L_s = format(L, '010b')
    bits = []
    for i, bit in enumerate(L_s[10-n:][::-1]):
        if bit == '0':
            bits.append(i)
    if bits != []:
        qc.x(bits)
    if n == 1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n-1)), n-1)
    if bits != []:
        qc.x(bits)

    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.visualization import plot_histogram
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import RZGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta, 0)
    else:
        qc.append(RZGate(-2*theta).control(n-1),range(n))
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.visualization import plot_histogram
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import RZGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if (( L>>i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta, 0)
    else:
        qc.append(RZGate(-2*theta).control(n-1),range(n))
    for i in range(n):
        if (( L>>i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.visualization import plot_histogram
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import RZGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(2*theta, 0)
    else:
        qc.append(RZGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.visualization import plot_histogram
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import PhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta, 0)
    else:
        qc.append(PhaseGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.visualization import plot_histogram
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import PhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(-2*theta, 0)
    else:
        qc.append(PhaseGate(theta).control(n-1),range(n))
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.visualization import plot_histogram
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import PhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    if n == 1:
        qc.rz(2*theta, 0)
    else:
        qc.append(PhaseGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    return qc
'''


'''python
from qiskit import QuantumCircuit
# from qiskit.visualization import plot_histogram
# from qiskit.quantum_info import Statevector
from qiskit.circuit.library import PhaseGate

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    if n==1:
        qc.p(theta,0)
    else:
        qc.append(PhaseGate(theta).control(n-1),range(n))
    for i in range(n):
        if not (( L>>i) & 1):
            qc.x(i)
    return qc

if __name__ == "__main__":
    qc = solve(4,1,2)
    print(qc.draw())
    print(qc.depth())
    # state = Statevector(qc)
    # plot_histogram(state)
'''


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

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

    for i in range(n):
        if L >> i == 1:
            qc.x(i)
    qc.append(ZGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if L >> i == 1:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if L >> i == 1:
            qc.x(i)
    qc.append(RZGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if L >> i == 1:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if L >> i == 1:
            qc.x(i)
    for i in range(n):
        if L >> i == 1:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if L >> i == 1:
            qc.x(i)
    qc.append(RZGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if L >> i == 1:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if L >> i == 1:
            qc.x(i)
    qc.append(RZGate(2*theta).control(2),[0,1,2])
    for i in range(n):
        if L >> i == 1:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if L >> i == 0:
            qc.x(i)
    qc.append(RZGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if L >> i == 1:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if L >> i == 0:
            qc.x(i)
    qc.append(RZGate(2*theta).control(n-1),range(n))
    for i in range(n):
        if L >> i == 0:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n-1):
        if L >> i == 0:
            qc.x(i)
    qc.x(n)
    qc.append(RZGate(2*theta).control(n-1),range(n))
    qc.x(n)
    for i in range(n-1):
        if L >> i == 0:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n-1):
        if L >> i == 0:
            qc.x(i)
    qc.x(n-1)
    qc.append(RZGate(2*theta).control(n-1),range(n))
    qc.x(n-1)
    for i in range(n-1):
        if L >> i == 0:
            qc.x(i)

    return qc
'''


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

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

    for i in range(n):
        if L >> i == 0:
            qc.x(i)
    qc.x(n-1)
    qc.append(RZGate(2*theta).control(n-1),range(n))
    qc.x(n-1)
    for i in range(n):
        if L >> i == 0:
            qc.x(i)

    return qc
'''


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

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

    if n == 1:
        qc.x(0)
        qc.rz(2*theta)
        qc.x(0)
    else:
        for i in range(n):
            if L >> i == 0:
                qc.x(i)
        qc.x(n-1)
        qc.append(RZGate(2*theta).control(n-1),range(n))
        qc.x(n-1)
        for i in range(n):
            if L >> i == 0:
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        qc.x(0)
        qc.rz(2*theta,0)
        qc.x(0)
    else:
        for i in range(n):
            if L >> i == 0:
                qc.x(i)
        qc.x(n-1)
        qc.append(RZGate(2*theta).control(n-1),range(n))
        qc.x(n-1)
        for i in range(n):
            if L >> i == 0:
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        for i in range(n):
            if L >> i == 0:
                qc.x(i)
        qc.x(n-1)
        qc.append(RZGate(2*theta).control(n-1),range(n))
        qc.x(n-1)
        for i in range(n):
            if L >> i == 0:
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 1:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        for i in range(n):
            if L >> i == 0:
                qc.x(i)
        qc.x(n-1)
        qc.append(RZGate(2*theta).control(n-1),range(n))
        qc.x(n-1)
        for i in range(n):
            if L >> i == 0:
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        for i in range(n-1):
            if (L >> i) & 1 == 0:
                qc.x(i)
        qc.x(n-1)
        qc.append(RZGate(2*theta).control(n-1),range(n))
        qc.x(n-1)
        for i in range(n-1):
            if (L >> i) & 1 == 0:
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        for i in range(n):
            if (L >> i) & 1 == 0:
                qc.x(i)
        qc.append(RZGate(2*theta).control(n-1),range(n))
        for i in range(n):
            if (L >> i) & 1 == 0:
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        exit()
        for i in range(n):
            if (L >> i) & 1 == 0:
                qc.x(i)

        qc.append(RZGate(2*theta).control(n-1),range(n))
        for i in range(n):
            if (L >> i) & 1 == 0:
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        for i in range(n):
            # check if i-th bit of l is 0 or 1
            if not ((L >> i) & 1):
                qc.x(i)
        else:
            # apply multiple controlled Z gate
            qc.append(RZGate(2*theta).control(n-1), range(n))
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)
        qc.append(RZGate(2*theta).control(n-1), range(n))
        for i in range(n):
            if not ((l >> i) & 1):
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.rz(2*theta,0)
            qc.x(0)
        else:
            qc.rz(2*theta,0)
    else:
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)
        qc.append(RZGate(2*theta).control(n-1), range(n))
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.PhaseGate(2*theta,0)
            qc.x(0)
        else:
            qc.PhaseGate(2*theta,0)
    else:
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)
        qc.append(PhaseGate(2*theta).control(n-1), range(n))
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.PhaseGate(2*theta)
            qc.x(0)
        else:
            qc.PhaseGate(2*theta)
    else:
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)
        qc.append(PhaseGate(2*theta).control(n-1), range(n))
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)

    return qc
'''


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

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

    if n == 1:
        if L == 0:
            qc.x(0)
            qc.append(PhaseGate(theta),[0])
            qc.x(0)
        else:
           qc.append(PhaseGate(theta),[0])
    else:
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)
        qc.append(PhaseGate(theta).control(n-1), range(n))
        for i in range(n):
            if not ((L >> i) & 1):
                qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (L >> i) & 1:
            qc.cp(theta, i, n-1)
    return qc

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    # Write your code here:
    for i in range(n):
        if (L >> i) & 1:
            for j in range(n-1):
                qc.cx(i, n-1)
            qc.p(theta, n-1)
            for j in range(n-1):
                qc.cx(i, n-1)
    return qc
'''


'''python
from qiskit import QuantumCircuit
import math

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

    # Convert L to its binary representation
    L_binary = bin(L)[2:].zfill(n)

    # Apply phase shift to the specific state |L>
    # Create the controlled phase shift operation for |L>
    for i, bit in enumerate(L_binary):
        if bit == '1':
            qc.x(i)  # Apply X gate to qubit i if the bit is 1

    # Apply the phase shift operation
    qc.u1(theta, 0)  # Apply the phase shift to the |L> state

    # Uncompute the qubits to get back to the original state
    for i, bit in enumerate(L_binary):
        if bit == '1':
            qc.x(i)  # Apply X gate to undo the earlier X gate if the bit was 1

    return qc
'''


'''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import X, Z, RZ
import numpy as np

def oracle(n: int, L: int, theta: float) -> QuantumCircuit:
    q = QuantumRegister(n, name='q')  # Quantum register for the computational basis states
    ancilla = QuantumRegister(1, name='ancilla')  # Ancilla qubit for phase application
    qc = QuantumCircuit(q, ancilla)

    # Initialize ancilla qubit in |1> state
    qc.x(ancilla)

    # Apply controlled-Z gates to check if the state equals L
    for i in range(n):
        if (L >> i) & 1:
            qc.cx(q[i], ancilla)  # Apply CX gate (controlled-X) to flip ancilla qubit

    # Apply RZ gate based on ancilla qubit
    qc.append(RZ(theta, ancilla), [ancilla])

    # Apply controlled-X gates to return ancilla qubit to |0> state
    for i in range(n):
        if (L >> i) & 1:
            qc.cx(q[i], ancilla)

    return qc
'''


'''python
from qiskit import QuantumCircuit

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

    # Convert L to binary representation and apply the RZ gate conditionally
    L_bin = [int(bit) for bit in format(L, f'0{n}b')]  # Convert L to a binary string

    for i in range(n):
        if L_bin[i] == 1:
            qc.rz(theta, i)

    return qc
'''


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

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

    # Apply phase shift to the state if it matches L
    for i in range(n):
        if (L >> i) & 1:  # Check if the i-th bit of L is 1
            qc.rz(theta, i)  # Apply RZ gate with angle theta to the i-th qubit

    return qc
'''


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

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

    # Apply phase shift to the state if it matches L
    for i in range(n):
        if (L >> i) & 1:  # Check if the i-th bit of L is 1
            qc.rz(-2 * theta, i)  # Apply RZ gate with angle theta to the i-th qubit

    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n + 1)  # Add an auxiliary qubit

    # Initialize auxiliary qubit in state |1>
    qc.x(n)  # The auxiliary qubit is at index n

    # Apply controlled-X gates to check if the state equals L
    for i in range(n):
        if (L >> i) & 1:  # If the i-th bit of L is 1
            qc.cx(i, n)  # Apply controlled-X gate

    # Apply RZ gate to the auxiliary qubit
    qc.rz(theta, n)  # Apply the RZ gate with angle theta

    # Reverse the controlled-X gates to restore the state of the auxiliary qubit
    for i in range(n):
        if (L >> i) & 1:  # If the i-th bit of L is 1
            qc.cx(i, n)  # Apply controlled-X gate

    return qc
'''


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

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

    # Convert L to binary string
    L_bin = [int(bit) for bit in format(L, f'0{n}b')]

    # Apply phase shifts based on the bits of L
    for i in range(n):
        if L_bin[i] == 1:
            qc.rz(-2 * theta, i)  # Apply RZ gate with angle -2*theta to the i-th qubit

    return qc
'''


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

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

    # Create the binary representation of L
    L_bin = format(L, f'0{n}b')  # Binary string with leading zeros

    # Apply controlled phase shifts
    for i, bit in enumerate(L_bin):
        if bit == '1':
            qc.cx(i, n)  # Apply controlled X gate with the auxiliary qubit (n-th qubit)

    # Apply RZ gate to the auxiliary qubit if its state is |1⟩
    qc.rz(theta, n)  # Apply RZ gate with angle theta

    # Reverse the controlled X gates
    for i, bit in enumerate(L_bin):
        if bit == '1':
            qc.cx(i, n)  # Apply controlled X gate with the auxiliary qubit (n-th qubit)

    return qc
'''


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

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

    # Convert L to binary representation
    L_bin = format(L, f'0{n}b')  # Binary string of length n

    # Apply RZ gate with -2 * theta to the qubits where L has 1
    for i, bit in enumerate(L_bin):
        if bit == '1':
            qc.rz(-2 * theta, i)  # Apply RZ gate with angle -2 * theta to qubit i

    return qc
'''


'''python
from qiskit import QuantumCircuit
from qiskit.circuit import QuantumRegister, ClassicalRegister
from qiskit.circuit.library import MCPhaseGate
import numpy as np

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    # Create a quantum circuit with n qubits
    qc = QuantumCircuit(n)

    # Convert L to binary representation
    L_bin = format(L, f'0{n}b')

    # Create the phase shift oracle
    # We need to apply a controlled phase shift to target |L>
    if L != 0:  # If L is 0, no phase shift is needed
        # We use the fact that e^iθ = cos(θ) + i*sin(θ)
        # We need to create a phase shift of -2*theta for |L>
        theta = -2 * theta

        # Apply the phase shift to the qubits corresponding to L
        for i, bit in enumerate(L_bin):
            if bit == '1':
                qc.h(i)  # Apply Hadamard gate
        qc.rz(theta, list(range(n)))  # Apply RZ gate to all qubits
        for i, bit in enumerate(L_bin):
            if bit == '1':
                qc.h(i)  # Apply Hadamard gate to invert back

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (L & (1 << i)):
            qc.x(i)

        qc.mcp(theta, list(range(n - 1)), n - 1)

    for i in range(n):
        if not (L & (1 << i)):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    for i in range(n):
        if not (L & (1 << i)):
            qc.x(i)

    if n == 1:
        qc.p(theta, 0)
    else:
        qc.mcp(theta, list(range(n - 1)), n - 1)

    for i in range(n):
        if not (L & (1 << i)):
            qc.x(i)

    return qc
'''


'''python
from qiskit import QuantumCircuit

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 L_binary = format(L, f'0{n}b')
    for i, bit in enumerate(L_binary):
        if bit == '1':
            qc.x(i)
    qc.rz(theta, range(n))
    for i, bit in enumerate(L_binary):
        if bit == '1':
            qc.x(i)
    return qc
'''


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

def solve(n: int, L: int, theta: float) -> QuantumCircuit:
    qc = QuantumCircuit(n)
    L_binary = format(L, f'0{n}b')

    for i, bit in enumerate(L_binary):
        if bit == '1':
            qc.x(i)

    qc.rz(theta, 0)

    for i, bit in enumerate(L_binary):
        if bit == '1':
            qc.x(i)
    return qc
'''