A2: Controlled Gate I

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Problem Statement

Given an integer nn, implement an (n+1)(n + 1)-qubit oracle OO on a quantum circuit qc\mathrm{qc}.

The oracle OO acts on basis states mkn\ket{m}\ket{k}_n, where 0m<20 \leq m < 2 and 0k<2n0 \leq k < 2^n, as follows:

  • If k=0k = 0,
mknO12(0+(1)m1)kn.\ket{m} \ket{k}_n \xrightarrow{O} \frac{1}{\sqrt{2}} (\ket{0} + (-1)^m \ket{1}) \ket{k}_n.
  • Otherwise,
mknOmkn.\ket{m} \ket{k}_n \xrightarrow{O} \ket{m} \ket{k}_n.

Constraints

  • 1n101 \leq n \leq 10
  • Integers must be encoded by little-endian.
  • Global phase is ignored in judge.
  • The submitted code must follow the specified format:
from qiskit import QuantumCircuit, QuantumRegister
 
 
def solve(n: int) -> QuantumCircuit:
    m, k = QuantumRegister(1), QuantumRegister(n)
    qc = QuantumCircuit(m, k)
    # Write your code here:
 
    return qc

Hints

Open
  • You can apply the quantum gate gg to all the qubits in the quantum register kk as follows:
from qiskit import QuantumRegister
 
k = QuantumRegister(n)
qc.g(k)
  • You can apply the multi-controlled gate of any quantum gate Gate\mathrm{Gate} as follows:
from qiskit.circuit.library import Gate
 
qc.append(Gate().control(n - 1), range(n))

Please sign in to submit your answer.