B2: XOR Oracle

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: admin

Editorial

The state after the application of the oracle y=yx1x2...xny' = y \oplus x_1 \oplus x_2 \oplus ... \oplus x_n can be represented by placing parentheses and nesting the operation \oplus for two qubits as follows:

yx1x2...xn=(((yx1)x2)...)xn)\begin{equation} y \oplus x_1 \oplus x_2 \oplus ... \oplus x_n = (((y \oplus x_1) \oplus x_2) \oplus ... ) \oplus x_n) \end{equation}

That is, using each qubit from x1x_1 to xnx_n as control bits and yy as the target bit, the oracle can be realized by applying a total of nn CXCX gates.

yCX(x1,y)yx1CX(x2,y)yx1x2       ...CX(xn,y)yx1x2...xn\begin{align} y &\xrightarrow{CX(x_1, y)} y \oplus x_1 \nonumber\\ &\xrightarrow{CX(x_2, y)} y \oplus x_1 \oplus x_2 \nonumber\\ &\ \ \ \ \ \ \ ... \nonumber\\ &\xrightarrow{CX(x_n, y)} y \oplus x_1 \oplus x_2 \oplus ... \oplus x_n\\ \end{align}

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit, QuantumRegister
 
 
def solve(n: int) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
 
    for i in range(n):
      qc.cx(x[i], y)
 
    return qc