5.1.2 Bit oracles

This idea works not only for the logical AND\operatorname{AND} function but in fact for any function

f:{0,1}n{0,1}\displaystyle f\colon\{0,1\}^{n}\to\{0,1\}

that takes nn bits as an input and returns a single bit. For any such function ff we can define a reversible operation on (n+1)(n+1) bits:

[x1,,xn,y][x1,,xn,yf(x1,,xn)]\displaystyle[x_{1},\dots,x_{n},y]\mapsto[x_{1},\dots,x_{n},y\oplus f(x_{1},% \dots,x_{n})] (5.4)

for all x1,,xn,y{0,1}x_{1},\dotsc,x_{n},y\in\{0,1\}. Just like Eq. 5.3, this operation is its own inverse, i.e., applying it twice is equivalent to doing nothing.

It is reassuring that we can implement any function f:{0,1}n{0,1}f:\{0,1\}^{n}\to\{0,1\} reversibly as in Eq. 5.4. First of all, it means that any computation on a classical computer can be done in such a way that we can recover the original input. Second, once the computation is made reversible, we can run it also on a quantum computer. This implies that quantum computers can compute everything that classical computers can! Third, this means that we can implement any function ff as an oracle on a quantum computer.

Let’s see how this actually works. The quantum version of the operation in Eq. 5.4 is defined as follows:

Uf|x1,,xn,y=|x1,,xn,yf(x1,,xn)U_{f}\left|x_{1},\dots,x_{n},y\right\rangle=\left|x_{1},\dots,x_{n},y\oplus f(% x_{1},\dots,x_{n})\right\rangle (5.5)

for all x1,,xn,y{0,1}x_{1},\dotsc,x_{n},y\in\{0,1\}. Eq. 5.5 defines how UfU_{f} acts on all basis states of n+1n+1 qubits. As usual, we extend this definition to an arbitrary state on n+1n+1 qubits by linearity. Since UfU_{f} simply permutes the basis states, you can check as in 4.4 that it defines a valid quantum operation.

We call the quantum operation UfU_{f} defined in Eq. 5.5 the bit oracle for ff (we could also call the function in Eq. 5.4 a classical bit oracle, but we will not need this name). The term ‘oracle’ simply means that applying this operation is like asking an all-powerful oracle to tell us the value of the function on any given input. We do not know how the oracle is implemented precisely nor where it gets its answer from, we will just count how many questions we need to ask the oracle to learn some property of the function ff. Many interesting algorithmic problems can be modeled in this fashion, as we will see in the remainder of this quest.

The concept of an oracle is a bit like playing the ‘guess my number’ game. Your friend (the oracle) comes up with some number and you ask them questions of the form “is your number x”? Your friend answers each of these questions with either “yes” or “no”. In other words, your friend is hiding a function that evaluates to “no” on all inputs, except on one (the number they have in mind). With every question you ask, you gain more information about which number it could possibly be. You may wonder how many questions you need to ask to determine the number. Moreover, what if you could ask your questions to a quantum oracle such as UfU_{f} instead of your friend? Could you figure out the answer with less questions? In this week’s quest, we will see several interesting examples where this indeed is the case!

Let us discuss some examples of bit oracles. Assume ff is the AND function. According to Eq. 5.1, we can interpret AND as multiplication modulo 2 since AND(x1,x2)=x1x2\operatorname{AND}(x_{1},x_{2})=x_{1}x_{2}. The corresponding bit oracle

UAND|a,b,c=|a,b,cab\displaystyle U_{\operatorname{AND}}\left|a,b,c\right\rangle=\left|a,b,c\oplus ab\right\rangle

is nothing but the Toffoli gate from 4.4. Even for n=1n=1 and the function f(x)=xf(x)=x that simply returns its input, we get an interesting result: for all a,b{0,1}a,b\in\{0,1\},

Uf|a,b=|a,ba.\displaystyle U_{f}\left|a,b\right\rangle=\left|a,b\oplus a\right\rangle.

This is just the familiar controlled-NOT gate CNOT12\mathrm{CNOT}_{1\to 2} from Eq. 3.64 in Section 3.2.4! Thus, the bit oracle construction reproduces several interesting quantum operations that we had previously defined by hand. In the following exercise you can try to implement all other bit oracles for functions of a single bit.

Exercise 5.1 (Bit oracle for a one-bit function).

Let f:{0,1}{0,1}f:\{0,1\}\to\{0,1\} be a function with a single input and output bit. Such a function is fully specified by the values f(0),f(1){0,1}f(0),f(1)\in\{0,1\}. These are two bits, so there are precisely four such functions. We just discussed how to implement the bit oracle UfU_{f} for the function f(x)=xf(x)=x. Can you implement the bit oracle UfU_{f} for the other three functions in Quirky?

Solution. The other three functions are f=NOTf=\mathrm{NOT} and the two constant functions f(0)=f(1)=0f(0)=f(1)=0 and f(0)=f(1)=1f(0)=f(1)=1.
  • For the NOT function:

    UNOT|a,b=|a,bNOT(a)=|a,ba1=|a,NOT(ba),\displaystyle U_{\mathrm{NOT}}\left|a,b\right\rangle=\left|a,b\oplus\mathrm{% NOT}(a)\right\rangle=\left|a,b\oplus a\oplus 1\right\rangle=\left|a,\mathrm{% NOT}(b\oplus a)\right\rangle,

    which we recognize can be written as a composition of a controlled-NOT operation and a NOT operation on the second qubit, i.e.,

    UNOT=(INOT)CNOT12.\displaystyle U_{\mathrm{NOT}}=(I\otimes\mathrm{NOT})\,\mathrm{CNOT}_{1\to 2}.

    In Quirky this looks as follows:

    [Uncaptioned image]

  • For the all-zeros function f(0)=f(1)=0f(0)=f(1)=0:

    Uf|a,b=|a,b,\displaystyle U_{f}\left|a,b\right\rangle=\left|a,b\right\rangle,

    so we do not have to do anything at all:

    [Uncaptioned image]

  • For the all-ones function f(0)=f(1)=1f(0)=f(1)=1:

    Uf|a,b=|a,b1=|a,NOT(b),\displaystyle U_{f}\left|a,b\right\rangle=\left|a,b\oplus 1\right\rangle=\left% |a,\mathrm{NOT}(b)\right\rangle,

    so we only need to invert the second qubit:

    [Uncaptioned image]