Editorial
For integers x x x with 0 ≤ x < 2 n 0 \leq x < 2^n 0 ≤ x < 2 n , applying the operator A = 2 ∣ 0 ⟩ ⟨ 0 ∣ − I A = 2\ket{0}\bra{0} - I A = 2 ∣ 0 ⟩ ⟨ 0 ∣ − I to ∣ x ⟩ \ket{x} ∣ x ⟩ results in the following:
( 2 ∣ 0 ⟩ ⟨ 0 ∣ − I ) ∣ x ⟩ = 2 ∣ 0 ⟩ ⟨ 0 ∣ x ⟩ − ∣ x ⟩ = { ∣ x ⟩ ( x = 0 ) − ∣ x ⟩ ( x ≠ 0 ) \begin{align}
(2\ket{0}\bra{0} - I) \ket{x} &= 2\ket{0} \braket{0|x} - \ket{x} \nonumber \\
&=
\begin{cases}
\ket{x} & (x = 0) \\
-\ket{x} & (x \neq 0)
\end{cases}
\end{align} ( 2 ∣ 0 ⟩ ⟨ 0 ∣ − I ) ∣ x ⟩ = 2 ∣ 0 ⟩ ⟨ 0∣ x ⟩ − ∣ x ⟩ = { ∣ x ⟩ − ∣ x ⟩ ( x = 0 ) ( x = 0 )
There are multiple approaches to implementing this operation.
Solution 1
Since this problem does not concern global phase, we can consider the following operation:
∣ x ⟩ → { − ∣ x ⟩ ( x = 0 ) ∣ x ⟩ ( x ≠ 0 ) \begin{equation}
\ket{x} \xrightarrow{}
\begin{cases}
- \ket{x} & (x = 0) \\
\ket{x} & (x\neq 0)
\end{cases}
\end{equation} ∣ x ⟩ { − ∣ x ⟩ ∣ x ⟩ ( x = 0 ) ( x = 0 )
By applying X X X gates to all qubits, ∣ 0 ⟩ \ket{0} ∣ 0 ⟩ is transformed to ∣ 2 n − 1 ⟩ \ket{2^n - 1} ∣ 2 n − 1 ⟩ .
Next, by designating one qubit as the target bit and the remaining ( n − 1 ) (n-1) ( n − 1 ) qubits as control bits, we can apply a multi-controlled Z Z Z gate to invert the sign of the computation basis state ∣ 2 n − 1 ⟩ \ket{2^n - 1} ∣ 2 n − 1 ⟩ only in the superposition.
Finally, by applying X X X gates again to all qubits, the desired operation is achieved.
For the case where n = 3 n = 3 n = 3 , the circuit diagram is shown below.
Solution 2
Similar to Solution 1, we leverage the fact that changes in the global phase are not considered.
Let the lower ( n − 1 ) (n-1) ( n − 1 ) bits of x x x be x l x_l x l , and the upper 1 1 1 bit be x h x_h x h . Also, let the number obtained by flipping all bits of x l x_l x l and x h x_h x h be represented as ¬ x l \lnot x_l ¬ x l and ¬ x h \lnot x_h ¬ x h , respectively. Thus, we have ∣ x ⟩ = ∣ x l ⟩ n − 1 ∣ x h ⟩ 1 \ket{x} = \ket{x_l}_{n-1} \ket{x_h}_1 ∣ x ⟩ = ∣ x l ⟩ n − 1 ∣ x h ⟩ 1 .
After applying the X X X gates to ∣ x ⟩ \ket{x} ∣ x ⟩ and then applying the H H H gate to the n n n -th bit, we obtain:
∣ x ⟩ → X ∣ ¬ x l ⟩ n − 1 ∣ ¬ x h ⟩ 1 → H { 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x h = 0 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x h = 1 ) \begin{equation}
\ket{x} \xrightarrow{X} \ket{\lnot x_l}_{n-1} \ket{\lnot x_h}_1 \xrightarrow{H}
\begin{cases}
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 - \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_h=0) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 + \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_h=1)
\end{cases}
\end{equation} ∣ x ⟩ X ∣ ¬ x l ⟩ n − 1 ∣ ¬ x h ⟩ 1 H { 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x h = 0 ) ( x h = 1 )
Next, by applying a multi-controlled X X X gate with the ( n − 1 ) (n-1) ( n − 1 ) bits excluding the n n n -th bit as control bits and the n n n -th bit as the target bit, we get:
{ 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x h = 0 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x h = 1 ) → M C X { 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) ( x l = 0 , x h = 0 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) ( x l = 0 , x h = 1 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x l ≠ 0 , x h = 0 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x l ≠ 0 , x h = 1 ) \begin{equation}
\begin{cases}
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 - \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_h=0) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 + \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_h=1)
\end{cases}
\xrightarrow{MCX}
\begin{cases}
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{1}_1 - \ket{\lnot x_l}_{n-1} \ket{0}_1) & (x_l=0, \ x_h=0) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{1}_1 + \ket{\lnot x_l}_{n-1} \ket{0}_1) & (x_l=0, \ x_h=1) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 - \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_l\neq 0, \ x_h=0) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 + \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_l\neq 0, \ x_h=1)
\end{cases}
\end{equation} { 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x h = 0 ) ( x h = 1 ) MCX ⎩ ⎨ ⎧ 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x l = 0 , x h = 0 ) ( x l = 0 , x h = 1 ) ( x l = 0 , x h = 0 ) ( x l = 0 , x h = 1 )
By noting that 1 2 ( ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 − ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 ) = − 1 2 ( ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 ) \frac{1}{\sqrt{2}}(\ket{\lnot x_l}_n\ket{1}_1-\ket{\lnot x_l}_n\ket{0}_1)=-\frac{1}{\sqrt{2}}(\ket{\lnot x_l}_n\ket{0}_1-\ket{\lnot x_l}_n\ket{1}_1) 2 1 ( ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 − ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 ) = − 2 1 ( ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 ) and 1 2 ( ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 + ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 ) = 1 2 ( ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 ) \frac{1}{\sqrt{2}}(\ket{\lnot x_l}_n\ket{1}_1+\ket{\lnot x_l}_n\ket{0}_1)=\frac{1}{\sqrt{2}}(\ket{\lnot x_l}_n\ket{0}_1+\ket{\lnot x_l}_n\ket{1}_1) 2 1 ( ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 + ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 ) = 2 1 ( ∣ ¬ x l ⟩ n ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n ∣ 1 ⟩ 1 ) , after applying the H H H gate to the n n n -th bit and applying the X X X gates to all qubits, we have:
{ 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) ( x l = 0 , x h = 0 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) ( x l = 0 , x h = 1 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x l ≠ 0 , x h = 0 ) 1 2 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x l ≠ 0 , x h = 1 ) → H { − ∣ ¬ x l ⟩ n − 1 ∣ ¬ x h ⟩ 1 ( x l = 0 , x h = 0 ) ∣ ¬ x l ⟩ n − 1 ∣ ¬ x h ⟩ 1 ( x l ≠ 0 ∨ x h = 1 ) → X { − ∣ x ⟩ ( x = 0 ) ∣ x ⟩ ( x ≠ 0 ) \begin{align}
\begin{cases}
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{1}_1 - \ket{\lnot x_l}_{n-1} \ket{0}_1) & (x_l=0, \ x_h=0) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{1}_1 + \ket{\lnot x_l}_{n-1} \ket{0}_1) & (x_l=0, \ x_h=1) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 - \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_l\neq 0, \ x_h=0) \\
\frac{1}{\sqrt{2}} (\ket{\lnot x_l}_{n-1} \ket{0}_1 + \ket{\lnot x_l}_{n-1} \ket{1}_1) & (x_l\neq 0, \ x_h=1)
\end{cases}
& \xrightarrow{H}
\begin{cases}
-\ket{\lnot x_l}_{n-1} \ket{\lnot x_h}_1 & (x_l=0, \ x_h=0) \\
\ket{\lnot x_l}_{n-1} \ket{\lnot x_h}_1 & (x_l\neq 0\lor x_h=1)
\end{cases} \\
& \xrightarrow{X}
\begin{cases}-\ket{x} & (x=0) \\
\ket{x} & (x\neq 0)
\end{cases}
\end{align} ⎩ ⎨ ⎧ 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 − ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) 2 1 ( ∣ ¬ x l ⟩ n − 1 ∣ 0 ⟩ 1 + ∣ ¬ x l ⟩ n − 1 ∣ 1 ⟩ 1 ) ( x l = 0 , x h = 0 ) ( x l = 0 , x h = 1 ) ( x l = 0 , x h = 0 ) ( x l = 0 , x h = 1 ) H { − ∣ ¬ x l ⟩ n − 1 ∣ ¬ x h ⟩ 1 ∣ ¬ x l ⟩ n − 1 ∣ ¬ x h ⟩ 1 ( x l = 0 , x h = 0 ) ( x l = 0 ∨ x h = 1 ) X { − ∣ x ⟩ ∣ x ⟩ ( x = 0 ) ( 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:
Solution 1
from qiskit import QuantumCircuit
from qiskit.circuit.library import ZGate
def solve ( n : int ) -> QuantumCircuit:
qc = QuantumCircuit(n)
qc.x( range (n))
qc.append(ZGate().control(n - 1 ), range (n))
qc.x( range (n))
return qc
Solution 2
from qiskit import QuantumCircuit
def solve ( n : int ) -> QuantumCircuit:
qc = QuantumCircuit(n)
qc.x( range (n))
qc.h(n - 1 )
qc.mcx( list ( range (n - 1 )), n - 1 )
qc.h(n - 1 )
qc.x( range (n))
return qc