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 |
|---|---|---|---|---|---|---|
QPC005_B2 | A6E6E157055EB | 3 | AC | 2058 ms | 145 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate
def A3(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
# Write your code here:
qc.x(m)
qc.x(k)
qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
qc.x(k)
return qc
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
qc = QuantumCircuit(a, b, c)
# Write your code here:
qc.x(c)
if m_left:
qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
if k_left % 2:
qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
qc.x(c)
for i in range(n - 1):
qc.compose(RZGate(2 ** (2 - n + i) * k_left * math.pi).control(1), [c[i], a[0]], inplace=True)
if m_left:
qc.compose(A3(n - 1), [a[0], *c], inplace=True)
return qc
''' |
QPC005_B2 | A77AC6C9BCE6D | 1 | WA | 1651 ms | 143 MiB | '''python
from qiskit import QuantumCircuit
import math
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
for i in range(1, n + 1):
angle = 2 * math.pi / (2**n) * k_left * (2**(i - 1))
qc.p(angle, i)
qc.cp(-angle * 2, 0, i)
angle = 2 * math.pi / 2 * m_left
qc.p(angle, 0)
return qc
''' |
QPC005_B2 | A77AC6C9BCE6D | 2 | WA | 1613 ms | 142 MiB | '''python
from qiskit import QuantumCircuit
import math
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
for i in range(1, n + 1):
angle = 2 * math.pi / (2**n) * k_left * (2**(i - 1))
qc.p(-angle, i)
qc.cp(angle * 2, 0, i)
angle = 2 * math.pi / 2 * m_left
qc.p(angle, 0)
return qc
''' |
QPC005_B2 | A77AC6C9BCE6D | 3 | WA | 1603 ms | 142 MiB | '''python
from qiskit import QuantumCircuit
import math
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
angle = 2 * math.pi / 2 * m_left
qc.p(angle, 0)
for i in range(1, n + 1):
angle = 2 * math.pi / (2**n) * k_left * (2**(i - 1))
qc.p(angle, i)
qc.cp(-angle * 2, 0, i)
return qc
''' |
QPC005_B2 | A77FC0E7E5F60 | 1 | RE | 1396 ms | 141 MiB | '''python
from qiskit import QuantumCircuit
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
if m_left == 1:
qc.x(m[0])
# 2. kレジスタにk_left加算 or -k_left加算(m制御)
if k_left != 0:
# m = 0 のとき加算, m = 1 のとき減算
# a. m=0: kに+k_left
qc.mcx([m[0]], k[0]) # m[0]=1のときのみ作用
for i in range(n):
if (k_left >> i) & 1:
qc.cx(m[0], k[i])
qc.mcx([m[0]], k[0]) # 戻す
# b. m=1: kに-k_left(= 2^n - k_left加算)
# (m[0]=1 の時にだけk_leftを差し引く加算を行う)
for i in range(n):
if (k_left >> i) & 1:
qc.cx(m[0], k[i])
return qc
''' |
QPC005_B2 | A77FC0E7E5F60 | 2 | WA | 1670 ms | 142 MiB | '''python
from qiskit import QuantumCircuit
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
m = n # mは最上位ビット、kは下位nビット
# 1. mレジスタのXOR(CNOT)
if m_left == 1:
qc.x(m)
# 2. kレジスタに制御加算・減算
# まず m==0 のとき加算 (Xで反転して制御→加算→戻す)
qc.x(m)
for i in range(n):
if (k_left >> i) & 1:
qc.cx(m, i)
qc.x(m)
# 次に m==1 のとき減算 (そのまま加算: -k_left = 2^n - k_left)
if k_left != 0:
for i in range(n):
if (k_left >> i) & 1:
qc.cx(m, i)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 1 | UME | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
def qft(n: int) -> QuantumCircuit:
qc = QuantumCircuit(n)
for i in reversed(range(n)):
qc.h(i)
for j in reversed(range(i)):
qc.cp(pi / 2 ** (i - j), j, i)
for i in range(n // 2):
qc.swap(i, n - i - 1)
return qc
import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFT
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
k_qubits = list(range(n))
m_qubit = n
qc.compose(qft(n), k_qubits, inplace=True)
for i in range(n):
angle = (2 * np.pi * k_left) / (2**(i + 1))
if angle != 0:
qc.p(angle, k_qubits[i])
qc.compose(qft(n).inverse(), k_qubits, inplace=True)
qc.compose(qft(n), k_qubits, inplace=True)
for i in range(n):
angle = -(2 * np.pi * (2 * k_left)) / (2**(i + 1))
if angle != 0:
qc.cp(angle, m_qubit, k_qubits[i])
qc.compose(qft(n).inverse(), k_qubits, inplace=True)
if m_left == 1:
qc.x(m_qubit)
return qc
''' | ||
QPC005_B2 | A7BAA68F1802F | 2 | WA | 1566 ms | 142 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
def qft(n: int) -> QuantumCircuit:
qc = QuantumCircuit(n)
for i in reversed(range(n)):
qc.h(i)
for j in reversed(range(i)):
qc.cp(pi / 2 ** (i - j), j, i)
for i in range(n // 2):
qc.swap(i, n - i - 1)
return qc
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
k_qubits = list(range(n))
m_qubit = n
qc.compose(qft(n), k_qubits, inplace=True)
for i in range(n):
angle = (2 * np.pi * k_left) / (2**(i + 1))
if angle != 0:
qc.p(angle, k_qubits[i])
qc.compose(qft(n).inverse(), k_qubits, inplace=True)
qc.compose(qft(n), k_qubits, inplace=True)
for i in range(n):
angle = -(2 * np.pi * (2 * k_left)) / (2**(i + 1))
if angle != 0:
qc.cp(angle, m_qubit, k_qubits[i])
qc.compose(qft(n).inverse(), k_qubits, inplace=True)
if m_left == 1:
qc.x(m_qubit)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 3 | RE | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RzGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
t=2 *pi / 2**(n-1)
qc.rz(t,1)
# Write your code here:
for i in range(2:n+1):
qc.x(0)
qc.mcx(list(range(2:n+1)),0)
qrz = RzGate(2*pi - t).control(n-1)
qc.append(qrz,qc[2:n+1],qc[0:2])
for i in range(2:n+1):
qc.x(0)
return qc
''' | ||
QPC005_B2 | A7BAA68F1802F | 4 | RE | 1556 ms | 141 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
t=2 *pi / 2**(n-1)
qc.rz(t,1)
# Write your code here:
for i in range(2,n+1):
qc.x(0)
qc.mcx(list(range(2,n+1)),0)
qrz = RzGate(2*pi - t).control(n-1)
qc.append(qrz,qc[2:n+1],qc[0:2])
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 5 | RE | 1495 ms | 140 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
t=2 *pi / 2**(n-1)
qc.rz(t,1)
# Write your code here:
for i in range(2,n+1):
qc.x(0)
qc.mcx(list(range(2,n+1)),0)
qrz = RZGate(2*pi - t).control(n-1)
qc.append(qrz,qc[2:n+1],qc[0:2])
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 6 | RE | 1490 ms | 140 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
t=2 *pi / 2**(n-1)
qc.rz(t,1)
# Write your code here:
for i in range(2,n+1):
qc.x(0)
qc.mcx(list(range(2,n+1)),0)
qrz = RZGate(2*pi - t).control(n-1)
qc.append(qrz,qc[2:n+1]+qc[0:2])
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 7 | WA | 1964 ms | 143 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
t=2 *pi *k_left / 2**(n-1)
qc.rz(t,1)
# Write your code here:
for i in range(2,n+1):
qc.x(0)
qc.mcx(list(range(2,n+1)),0)
qrz = RZGate(2*pi - t).control(n-1)
control_qubits = list(range(2, n + 1))
target_qubit = 1
qc.append(qrz, control_qubits + [target_qubit])
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 8 | RE | 1467 ms | 141 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
for i in range(2,n+1):
t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
qc.crz(t,i,1)
qc.append(qrz, control_qubits + [target_qubit])
# Write your code here:
for i in range(2,n+1):
qc.x(0)
qc.mcx(list(range(2,n+1)),0)
qc.append(qrz, control_qubits + [target_qubit])
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 9 | RE | 1460 ms | 141 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
for i in range(2,n+1):
t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
qc.crz(t,i,1)
qc.append(qrz, control_qubits + [target_qubit])
# Write your code here:
for i in range(2,n+1):
qc.x(0)
qc.mcx(list(range(2,n+1)),0)
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 10 | WA | 1610 ms | 142 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
for i in range(2,n+1):
t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
qc.crz(t,i,1)
# Write your code here:
for i in range(2,n+1):
qc.x(0)
qc.mcx(list(range(2,n+1)),0)
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | A7BAA68F1802F | 11 | WA | 1669 ms | 142 MiB | '''python
from math import ceil,floor,acos,asin,atan,sqrt,pi,gcd,sin,cos,tan,log2
import numpy as np
from qiskit import QuantumCircuit,QuantumRegister
from qiskit.circuit.library import RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left==1:
qc.x(0)
for i in range(2,n+1):
t=2 *pi *k_left * 2**(i-2) / 2**(n-1)
qc.crz(t,i,1)
# Write your code here:
for i in range(2,n+1):
qc.x(0)
if m_left==1:
qc.mcx(list(range(2,n+1)),0)
for i in range(2,n+1):
qc.x(0)
return qc
''' |
QPC005_B2 | AAC0A0DA56508 | 1 | WA | 1652 ms | 143 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate, XGate, HGate, SwapGate, RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
qc.cx(0, 1)
# now qubit 1 is exactly m
qc.cx(1, 0)
###
# rotate qubit 0 by (-1)^m c k_left
for i in range(n - 1):
theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
qc.append(RZGate(-theta).control(1), [i + 2, 0])
qc.append(RZGate(theta * 2).control(2), [i + 2, 1, 0])
if m_left == 1:
qc.x(1)
###
qc.cx(1, 0)
qc.cx(0, 1)
return qc
''' |
QPC005_B2 | AAC0A0DA56508 | 2 | WA | 1724 ms | 145 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate, XGate, HGate, SwapGate, RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
qc.cx(0, 1)
# now qubit 1 is exactly m
qc.cx(1, 0)
###
# rotate qubit 0 by (-1)^m c k_left
for i in range(n - 1):
theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
qc.append(RZGate(-theta).control(1), [i + 2, 0])
qc.append(RZGate(theta * 2).control(2), [i + 2, 1, 0])
if m_left == 1:
qc.x(range(2, n + 1))
qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
###
qc.cx(1, 0)
qc.cx(0, 1)
qc.x(range(2, n + 1))
if m_left == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
if k_left % 2 == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
return qc
''' |
QPC005_B2 | AAC0A0DA56508 | 3 | WA | 1536 ms | 145 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate, XGate, HGate, SwapGate, RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
qc.cx(0, 1)
# now qubit 1 is exactly m
qc.cx(1, 0)
###
# rotate qubit 0 by (-1)^m c k_left
for i in range(n - 1):
theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
qc.append(RZGate(theta).control(1), [i + 2, 0])
qc.append(RZGate(-theta * 2).control(2), [i + 2, 1, 0])
if m_left == 1:
qc.x(range(2, n + 1))
qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
###
qc.cx(1, 0)
qc.cx(0, 1)
qc.x(range(2, n + 1))
if m_left == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
if k_left % 2 == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
return qc
''' |
QPC005_B2 | AAC0A0DA56508 | 4 | WA | 1627 ms | 142 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate, XGate, HGate, SwapGate, RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
qc.cx(0, 1)
# now qubit 1 is exactly m
qc.cx(1, 0)
###
# rotate qubit 0 by (-1)^m c k_left
for i in range(n - 1):
theta = 2 * math.pi * k_left * 2 ** i / 2 ** (n - 1)
qc.append(RZGate(-theta).control(2), [i + 2, 1, 0])
qc.x(1)
qc.append(RZGate(theta).control(2), [i + 2, 1, 0])
qc.x(1)
if m_left == 1:
qc.x(range(2, n + 1))
qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
###
qc.cx(1, 0)
qc.cx(0, 1)
qc.x(range(2, n + 1))
if m_left == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
if k_left % 2 == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
return qc
''' |
QPC005_B2 | AAC0A0DA56508 | 5 | RE | 1498 ms | 141 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate, XGate, HGate, SwapGate, RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
qc.cx(0, 1)
# now qubit 1 is exactly m
qc.cx(1, 0)
###
# rotate qubit 0 by (-1)^m c k_left
for i in range(n - 1):
theta = 2 * math.pi * k_left * 2 ** i / 2 ** n
qc.append(RZGate(-theta).control(2), [i + 2, 1, 0])
# qc.x(1)
qc.append(RZGate(theta * 2).control(2), [i + 2, 0])
# qc.x(1)
# apply X^m_left
if m_left == 1:
qc.x(range(2, n + 1))
qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
###
qc.cx(1, 0)
qc.cx(0, 1)
qc.x(range(2, n + 1))
if m_left == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
if k_left % 2 == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
return qc
''' |
QPC005_B2 | AAC0A0DA56508 | 6 | WA | 1631 ms | 146 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate, XGate, HGate, SwapGate, RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
qc.cx(0, 1)
# now qubit 1 is exactly m
qc.cx(1, 0)
###
# rotate qubit 0 by (-1)^m c k_left
for i in range(n - 1):
theta = 2 * math.pi * k_left * 2 ** i / 2 ** n
qc.append(RZGate(-theta).control(2), [i + 2, 1, 0])
# qc.x(1)
qc.append(RZGate(theta * 2).control(1), [i + 2, 0])
# qc.x(1)
# apply X^m_left
if m_left == 1:
qc.x(range(2, n + 1))
qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
###
qc.cx(1, 0)
qc.cx(0, 1)
qc.x(range(2, n + 1))
if m_left == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
if k_left % 2 == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
return qc
''' |
QPC005_B2 | AAC0A0DA56508 | 7 | WA | 1552 ms | 146 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import ZGate, XGate, HGate, SwapGate, RZGate
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
qc.cx(0, 1)
# now qubit 1 is exactly m
qc.cx(1, 0)
###
# rotate qubit 0 by (-1)^m c k_left
for i in range(n - 1):
theta = 2 * math.pi * k_left * 2 ** i / 2 ** n
qc.append(RZGate(theta).control(2), [i + 2, 1, 0])
# qc.x(1)
qc.append(RZGate(-theta * 2).control(1), [i + 2, 0])
# qc.x(1)
# apply X^m_left
if m_left == 1:
qc.x(range(2, n + 1))
qc.append(XGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
###
qc.cx(1, 0)
qc.cx(0, 1)
qc.x(range(2, n + 1))
if m_left == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [0])
if k_left % 2 == 1:
qc.append(ZGate().control(n - 1), list(range(2, n + 1)) + [1])
qc.x(range(2, n + 1))
return qc
''' |
QPC005_B2 | AEC42DC0B4668 | 1 | WA | 1622 ms | 143 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from math import pi, acos, sqrt, asin
from qiskit.circuit.library import XGate, ZGate, HGate, PhaseGate, RZGate
# def B1(n: int, k_const: int) -> QuantumCircuit:
# qc = QuantumCircuit(n)
# for i in range(n):
# qc.append(PhaseGate(2 * pi / (1 << n) * k_const), [i])
# k_const *= 2
# k_const %= (1 << n)
# return qc
# X を通すと回転量が -1 倍
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
N = n - 1
for i in range(N):
qc.append(RZGate(2 * pi / (1 << n) * k_left).control(1), [i + 2, 0])
k_left *= 2
k_left %= (1 << n)
if m_left:
qc.x(0)
qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
if k_left & 1:
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
return qc
''' |
QPC005_B2 | AEC42DC0B4668 | 2 | AC | 1777 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from math import pi, acos, sqrt, asin
from qiskit.circuit.library import XGate, ZGate, HGate, PhaseGate, RZGate
# def B1(n: int, k_const: int) -> QuantumCircuit:
# qc = QuantumCircuit(n)
# for i in range(n):
# qc.append(PhaseGate(2 * pi / (1 << n) * k_const), [i])
# k_const *= 2
# k_const %= (1 << n)
# return qc
# X を通すと回転量が -1 倍
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# qc.x(0)
# qc.x(1)
# qc.x(2)
# Write your code here:
N = n - 1
k = k_left
for i in range(N):
qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 0])
k *= 2
k %= (1 << n)
if m_left:
qc.x(0)
qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
if k_left & 1:
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])
return qc
''' |
QPC005_B2 | AF959EEC7F235 | 1 | WA | 1802 ms | 142 MiB | '''python
from qiskit import QuantumCircuit
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left == 1:
qc.x(n)
if k_left != 0:
angle = 2 * 3.14159265359 / (2**(n-1)) * k_left
qc.rz(angle, 0)
for i in range(n):
if k_left != 0:
angle = 2 * 3.14159265359 / (2**(n-1)) * k_left
qc.crz(-2*angle, n, i)
return qc
''' |
QPC005_B2 | AF959EEC7F235 | 2 | WA | 1625 ms | 142 MiB | '''python
from qiskit import QuantumCircuit
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
if m_left == 1:
qc.x(n)
if k_left != 0:
angle = 2 * 3.14159265359 * k_left / (2**n)
for i in range(n):
qc.rz(angle / (2**i), i)
for i in range(n):
qc.cx(n, i)
qc.rz(-angle / (2**i), i)
qc.cx(n, i)
return qc
''' |
QPC005_B2 | AF959EEC7F235 | 3 | WA | 1598 ms | 143 MiB | '''python
from qiskit import QuantumCircuit
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Apply X gate to control qubit if m_left = 1
if m_left == 1:
qc.x(n)
# Apply controlled rotation gates if k_left != 0
if k_left != 0:
angle = 2 * 3.14159265359 * k_left / (2**n)
# Apply rotation gates to target qubits
for i in range(n):
qc.rz(angle / (2**i), i)
# Apply controlled rotation gates
for i in range(n):
qc.cx(n, i)
qc.rz(-angle / (2**i), i)
qc.cx(n, i)
return qc
''' |
QPC005_B3 | A85063FEE6740 | 1 | WA | 1798 ms | 142 MiB | '''python
from qiskit import QuantumCircuit
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# Write your code here:
return qc
''' |
QPC005_B3 | A9B0542AAB32E | 1 | AC | 1922 ms | 145 MiB | '''python
from qiskit import QuantumCircuit
import math
def _negctrl_x(qc: QuantumCircuit, ctrls, tgt):
if not ctrls:
qc.x(tgt)
return
for q in ctrls:
qc.x(q)
qc.mcx(ctrls, tgt)
for q in ctrls:
qc.x(q)
def _negctrl_z(qc: QuantumCircuit, ctrls, tgt):
if not ctrls:
qc.z(tgt)
return
for q in ctrls:
qc.x(q)
qc.h(tgt)
qc.mcx(ctrls, tgt)
qc.h(tgt)
for q in ctrls:
qc.x(q)
def solve(n: int, m_right: int, k_right: int):
qc = QuantumCircuit(n + 1)
a, b = 0, 1
c_qubits = list(range(2, n + 1))
base_angle = 2 * math.pi / (1 << (n - 1))
if m_right & 1:
qc.x(b)
_negctrl_x(qc, c_qubits, b)
k_mod = k_right % (1 << n)
if k_mod:
for j, cq in enumerate(c_qubits):
exponent = (k_mod * (1 << j)) % (1 << n)
if exponent == 0:
continue
angle = exponent * base_angle
qc.crz(angle, cq, b)
if m_right & 1:
_negctrl_z(qc, c_qubits, a)
if k_right & 1:
_negctrl_z(qc, c_qubits, b)
return qc
''' |
QPC005_B3 | ABC141F00433E | 1 | WA | 1754 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from math import pi, acos, sqrt, asin
from qiskit.circuit.library import XGate, ZGate, HGate, PhaseGate, RZGate
# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# X = 0 なら, R^{k} -> R^{k + kr} or R^{k - kr}
# X = 1 なら, R^{-k} -> R^{-k - kr} or R^{-k + kr}
# |01> と |11> を +kr 倍, |00> と |10> を -kr 倍したい
# 1 bit 目にあるじゃん
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# qc.x(0)
# qc.x(1)
# qc.x(2)
# Write your code here:
N = n - 1
k = k_left
qc.cx(0, 1)
for i in range(N):
qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 1])
k *= 2
k %= (1 << n)
if m_left:
qc.x(0)
qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
if k_left & 1:
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])
return qc
''' |
QPC005_B3 | ABC141F00433E | 2 | WA | 1569 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from math import pi, acos, sqrt, asin
from qiskit.circuit.library import XGate, ZGate, HGate, PhaseGate, RZGate
# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# X = 0 なら, R^{k} -> R^{k + kr} or R^{k - kr}
# X = 1 なら, R^{-k} -> R^{-k - kr} or R^{-k + kr}
# |01> と |11> を +kr 倍, |00> と |10> を -kr 倍したい
# 1 bit 目にあるじゃん
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# qc.x(0)
# qc.x(1)
# qc.x(2)
# Write your code here:
N = n - 1
k = k_left
if m_left:
k *= -1
for i in range(N):
qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 1])
k *= 2
k %= (1 << n)
if m_left:
qc.x(0)
qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
if k_left & 1:
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])
return qc
''' |
QPC005_B3 | ABC141F00433E | 3 | WA | 1687 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from math import pi, acos, sqrt, asin
from qiskit.circuit.library import XGate, ZGate, HGate, PhaseGate, RZGate
# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# X = 0 なら, R^{k} -> R^{k + kr} or R^{k - kr}
# X = 1 なら, R^{-k} -> R^{-k - kr} or R^{-k + kr}
# Xr = 0 のとき,|01> と |11> を +kr 倍, |00> と |10> を -kr 倍したい
# 1 bit 目にあるじゃん
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# qc.x(0)
# qc.x(1)
# qc.x(2)
# qc.x(3)
# Write your code here:
N = n - 1
k = k_left
for i in range(N):
qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 1])
k *= 2
k %= (1 << n)
if m_left:
qc.x(0)
qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
if k_left & 1:
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])
return qc
''' |
QPC005_B3 | ABC141F00433E | 4 | WA | 1533 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from math import pi, acos, sqrt, asin
from qiskit.circuit.library import XGate, ZGate, HGate, PhaseGate, RZGate, SwapGate
# X を通すと回転量が -1 倍
# X がかかっているかどうかを取得しないといけないのか
# m = 0 なら, R^{k} -> R^{k + kr} or R^{k - kr}
# m = 1 なら, R^{-k} -> R^{-k - kr} or R^{-k + kr}
# mr = 0, m = 0 のとき, -k |00> + k |11> --> (-k - kr) |00> + (k + kr) |11>
# mr = 0, m = 1 のとき, -k |10> + k |01> --> (k - kr) |10> + (-k + kr) |01>
# 0 bit 目に R を適用,swap(0, 1)
# mr = 1, m = 0 のとき, -k |00> + k |11> --> (-k - kr) |10> + (k + kr) |01>
# mr = 1, m = 1 のとき, k |01> - k |10> --> (k - kr) |00> + (-k + kr) |11>
# 0 bit 目に R を適用,swap(0, 1) をして,x(0)
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# qc.x(0)
# qc.x(1)
qc.x(2)
# qc.x(3)
# Write your code here:
N = n - 1
k = k_left
for i in range(N):
qc.append(RZGate(2 * pi / (1 << N) * k).control(1), [i + 2, 0])
k *= 2
k %= (1 << n)
qc.swap(0, 1)
qc.append(SwapGate().control(N, ctrl_state='0' * N), list(range(2, N + 2)) + [0, 1])
if m_left:
qc.x(0)
qc.append(XGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [0])
if k_left & 1:
qc.append(ZGate().control(N, ctrl_state='0' * N), list(range(2, 2 + N)) + [1])
return qc
''' |
QPC005_B3 | AD543851F0726 | 1 | RE | 1628 ms | 140 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate
def A3(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
# Write your code here:
qc.x(m[0])
qc.x(k)
qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
qc.x(k)
return qc
def solve(n: int, m_left: int, k_left: int) -> QuantumCircuit:
a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
qc = QuantumCircuit(a, b, c)
# Write your code here:
qc.x(c)
if m_left:
qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
if k_left % 2:
qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
qc.x(c)
if m_right:
qc.compose(A3(n - 1), [b[0], *c], inplace=True)
for i in range(n - 1):
qc.compose(RZGate(2 ** (2 - n + i) * k_left * math.pi).control(1), [c[i], b[0]], inplace=True)
return qc
''' |
QPC005_B3 | AD543851F0726 | 2 | RE | 1497 ms | 140 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate
def A3(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
# Write your code here:
qc.x(m[0])
qc.x(k)
qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
qc.x(k)
return qc
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
qc = QuantumCircuit(a, b, c)
# Write your code here:
qc.x(c)
if m_left:
qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
if k_left % 2:
qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
qc.x(c)
if m_right:
qc.compose(A3(n - 1), [b[0], *c], inplace=True)
for i in range(n - 1):
qc.compose(RZGate(2 ** (2 - n + i) * k_right * math.pi).control(1), [c[i], b[0]], inplace=True)
return qc
''' |
QPC005_B3 | AD543851F0726 | 3 | RE | 1602 ms | 140 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate
def A3(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
# Write your code here:
qc.x(m[0])
qc.x(k)
qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
qc.x(k)
return qc
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
qc = QuantumCircuit(a, b, c)
# Write your code here:
qc.x(c)
if m_left:
qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
if k_left % 2:
qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
qc.x(c)
if m_right:
qc.compose(A3(n - 1), [b[0], *c], inplace=True)
for i in range(n - 1):
qc.compose(RZGate(2 ** (2 - n + i) * k_right * math.pi).control(1), [c[i], b[0]], inplace=True)
return qc
''' |
QPC005_B3 | AD543851F0726 | 4 | AC | 1826 ms | 145 MiB | '''python
import math
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import XGate, ZGate, RZGate
def A3(n: int) -> QuantumCircuit:
m, k = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(m, k)
# Write your code here:
qc.x(m[0])
qc.x(k)
qc.compose(XGate().control(len(k)),[*k, m[0]], inplace=True)
qc.x(k)
return qc
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c = QuantumRegister(1), QuantumRegister(1), QuantumRegister(n - 1)
qc = QuantumCircuit(a, b, c)
# Write your code here:
qc.x(c)
if m_right:
qc.compose(ZGate().control(len(c)), [*c, a[0]], inplace=True)
if k_right % 2:
qc.compose(ZGate().control(len(c)), [*c, b[0]], inplace=True)
qc.x(c)
if m_right:
qc.compose(A3(n - 1), [b[0], *c], inplace=True)
for i in range(n - 1):
qc.compose(RZGate(2 ** (2 - n + i) * k_right * math.pi).control(1), [c[i], b[0]], inplace=True)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 1 | WA | 1901 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
from qiskit.circuit.library.standard_gates import ZGate, XGate, XXPlusYYGate, RZZGate
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
qc.x(c)
qc.x(anc)
qc.mcx(c, anc)
qc.x(c)
if m_right == 0:
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], a)
else:
qc.cx(anc, a)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], a)
qc.x(anc)
qc.h([*a, *b])
if m_right:
qc.cx(anc, a)
if k_right % 2 == 1:
qc.cx(anc, b)
qc.h([*a, *b])
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 2 | WA | 1688 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], b)
if m_right == 1:
qc.cx(anc, b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 3 | WA | 1691 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right == 1:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(-zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 4 | WA | 1977 ms | 142 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right == 1:
qc.cx(anc, b)
sign = -1 if m_right == 0 else 1
for i in range(n - 1):
qc.crz(sign * zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 5 | WA | 2107 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right == 1:
qc.cx(anc, b)
sign = 1 if m_right == 0 else -1
for i in range(n - 1):
qc.crz(sign * zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 6 | WA | 1665 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
sign = -1 if m_right == 0 else 1
for i in range(n - 1):
qc.crz(sign * zangle * k_right * (2**i), c[i], b)
if m_right == 1:
qc.cx(anc, b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 7 | WA | 1620 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right == 1:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(-zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_B3 | AE4F1FBF1902F | 8 | AC | 1794 ms | 145 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
import numpy as np
def solve(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right == 1:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
''' |
QPC005_EX | ACCDBBE2E33B6 | 1 | RE | 1569 ms | 141 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate
import numpy as np
def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_left:
qc.cz(anc, a)
if k_left % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
for i in range(n - 1):
qc.crz(zangle * k_left * (2**i), c[i], a)
if m_left == 1:
qc.cx(anc, a)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right == 1:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def shift(n):
qc = QuantumCircuit(n)
for i in range(1, n):
qc.swap(0, i)
return qc
def cneg(n):
control, x = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(control, x)
qc.cx(control, x)
for idx in reversed(range(n)):
qc.mcx([control] + x[:idx], x[idx])
return qc
def qft(n):
qc = QuantumCircuit(n)
thetas = []
for k in range(0, 1 + n):
thetas.append(2 * np.pi / (2**k))
for idx in range(0, n):
qc.h(n - 1 - idx)
for jdx in range(idx + 1, n):
thetaidx = jdx - idx + 1
qc.cp(thetas[thetaidx], n - 1 - jdx, n - 1 - idx)
for idx in range(0, n // 2):
qc.swap(idx, n - idx - 1)
return qc
def mk_to_emk(n):
m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
qc = QuantumCircuit(m, k, anc)
qc.compose(qft(n), k, inplace=True)
qc.compose(shift(n), k, inplace=True)
qc.compose(cneg(n), k, inplace=True)
qc.x(k[1:])
qc.mcx(k[1:], anc)
qc.x(k[1:])
# c=0
qc.ch(anc, m)
qc.x(k[1:])
# c>0
qc.cx([*anc, k[0]], m)
qc.mcx(k[1:], anc)
qc.x(k[1:])
return qc
def solve(
n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
qc.compose(mk_to_emk(n), inplace=True)
qc.compose(b3(n, m_before, k_before), inplace=True)
qc.compose(b2(n, m_after, k_after), inplace=True)
qc.compose(mk_to_emk(n).inverse(), inplace=True)
return qc
''' |
QPC005_EX | ACCDBBE2E33B6 | 2 | WA | 1914 ms | 143 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate
import numpy as np
def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_left:
qc.cz(anc, a)
if k_left % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
for i in range(n - 1):
qc.crz(zangle * k_left * (2**i), c[i], a)
if m_left:
qc.cx(anc, a)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def shift(n):
qc = QuantumCircuit(n)
for i in range(1, n):
qc.swap(0, i)
return qc
def cneg(n):
control, x = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(control, x)
qc.cx(control, x)
for idx in reversed(range(n)):
qc.mcx([control] + x[:idx], x[idx])
return qc
def qft(n):
qc = QuantumCircuit(n)
thetas = []
for k in range(0, 1 + n):
thetas.append(2 * np.pi / (2**k))
for idx in range(0, n):
qc.h(n - 1 - idx)
for jdx in range(idx + 1, n):
thetaidx = jdx - idx + 1
qc.cp(thetas[thetaidx], n - 1 - jdx, n - 1 - idx)
for idx in range(0, n // 2):
qc.swap(idx, n - idx - 1)
return qc
def mk_to_emk(n):
m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
qc = QuantumCircuit(m, k, anc)
qc.compose(qft(n), k, inplace=True)
qc.compose(shift(n), k, inplace=True)
qc.compose(cneg(n - 1), k, inplace=True)
qc.x(k[1:])
qc.mcx(k[1:], anc)
qc.x(k[1:])
# c=0
qc.ch(anc, m)
qc.x(anc)
# c>0
qc.mcx([*anc, k[0]], m)
qc.x(k[1:])
qc.x(anc)
qc.mcx(k[1:], anc)
qc.x(k[1:])
return qc
def solve(
n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
qc.compose(mk_to_emk(n), inplace=True)
qc.compose(b3(n, m_before, k_before), inplace=True)
qc.compose(b2(n, m_after, k_after), inplace=True)
qc.compose(mk_to_emk(n).inverse(), inplace=True)
return qc
''' |
QPC005_EX | ACCDBBE2E33B6 | 3 | RE | 1948 ms | 140 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate
import numpy as np
def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_left:
qc.cz(anc, a)
if k_left % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
for i in range(n - 1):
qc.crz(zangle * k_left * (2**i), c[i], a)
if m_left:
qc.cx(anc, a)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def shift(n):
qc = QuantumCircuit(n)
for i in range(1, n):
qc.swap(0, i)
return qc
def cneg(n):
control, x = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(control, x)
qc.cx(control, x)
for idx in reversed(range(n)):
qc.mcx([control] + x[:idx], x[idx])
return qc
def qft(n):
qc = QuantumCircuit(n)
thetas = []
for k in range(0, 1 + n):
thetas.append(2 * np.pi / (2**k))
for idx in range(0, n):
qc.h(n - 1 - idx)
for jdx in range(idx + 1, n):
thetaidx = jdx - idx + 1
qc.cp(thetas[thetaidx], n - 1 - jdx, n - 1 - idx)
for idx in range(0, n // 2):
qc.swap(idx, n - idx - 1)
return qc
def mk_to_emk(n):
m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
qc = QuantumCircuit(m, k, anc)
qc.compose(qft(n), k, inplace=True)
qc.compose(shift(n), k, inplace=True)
qc.compose(cneg(n - 1), k, inplace=True)
qc.x(k[0])
qc.x(k[1:])
qc.mcx(k[1:], anc)
qc.x(k[1:])
# c=0
qc.ch(anc, m)
qc.x(anc)
# c>0
qc.mcx([*anc, k[0]], m)
qc.x(k[1:])
qc.x(anc)
qc.mcx(k[1:], anc)
qc.x(k[1:])
return qc
''' |
QPC005_EX | ACCDBBE2E33B6 | 4 | WA | 2236 ms | 146 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate
import numpy as np
def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_left:
qc.cz(anc, a)
if k_left % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
for i in range(n - 1):
qc.crz(zangle * k_left * (2**i), c[i], a)
if m_left:
qc.cx(anc, a)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def shift(n):
qc = QuantumCircuit(n)
for i in range(1, n):
qc.swap(0, i)
return qc
def cneg(n):
control, x = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(control, x)
qc.cx(control, x)
for idx in reversed(range(n)):
qc.mcx([control] + x[:idx], x[idx])
return qc
def qft(n):
qc = QuantumCircuit(n)
thetas = []
for k in range(0, 1 + n):
thetas.append(2 * np.pi / (2**k))
for idx in range(0, n):
qc.h(n - 1 - idx)
for jdx in range(idx + 1, n):
thetaidx = jdx - idx + 1
qc.cp(thetas[thetaidx], n - 1 - jdx, n - 1 - idx)
for idx in range(0, n // 2):
qc.swap(idx, n - idx - 1)
return qc
def mk_to_emk(n):
m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
qc = QuantumCircuit(m, k, anc)
qc.compose(qft(n), k, inplace=True)
qc.compose(shift(n), k, inplace=True)
qc.compose(cneg(n - 1), k, inplace=True)
qc.x(k[0])
qc.x(k[1:])
qc.mcx(k[1:], anc)
qc.x(k[1:])
# c=0
qc.ch(anc, m)
qc.x(anc)
# c>0
qc.mcx([*anc, k[0]], m)
qc.x(k[1:])
qc.x(anc)
qc.mcx(k[1:], anc)
qc.x(k[1:])
return qc
def solve(
n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
m, k, anc = (
QuantumRegister(1),
QuantumRegister(n),
QuantumRegister(1),
)
qc = QuantumCircuit(m, k, anc)
qc.compose(mk_to_emk(n), inplace=True)
qc.compose(b3(n, m_before, k_before), inplace=True)
qc.compose(b2(n, m_after, k_after), inplace=True)
qc.compose(mk_to_emk(n).inverse(), inplace=True)
return qc
''' |
QPC005_EX | ACCDBBE2E33B6 | 5 | AC | 1878 ms | 147 MiB | '''python
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import HGate
import numpy as np
def b2(n: int, m_left: int, k_left: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_left:
qc.cz(anc, a)
if k_left % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
for i in range(n - 1):
qc.crz(zangle * k_left * (2**i), c[i], a)
if m_left:
qc.cx(anc, a)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def b3(n: int, m_right: int, k_right: int) -> QuantumCircuit:
a, b, c, anc = (
QuantumRegister(1),
QuantumRegister(1),
QuantumRegister(n - 1),
QuantumRegister(1),
)
qc = QuantumCircuit(a, b, c, anc)
zangle = 2 * np.pi / (2 ** (n - 1))
# Ancilla (|anc> = |c=0>)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
# c = 0
if m_right:
qc.cz(anc, a)
if k_right % 2 == 1:
qc.cz(anc, b)
# c != 0
qc.x(anc)
if m_right:
qc.cx(anc, b)
for i in range(n - 1):
qc.crz(zangle * k_right * (2**i), c[i], b)
# Uncompute
qc.x(anc)
qc.x(c)
qc.mcx(c, anc)
qc.x(c)
return qc
def shift(n):
qc = QuantumCircuit(n)
for i in range(1, n):
qc.swap(0, i)
return qc
def cneg(n):
control, x = QuantumRegister(1), QuantumRegister(n)
qc = QuantumCircuit(control, x)
qc.cx(control, x)
for idx in reversed(range(n)):
qc.mcx([control] + x[:idx], x[idx])
return qc
def qft(n):
qc = QuantumCircuit(n)
thetas = []
for k in range(0, 1 + n):
thetas.append(2 * np.pi / (2**k))
for idx in range(0, n):
qc.h(n - 1 - idx)
for jdx in range(idx + 1, n):
thetaidx = jdx - idx + 1
qc.cp(thetas[thetaidx], n - 1 - jdx, n - 1 - idx)
for idx in range(0, n // 2):
qc.swap(idx, n - idx - 1)
return qc
def mk_to_emk(n):
m, k, anc = QuantumRegister(1), QuantumRegister(n), QuantumRegister(1)
qc = QuantumCircuit(m, k, anc)
qc.compose(qft(n), k, inplace=True)
qc.compose(shift(n), k, inplace=True)
qc.compose(cneg(n - 1), k, inplace=True)
qc.x(k[1:])
qc.mcx(k[1:], anc)
qc.x(k[1:])
# c=0
qc.ch(anc, m)
qc.x(anc)
# c>0
qc.cx(anc, k[0])
qc.mcx([*anc, k[0]], m)
qc.x(k[1:])
qc.x(anc)
qc.mcx(k[1:], anc)
qc.x(k[1:])
return qc
def solve(
n: int, m_before: int, k_before: int, m_after: int, k_after: int
) -> QuantumCircuit:
m, k, anc = (
QuantumRegister(1),
QuantumRegister(n),
QuantumRegister(1),
)
qc = QuantumCircuit(m, k, anc)
qc.compose(mk_to_emk(n), inplace=True)
qc.compose(b3(n, m_before, k_before), inplace=True)
qc.compose(b2(n, m_after, k_after), inplace=True)
qc.compose(mk_to_emk(n).inverse(), inplace=True)
return qc
''' |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.