problem
stringclasses
67 values
user
stringlengths
13
13
submission_order
int64
1
57
result
stringclasses
10 values
execution_time
stringlengths
0
8
memory
stringclasses
88 values
code
stringlengths
47
7.62k
QPC001_B3
AFC5344A7A3BD
9
RE
1441 ms
153 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(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.z(0) else: # apply multiple controlled z gate qc.append(ZGate().control(n - 1), range(n)) for i in range(n): if not ((l >> i) & 1): qc.x(i) return qc '''
QPC001_B3
AFC5344A7A3BD
10
RE
1368 ms
153 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(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.z(0) else: # apply multiple controlled z gate qc.append(ZGate().control(n - 1), range(n)) for i in range(n): if not ((l >> i) & 1): qc.x(i) return qc '''
QPC001_B3
AFC5344A7A3BD
11
RE
1339 ms
154 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(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.z(0) else: # apply multiple controlled z gate qc.append(ZGate().control(n - 1), range(n)) for i in range(n): if not ((l >> i) & 1): qc.x(i) return qc '''
QPC001_B3
AFC5344A7A3BD
12
RE
1285 ms
153 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(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.z(0) else: # apply multiple controlled z gate qc.append(ZGate().control(n - 1), list(range(n))) for i in range(n): if not ((l >> i) & 1): qc.x(i) return qc '''
QPC001_B3
AFC5344A7A3BD
13
RE
1287 ms
153 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(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.z(0) else: # apply multiple controlled z gate qc.append(ZGate().control(n - 1), range(n)) for i in range(n): if not ((l >> i) & 1): qc.x(i) return qc '''
QPC001_B3
AFD534DE7C675
1
RE
1053 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc = QuantumCircuit(n) qc.append(ZGate().control(n - 1), range(n)) return qc '''
QPC001_B3
AFD534DE7C675
2
RE
1159 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc.append(ZGate().control(n - 1), range(n)) return qc '''
QPC001_B3
AFD534DE7C675
3
RE
1524 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) for bits in range(L): for i in range(n): if (bits>>i)&1: pass else: qc.x(i) qc.append(ZGate().control(n-1), range(n)) for i in range(n): if (bits>>i)&1: pass else: qc.x(i) return qc '''
QPC001_B3
AFD534DE7C675
4
AC
3000 ms
95 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if n == 1: if L == 1: qc.x(0) qc.z(0) qc.x(0) else: qc.z(0) qc.x(0) qc.z(0) qc.x(0) return qc for bits in range(L): for i in range(n): if (bits>>i)&1: pass else: qc.x(i) qc.append(ZGate().control(n-1), range(n)) for i in range(n): if (bits>>i)&1: pass else: qc.x(i) return qc '''
QPC001_B3
AFD6426194F25
1
RE
1210 ms
80 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(L): for d in range(n): if (1<<d) & l == 0: qc.x(d) qc.append(ZGate().control(n-1), range(n)) for d in range(n): if (1<<d) & l == 0: qc.x(d) return qc '''
QPC001_B3
AFD6426194F25
2
WA
1171 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(L): for d in range(n): if (1<<d) & l == 0: qc.x(d) if n > 1: qc.append(ZGate().control(n-1), range(n)) for d in range(n): if (1<<d) & l == 0: qc.x(d) return qc '''
QPC001_B3
AFD6426194F25
3
AC
2181 ms
95 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(L): for d in range(n): if (1<<d) & l == 0: qc.x(d) if n > 1: qc.append(ZGate().control(n-1), range(n)) else: qc.z(0) for d in range(n): if (1<<d) & l == 0: qc.x(d) return qc '''
QPC001_B3
AFE5EC74FECCE
1
AC
2930 ms
93 MiB
'''python from qiskit import QuantumCircuit import math def find_one_positions(n): positions = [] position = 0 while n: if n & 1: positions.append(position) n >>= 1 position += 1 return positions def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # qc.h(0) # qc.cnot(0, 1) # qc.h(1) neg_list = [0]*(2**n) for i in range(L, 2**n): print(neg_list) pos1 = find_one_positions(i) if neg_list[i] == 0: if len(pos1) == 1: qc.p(math.pi, pos1[0]) else: qc.mcp(math.pi, pos1[1:], pos1[0]) for m in range(2**n): pos1m = find_one_positions(m) if all(elem in pos1m for elem in pos1): print(m) neg_list[m] = 1 - neg_list[m] return qc '''
QPC001_B4
A0151CF838AB2
1
RE
1012 ms
91 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: from qiskit.circuit.library import ZGate for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>(j) & 1)): qc.x(j) qc.x(i-1) # i番目の0と1を入れ替えておく.これで1になるものに対し係数の符号を反転させる if(i==n): qc.x(n-1) else: v=[] for k in range(i,n): v.append(k) v.append(i-1) qc.append(ZGate.control(n-i),v) qc.x(i-1) for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>(j) & 1)): qc.x(j) return qc '''
QPC001_B4
A0151CF838AB2
2
WA
892 ms
90 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: from qiskit.circuit.library import ZGate for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>j & 1)): qc.x(j) qc.x(i-1) # i番目の0と1を入れ替えておく.これで1になるものに対し係数の符号を反転させる if(i==n): qc.x(n-1) else: v=[] for k in range(i,n): v.append(k) v.append(i-1) qc.append(ZGate().control(n-i),v) qc.x(i-1) for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>(j) & 1)): qc.x(j) return qc '''
QPC001_B4
A0151CF838AB2
3
WA
887 ms
91 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: from qiskit.circuit.library import ZGate for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>j & 1)): qc.x(j) qc.x(i-1) # i番目の0と1を入れ替えておく.これで1になるものに対し係数の符号を反転させる if(i==n): qc.z(n-1) else: v=[] for k in range(i,n): v.append(k) v.append(i-1) qc.append(ZGate().control(n-i),v) qc.x(i-1) for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>(j) & 1)): qc.x(j) return qc '''
QPC001_B4
A0151CF838AB2
4
AC
1751 ms
94 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: from qiskit.circuit.library import ZGate for i in range(1,n+1):# 第i+1 qubitから第n qubit までbin(L)と一致するような量子ビットb<Lに対して操作を行う if not ((L>>i-1)&1): continue for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>j & 1)): qc.x(j) qc.x(i-1) # i番目の0と1を入れ替えておく.これで1になるものに対し係数の符号を反転させる if(i==n): qc.z(n-1) else: qc.append(ZGate().control(n-i),range(i-1,n)) qc.x(i-1) for j in range(i,n):# bin(L)の第i+1桁からn桁目までのうち0であるものに対しxを作用させる(i+1桁以降が全部1になる) if(not (L>>j & 1)): qc.x(j) return qc '''
QPC001_B4
A01E97158DA31
1
DLE
1481 ms
92 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) for l in range(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.z(0) else: # apply multiple controlled Z gate qc.append(ZGate().control(n - 1), range(n)) for i in range(n): if not ((l >> i) & 1): qc.x(i) return qc '''
QPC001_B4
A01E97158DA31
2
AC
1623 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L:int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): if not (L >> i) & 1: continue for j in range(i + 1, n): if not (L >> j) & 1: qc.x(j) qc.x(i) if i == n - 1: qc.z(i) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1, n): if not (L >> j) & 1: qc.x(j) return qc '''
QPC001_B4
A0318FE244CDA
1
AC
1564 ms
92 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if (L & (1 << (n-1))) == 0: qc.x(n-1) else: qc.x(n-1) qc.z(n-1) qc.x(n-1) for i in range(n-2, -1,-1): b = (L & (1 << i)) if b == 0: qc.x(i) else: data = [] for t in range(n-1,i,-1): data.append(t) qc.x(i) qc.mcp(lam=math.pi,control_qubits=data, target_qubit=i) qc.x(i) for i in range(n): if(L & (1 << i)) == 0: qc.x(i) return qc '''
QPC001_B4
A0392EC280FDD
1
AC
2045 ms
160 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): if L & (1 << i) == 0: continue for j in range(i + 1,n): if L & (1 << j) == 0: qc.x(j) qc.x(i) if i == n - 1: qc.z(i) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1,n): if L & (1 << j) == 0: qc.x(j) return qc '''
QPC001_B4
A054E6D9B04ED
1
AC
2863 ms
185 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: bits = [int(x) for x in f"{L:0{n}b}"] bits.reverse() for i in reversed(range(n)): if bits[i] == 0: continue for j in range(i + 1, n): if bits[j] == 0: qc.x(j) qc.x(i) if i == n - 1: qc.z(i) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1, n): if bits[j] == 0: qc.x(j) return qc '''
QPC001_B4
A0AF5F5F2CE47
1
AC
1789 ms
92 MiB
'''python from qiskit import QuantumCircuit import math # def intlog2_math(v): # return int(math.floor(math.log2(v))) def get_bit(x, y): return 1 if x & int(2 ** y) != 0 else 0 def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: M = n if M == 0: return qc b = get_bit(L, M - 1) if b == 0: qc.x(M - 1) else: qc.x(M - 1) qc.z(M - 1) qc.x(M - 1) for i in list(range(M - 1))[::-1]: b = get_bit(L, i) if b == 0: qc.x(i) else: qc.x(i) qc.h(i) qc.mcx(list(range(i + 1, M)), i) qc.h(i) qc.x(i) for i in range(M): b = get_bit(L, i) if b == 0: qc.x(i) return qc '''
QPC001_B4
A0F7C763A034B
1
AC
2614 ms
183 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: i = 0 while(L > 0) and i < n: r = L % 2 L = L // 2 if(r == 1): if i == n-1: qc.x(i) qc.z(i) qc.x(i) else: qc.x(i) qc.mcp(math.pi, [x for x in range(i+1, n)], i, ctrl_state=L) qc.x(i) i += 1 return qc '''
QPC001_B4
A1333462B19D5
1
AC
1195 ms
92 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if L & (1 << (n - 1)): qc.x(n-1) qc.z(n-1) qc.x(n-1) else: qc.x(n-1) for i in range(n - 2, -1, -1): if L & (1 << (i)) == 0: qc.x(i) else: qc.x(i) qc.h(i) qc.mcx(list(range(i+1,n)), i) qc.h(i) qc.x(i) for i in range(n): if L & (1 << i) == 0: qc.x(i) return qc '''
QPC001_B4
A16408BC857A1
1
RE
1616 ms
154 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: lbit = l.bit_length() for i in range(lbit - 1): for j in range(i + 1, n): qc.x(j) qc.append(ZGate().control(n - 1), range(i, n)) for j in range(i + 1, n): qc.x(j) for i in range(2**lbit, l): for j in range(n): if (i >> j) & 1 == 0: qc.x(j) qc.append(ZGate().control(n - 1), range(n)) for j in range(n): if (i >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A16408BC857A1
2
RE
1578 ms
157 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: lbit = L.bit_length() for i in range(lbit - 1): for j in range(i + 1, n): qc.x(j) qc.append(ZGate().control(n - 1), range(i, n)) for j in range(i + 1, n): qc.x(j) for i in range(2**lbit, L): for j in range(n): if (i >> j) & 1 == 0: qc.x(j) qc.append(ZGate().control(n - 1), range(n)) for j in range(n): if (i >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A16408BC857A1
3
RE
1599 ms
157 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: lbit = L.bit_length() for i in range(lbit - 1): for j in range(i + 1, n): qc.x(j) qc.append(ZGate().control(n - 1), range(i, n)) for j in range(i + 1, n): qc.x(j) for i in range(2**(lbit - 1), L): for j in range(n): if (i >> j) & 1 == 0: qc.x(j) qc.append(ZGate().control(n - 1), range(n)) for j in range(n): if (i >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A16408BC857A1
4
WA
1814 ms
161 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: lbit = L.bit_length() for i in range(lbit - 1): for j in range(i + 1, n): qc.x(j) if i == n - 1: qc.z(n - 1) else: qc.append(ZGate().control(n - i - 1), range(i, n)) for j in range(i + 1, n): qc.x(j) for i in range(2**(lbit - 1), L): for j in range(n): if (i >> j) & 1 == 0: qc.x(j) qc.append(ZGate().control(n - 1), range(n)) for j in range(n): if (i >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A16408BC857A1
5
RE
1722 ms
160 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): qc.x(i) qc.append(ZGate().control(n - 1), range(n)) for i in range(n): qc.x(i) lbit = L.bit_length() for i in range(lbit - 1): for j in range(i + 1, n): qc.x(j) if i == n - 1: qc.z(n - 1) else: print(f'num: {n - i}, c: ({i}, {n})', flush=True) qc.append(ZGate().control(n - i - 1), range(i, n)) for j in range(i + 1, n): qc.x(j) for i in range(2**(lbit - 1), L): for j in range(n): if (i >> j) & 1 == 0: qc.x(j) qc.append(ZGate().control(n - 1), range(n)) for j in range(n): if (i >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A16408BC857A1
6
WA
1961 ms
160 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n == 1: qc.x(0) qc.z(0) qc.x(0) return qc for i in range(n): if (L >> i) & 1 == 0: continue qc.x(i) for j in range(i + 1, n): if (L >> j) & 1 == 0: qc.x(j) if i == n - 1: qc.z(n - 1) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1, n): if (L >> j) & 1 == 0: qc.x(j) for j in range(n): if (L >> j) & 1 == 0: qc.x(j) qc.append(ZGate().control(n - 1), range(n)) for j in range(n): if (L >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A16408BC857A1
7
WA
1929 ms
160 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n == 1: qc.x(0) qc.z(0) qc.x(0) return qc if L == 2**n: qc.z(0) qc.x(0) qc.z(0) qc.x(0) for i in range(n): if (L >> i) & 1 == 0: continue qc.x(i) for j in range(i + 1, n): if (L >> j) & 1 == 0: qc.x(j) if i == n - 1: qc.z(n - 1) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1, n): if (L >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A16408BC857A1
8
AC
1673 ms
161 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if L == 2**n: qc.z(0) qc.x(0) qc.z(0) qc.x(0) return qc if n == 1: qc.x(0) qc.z(0) qc.x(0) return qc for i in range(n): if (L >> i) & 1 == 0: continue qc.x(i) for j in range(i + 1, n): if (L >> j) & 1 == 0: qc.x(j) if i == n - 1: qc.z(n - 1) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1, n): if (L >> j) & 1 == 0: qc.x(j) return qc '''
QPC001_B4
A17E742F07804
1
DLE
2528 ms
94 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # for i in range(n): # qc.h(i) for i in range(L): # 2進数の配列に変換 b = list(map(int, list(format(i, "0"+str(n)+"b")))) print(b) for k in range(n): if b[k] == 0: qc.x(n-k-1) if n == 1: qc.z(0) else: qc.append(ZGate().control(n-1), range(n)) for k in range(n): if b[k] == 0: qc.x(n-k-1) return qc '''
QPC001_B4
A1BF4775FDEFF
1
RE
921 ms
79 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): if not (L >> i) & 1: continue for j in range(i + 1, n): if not (L >> j) & 1: qc.x(j) qc.x(i) if i == n - 1: qc.z(i) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1, n): if not (L >> j) & 1: qc.x(j) return qc '''
QPC001_B4
A1BF4775FDEFF
2
AC
2656 ms
95 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): if not (L >> i) & 1: continue for j in range(i + 1, n): if not (L >> j) & 1: qc.x(j) qc.x(i) if i == n - 1: qc.z(i) else: qc.append(ZGate().control(n - i - 1), range(i, n)) qc.x(i) for j in range(i + 1, n): if not (L >> j) & 1: qc.x(j) return qc '''
QPC001_B4
A1C668DE49944
1
RE
978 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import MCXGate def solve(n: int, L: int) -> QuantumCircuit: numbin = bin(L + (1<<n))[3:] qc = QuantumCircuit(n) numbin = numbin.rstrip("0") print(numbin) if numbin[0]== "1": qc.x(n-1) qc.z(n-1) qc.x(n-1) else: qc.x(n-1) for pos, v in enumerate(numbin[1:]): pos += 1 if v=="0": qc.x(n-1-pos) else: qc.x(n-1-pos) nn = pos + 1 circuit=QuantumCircuit(nn) circuit.h(nn-1) gate = MCXGate(nn-1) circuit.append(gate, range(nn)) circuit.h(nn-1) qc.append(circuit.to_gate(), range(n-1,n-2-pos,-1)) qc.x(n-pos-1) for pos, v in enumerate(numbin): if v == "0": qc.x(n-pos-1) qc = qc.decompose() return qc qc1 = QuantumCircuit(n) qc1.append(qc, range(n-1,-1,-1)) qc1 = qc1.decompose() return qc1 '''
QPC001_B4
A1C668DE49944
2
AC
1654 ms
92 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import MCXGate def solve(n: int, L: int) -> QuantumCircuit: if L == (1<<n): qc = QuantumCircuit(n) return qc numbin = bin(L + (1<<n))[3:] qc = QuantumCircuit(n) numbin = numbin.rstrip("0") print(numbin) if numbin[0]== "1": qc.x(n-1) qc.z(n-1) qc.x(n-1) else: qc.x(n-1) for pos, v in enumerate(numbin[1:]): pos += 1 if v=="0": qc.x(n-1-pos) else: qc.x(n-1-pos) nn = pos + 1 circuit=QuantumCircuit(nn) circuit.h(nn-1) gate = MCXGate(nn-1) circuit.append(gate, range(nn)) circuit.h(nn-1) qc.append(circuit.to_gate(), range(n-1,n-2-pos,-1)) qc.x(n-pos-1) for pos, v in enumerate(numbin): if v == "0": qc.x(n-pos-1) qc = qc.decompose() return qc qc1 = QuantumCircuit(n) qc1.append(qc, range(n-1,-1,-1)) qc1 = qc1.decompose() return qc1 '''
QPC001_B4
A1E502563CE91
1
DLE
1023 ms
90 MiB
'''python from qiskit import QuantumCircuit import math def find_one_positions(n): positions = [] position = 0 while n: if n & 1: positions.append(position) n >>= 1 position += 1 return positions def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: neg_list = [0]*(2**n) for i in range(L, 2**n): print(neg_list) pos1 = find_one_positions(i) if neg_list[i] == 0: if len(pos1) == 1: qc.p(math.pi, pos1[0]) else: qc.mcp(math.pi, pos1[1:], pos1[0]) for m in range(2**n): pos1m = find_one_positions(m) if all(elem in pos1m for elem in pos1): print(m) neg_list[m] = 1 - neg_list[m] return qc '''
QPC001_B4
A1E502563CE91
2
RE
1020 ms
92 MiB
'''python from qiskit import QuantumCircuit import math def find_one_positions(n): """ Returns the positions of '1' bits in the binary representation of a natural number n. The position is counted from the right, starting with 0. """ positions = [] position = 0 while n: if n & 1: positions.append(position) n >>= 1 position += 1 return positions def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc.h(0) qc.cnot(0, 1) qc.h(1) if L == 1: for i in range(0, n, -1): for j in range(i, n): qc.x(j) if i == n - 1: qc.p(math.pi, i) else: qc.mcp(math.pi, list(range(i+1,n)), i) for j in range(i, n): qc.x(j) else: neg_list = [0]*(2**n) for i in range(L, 2**n): print(neg_list) pos1 = find_one_positions(i) if neg_list[i] == 0: if len(pos1) == 1: qc.p(math.pi, pos1[0]) else: qc.mcp(math.pi, pos1[1:], pos1[0]) for m in range(2**n): pos1m = find_one_positions(m) if all(elem in pos1m for elem in pos1): print(m) neg_list[m] = 1 - neg_list[m] return qc '''
QPC001_B4
A1E502563CE91
3
WA
1036 ms
91 MiB
'''python from qiskit import QuantumCircuit import math def find_one_positions(n): """ Returns the positions of '1' bits in the binary representation of a natural number n. The position is counted from the right, starting with 0. """ positions = [] position = 0 while n: if n & 1: positions.append(position) n >>= 1 position += 1 return positions def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if L == 1: for i in range(0, n, -1): for j in range(i, n): qc.x(j) if i == n - 1: qc.p(math.pi, i) else: qc.mcp(math.pi, list(range(i+1,n)), i) for j in range(i, n): qc.x(j) else: neg_list = [0]*(2**n) for i in range(L, 2**n): print(neg_list) pos1 = find_one_positions(i) if neg_list[i] == 0: if len(pos1) == 1: qc.p(math.pi, pos1[0]) else: qc.mcp(math.pi, pos1[1:], pos1[0]) for m in range(2**n): pos1m = find_one_positions(m) if all(elem in pos1m for elem in pos1): print(m) neg_list[m] = 1 - neg_list[m] return qc '''
QPC001_B4
A2BF9B7F0995D
1
DLE
2480 ms
145 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n == 1: for l in range(L): if l & 1 == 0: qc.x(0) qc.z(0) if l & 1 == 0: qc.x(0) return qc for l in range(L): for i in range(n): if (l >> i) & 1 == 0: qc.x(i) qc.append(ZGate().control(n-1), range(n)) for i in range(n): if (l >> i) & 1 == 0: qc.x(i) return qc '''
QPC001_B4
A2CB91A2FEBF0
1
RE
2031 ms
157 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Apply a multi-controlled Z gate for states 0 to L-1 # We will use a series of CNOT gates to create the effect of a multi-controlled Z gate for i in range(L): # Convert i to binary and apply CNOTs binary_representation = format(i, f'0{n}b') # Get binary representation of i with n bits for j in range(n): if binary_representation[j] == '1': qc.x(j) # Apply X gate to flip the qubit to |1> if needed qc.h(n-1) # Apply Hadamard to the last qubit to create superposition qc.mct(list(range(n-1)), n-1) # Apply multi-controlled Toffoli (mct) to flip the last qubit qc.h(n-1) # Apply Hadamard to the last qubit to revert it back for j in range(n): if binary_representation[j] == '1': qc.x(j) # Revert the X gates to restore original state return qc '''
QPC001_B4
A2CB91A2FEBF0
2
AC
2337 ms
160 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library.standard_gates import ZGate def solve(n: int, L: int) -> QuantumCircuit: """ Return an n-qubit circuit that multiplies the amplitudes of the basis states |0>, |1>, …, |L-1> (little-endian integers) by −1 and leaves all other amplitudes unchanged. Global phase is ignored by the judge, so nothing has to be done for L = 0 or L = 2**n. The construction keeps the depth well below the required limit (≤ 50 for n ≤ 10). """ qc = QuantumCircuit(n) total = 1 << n if L == 0 or L == total: # nothing to mark or global phase only return qc # Work from the most-significant bit (n−1) down to the least-significant. # # If the j-th bit of L is 1 we have to flip the sign of all states whose # – more-significant bits equal the prefix of L, and # – bit j equals 0. # # To realise a *positive* multi-controlled phase, every control must be # in state |1>. Hence, before applying the gate # – every control qubit whose prefix bit is 0 is flipped with X, # – the (would-be) target qubit j is also flipped so that the states # with bit j = 0 are mapped to |1>. # After the controlled-Z the temporary X’s are undone. # for j in range(n - 1, -1, -1): # j = MSB … LSB if ((L >> j) & 1) == 0: # skip if that bit of L is 0 continue # More-significant qubits (the prefix) prefix = list(range(j + 1, n)) flipped_prefix = [] # remember X’s to undo later # 1) Bring every “0” of the prefix to “1” (positive control) for k in prefix: if ((L >> k) & 1) == 0: # L’s prefix bit is 0 qc.x(k) flipped_prefix.append(k) # 2) Flip target qubit j so that ‘bit j = 0’ → |1> qc.x(j) # 3) Apply the multi-controlled Z (all controls are now |1>) if prefix: # at least one control mcz = ZGate().control(len(prefix)) # all-ones ctrl_state qc.append(mcz, prefix + [j]) else: # j is the MSB → plain Z qc.z(j) # 4) Undo the X on the target qubit qc.x(j) # 5) Restore the prefix qubits that were flipped for k in flipped_prefix: qc.x(k) return qc '''
QPC001_B4
A310E10829EAA
1
UME
'''python from qiskit import QuantumCircuit from qiskit.circuit.library.standard_gates import XGate, ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) L &= 1 << n def rec(n: int, L: int, control: list[int]): if n == 0: return nonlocal qc i = n - 1 w = 1 << i qc.append(XGate().control(len(control)), list(control) + [i]) rev = L >= w if rev: qc.append(ZGate().control(len(control)), list(control) + [i]) L -= w qc.append(XGate().control(len(control)), list(control) + [i]) rec(i, L, control + [i]) if not rev: qc.append(XGate().control(len(control)), list(control) + [i]) return qc '''
QPC001_B4
A310E10829EAA
2
WA
919 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import XGate, ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) L &= 1 << n def rec(n: int, L: int, control: list[int]): if n == 0: return nonlocal qc i = n - 1 w = 1 << i qc.append(XGate().control(len(control)), list(control) + [i]) rev = L >= w if rev: qc.append(ZGate().control(len(control)), list(control) + [i]) L -= w qc.append(XGate().control(len(control)), list(control) + [i]) rec(i, L, control + [i]) if not rev: qc.append(XGate().control(len(control)), list(control) + [i]) return qc '''
QPC001_B4
A310E10829EAA
3
WA
1035 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import XGate, ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) L &= 1 << n def rec(n: int, L: int, control: list[int]): if n == 0: return nonlocal qc i = n - 1 w = 1 << i def X(): nonlocal qc if control: qc.append(XGate().control(len(control)), list(control) + [i]) else: qc.x(i) def Z(): nonlocal qc if control: qc.append(ZGate().control(len(control)), list(control) + [i]) else: qc.z(i) X() rev = L >= w if rev: Z() L -= w X() rec(i, L, control + [i]) if not rev: X() rec(n, L, []) return qc '''
QPC001_B4
A310E10829EAA
4
AC
2073 ms
94 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import XGate, ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) L %= 1 << n def rec(n: int, L: int, control: list[int]): if n == 0: return nonlocal qc i = n - 1 w = 1 << i def X(): nonlocal qc if control: qc.append(XGate().control(len(control)), list(control) + [i]) else: qc.x(i) def Z(): nonlocal qc if control: qc.append(ZGate().control(len(control)), list(control) + [i]) else: qc.z(i) X() rev = L >= w if rev: Z() L -= w X() rec(i, L, control + [i]) if not rev: X() rec(n, L, []) return qc '''
QPC001_B4
A32E052FC214C
1
RE
912 ms
79 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(L): qc.z(i) return qc '''
QPC001_B4
A3393D91CBB67
1
RE
850 ms
79 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): w = (1 << (n - i - 1)) if L & (1 << i) == 0: controls = [] for k in range(i): controls.append(k) qc.mcp(math.pi, controls, i) qc.x(i) for i in reverse(range(n)): w = (1 << (n - i - 1)) if L & (1 << i) == 0: qc.x(i) return qc '''
QPC001_B4
A3393D91CBB67
2
RE
766 ms
79 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): w = (1 << (n - i - 1)) if L & (1 << i) == 0: controls = [] for k in range(i): controls.append(k) qc.mcp(math.pi, controls, i) qc.x(i) for i in reverse(range(n)): w = (1 << (n - i - 1)) if L & (1 << i) == 0: qc.x(i) return qc '''
QPC001_B4
A3393D91CBB67
3
RE
847 ms
79 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): w = (1 << (n - i - 1)) if L & (1 << i) == 0: controls = [] for k in range(i): controls.append(k) qc.mcp(math.pi, controls, i) qc.x(i) for i in reverse(range(n)): w = (1 << (n - i - 1)) if L & (1 << i) == 0: qc.x(i) return qc '''
QPC001_B4
A3393D91CBB67
4
WA
900 ms
92 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): w = (1 << (n - i - 1)) if L & (1 << i) == 0: controls = [] for k in range(i): controls.append(k) if len(controls) > 0: qc.mcp(math.pi, controls, i) qc.x(i) for i in reversed(range(n)): w = (1 << (n - i - 1)) if L & (1 << i) == 0: qc.x(i) return qc '''
QPC001_B4
A3393D91CBB67
5
WA
954 ms
90 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): if L & (1 << i) == 0: controls = [] for k in range(i): controls.append(n-k-1) if len(controls) > 0: qc.mcp(math.pi, controls, n-i-1) qc.x(n-i-1) for i in reversed(range(n)): if L & (1 << i) == 0: qc.x(n-i-1) return qc '''
QPC001_B4
A3393D91CBB67
6
WA
1054 ms
91 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): if L & (1 << i) == 0: controls = [] for k in range(i): controls.append(k) if len(controls) > 0: qc.mcp(math.pi, controls, i) else: qc.z(i) qc.x(i) for i in reversed(range(n)): if L & (1 << i) == 0: qc.x(i) return qc '''
QPC001_B4
A3393D91CBB67
7
WA
945 ms
92 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): if L & (1 << i) == 0: controls = [] for k in range(i): controls.append(n-k-1) if len(controls) > 0: qc.mcp(math.pi, controls, n-i-1) else: qc.z(n-i-1) qc.x(n-i-1) for i in reversed(range(n)): if L & (1 << i) == 0: qc.x(n-i-1) return qc '''
QPC001_B4
A3393D91CBB67
8
WA
940 ms
91 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # グローバル位相は無視して良いので、全部ひっくり返す場合は何もしない if L == 1 << n: return qc # 引く L2 = (1 << n) - L print("L2 = ", L2) for i in range(n): if L2 & (1 << (n - i - 1)) != 0: print("i = ", i) controls = [] for k in range(i): controls.append(k) if len(controls) > 0: qc.mcp(math.pi, controls, i) else: qc.z(i) qc.x(i) for i in reversed(range(n)): if L2 & (1 << (n - i - 1)) != 0: qc.x(i) return qc '''
QPC001_B4
A3393D91CBB67
9
AC
2880 ms
92 MiB
'''python from qiskit import QuantumCircuit import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # グローバル位相は無視して良いので、全部ひっくり返す場合は何もしない if L == 1 << n: return qc # 引く L2 = (1 << n) - L print("L2 = ", L2) for i in range(n): if L2 & (1 << (n - i - 1)) != 0: print("i = ", i) controls = [] for k in range(i): controls.append(n-1-k) if len(controls) > 0: qc.mcp(math.pi, controls, n-1-i) else: qc.z(n-1-i) qc.x(n-1-i) for i in reversed(range(n)): if L2 & (1 << (n - i - 1)) != 0: qc.x(n-1-i) return qc '''
QPC001_B4
A35D91246E218
1
WA
861 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) def f(l: int, r: int, d: int): # qc.barrier() # print(l, r, d, li) if d < 0 or r - l == 0: return N = 2 ** d if r - l < N: li.append(d) f(l, r, d - 1) else: if d == n - 1: qc.x(d) qc.z(d) qc.x(d) f(l + N, r, d - 1) else: if len(li) > 0: qc.x(li) qc.x(d) qc.append(ZGate().control(n - d - 1), range(d, n)) if len(li) > 0: qc.x(li) qc.x(d) f(l + N, r, d - 1) li = list() f(0, L, n - 1) return qc '''
QPC001_B4
A35D91246E218
2
AC
2800 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) def f(l: int, r: int, d: int): # qc.barrier() # print(l, r, d, li) if d < 0 or r - l == 0: return N = 2 ** d if r - l < N: li.append(d) f(l, r, d - 1) else: if d == n - 1: qc.x(d) qc.z(d) qc.x(d) f(l + N, r, d - 1) else: if len(li) > 0: qc.x(li) qc.x(d) qc.append(ZGate().control(n - d - 1), range(d, n)) if len(li) > 0: qc.x(li) qc.x(d) f(l + N, r, d - 1) li = list() f(0, L, n - 1) # |1111>を反転する if 2 ** n == L: if n == 1: qc.z(0) else: qc.append(ZGate().control(n - 1), range(n)) return qc '''
QPC001_B4
A369A87CB4565
1
RE
1528 ms
140 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: theta = 2*math.pi for i in range(L-1): qc.ry(theta, i) return qc '''
QPC001_B4
A369A87CB4565
2
DLE
1619 ms
145 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for l in range(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.z(0) else: # apply multiple controlled Z gate qc.append(ZGate().control(n - 1), range(n)) for i in range(n): if not ((l >> i) & 1): qc.x(i) return qc '''
QPC001_B4
A36AA47D286E8
1
WA
2095 ms
96 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n == 1: qc.z(0) return qc mask = 2 ** (n - 1) - 1 qc.x(n-1) for l in range(L): if l >> (n - 1) != 0: qc.x(n-1) qc.append(ZGate().control(n-1, None, l & mask), range(n)) if l >> (n - 1) != 0: qc.x(n-1) qc.x(n-1) return qc '''
QPC001_B4
A36AA47D286E8
2
RE
963 ms
80 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if L == 1: return qc if n == 1: qc.z(0) return qc L -= 1 for i in reversed(range(n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): ctrl |= 1 << (n - 1 - j) qc.append(ZGate().control(n - 1 - i, None, ctrl), reversed(range(i, n))) qc.x(i) qc.append(ZGate().control(n - 1 - i, None, ctrl), reversed(range(i, n))) qc.x(i) return qc '''
QPC001_B4
A36AA47D286E8
3
WA
1034 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if L == 1: return qc if n == 1: qc.z(0) return qc L -= 1 for i in reversed(range(1, n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): ctrl |= 1 << (n - 1 - j) print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}") qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) return qc '''
QPC001_B4
A36AA47D286E8
4
WA
1054 ms
92 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if L == 1: return qc if n == 1: qc.z(0) return qc L -= 1 for i in reversed(range(1, n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) print( f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}") qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) ctrl = 0 for j in range(1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) if L & 1 != 0: qc.x(0) qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) if L & 1 != 0: qc.x(0) return qc '''
QPC001_B4
A36AA47D286E8
5
WA
848 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if L == 1: return qc if n == 1: qc.z(0) return qc L -= 1 for i in reversed(range(1, n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) print( f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}") qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) ctrl = 0 for j in range(1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) if L & 1 == 0: qc.x(0) print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) if L & 1 == 0: qc.x(0) return qc '''
QPC001_B4
A36AA47D286E8
6
RE
1337 ms
92 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc.h(range(n)) if L == 1: qc.x(n-1) qc.append(ZGate().control(n-1, None, 0), range(n)) qc.x(n-1) return qc if n == 1: qc.z(0) return qc L -= 1 for i in reversed(range(1, n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) print( f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}, target={i - 1}") qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) ctrl = 0 for j in range(1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) qc.x(0) print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) qc.x(0) if L & 1 != 0: print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) return qc '''
QPC001_B4
A36AA47D286E8
7
WA
1269 ms
92 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate # from qiskit import Aer, transpile, execute def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n == 1: qc.z(0) return qc if L == 1: qc.x(n-1) qc.append(ZGate().control(n-1, None, 0), range(n)) qc.x(n-1) return qc L -= 1 for i in reversed(range(1, n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) print( f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}, target={i - 1}") qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) ctrl = 0 for j in range(1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) qc.x(0) print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) qc.x(0) if L & 1 != 0: print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) return qc '''
QPC001_B4
A36AA47D286E8
8
WA
1076 ms
92 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate # from qiskit import Aer, transpile, execute def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n == 1: qc.z(0) return qc if L == 1: qc.x(n-1) qc.append(ZGate().control(n-1, None, 0), range(n)) qc.x(n-1) return qc L -= 1 for i in reversed(range(1, n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) print( f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}, target={i - 1}") qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) ctrl = 0 for j in range(1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) qc.x(0) print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) qc.x(0) if L & 1 != 0: print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) return qc '''
QPC001_B4
A36AA47D286E8
9
AC
2465 ms
95 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate # from qiskit import Aer, transpile, execute def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n == 1: qc.x(0) qc.z(0) qc.x(0) if L == 2: qc.z(0) return qc if L == 1: qc.x(n-1) qc.append(ZGate().control(n-1, None, 0), range(n)) qc.x(n-1) return qc L -= 1 for i in reversed(range(1, n)): if L & (1 << i) == 0: continue ctrl = 0 for j in range(i + 1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) print( f"nqubits={n-i}, ctrl={ctrl}, qubits={list(reversed(range(i - 1, n)))}, target={i - 1}") qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) qc.append(ZGate().control(n - i, None, ctrl), reversed(range(i - 1, n))) qc.x(i - 1) ctrl = 0 for j in range(1, n): if L & (1 << j) != 0: ctrl |= 1 << (n - 1 - j) qc.x(0) print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) qc.x(0) if L & 1 != 0: print( f"nqubits={n-1}, ctrl={ctrl}, qubits={list(reversed(range(n)))}") qc.append(ZGate().control(n-1, None, ctrl), reversed(range(n))) return qc '''
QPC001_B4
A37982A0CBE93
1
AC
2839 ms
161 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) #For each group for i in range(n): #if ith bit of l is 0, no ZGate if not ((L >> i) & 1): continue #subset[i] < l[i] so subset[i] == 0 qc.x(i) #get ready for CZGate for j in range(i+1, n): #if j (>i) th bit of l is 0, flip bit if not ((L >> j) & 1): qc.x(j) #For last subset which has no control bit if i == n - 1: qc.z(i) else: #Apply CZGate that is in the subset qc.append(ZGate().control(n - i - 1), range(i, n)) #Reverse the preparation qc.x(i) for j in range(i+1, n): #if j (>i) th bit of l is 0, flip bit if not ((L >> j) & 1): qc.x(j) return qc '''
QPC001_B4
A3E1BF81F4F76
1
AC
1662 ms
92 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import MCPhaseGate import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if (L>>(n-1))%2==0: qc.x(n-1) else: qc.x(n-1) qc.z(n-1) qc.x(n-1) for i in range(n-2,-1,-1): if (L>>i)%2==0: qc.x(i) else: qc.x(i) qc.mcp(math.pi, list(range(i+1,n)), i) qc.x(i) for i in range(n): if (L>>i)%2==0: qc.x(i) return qc '''
QPC001_B4
A42D43648C5C1
1
WA
1333 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if L == (1 << n): for i in range(n): qc.z(i) else: for i in range(n): qc.x(i) for i in range(n - 1, -1, -1): if ((L >> i) & 1) == 1: print(i) if i == n - 1: qc.z(n - 1) else: qc.append(ZGate().control(n - 1 - i), range(i, n)) qc.x(i) for i in range(n): if ((L >> i) & 1) == 0: qc.x(i) return qc '''
QPC001_B4
A42D43648C5C1
2
WA
1025 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if L == (1 << n): for i in range(n): qc.z(i) else: for i in range(n): qc.x(i) for i in range(n - 1, -1, -1): if ((L >> i) & 1) == 1: if i == n - 1: qc.z(n - 1) else: qc.append(ZGate().control(n - 1 - i), range(i, n)) qc.x(i) for i in range(n): if ((L >> i) & 1) == 0: qc.x(i) return qc '''
QPC001_B4
A42D43648C5C1
3
AC
2033 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if L < (1 << n): for i in range(n): qc.x(i) for i in range(n - 1, -1, -1): if ((L >> i) & 1) == 1: if i == n - 1: qc.z(n - 1) else: qc.append(ZGate().control(n - 1 - i), range(i, n)) qc.x(i) for i in range(n): if ((L >> i) & 1) == 0: qc.x(i) return qc '''
QPC001_B4
A43201A5E45AD
1
AC
2047 ms
92 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if n<3: for l in range(L): # print(l) # i=0 for i in range(n): if not (1<<i)&l: qc.x(i) # print("i=",i) if n>1: qc.h(0) qc.mcx(list(range(1,n)),0) qc.h(0) else: qc.z(0) for i in range(n): if not (1<<i)&l: qc.x(i) return qc l=L-1 for i in range(n): if not (1<<i)&l: qc.x(i) # print("i=",i) qc.h(0) qc.mcx(list(range(1,n)),0) qc.h(0) for i in range(n): if not (1<<i)&l: qc.x(i) j=0 while j<n: print("l1=",l) print("j1=",j) if l%2==0: l=l//2 j+=1 continue l=l-1 # j+=1 print("l=",l) print("j=",j) for i in range(n-j): if not (1<<i)&l: qc.x(i+j) # print("i=",i) print(n,j) if j+1<n: qc.h(j) print(list(range(j+1,n)),j) qc.mcx(list(range(j+1,n)),j) qc.h(j) else: qc.z(j) for i in range(n-j): if not (1<<i)&l: qc.x(i+j) # print("i=",i) return qc '''
QPC001_B4
A43BC94A4695C
1
RE
1022 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n): if (L>>i)&1==0: continue for j in range(i,n): if (L>>j)&1==0: qc.x(j) if n==1: qc.z(0) else: qc.append(ZGate().control(n-1),range(i,n)) for j in range(i,n): if (L>>j)&1==0: qc.x(j) return qc '''
QPC001_B4
A43BC94A4695C
2
RE
1094 ms
91 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: from qiskit.circuit.library import ZGate for i in range(n): if (L>>i)&1==0: continue for j in range(i,n): if (L>>j)&1==0: qc.x(j) if n==1: qc.z(0) else: qc.append(ZGate().control(n-1),range(i,n)) for j in range(i,n): if (L>>j)&1==0: qc.x(j) return qc '''
QPC001_B4
A4423F6FAD84D
1
WA
922 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] if n != 1: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) # if ctrlarg[i-1] == '1': # qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) # if ctrlarg[i-1] == '1': # qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) else: if L == 2: qc.z(0) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
2
WA
828 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] if n != 1: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: if L == 2: qc.z(0) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
3
WA
1017 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] if n != 1: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) qc.h(range(n)) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: if L == 2: qc.z(0) for i in range(n//2): qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
4
WA
991 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] if n != 1: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: if L == 2: qc.z(0) for i in range(n//2): qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
5
WA
1169 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] if n != 1 and 2**n != L: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break elif 2**n == L: qc.x(0) qc.x(0) else: if L == 2: qc.z(0) # for i in range(n//2): # qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
6
WA
992 ms
90 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] if n != 1 and 2**n != L: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) #qc.h(range(n)) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break elif 2**n == L: qc.x(0) qc.x(0) else: if L == 2: qc.z(0) for i in range(n//2): qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
7
WA
1772 ms
94 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] for i in range(n//2): qc.swap(i, n - 1 - i) if n != 1 and 2**n != L: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break elif 2**n == L: qc.x(0) qc.x(0) else: if L == 2: qc.z(0) for i in range(n//2): qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
8
WA
1145 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] if n != 1 and 2**n != L: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(n//2): qc.swap(i, n - 1 - i) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break for i in range(n//2): qc.swap(i, n - 1 - i) elif 2**n == L and n != 1: qc.x(0) qc.x(0) else: if L == 2: qc.z(0) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
9
WA
958 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] for i in range(n//2): qc.swap(i, n - 1 - i) if n != 1 and 2**n != L: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break elif 2**n == L and n != 1: qc.x(0) qc.x(0) else: if L == 2: qc.z(0) for i in range(n//2): qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
10
WA
1090 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] for i in range(n//2): qc.swap(i, n - 1 - i) if n != 1 and 2**n != L: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break elif 2**n == L and n != 1: qc.x(0) qc.x(0) else: if L == 2: qc.z(0) for i in range(n//2): qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4423F6FAD84D
11
AC
2216 ms
94 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate, MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) ctrlarg = [] for i in range(n//2): qc.swap(i, n - 1 - i) if n != 1 and 2**n != L: strL = bin(L) strL = strL[2:] while len(strL) < n: strL = '0' + strL print(strL) for i in range(len(strL)): if i > 0: print(i) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) qc.append(ZGate().control(i), qargs=[j for j in range(i+1)]) for ind in range(len(ctrlarg)): if ctrlarg[ind] == '0': qc.x(ind) if ctrlarg[i-1] == '1': qc.x(i) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break else: print(i) qc.z(0) ctrlarg.append(strL[i]) if '1' not in strL[i+1:]: break elif 2**n == L and n != 1: qc.x(0) qc.x(0) else: if L == 1: qc.z(0) for i in range(n//2): qc.swap(i, n - 1 - i) # Write your code here: return qc '''
QPC001_B4
A4437D341C2C2
1
RE
825 ms
79 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: l=[] for i in range(n-1,-1,-1): l.append(i) if (1<<i)<=L: qc.x(i) qc.append(GlobalPhaseGate(phase=math.pi).control(len(l)), l) qc.x(i) L-=(1<<i) return qc '''
QPC001_B4
A4437D341C2C2
2
WA
1181 ms
91 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import GlobalPhaseGate import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: l=[] for i in range(n-1,-1,-1): l.append(i) if (1<<i)<=L: qc.x(i) qc.append(GlobalPhaseGate(phase=math.pi).control(len(l)), l) qc.x(i) L-=(1<<i) return qc '''
QPC001_B4
A4437D341C2C2
3
AC
1836 ms
94 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import GlobalPhaseGate import math def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: l=[] t=[] for i in range(n-1,-1,-1): j=i l.append(j) if (1<<i)>=L: qc.append(GlobalPhaseGate(phase=math.pi).control(len(l)), l) qc.x(j) t.append(j) else: L-=(1<<i) for j in t: qc.x(j) return qc '''
QPC001_B4
A481C9149C381
1
WA
1212 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if n == 1: qc.x(0) qc.z(0) qc.x(0) if L == 2: qc.z(0) return qc flip = list() for i in reversed(range(n)): flip.append( L // 2**(i) ) L = L % 2**(i) print(flip) if flip[0]: qc.x(n-1) qc.z(n-1) qc.x(n-1) for i in range(1,n): if flip[i]: for j in range(i-1): if not flip[j]: qc.x(n-1-j) qc.x(n-1-i) qc.append(ZGate().control(i), range(n-1-i,n)) for j in range(i-1): if not flip[j]: qc.x(n-1-j) qc.x(n-1-i) return qc '''
QPC001_B4
A481C9149C381
2
WA
1134 ms
93 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if n == 1: qc.x(0) qc.z(0) qc.x(0) if L == 2: qc.z(0) return qc if L == 2**n: return qc flip = list() for i in reversed(range(n)): flip.append( L // 2**(i) ) L = L % 2**(i) print(flip) if flip[0]: qc.x(n-1) qc.z(n-1) qc.x(n-1) for i in range(1,n): if flip[i]: for j in range(i-1): if not flip[j]: qc.x(n-1-j) qc.x(n-1-i) qc.append(ZGate().control(i), range(n-1-i,n)) for j in range(i-1): if not flip[j]: qc.x(n-1-j) qc.x(n-1-i) return qc '''
QPC001_B4
A481C9149C381
3
AC
2196 ms
94 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) if n == 1: qc.x(0) qc.z(0) qc.x(0) if L == 2: qc.z(0) return qc if L == 2**n: return qc flip = list() for i in reversed(range(n)): flip.append( L // 2**(i) ) L = L % 2**(i) print(flip) if flip[0]: qc.x(n-1) qc.z(n-1) qc.x(n-1) for i in range(1,n): if flip[i]: for j in range(i): if not flip[j]: qc.x(n-1-j) qc.x(n-1-i) qc.append(ZGate().control(i), range(n-1-i,n)) for j in range(i): if not flip[j]: qc.x(n-1-j) qc.x(n-1-i) return qc '''
QPC001_B4
A49C174C35D20
1
RE
888 ms
79 MiB
'''python from qiskit import QuantumCircuit def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n - 1, -1, -1): if (((L - 1) >> i) & 1): qc.x(i) if i == n - 1: qc.z(i) else: controls = list(range(i + 1, n)) cz_gate = ZGate().control(len(controls)) qc.append(cz_gate, controls + [i]) qc.x(i) else: qc.x(i) controls = list(range(1, n)) cz_gate = ZGate().control(len(controls)) qc.append(cz_gate, controls + [0]) for i in range(n - 1, -1, -1): if not (((L - 1) >> i) & 1): qc.x(i) return qc '''
QPC001_B4
A49C174C35D20
2
RE
740 ms
80 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n - 1, -1, -1): if (((L - 1) >> i) & 1): qc.x(i) if i == n - 1: qc.z(i) else: controls = list(range(i + 1, n)) cz_gate = ZGate().control(len(controls)) qc.append(cz_gate, controls + [i]) qc.x(i) else: qc.x(i) controls = list(range(1, n)) cz_gate = ZGate().control(len(controls)) qc.append(cz_gate, controls + [0]) for i in range(n - 1, -1, -1): if not (((L - 1) >> i) & 1): qc.x(i) return qc '''
QPC001_B4
A49C174C35D20
3
AC
3000 ms
95 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import ZGate def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: for i in range(n - 1, -1, -1): if (((L - 1) >> i) & 1): qc.x(i) if i == n - 1: qc.z(i) else: controls = list(range(i + 1, n)) cz_gate = ZGate().control(len(controls)) qc.append(cz_gate, controls + [i]) qc.x(i) else: qc.x(i) if n > 1: controls = list(range(1, n)) cz_gate = ZGate().control(len(controls)) qc.append(cz_gate, controls + [0]) else: qc.z(0) for i in range(n - 1, -1, -1): if not (((L - 1) >> i) & 1): qc.x(i) return qc '''
QPC001_B4
A4B7DB3F01EC6
1
WA
993 ms
91 MiB
'''python from qiskit import QuantumCircuit import math from qiskit.circuit.library import MCMT def solve(n: int, L: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): qc.h(i) for i in range(n-1,-1,-1): lst = [] for j in range(i+1,n): lst.append(j) if (L>>j)&1: pass else: qc.x(j) if len(lst)==0: if (L>>i)&1: qc.x(i) qc.z(i) qc.x(i) else: if (L>>i)&1: qc.x(i) qc.append(MCMT('z', len(lst), 1), lst+[i]) qc.x(i) for j in range(i+1,n): lst.append(j) if (L>>j)&1: pass else: qc.x(j) return qc '''