Editorial
When an X X X gate and an H H H gate are applied sequentially to the rightmost qubit, it results in the following:
∣ x ⟩ n ∣ 0 ⟩ 1 → X ∣ x ⟩ n ∣ 1 ⟩ 1 → H 1 2 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) \begin{equation}
\ket{x}_n\ket{0}_1 \xrightarrow{X} \ket{x}_n\ket{1}_1 \xrightarrow{H} \frac{1}{\sqrt{2}} (\ket{x}_n\ket{0}_1 - \ket{x}_n\ket{1}_1)
\end{equation} ∣ x ⟩ n ∣ 0 ⟩ 1 X ∣ x ⟩ n ∣ 1 ⟩ 1 H 2 1 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 )
By applying the oracle O O O , it results in the following:
1 2 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) → O { 1 2 ( ∣ x ⟩ n ∣ 1 ⟩ 1 − ∣ x ⟩ n ∣ 0 ⟩ 1 ) ( f ( x ) = 1 ) 1 2 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) ( f ( x ) = 0 ) \begin{equation}
\frac{1}{\sqrt{2}} (\ket{x}_n\ket{0}_1 - \ket{x}_n\ket{1}_1)
\xrightarrow{O}
\begin{cases}
\frac{1}{\sqrt{2}} (\ket{x}_n\ket{1}_1 - \ket{x}_n\ket{0}_1) & (f(x)=1) \\
\frac{1}{\sqrt{2}} (\ket{x}_n\ket{0}_1 - \ket{x}_n\ket{1}_1) & (f(x)=0)
\end{cases}
\end{equation} 2 1 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) O { 2 1 ( ∣ x ⟩ n ∣ 1 ⟩ 1 − ∣ x ⟩ n ∣ 0 ⟩ 1 ) 2 1 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) ( f ( x ) = 1 ) ( f ( x ) = 0 )
By noting that 1 2 ( ∣ x ⟩ n ∣ 1 ⟩ 1 − ∣ x ⟩ n ∣ 0 ⟩ 1 ) = − 1 2 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) \frac{1}{\sqrt{2}} (\ket{x}_n\ket{1}_1 - \ket{x}_n\ket{0}_1) = - \frac{1}{\sqrt{2}} (\ket{x}_n\ket{0}_1 - \ket{x}_n\ket{1}_1) 2 1 ( ∣ x ⟩ n ∣ 1 ⟩ 1 − ∣ x ⟩ n ∣ 0 ⟩ 1 ) = − 2 1 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) , and applying an H H H gate and then an X X X gate in sequence to the rightmost qubit, we obtain the following, which indicates that the expected operation has been implemented:
{ 1 2 ( ∣ x ⟩ n ∣ 1 ⟩ 1 − ∣ x ⟩ n ∣ 0 ⟩ 1 ) ( f ( x ) = 1 ) 1 2 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) ( f ( x ) = 0 ) → H { − ∣ x ⟩ n ∣ 1 ⟩ 1 ( f ( x ) = 1 ) ∣ x ⟩ n ∣ 1 ⟩ 1 ( f ( x ) = 0 ) → X { − ∣ x ⟩ n ∣ 0 ⟩ 1 ( f ( x ) = 1 ) ∣ x ⟩ n ∣ 0 ⟩ 1 ( f ( x ) = 0 ) \begin{align}
\begin{cases}
\frac{1}{\sqrt{2}} (\ket{x}_n\ket{1}_1 - \ket{x}_n\ket{0}_1) & (f(x)=1) \\
\frac{1}{\sqrt{2}} (\ket{x}_n\ket{0}_1 - \ket{x}_n\ket{1}_1) & (f(x)=0)
\end{cases}
& \xrightarrow{H}
\begin{cases}
-\ket{x}_n\ket{1}_1 & (f(x)=1) \\
\ket{x}_n\ket{1}_1 & (f(x)=0)
\end{cases} \\
& \xrightarrow{X}
\begin{cases}-\ket{x}_n\ket{0}_1 & (f(x)=1) \\
\ket{x}_n\ket{0}_1 & (f(x)=0)
\end{cases}
\end{align} { 2 1 ( ∣ x ⟩ n ∣ 1 ⟩ 1 − ∣ x ⟩ n ∣ 0 ⟩ 1 ) 2 1 ( ∣ x ⟩ n ∣ 0 ⟩ 1 − ∣ x ⟩ n ∣ 1 ⟩ 1 ) ( f ( x ) = 1 ) ( f ( x ) = 0 ) H { − ∣ x ⟩ n ∣ 1 ⟩ 1 ∣ x ⟩ n ∣ 1 ⟩ 1 ( f ( x ) = 1 ) ( f ( x ) = 0 ) X { − ∣ x ⟩ n ∣ 0 ⟩ 1 ∣ x ⟩ n ∣ 0 ⟩ 1 ( f ( x ) = 1 ) ( f ( x ) = 0 )
For the case where n = 3 n = 3 n = 3 , the circuit diagram 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.x(y)
qc.h(y)
qc.compose(o, inplace = True )
qc.h(y)
qc.x(y)
return qc