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
QPC002_B8
A62F577BBFF2E
1
WA
1339 ms
143 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library.standard_gates import PhaseGate import math def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(-2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) for i in range(n): for j in range(m): qc.append(PhaseGate(-2 * math.pi * S[i] / pow(2, m - j)).control(1), [i, n + j]) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) for i in range(m): if ((L >> i) & 1) == 0: qc.x(n + i) if m == 1: qc.p(theta, n) else: qc.append(PhaseGate(theta).control(m - 1), list(range(n, n + m))) for i in range(m): if ((L >> i) & 1) == 0: qc.x(n + i) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) for i in range(n): for j in range(m): qc.append(PhaseGate(2 * math.pi * S[i] / pow(2, m - j)).control(1), [i, n + j]) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(-2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) return qc '''
QPC002_B8
A62F577BBFF2E
2
AC
2784 ms
183 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library.standard_gates import PhaseGate import math def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(-2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) for i in range(n): for j in range(m): qc.append(PhaseGate(-2 * math.pi * S[i] / pow(2, m - j)).control(1), [i, n + j]) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) for i in range(m): if ((L >> i) & 1) == 0: qc.x(n + i) if m == 1: qc.p(theta, n) else: qc.append(PhaseGate(theta).control(m - 1), list(range(n, n + m))) for i in range(m): if ((L >> i) & 1) == 0: qc.x(n + i) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(-2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) for i in range(n): for j in range(m): qc.append(PhaseGate(2 * math.pi * S[i] / pow(2, m - j)).control(1), [i, n + j]) for i in range(m - 1, -1, -1): for j in range(m - 1, i, -1): qc.append(PhaseGate(2 * math.pi * math.pow(2, i - 1 - j)).control(1), [n + i, n + j]) qc.h(n + i) for i in range(m // 2): qc.cx(n + i, n + m - 1 - i) qc.cx(n + m - 1 - i, n + i) qc.cx(n + i, n + m - 1 - i) return qc '''
QPC002_B8
A6484B66CD984
1
RE
1077 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library.standard_gates import U1Gate from math import pi def QFT(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.swap(i, n-i-1) for i in range(n): qc.h(i) for j in range(i+1, n): qc.append(U1Gate(pi/2**(j-i)).control(1), [j, i]) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # y をフーリエ変換 for i in range(m): qc.h(n + i) # S[i] をフーリエ変換したものを掛ける for i in range(n): for j in range(m): qc.append(U1Gate(2 * pi * S[i] / 2 ** (m - j)).control(1), [i, n + j]) # y を逆フーリエ変換 for i in range(m//2): qc.swap(n + i, n + m-i-1) for i in range(m): qc.h(n + i) for j in range(i+1, m): qc.append(U1Gate(-pi/2**(j-i)).control(1), [n + j, n + i]) # y == L のところに e^iθ を掛ける for i in range(m): if ~L & (1 << i): qc.x(n + i) qc.append(U1Gate(theta).control(m-1) if m else U1Gate(theta), range(n, n+m)) for i in range(m): if ~L & (1 << i): qc.x(n + i) # さっきの逆 # y をフーリエ変換 for i in range(m//2): qc.swap(n + i, n + m-i-1) for i in range(m): qc.h(n + i) for j in range(i+1, m): qc.append(U1Gate(pi/2**(j-i)).control(1), [n + j, n + i]) # S[i] をフーリエ変換したものを割る for i in range(n): for j in range(m): qc.append(U1Gate(-2 * pi * S[i] / 2 ** (m - j)).control(1), [i, n + j]) # y をフーリエ変換 for i in range(m): qc.h(n + i) return qc '''
QPC002_B8
A6484B66CD984
2
AC
1782 ms
183 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library.standard_gates import U1Gate from math import pi def QFT(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.swap(i, n-i-1) for i in range(n): qc.h(i) for j in range(i+1, n): qc.append(U1Gate(pi/2**(j-i)).control(1), [j, i]) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # y をフーリエ変換 for i in range(m): qc.h(n + i) # S[i] をフーリエ変換したものを掛ける for i in range(n): for j in range(m): qc.append(U1Gate(2 * pi * S[i] / 2 ** (m - j)).control(1), [i, n + j]) # y を逆フーリエ変換 for i in range(m//2): qc.swap(n + i, n + m-i-1) for i in range(m): qc.h(n + i) for j in range(i+1, m): qc.append(U1Gate(-pi/2**(j-i)).control(1), [n + j, n + i]) # y == L のところに e^iθ を掛ける for i in range(m): if ~L & (1 << i): qc.x(n + i) qc.append(U1Gate(theta).control(m-1) if m > 1 else U1Gate(theta), range(n, n+m)) for i in range(m): if ~L & (1 << i): qc.x(n + i) # さっきの逆 # y をフーリエ変換 for i in range(m//2): qc.swap(n + i, n + m-i-1) for i in range(m): qc.h(n + i) for j in range(i+1, m): qc.append(U1Gate(pi/2**(j-i)).control(1), [n + j, n + i]) # S[i] をフーリエ変換したものを割る for i in range(n): for j in range(m): qc.append(U1Gate(-2 * pi * S[i] / 2 ** (m - j)).control(1), [i, n + j]) # y をフーリエ変換 for i in range(m): qc.h(n + i) return qc '''
QPC002_B8
A717787B16F78
1
WA
1238 ms
153 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: # Compute the value of L mod 2^m L_mod = L % (2 ** m) # Apply the function f(x) = S0*x0 + S1*x1 + ... + S(n-1)*x(n-1) for i in range(n): if S[i] != 0: qc.cx(x[i], y[0]) # Add a controlled-NOT (CNOT) to y based on x[i] for j in range(1, m): if S[i] & (1 << j): qc.cx(x[i], y[j]) # Apply the phase shift conditional on f(x) == L mod 2^m for i in range(m): if (L_mod & (1 << i)) != 0: qc.h(y[i]) # Apply the phase shift qc.rz(theta, y[0]) # Revert the state to its original form for i in range(m): if (L_mod & (1 << i)) != 0: qc.h(y[i]) return qc '''
QPC002_B8
A717787B16F78
2
AC
1948 ms
183 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): qc.cp(2*math.pi/(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n: int, m: int, S: list[int]) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
A8433E17B2CF5
1
RE
1824 ms
156 MiB
'''python from qiskit import QuantumRegister, QuantumCircuit # Let n = len(qubits) # if no ctrl_qubits, adds 2 * len(qubits) circuit depth # otherwise, # if n % 2 == 0, adds n * (n/2 + 1) circuit depth # otherwise, adds (n-1) * ((n-1)/2 + 1) + n circuit depth def apply_QFT(qc, qubits, ctrl_qubit = None, inverse = False): from numpy import pi coef = -pi if inverse else pi if ctrl_qubit: for i in reversed(range(len(qubits))): qc.ch(ctrl_qubit, qubits[i]) for j in reversed(range(i)): qc.mcp(coef / 2**(i - j), [ctrl_qubit, qubits[j]], qubits[i]) for i in range(len(qubits) // 2): qc.cswap(ctrl_qubit, qubits[i], qubits[len(qubits) - 1 - i]) else: for i in reversed(range(len(qubits))): qc.h(qubits[i]) for j in reversed(range(i)): qc.cp(coef / 2**(i - j), qubits[j], qubits[i]) for i in range(len(qubits) // 2): qc.swap(qubits[i], qubits[len(qubits) - 1 - i]) return qc def solve(n, m, L, s, theta) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) from numpy import pi for j in range(m): qc.h(y[j]) for i in range(n): for j in range(m): qc.cp(pi * s[i] / 2**(m - 1 - j), x[i], y[j]) apply_QFT(qc, y, inverse = True) if m == 1: if L == 0: qc.x(y[0]) qc.p(theta) if L == 0: qc.x(y[0]) else: for j in range(m): if ~L >> j & 1: qc.x(y[j]) qc.mcp(theta, range(m - 1), m - 1) for j in range(m): if ~L >> j & 1: qc.x(y[j]) apply_QFT(qc, y) for i in range(n): for j in range(m): qc.cp(-pi * s[i] / 2**(m - 1 - j), x[i], y[j]) for j in range(m): qc.h(y[j]) return qc '''
QPC002_B8
A8433E17B2CF5
2
RE
1757 ms
158 MiB
'''python from qiskit import QuantumRegister, QuantumCircuit # Let n = len(qubits) # if no ctrl_qubits, adds 2 * len(qubits) circuit depth # otherwise, # if n % 2 == 0, adds n * (n/2 + 1) circuit depth # otherwise, adds (n-1) * ((n-1)/2 + 1) + n circuit depth def apply_QFT(qc, qubits, ctrl_qubit = None, inverse = False): from numpy import pi coef = -pi if inverse else pi if ctrl_qubit: for i in reversed(range(len(qubits))): qc.ch(ctrl_qubit, qubits[i]) for j in reversed(range(i)): qc.mcp(coef / 2**(i - j), [ctrl_qubit, qubits[j]], qubits[i]) for i in range(len(qubits) // 2): qc.cswap(ctrl_qubit, qubits[i], qubits[len(qubits) - 1 - i]) else: for i in reversed(range(len(qubits))): qc.h(qubits[i]) for j in reversed(range(i)): qc.cp(coef / 2**(i - j), qubits[j], qubits[i]) for i in range(len(qubits) // 2): qc.swap(qubits[i], qubits[len(qubits) - 1 - i]) return qc def solve(n, m, L, s, theta) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) from numpy import pi for j in range(m): qc.h(y[j]) for i in range(n): for j in range(m): qc.cp(pi * s[i] / 2**(m - 1 - j), x[i], y[j]) apply_QFT(qc, y, inverse = True) if m == 1: if L == 0: qc.x(y[0]) qc.p(theta) if L == 0: qc.x(y[0]) else: for j in range(m): if ~L >> j & 1: qc.x(y[j]) qc.mcp(theta, list(range(m - 1)), m - 1) for j in range(m): if ~L >> j & 1: qc.x(y[j]) apply_QFT(qc, y) for i in range(n): for j in range(m): qc.cp(-pi * s[i] / 2**(m - 1 - j), x[i], y[j]) for j in range(m): qc.h(y[j]) return qc '''
QPC002_B8
A8433E17B2CF5
3
WA
2387 ms
160 MiB
'''python from qiskit import QuantumRegister, QuantumCircuit # Let n = len(qubits) # if no ctrl_qubits, adds 2 * len(qubits) circuit depth # otherwise, # if n % 2 == 0, adds n * (n/2 + 1) circuit depth # otherwise, adds (n-1) * ((n-1)/2 + 1) + n circuit depth def apply_QFT(qc, qubits, ctrl_qubit = None, inverse = False): from numpy import pi coef = -pi if inverse else pi if ctrl_qubit: for i in reversed(range(len(qubits))): qc.ch(ctrl_qubit, qubits[i]) for j in reversed(range(i)): qc.mcp(coef / 2**(i - j), [ctrl_qubit, qubits[j]], qubits[i]) for i in range(len(qubits) // 2): qc.cswap(ctrl_qubit, qubits[i], qubits[len(qubits) - 1 - i]) else: for i in reversed(range(len(qubits))): qc.h(qubits[i]) for j in reversed(range(i)): qc.cp(coef / 2**(i - j), qubits[j], qubits[i]) for i in range(len(qubits) // 2): qc.swap(qubits[i], qubits[len(qubits) - 1 - i]) return qc def solve(n, m, L, s, theta) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) from numpy import pi for j in range(m): qc.h(y[j]) for i in range(n): for j in range(m): qc.cp(pi * s[i] / 2**(m - 1 - j), x[i], y[j]) apply_QFT(qc, y, inverse = True) if m == 1: if L == 0: qc.x(y[0]) qc.p(theta, y[0]) if L == 0: qc.x(y[0]) else: for j in range(m): if ~L >> j & 1: qc.x(y[j]) qc.mcp(theta, list(range(m - 1)), m - 1) for j in range(m): if ~L >> j & 1: qc.x(y[j]) apply_QFT(qc, y) for i in range(n): for j in range(m): qc.cp(-pi * s[i] / 2**(m - 1 - j), x[i], y[j]) for j in range(m): qc.h(y[j]) return qc '''
QPC002_B8
A8433E17B2CF5
4
AC
2898 ms
161 MiB
'''python from qiskit import QuantumRegister, QuantumCircuit # Let n = len(qubits) # if no ctrl_qubits, adds 2 * len(qubits) circuit depth # otherwise, # if n % 2 == 0, adds n * (n/2 + 1) circuit depth # otherwise, adds (n-1) * ((n-1)/2 + 1) + n circuit depth def apply_QFT(qc, qubits, ctrl_qubit = None, inverse = False): from numpy import pi coef = -pi if inverse else pi if ctrl_qubit: for i in reversed(range(len(qubits))): qc.ch(ctrl_qubit, qubits[i]) for j in reversed(range(i)): qc.mcp(coef / 2**(i - j), [ctrl_qubit, qubits[j]], qubits[i]) for i in range(len(qubits) // 2): qc.cswap(ctrl_qubit, qubits[i], qubits[len(qubits) - 1 - i]) else: for i in reversed(range(len(qubits))): qc.h(qubits[i]) for j in reversed(range(i)): qc.cp(coef / 2**(i - j), qubits[j], qubits[i]) for i in range(len(qubits) // 2): qc.swap(qubits[i], qubits[len(qubits) - 1 - i]) return qc def solve(n, m, L, s, theta) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) from numpy import pi for j in range(m): qc.h(y[j]) for i in range(n): for j in range(m): qc.cp(pi * s[i] / 2**(m - 1 - j), x[i], y[j]) apply_QFT(qc, y, inverse = True) if m == 1: if L == 0: qc.x(y[0]) qc.p(theta, y[0]) if L == 0: qc.x(y[0]) else: for j in range(m): if ~L >> j & 1: qc.x(y[j]) qc.mcp(theta, y[:m - 1], y[m - 1]) for j in range(m): if ~L >> j & 1: qc.x(y[j]) apply_QFT(qc, y) for i in range(n): for j in range(m): qc.cp(-pi * s[i] / 2**(m - 1 - j), x[i], y[j]) for j in range(m): qc.h(y[j]) return qc '''
QPC002_B8
A853740AD829B
1
RE
1379 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: # Compute the function f(x) using controlled additions for i in range(n): if S[i] != 0: for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) # Create the comparison register comparison = QuantumRegister(m, 'comparison') qc.add_register(comparison) # Initialize the comparison register to the value L for j in range(m): if (L >> j) & 1: qc.x(comparison[j]) # Apply a controlled phase shift based on the comparison for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], comparison[j]) # Apply phase shift based on comparison qc.append(QuantumCircuit(m).compose(QuantumCircuit(m, name='phase_shift'), inplace=False), comparison) qc.rz(2 * np.pi * theta, comparison) return qc '''
QPC002_B8
A853740AD829B
2
UGE
1200 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: # Compute the function f(x) using controlled additions for i in range(n): if S[i] != 0: for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) # Create the comparison register comparison = QuantumRegister(m, 'comparison') qc.add_register(comparison) # Initialize the comparison register to the value L for j in range(m): if (L >> j) & 1: qc.x(comparison[j]) # Apply a controlled phase shift based on the comparison for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], comparison[j]) # Apply phase shift based on comparison qc.append(QuantumCircuit(m).compose(QuantumCircuit(m, name='phase_shift'), inplace=False), comparison) qc.rz(2 * np.pi * theta, comparison) return qc '''
QPC002_B8
A853740AD829B
3
RE
1081 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: for i in range(2**n): # Compute f(x) for the current x f_x = sum(S[j] * ((i >> j) & 1) for j in range(n)) f_x_mod = f_x % (2**m) # Apply phase shift if f(x) == L (mod 2^m) if f_x_mod == L: # Apply phase shift to |x⟩ and |0⟩ (for simplicity, we use the gate directly) angle = 2 * np.pi * theta / (2 ** m) qc.u1(angle, y[0]) # Apply phase shift to the first qubit in y # Note: This is a simplified example. For a full implementation, controlled phase operations are needed. return qc '''
QPC002_B8
A85FB03577243
1
WA
1203 ms
144 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Step 1: Compute f(x) in the y register for i in range(n): for j in range(m): if (S[i] & (1 << j)) != 0: qc.cx(x[i], y[j]) # Step 2: Compare f(x) with L for j in range(m): if (L & (1 << j)) == 0: qc.x(y[j]) # Step 3: Apply controlled phase rotation qc.mcry(theta, y[:], x[n-1]) # Step 4: Uncompute steps 2 and 1 for j in range(m): if (L & (1 << j)) == 0: qc.x(y[j]) for i in range(n-1, -1, -1): for j in range(m-1, -1, -1): if (S[i] & (1 << j)) != 0: qc.cx(x[i], y[j]) return qc '''
QPC002_B8
A85FB03577243
2
WA
1112 ms
154 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: # Step 1: Compute f(x) in the y register for i in range(n): for j in range(m): if (S[i] & (1 << j)) != 0: qc.cx(x[i], y[j]) # Step 2: Compare f(x) with L for j in range(m): if (L & (1 << j)) == 0: qc.x(y[j]) # Step 3: Apply controlled phase rotation qc.mcry(theta, y[:], x[n-1]) # Step 4: Uncompute steps 2 and 1 for j in range(m): if (L & (1 << j)) == 0: qc.x(y[j]) for i in range(n-1, -1, -1): for j in range(m-1, -1, -1): if (S[i] & (1 << j)) != 0: qc.cx(x[i], y[j]) return qc '''
QPC002_B8
A85FB03577243
3
RE
1233 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x = QuantumRegister(n, 'x') y = QuantumRegister(m, 'y') qc = QuantumCircuit(x, y) # Step 1: Compute f(x) = S0*x0 + S1*x1 + ... + Sn-1*xn-1 for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) # Step 2: Compare f(x) mod 2^m with L L_bin = format(L, f'0{m}b') for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Step 3: Apply the phase shift theta if comparison is successful qc.mcx(y, y[0]) # Multi-controlled X gate on ancilla to check if f(x) == L mod 2^m qc.p(theta, y[0]) # Apply the phase shift # Step 4: Reverse the comparison operation to uncompute ancilla qubits qc.mcx(y, y[0]) # Reverse Step 2 for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Reverse Step 1: Uncompute f(x) for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) return qc '''
QPC002_B8
A85FB03577243
4
QLE
1497 ms
144 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x = QuantumRegister(n, 'x') y = QuantumRegister(m, 'y') qc = QuantumCircuit(x, y) # Step 1: Compute f(x) = S0*x0 + S1*x1 + ... + Sn-1*xn-1 for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) # Step 2: Compare f(x) mod 2^m with L L_bin = format(L, f'0{m}b') for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Step 3: Apply the phase shift theta if comparison is successful # Use an ancilla qubit for the multi-controlled phase shift if m > 1: ancilla = QuantumRegister(1, 'ancilla') qc.add_register(ancilla) qc.mcx(y, ancilla[0]) # Multi-controlled X gate to check if f(x) == L mod 2^m qc.p(theta, ancilla[0]) # Apply the phase shift qc.mcx(y, ancilla[0]) # Uncompute the ancilla else: qc.p(theta, y[0]) # For m=1, we directly apply the phase shift without ancilla # Step 4: Reverse the comparison operation to uncompute ancilla qubits for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Reverse Step 1: Uncompute f(x) for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) return qc '''
QPC002_B8
A85FB03577243
5
RE
1282 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x = QuantumRegister(n, 'x') y = QuantumRegister(m, 'y') qc = QuantumCircuit(x, y) # Step 1: Compute f(x) = S0*x0 + S1*x1 + ... + Sn-1*xn-1 for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) # Step 2: Compare f(x) mod 2^m with L L_bin = format(L, f'0{m}b') for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Step 3: Apply the phase shift theta if comparison is successful qc.mcx(y[:-1], y[-1]) # Multi-controlled X gate on y[0:m-1] controlling y[m-1] qc.p(theta, y[-1]) # Apply the phase shift to the last qubit in y register qc.mcx(y[:-1], y[-1]) # Uncompute the mcx operation # Step 4: Reverse the comparison operation to uncompute ancilla qubits for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Reverse Step 1: Uncompute f(x) for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) return qc '''
QPC002_B8
A85FB03577243
6
RE
1050 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x = QuantumRegister(n, 'x') y = QuantumRegister(m, 'y') qc = QuantumCircuit(x, y) # Step 1: Calculate f(x) = S0*x0 + S1*x1 + ... + Sn-1*xn-1 for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) # Step 2: Apply an X gate to y qubits where L has a 0 in the binary representation L_bin = format(L, f'0{m}b') for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Step 3: Apply the phase shift theta if f(x) == L (mod 2^m) qc.mcx(list(y), y[0]) # Multi-controlled X gate on all y qubits controlling y[0] qc.p(theta, y[0]) # Apply the phase shift to y[0] if the condition is satisfied qc.mcx(list(y), y[0]) # Uncompute the mcx operation to maintain reversibility # Step 4: Reverse the X gates to restore y qubits to their original state for j in range(m): if L_bin[j] == '0': qc.x(y[j]) # Step 5: Reverse the computation of f(x) by applying CX gates again for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) return qc '''
QPC002_B8
A85FB03577243
7
WA
1166 ms
144 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Calculate the target value of f(x) target_value = L % (2 ** m) # Add a quantum phase oracle # This requires applying controlled phase shifts based on S for i in range(n): if S[i] != 0: qc.cx(x[i], y[0]) # Example, use controlled-X gates here as placeholders # Apply a conditional phase gate # We need to encode the function and apply the phase based on the result qc.append( # Replace with actual implementation # Controlled phase gate applying e^{i*theta} if f(x) == L (mod 2^m) QuantumCircuit(m).to_gate().control(n), [*x, *y] ) return qc '''
QPC002_B8
A85FB03577243
8
WA
1313 ms
143 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x = QuantumRegister(n, name='x') y = QuantumRegister(m, name='y') qc = QuantumCircuit(x, y) # Calculate f(x) = S0 * x0 + S1 * x1 + ... + Sn-1 * xn-1 # Encode the phase flip for f(x) = L mod 2^m # Define the value to compare against target_value = L % (2 ** m) # Add phase flip for f(x) == target_value for i in range(n): for j in range(m): if (S[i] >> j) & 1: # Check if the j-th bit of S[i] is 1 qc.cx(x[i], y[j]) # Controlled-X gate # Apply the conditional phase shift # We will use a phase flip if f(x) == target_value for k in range(m): if ((target_value >> k) & 1) == 1: qc.rz(2 * theta, y[k]) # Apply the phase shift to the y register return qc '''
QPC002_B8
A85FB03577243
9
UME
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit import Gate from qiskit.circuit.library import ZGate, PhaseGate, U1 def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Define the function f(x) using an auxiliary qubit aux = QuantumRegister(1) qc.add_register(aux) # Apply the controlled operations to implement f(x) = L # Create the controlled addition circuit qc.add_register(QuantumRegister(n + m + 1)) # Apply the controlled addition for i in range(n): if S[i] != 0: qc.cx(x[i], aux[0]) # Controlled NOT gate (used for addition) qc.rz(2 * S[i] * theta, aux[0]) # Apply the phase gate to |x⟩_n |0⟩_m if f(x) = L qc.append(PhaseGate(theta), [aux[0]]) # Clean up: Remove auxiliary qubit qc.remove_register(aux) return qc '''
QPC002_B8
A85FB03577243
10
RE
1091 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x = QuantumRegister(n, name='x') y = QuantumRegister(m, name='y') qc = QuantumCircuit(x, y) # Adding an ancillary qubit for the phase application aux = QuantumRegister(1, name='aux') qc.add_register(aux) # Initialize auxiliary qubit to |1> qc.x(aux[0]) # Apply the addition of S coefficients for i in range(n): if S[i] != 0: # Apply a controlled-phase gate to simulate the effect of S[i] qc.cp(2 * np.pi * S[i] / (2**m), x[i], aux[0]) # Apply the phase gate based on theta qc.append(PhaseGate(theta), [aux[0]]) # Clean up: Remove auxiliary qubit qc.remove_register(aux) return qc '''
QPC002_B8
A85FB03577243
11
RE
1173 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: # Define quantum registers x = QuantumRegister(n, name='x') # Input register y = QuantumRegister(m, name='y') # Output register # Create quantum circuit qc = QuantumCircuit(x, y) # To implement f(x) = S0*x0 + S1*x1 + ... + Sn-1*xn-1 (mod 2^m) # Use an additional auxiliary qubit for phase application aux = QuantumRegister(1, name='aux') qc.add_register(aux) # Initialize auxiliary qubit to |1> qc.x(aux[0]) # Apply the function f(x) as controlled addition for i in range(n): if S[i] != 0: for j in range(m): # Apply controlled addition for each bit of y qc.cu1(np.pi * S[i] / (2 ** j), x[i], y[j]) # Apply phase shift if f(x) = L (mod 2^m) qc.append(QuantumCircuit(*y, *aux).u1(theta, aux[0]), [aux[0]]) # Clean up: Remove auxiliary qubit qc.remove_register(aux) return qc '''
QPC002_B8
A85FB03577243
12
RE
1246 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import PhaseGate def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x = QuantumRegister(n, name='x') y = QuantumRegister(m, name='y') qc = QuantumCircuit(x, y) # Define the auxiliary qubit aux = QuantumRegister(1, name='aux') qc.add_register(aux) # Initialize auxiliary qubit to |1> qc.x(aux[0]) # Apply the function f(x) using controlled-phase gates # To simulate f(x) mod 2^m using controlled gates for i in range(n): if S[i] != 0: for j in range(m): qc.cx(x[i], y[j]) # Apply controlled X gates qc.rz(2 * np.pi * S[i] / (2**m), y[j]) # Apply rotation for phase # Apply phase shift theta if f(x) = L (mod 2^m) qc.append(PhaseGate(theta), [aux[0]]) # Clean up auxiliary qubit qc.remove_register(aux) return qc '''
QPC002_B8
A85FB03577243
13
RE
1189 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import MCXGate, PhaseGate import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: # Quantum registers x = QuantumRegister(n, name='x') y = QuantumRegister(m, name='y') qc = QuantumCircuit(x, y) # Create an additional auxiliary qubit for phase application aux = QuantumRegister(1, name='aux') qc.add_register(aux) # Initialize auxiliary qubit to |1> qc.x(aux[0]) # Apply the function f(x) = S0*x0 + S1*x1 + ... + Sn-1*xn-1 mod 2^m # Here we use controlled additions to accumulate the result in the y register for i in range(n): if S[i] != 0: for j in range(m): qc.cx(x[i], y[j]) qc.rz(2 * np.pi * S[i] / (2 ** j), y[j]) # Apply phase shift if f(x) = L mod 2^m phase_shift = np.exp(1j * theta) for k in range(m): if (L >> k) & 1: qc.cx(aux[0], y[k]) qc.p(theta, y[k]) # Cleanup: Remove auxiliary qubit qc.remove_register(aux) return qc '''
QPC002_B8
A94F6F425C58C
1
RE
1317 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: # Step 1: Compute f(x) and store in y register for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) # Step 2: Apply modular addition of L to the y register for j in range(m): if (L >> j) & 1: qc.x(y[j]) # Step 3: Apply the controlled phase shift based on whether y equals 0 (i.e., f(x) = L mod 2^m) qc.append(QFT(m, do_swaps=False).inverse(), y) # Apply inverse QFT to y qc.mcx(list(y), y[0]) # Apply multi-controlled X gate if y is in |0> qc.u1(theta, y[0]) # Apply phase shift of theta qc.mcx(list(y), y[0]) # Uncompute the multi-controlled X gate qc.append(QFT(m, do_swaps=False), y) # Apply QFT to return y to original state # Step 4: Undo the modular addition of L for j in range(m): if (L >> j) & 1: qc.x(y[j]) # Step 5: Uncompute f(x) from y register for i in range(n): for j in range(m): if (S[i] >> j) & 1: qc.cx(x[i], y[j]) return qc '''
QPC002_B8
AB316C6E051C5
1
RE
1353 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from math import pi def r(qc:QuantumCircuit, ctrl:int, targ:int, l:int): angle = 2*math.pi(1<<l), ctrl, targ qc.cp(angle, ctrl,targ) def qft(n:int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1,i) qc.cx(i,n-i-1) for i in range(0,n): qc.h(i) for j in range(1, n-1): r(qc, i+j, i, j+1) return qc def B_6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for i in range(m): for j in range(n): theta = 2*math.pi*S[j]/(1<<m)*(1<<i) qc.cp(theta, y[i], x[j]) qc = qc.compose(qft(m).inverse(), y) return qc def B_2(n:int, L:int, theta:float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c= n*[0] for a in range(n): b[a]=L%2 L//=2 for d in range(n): if b[d]==0: qc.x(d) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(B_6(n, m, S)) qc = qc.compose(B_2(m, L, theta), y) qc = qc.compose(B_6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
2
RE
1101 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from math import pi def r(qc:QuantumCircuit, ctrl:int, targ:int, l:int): angle = 2*math.pi(1<<l), ctrl, targ qc.cp(angle, ctrl,targ) def qft(n:int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1,i) qc.cx(i,n-i-1) for i in range(0,n): qc.h(i) for j in range(1, n-1): r(qc, i+j, i, j+1) return qc def B_6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for i in range(m): for j in range(n): theta = 2*math.pi*S[j]/(1<<m)*(1<<i) qc.cp(theta, y[i], x[j]) qc = qc.compose(qft(m).inverse(), y) return qc def B_2(n:int, L:int, theta:float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c= n*[0] for a in range(n): c[a]=L%2 L//=2 for d in range(n): if c[d]==0: qc.x(d) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if c[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(B_6(n, m, S)) qc = qc.compose(B_2(m, L, theta), y) qc = qc.compose(B_6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
3
RE
1528 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from math import pi def r(qc:QuantumCircuit, ctrl:int, targ:int, l:int): angle = 2*math.pi(1<<l) qc.cp(angle, ctrl,targ) def qft(n:int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1,i) qc.cx(i,n-i-1) for i in range(0,n): qc.h(i) for j in range(1, n-1): r(qc, i+j, i, j+1) return qc def B_6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for i in range(m): for j in range(n): theta = 2*math.pi*S[j]/(1<<m)*(1<<i) qc.cp(theta, y[i], x[j]) qc = qc.compose(qft(m).inverse(), y) return qc def B_2(n:int, L:int, theta:float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c= n*[0] for a in range(n): c[a]=L%2 L//=2 for d in range(n): if c[d]==0: qc.x(d) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if [i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(B_6(n, m, S)) qc = qc.compose(B_2(m, L, theta), y) qc = qc.compose(B_6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
4
RE
1470 ms
142 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from math import pi def r(qc:QuantumCircuit, ctrl:int, targ:int, l:int): angle = 2*math.pi(1<<l) qc.cp(angle, ctrl,targ) def qft(n:int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1,i) qc.cx(i,n-i-1) for i in range(0,n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def B_6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for i in range(m): for j in range(n): theta = 2*math.pi*S[j]/(1<<m)*(1<<i) qc.cp(theta, y[i], x[j]) qc = qc.compose(qft(m).inverse(), y) return qc def B_2(n:int, L:int, theta:float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c= n*[0] for a in range(n): c[a]=L%2 L//=2 for d in range(n): if c[d]==0: qc.x(d) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if [i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(B_6(n, m, S)) qc = qc.compose(B_2(m, L, theta), y) qc = qc.compose(B_6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
5
RE
1137 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from math import pi def r(qc:QuantumCircuit, control:int, target:int, l:int): angle = 2*math.pi(1<<l) qc.cp(angle, control, target) def qft(n:int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def B_6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for i in range(m): for j in range(n): qc.cp(2*math.pi*S[j]/(1<<m)*(1<<i), y[i], x[j]) qc = qc.compose(qft(m).inverse(), y) return qc def B_2(n:int, L:int, theta:float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c= [0]*n for a in range(n): c[a]=L%2 L//=2 for a in range(n): if c[a]==0: qc.x(a) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if [i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(B_6(n, m, S)) qc = qc.compose(B_2(m, L, theta), y) qc = qc.compose(B_6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
6
RE
1110 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): angle = 2*math.pi(1<<l) qc.cp(angle, control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def B_6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for i in range(m): for j in range(n): qc.cp(2*math.pi*S[j]/(1<<m)*(1<<i), y[i], x[j]) qc = qc.compose(qft(m).inverse(), y) return qc def B_2(n:int, L:int, theta:float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c= [0]*n for a in range(n): c[a]=L%2 L//=2 for a in range(n): if c[a]==0: qc.x(a) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if [i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(B_6(n, m, S)) qc = qc.compose(B_2(m, L, theta), y) qc = qc.compose(B_6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
7
RE
1370 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): #angle = 2*math.pi(1<<l) qc.cp(2*math.pi(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n:int, L:int, theta:float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c= [0]*n for a in range(n): c[a]=L%2 L//=2 for a in range(n): if c[a]==0: qc.x(a) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if [i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
8
RE
1243 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): #angle = 2*math.pi(1<<l) qc.cp(2*math.pi/(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
9
RE
1096 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): angle = 2*math.pi(1<<l) qc.cp(angle, control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for i in range(m): for j in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<i), y[i], x[j]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: c=[0]*n for i in range(n): c[i]=L%2 L//=2 for i in range(n): if c[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if c[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
10
RE
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): #angle = 2*math.pi(1<<l) qc.cp(2*math.pi(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k, y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
11
RE
1446 ms
153 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): #angle = 2*math.pi(1<<l) qc.cp(2*math.pi(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n:int, m:int, S:list[int]) -> QuantumCircuit: x = QuantumRegister(n) y = QuantumRegister(m) qc = QuantumCircuit(x,y) #Appy superposition gate on y-reg qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AB316C6E051C5
12
AC
1769 ms
184 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): qc.cp(2*math.pi/(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n: int, m: int, S: list[int]) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AB9B56579FC8B
1
AC
2032 ms
183 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math from qiskit import QuantumCircuit def B2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): if not (L>>i & 1): qc.x(i) if n==1: qc.p(theta, 0) else: qc.mcp(theta, list(range(n-1)), n-1) for i in range(n): if not (L>>i & 1): qc.x(i) return qc def QFT(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): qc.h(n-i-1) for j in range(1, n-i): qc.cp(math.pi/2**j, n-i-j-1, n-i-1) for i in range(n//2): qc.swap(i, n-i-1) return qc def IQFT(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): qc.h(n-i-1) for j in range(1, n-i): qc.cp(-math.pi/2**j, n-i-j-1, n-i-1) for i in range(n//2): qc.swap(i, n-i-1) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: for j in range(m): qc.h(y[j]) # Oracle B5 for i in range(n): qc.cp(2*math.pi*S[i]*2**j/2**m, y[j], x[i]) # IQFT qc.compose(IQFT(m), y, inplace=True) qc.compose(B2(m, L, theta), y, inplace=True) # QFT qc.compose(QFT(m), y, inplace=True) for j in range(m): # Oracle B5(minus) for i in range(n): qc.cp(-2*math.pi*S[i]*2**j/2**m, y[j], x[i]) # IQFT qc.compose(IQFT(m), y, inplace=True) return qc '''
QPC002_B8
AC1650E285B52
1
WA
1933 ms
142 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): qc.cp(2*math.pi/(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n: int, m: int, S: list[int]) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(n, L, theta)) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AC1650E285B52
2
RE
1570 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): qc.cp(2*math.pi/(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n: int, m: int, S: list[int]) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(n, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AC1650E285B52
3
AC
2288 ms
184 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math def r(qc: QuantumCircuit, control: int, target: int, l: int): qc.cp(2*math.pi/(1<<l), control, target) def qft(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n//2): qc.cx(i, n-i-1) qc.cx(n-i-1, i) qc.cx(i, n-i-1) for i in range(0, n): qc.h(i) for j in range(1, n-i): r(qc, i+j, i, j+1) return qc def b6(n: int, m: int, S: list[int]) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for k in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(1<<m)*(1<<k), y[k], x[i]) qc = qc.compose(qft(m).inverse(), y) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: b=[0]*n for i in range(n): b[i]=L%2 L//=2 for i in range(n): if b[i]==0: qc.x(i) if n>1: qc.mcp(theta, list(range(n-1)), n-1) else: qc.p(theta, 0) for i in reversed(range(n)): if b[i]==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc = qc.compose(b6(n, m, S)) qc = qc.compose(b2(m, L, theta), y) qc = qc.compose(b6(n, m, S).inverse()) return qc '''
QPC002_B8
AD5F3F54E09FE
1
RE
1082 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister def PS_oracle(n, L, theta): qc = QuantumCircuit(n) # Write your code here: #for i in range(n): # qc.h(i) binary = bin(L) ctrl_state = binary[-1:1:-1] print(ctrl_state) ctrl_state = ctrl_state + (n - len(ctrl_state))*'0' print(ctrl_state) print(ctrl_state[0]) if n > 1: if ctrl_state[0] == '0': ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.x(0) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) qc.x(0) else : ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) #qc.measure_all() else : if L == 0: qc.x(0) qc.p(theta , 0) if L == 0: qc.x(0) return qc def qft(n): qc = QuantumCircuit(n) thetas = [] for k in range(0, 1+n): thetas.append(2 * math.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 solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for idx in range(0, n): for jdx in range(0, m): theta = 2 * math.pi * S[idx] * (2**jdx) / (2**m) qc.cp(theta, x[idx], y[jdx]) qc = qc.compose(qft(m).inverse(), y) qc = qc.compose(PS_oracle(m, L, theta), y) return qc '''
QPC002_B8
AD5F3F54E09FE
2
WA
1252 ms
182 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math import numpy as np def PS_oracle(n, L, theta): qc = QuantumCircuit(n) # Write your code here: #for i in range(n): # qc.h(i) binary = bin(L) ctrl_state = binary[-1:1:-1] print(ctrl_state) ctrl_state = ctrl_state + (n - len(ctrl_state))*'0' print(ctrl_state) print(ctrl_state[0]) if n > 1: if ctrl_state[0] == '0': ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.x(0) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) qc.x(0) else : ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) #qc.measure_all() else : if L == 0: qc.x(0) qc.p(theta , 0) if L == 0: qc.x(0) return qc def qft(n): qc = QuantumCircuit(n) thetas = [] for k in range(0, 1+n): thetas.append(2 * math.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 solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for idx in range(0, n): for jdx in range(0, m): theta = 2 * math.pi * S[idx] * (2**jdx) / (2**m) qc.cp(theta, x[idx], y[jdx]) qc = qc.compose(qft(m).inverse(), y) qc = qc.compose(PS_oracle(m, L, theta), y) return qc '''
QPC002_B8
AD5F3F54E09FE
3
WA
1421 ms
154 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math import numpy as n def PS_oracle(n, L, theta): qc = QuantumCircuit(n) # Write your code here: #for i in range(n): # qc.h(i) binary = bin(L) ctrl_state = binary[-1:1:-1] print(ctrl_state) ctrl_state = ctrl_state + (n - len(ctrl_state))*'0' print(ctrl_state) print(ctrl_state[0]) if n > 1: if ctrl_state[0] == '0': ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.x(0) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) qc.x(0) else : ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) #qc.measure_all() else : if L == 0: qc.x(0) qc.p(theta , 0) if L == 0: qc.x(0) return qc def qft(n): qc = QuantumCircuit(n) thetas = [] for k in range(0, 1+n): thetas.append(2 * math.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 solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for idx in range(0, n): for jdx in range(0, m): theta = 2 * math.pi * S[idx] * (2**jdx) / (2**m) qc.cp(theta, x[idx], y[jdx]) qc = qc.compose(qft(m).inverse(), y) qc = qc.compose(PS_oracle(m, L, theta), y) qc = qc.compose(qft(m), y) for idx in range(0, n): for jdx in range(0, m): theta = -2 * math.pi * S[idx] * (2**jdx) / (2**m) qc.cp(theta, x[idx], y[jdx]) qc.h(y) return qc '''
QPC002_B8
AD5F3F54E09FE
4
WA
1016 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math import numpy as n def PS_oracle(n, L, theta): qc = QuantumCircuit(n) # Write your code here: #for i in range(n): # qc.h(i) binary = bin(L) ctrl_state = binary[-1:1:-1] print(ctrl_state) ctrl_state = ctrl_state + (n - len(ctrl_state))*'0' print(ctrl_state) print(ctrl_state[0]) if n > 1: if ctrl_state[0] == '0': ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.x(0) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) qc.x(0) else : ctrl_state = ctrl_state[-1:0:-1] print(ctrl_state) qc.mcp(theta, control_qubits= [i for i in range(1,n)] , ctrl_state = ctrl_state,target_qubit= 0) #qc.measure_all() else : if L == 0: qc.x(0) qc.p(theta , 0) if L == 0: qc.x(0) return qc def qft(n): qc = QuantumCircuit(n) thetas = [] for k in range(0, 1+n): thetas.append(2 * math.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 solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: qc.h(y) for idx in range(0, n): for jdx in range(0, m): theta = 2 * math.pi * S[idx] * (2**jdx) / (2**m) qc.cp(theta, x[idx], y[jdx]) qc = qc.compose(qft(m).inverse(), y) qc = qc.compose(PS_oracle(m, L, theta), y) qc = qc.compose(qft(m), y) for idx in range(0, n): for jdx in range(0, m): theta = -2 * math.pi * S[idx] * (2**jdx) / (2**m) qc.cp(theta, x[idx], y[jdx]) qc = qc.compose(qft(m).inverse(), y) return qc '''
QPC002_B8
AD5F3F54E09FE
5
AC
1749 ms
183 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math from qiskit import QuantumCircuit def B2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): if not (L>>i & 1): qc.x(i) if n==1: qc.p(theta, 0) else: qc.mcp(theta, list(range(n-1)), n-1) for i in range(n): if not (L>>i & 1): qc.x(i) return qc def QFT(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): qc.h(n-i-1) for j in range(1, n-i): qc.cp(math.pi/2**j, n-i-j-1, n-i-1) for i in range(n//2): qc.swap(i, n-i-1) return qc def IQFT(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): qc.h(n-i-1) for j in range(1, n-i): qc.cp(-math.pi/2**j, n-i-j-1, n-i-1) for i in range(n//2): qc.swap(i, n-i-1) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: for j in range(m): qc.h(y[j]) # Oracle B5 for i in range(n): qc.cp(2*math.pi*S[i]*2**j/2**m, y[j], x[i]) # IQFT qc.compose(IQFT(m), y, inplace=True) qc.compose(B2(m, L, theta), y, inplace=True) # QFT qc.compose(QFT(m), y, inplace=True) for j in range(m): # Oracle B5(minus) for i in range(n): qc.cp(-2*math.pi*S[i]*2**j/2**m, y[j], x[i]) # IQFT qc.compose(IQFT(m), y, inplace=True) return qc '''
QPC002_B8
AD70A27E32943
1
UME
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit import Gate import numpy as np def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: def compute_f(x_state): return sum(S[i] * int(x_state[i]) for i in range(n)) # Iterate over all possible states for the n qubits for i in range(2 ** n): x_state = [int(bit) for bit in format(i, f'0{n}b')] f_x = compute_f(x_state) f_x_mod = f_x % (2 ** m) # Apply the phase based on the condition if f_x_mod == L: # Apply phase shift qc.u1(theta, y[0]) # Applying phase gate U1 is equivalent to exp(i*theta) for one qubit return qc '''
QPC002_B8
AD70A27E32943
2
RE
1317 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from math import pi def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # Write your code here: for i in range(n): for j in range(m): angle = pi * S[i] / (2**j) qc.cu1(angle, i, n+j) # Compare f(x) with L (mod 2^m) # ... (Implement comparison logic here) # Apply phase shift if f(x) = L (mod 2^m) # ... (Implement controlled phase gate based on comparison result) return qc '''
QPC002_B8
ADCDAC93A4034
1
AC
1871 ms
156 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister import math # from qiskit.quantum_info import Statevector def quantum_fourier_transform(n: int) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n-1, -1, -1): qc.h(i) for j in range(i-1, -1, -1): qc.cp(2*math.pi/(2**(i-j+1)), j, i) i = 0 while i<n-i-1: qc.swap(i, n-i-1) i += 1 return qc def b7(n: int, m: int, S: list[int]) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) qc.compose(quantum_fourier_transform(m), y, inplace=True) for j in range(m): for i in range(n): qc.cp(2*math.pi*S[i]/(2**m)*(2**j), x[i], y[j]) qc.compose(quantum_fourier_transform(m).inverse(), y, inplace=True) return qc def b2(n: int, L: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) for i in range(n): if ((L>>i)&1)==0: qc.x(i) if n==1: qc.p(theta, 0) else: qc.mcp(theta, list(range(n-1)), n-1) for i in range(n): if ((L>>i)&1)==0: qc.x(i) return qc def solve(n: int, m: int, L: int, S: list[int], theta: float) -> QuantumCircuit: x, y = QuantumRegister(n), QuantumRegister(m) qc = QuantumCircuit(x, y) # init = [0]*(2**(n+m)) # init[3] = 1 # qc.initialize(init) # Write your code here: qc.compose(b7(n, m, S), inplace=True) qc.compose(b2(m, L, theta), y, inplace=True) qc.compose(b7(n, m, S).inverse(), inplace=True) return qc # if __name__ == "__main__": # qc = solve(2, 2, 3, [1, 2], 3.14159) # print(Statevector(qc)) '''
QPC002_Ex
A068CD909CA26
1
RE
1313 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: u0 = U rs = get_rs(n) rt = R(math.pi/3) u1 = compose(u0, rs, rt) u2 = compose(u1, rs, rt) u3 = compose(u2, rs, rt) u3 = compose(u2, rs, rt) return u3 '''
QPC002_Ex
A068CD909CA26
2
RE
1214 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: u0 = U rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u3 = compose(n, u2, rs, rt) return u3 '''
QPC002_Ex
A068CD909CA26
3
RE
1083 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u3 = compose(n, u2, rs, rt) return u3 '''
QPC002_Ex
A068CD909CA26
4
RE
1105 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
5
WA
2533 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
6
WA
1917 ms
144 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.5: def UInv(): return U().inverse() return solve(n, 1/P, UInv, R) qc = QuantumCircuit(n) # Write your code here: u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
7
WA
1264 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.5: return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4.inverse() def solve2(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rt, rs) u2 = compose(n, u1, rt, rs) u3 = compose(n, u2, rt, rs) u4 = compose(n, u3, rt, rs) return u4 '''
QPC002_Ex
A068CD909CA26
8
WA
2732 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.5: return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rt, rs) u2 = compose(n, u1, rt, rs) u3 = compose(n, u2, rt, rs) u4 = compose(n, u3, rt, rs) return u4.inverse() def solve2(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
9
WA
3355 ms
147 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 1.0/4: return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U().inverse() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rt, rs) u2 = compose(n, u1, rt, rs) u3 = compose(n, u2, rt, rs) u4 = compose(n, u3, rt, rs) return u4.inverse() def solve2(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
10
WA
2520 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.5: return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U().inverse() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rt, rs) u2 = compose(n, u1, rt, rs) u3 = compose(n, u2, rt, rs) u4 = compose(n, u3, rt, rs) return u4.inverse() def solve2(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
11
WA
3121 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 1/np.sqrt(2): return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U().inverse() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rt, rs) u2 = compose(n, u1, rt, rs) u3 = compose(n, u2, rt, rs) u4 = compose(n, u3, rt, rs) return u4.inverse() def solve2(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
12
WA
2515 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(math.pi/3, 0) else: qc.mcp(math.pi/3, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: return solve1(n, U, R) if P < 0.5: return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U().inverse() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rt, rs) u2 = compose(n, u1, rt, rs) u3 = compose(n, u2, rt, rs) u4 = compose(n, u3, rt, rs) return u4.inverse() def solve2(n, U, R): u0 = U() rs = get_rs(n) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
13
RE
1337 ms
140 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(theta, n): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: return solve1(n, U, R) if P < 0.5: return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U() rs = get_rs(n, -math.pi/3) rt = R(-math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 def solve2(n, U, R): u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
14
WA
3042 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: return solve1(n, U, R) if P < 0.5: return solve1(n, U, R) else: return solve2(n, U, R) # make w to 0, inverse def solve1(n, U, R): u0 = U() rs = get_rs(n, -math.pi/3) rt = R(-math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 def solve2(n, U, R): u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
15
RE
1244 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: u0 = U() rs = get_rs(n, math.pi/2) rt = R(math.pi/2) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u0 '''
QPC002_Ex
A068CD909CA26
16
WA
1334 ms
142 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: u0 = U() rs = get_rs(n, math.pi/2) rt = R(math.pi/2) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
17
DLE
1269 ms
145 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) u5 = compose(n, u4, rs, rt) return u5 '''
QPC002_Ex
A068CD909CA26
18
WA
1976 ms
142 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: u0 = U() rs = R(math.pi/3) rt = get_rs(n, math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4.inverse() '''
QPC002_Ex
A068CD909CA26
19
WA
2057 ms
145 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: u0 = U().inverse() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
20
WA
1580 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: u0 = QuantumCircuit(n) u0.h(range(n)) rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
21
WA
1452 ms
142 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
22
WA
1296 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.3: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
23
WA
1492 ms
143 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.2: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
24
WA
1537 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.1: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
25
WA
1537 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.1: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
26
WA
3283 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if 0.4 < P < 0.5: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A068CD909CA26
27
AC
3121 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.05: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A0AA2A3E8DDBA
1
RE
1134 ms
141 MiB
'''python from qiskit import QuantumCircuit import numpy as np """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # Apply U to the initial state |0> qc.compose(U(), inplace=True) # Calculate the optimal number of repetitions of R(θ) # Grover-like amplification strategy theta = np.arccos(np.sqrt(P)) k = int(np.round(np.pi / (4 * theta))) for _ in range(min(k, 100)): qc.compose(R(2 * theta), inplace=True) return qc '''
QPC002_Ex
A390193FB3F99
1
RE
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc.compose(U(), inplace=True) theta = 2 * 3.141592653589793 * 0.5 qc.compose(R(theta), inplace=True) return qc '''
QPC002_Ex
A390193FB3F99
2
RE
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc.compose(U(), inplace=True) theta = 2 * P for _ in range(100): qc.compose(R(theta), inplace=True) return qc '''
QPC002_Ex
A390193FB3F99
3
RE
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) ancilla_qubits = QuantumRegister(n) qc.add_register(ancilla_qubits) qc.compose(U(), inplace=True) qc.append(QFTInverse(n), ancilla_qubits) return qc '''
QPC002_Ex
A390193FB3F99
4
UME
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import QFTInverse def U(): pass def R(theta): pass def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) ancilla_qubits = QuantumRegister(n) qc.add_register(ancilla_qubits) qc.compose(U(), inplace=True) qc.append(QFTInverse(n), ancilla_qubits) return qc return qc '''
QPC002_Ex
A390193FB3F99
5
UME
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import QFTInverse def U(): # pass def R(theta): pass def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) ancilla_qubits = QuantumRegister(n) qc.add_register(ancilla_qubits) qc.compose(U(), inplace=True) qc.append(QFTInverse(n), ancilla_qubits) return qc '''
QPC002_Ex
A390193FB3F99
6
RE
1271 ms
141 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.05: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return qc '''
QPC002_Ex
A3D94D8C4B68A
1
RE
1594 ms
141 MiB
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc.compose(U(), inplace=True) # Apply the R(θ) gate theta = 2 * np.arccos(np.sqrt(P)) # Compute θ such that sin^2(θ/2) = P qc.compose(R(theta), inplace=True) return qc '''
QPC002_Ex
A3D94D8C4B68A
2
RE
1081 ms
141 MiB
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # Apply the unitary gate U to the quantum circuit qc.compose(U(), inplace=True) # Choose theta for the R(theta) gate to maximize overlap theta = np.angle(1 - np.sqrt(P)) # Example choice, you may need to optimize this # Apply the parametric gate R(theta) qc.compose(R(theta), inplace=True) return qc '''
QPC002_Ex
A3D94D8C4B68A
3
WA
1057 ms
141 MiB
'''python from qiskit import QuantumCircuit import numpy as np """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # Apply the unitary gate U to the quantum circuit qc.compose(U(), inplace=True) # Choose theta for the R(theta) gate to maximize overlap theta = np.angle(1 - np.sqrt(P)) # Example choice, you may need to optimize this # Apply the parametric gate R(theta) qc.compose(R(theta), inplace=True) return qc '''
QPC002_Ex
A3D94D8C4B68A
4
WA
1071 ms
141 MiB
'''python from qiskit import QuantumCircuit import numpy as np """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: qc.compose(U(), inplace=True) theta = np.arccos(1 - P) * 2 # Derived from P for optimal θ qc.compose(R(theta), inplace=True) return qc '''
QPC002_Ex
A4998B253E0BA
1
WA
1358 ms
139 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, angle): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(angle, 0) else: qc.mcp(angle, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if P < 0.05: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return qc '''
QPC002_Ex
A4998B253E0BA
2
WA
1347 ms
141 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: if P < 0.05: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return qc '''
QPC002_Ex
A4998B253E0BA
3
AC
3176 ms
146 MiB
'''python from qiskit import QuantumCircuit from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.05: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A6DEA4F1DEC2B
1
WA
1229 ms
143 MiB
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Write your code here: # Apply U gate once qc.compose(U(), inplace=True) # Iteratively apply R(theta) gate with incremental phase shifts for _ in range(100): qc.compose(R(0.01), inplace=True) # Small phase shift for iterative approach qc.compose(U(), inplace=True) return qc '''
QPC002_Ex
A6DEA4F1DEC2B
2
DLE
1400 ms
145 MiB
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Apply U gate first to move the initial state qc.compose(U(), inplace=True) # Apply the Grover-like iteration to refine the state towards |omega> # Here we use a loop with the max allowed number of R applications, but this could be tuned for _ in range(100): qc.compose(R(0), inplace=True) # Apply R(θ) with given θ qc.compose(U().inverse(), inplace=True) # Apply U inverse qc.compose(R(0), inplace=True) # Apply R(θ) again qc.compose(U(), inplace=True) # Apply U again return qc '''
QPC002_Ex
A6DEA4F1DEC2B
3
WA
1078 ms
141 MiB
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Apply U to create an initial superposition qc.compose(U(), inplace=True) # Apply R(θ) once or a few times to refine the state qc.compose(R(0), inplace=True) # Apply the inverse of U to bring the state closer to |ω> qc.compose(U().inverse(), inplace=True) # Apply R(θ) again if needed (depending on the results of the first application) qc.compose(R(0), inplace=True) # A final application of U to finalize the state preparation qc.compose(U(), inplace=True) return qc '''
QPC002_Ex
A6DEA4F1DEC2B
4
RE
1045 ms
140 MiB
'''python from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.05: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A6DEA4F1DEC2B
5
AC
3242 ms
146 MiB
'''python from qiskit import QuantumCircuit, QuantumRegister from qiskit.circuit.library import GlobalPhaseGate import numpy as np import math def get_rs(n, theta): qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(0, n-1)), n-1) qc.x(range(n)) return qc def compose(n, u, rs, rt): qc = QuantumCircuit(n) qc.compose(u, inplace=True) qc.compose(rt, inplace=True) qc.compose(u.inverse(), inplace=True) qc.compose(rs, inplace=True) qc.compose(u, inplace=True) return qc """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: if P < 0.05: u0 = QuantumCircuit(n) u0.ry(1, range(n)) u0.h(range(n)) else: u0 = U() rs = get_rs(n, math.pi/3) rt = R(math.pi/3) u1 = compose(n, u0, rs, rt) u2 = compose(n, u1, rs, rt) u3 = compose(n, u2, rs, rt) u4 = compose(n, u3, rs, rt) return u4 '''
QPC002_Ex
A7478C5998F3A
1
AC
2295 ms
164 MiB
'''python import math from qiskit import QuantumCircuit from scipy.special import eval_chebyt def R_0(n: int, theta: float) -> QuantumCircuit: qc = QuantumCircuit(n) qc.x(range(n)) if n == 1: qc.p(theta, 0) else: qc.mcp(theta, list(range(n - 1)), n - 1) qc.x(range(n)) return qc # Calculate the minimum number of query L def calculate_L(delta: float, lambda_: float) -> int: L = int(math.ceil(math.log(2 / delta) / math.sqrt(lambda_))) # Check if L is odd if L % 2 == 0: L += 1 return L def calculate_gamma(L: int, delta: float) -> float: # Calculate the Chebyshev polynomial T_{1/L}(1/δ) chebyshev_value = eval_chebyt(1 / L, 1 / delta) # γ is the inverse of the Chebyshev value return 1 / chebyshev_value # Define the fixed-point Grover's algorithm def fixed_point_grover(n: int, delta: float, lambda_: float, U, R) -> QuantumCircuit: L = calculate_L(delta, lambda_) # Calculate the angles for Grover's iteration gamma = calculate_gamma(L, delta) angles = [ -2 * math.atan(1 / (math.tan(2 * math.pi * j / L) * math.sqrt(1 - gamma**2))) for j in range(1, L // 2 + 1) ] qc = QuantumCircuit(n) qc.compose(U(), inplace=True) for alpha, beta in zip(angles, reversed(angles)): qc.compose(R(beta), inplace=True) qc.compose(U().inverse(), inplace=True) qc.compose(R_0(n, alpha), inplace=True) qc.compose(U(), inplace=True) return qc def solve(n: int, P: float, U, R) -> QuantumCircuit: delta = 0.01 lambda_ = P return fixed_point_grover(n, delta, lambda_, U, R) '''
QPC002_Ex
A757A60300768
1
WA
2025 ms
165 MiB
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Step 1: Apply the gate U to the initial state |0> qc.compose(U(), inplace=True) # Step 2: Determine the number of times to apply R(theta) # We will apply R(theta) multiple times to converge to |omega> # The number of applications can be adjusted based on P num_R_applications = min(100, int(100 * (1 - P))) # Example heuristic # Step 3: Apply R(theta) multiple times theta = 0.1 # Example value for theta, can be adjusted for _ in range(num_R_applications): qc.compose(R(theta), inplace=True) return qc '''
QPC002_Ex
A757A60300768
2
DLE
1987 ms
156 MiB
'''python from qiskit import QuantumCircuit """ You can apply U and R as follows: qc.compose(U(), inplace=True) qc.compose(R(theta), inplace=True) """ def solve(n: int, P: float, U, R) -> QuantumCircuit: qc = QuantumCircuit(n) # Initialize the state |omega> using U and R gates qc.compose(U(), inplace=True) qc.compose(R(0), inplace=True) # Initialize |omega> with R(0) # Apply R(theta) gate to prepare the state |omega> for _ in range(100): qc.compose(R(0.01), inplace=True) # Apply R(0.01) gate iteratively return qc '''