Editorial
Applying an oracle O to the state ∣x⟩n∣0⟩1 results in the following:
∣x⟩n∣0⟩1O{∣x⟩n∣1⟩1∣x⟩n∣0⟩1(f(x)=1)(f(x)=0)
Next, by applying a Z gate to the rightmost qubit, we get:
{∣x⟩n∣1⟩1∣x⟩n∣0⟩1(f(x)=1)(f(x)=0)Z{−∣x⟩n∣1⟩1∣x⟩n∣0⟩1(f(x)=1)(f(x)=0)
Applying the oracle O again results in the following, realizing the desired operation.
{−∣x⟩n∣1⟩1∣x⟩n∣0⟩1(f(x)=1)(f(x)=0)O{−∣x⟩n∣0⟩1∣x⟩n∣0⟩1(f(x)=1)(f(x)=0)
he circuit diagram for the case where n=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