B2: Convert Bit-Flip into Phase-Flip I

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: Not_Leonian

Editorial

Applying an oracle OO to the state xn01\ket{x}_n\ket{0}_1 results in the following:

xn01O{xn11(f(x)=1)xn01(f(x)=0)\begin{equation} \ket{x}_n \ket{0}_1 \xrightarrow{O} \begin{cases} \ket{x}_n \ket{1}_1 & (f(x) = 1) \\ \ket{x}_n \ket{0}_1 & (f(x) = 0) \end{cases} \end{equation}

Next, by applying a ZZ gate to the rightmost qubit, we get:

{xn11(f(x)=1)xn01(f(x)=0)Z{xn11(f(x)=1)xn01(f(x)=0)\begin{equation} \begin{cases} \ket{x}_n\ket{1}_1 & (f(x)=1) \\ \ket{x}_n\ket{0}_1 & (f(x)=0) \end{cases} \xrightarrow{Z} \begin{cases} -\ket{x}_n\ket{1}_1 & (f(x)=1) \\ \ket{x}_n\ket{0}_1 & (f(x)=0) \end{cases} \end{equation}

Applying the oracle OO again results in the following, realizing the desired operation.

{xn11(f(x)=1)xn01(f(x)=0)O{xn01(f(x)=1)xn01(f(x)=0)\begin{equation} \begin{cases} -\ket{x}_n\ket{1}_1 & (f(x)=1) \\ \ket{x}_n\ket{0}_1 & (f(x)=0) \end{cases} \xrightarrow{O} \begin{cases} -\ket{x}_n\ket{0}_1 & (f(x)=1) \\ \ket{x}_n\ket{0}_1 & (f(x)=0) \end{cases} \end{equation}

he circuit diagram for the case where n=3n = 3 is shown below.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit, QuantumRegister
 
 
def solve(n: int, o: QuantumCircuit) -> QuantumCircuit:
    x, y = QuantumRegister(n), QuantumRegister(1)
    qc = QuantumCircuit(x, y)
 
    qc.compose(o, inplace=True)
    qc.z(y)
    qc.compose(o, inplace=True)
 
    return qc