Phys 150 All
Phys 150 All
a · x ⌘ a0 x0 a1 x1 ··· an 1 xn 1 . (11.2)
a · x = a 0 x0 a 1 x1 · · · a n 1 xn 1 (11.3)
⇢
1 if an odd number of terms is 1
= (11.4)
0 if an even number of terms is 1
For example for n = 4, if the bits of a are 1101 and the bits of x are 1110 (recall that the zeroth bit is
the least significant, i.e. the rightmost one) then1
Hence, for these values of a and x, f (x) = 0. If we take x = 1000 then f (x) = 0 + 0 + 0 + 1 mod 2 = 1.
Classically we can only determine the bits of a one at a time. The k-th bit of a can be determined
by feeding in x = 2k . To see this, consider the binary representations of a and x:
a = a0 + a1 21 + · · · + ak 2k + · · · + an 12
n 1
,
(11.6)
x = x0 + x1 21 + · · · + xk 2k + · · · + xn 1 2n 1
.
97
98 CHAPTER 11. THE BERNSTEIN-VAZIRANI ALGORITHM
We will see that the quantum algorithm succeeds in determining a with just one call!
A schematic diagram of a general reversible unitary transformation which takes an n-bit input x
in the upper register and generates an m-bit output f (x) in the lower register is shown in Fig. 9.2. For
the Bernstein-Vazirani Algorithm there are n qubits in the upper register but only 1 qubit in the lower
register. In addition, the unitary Uf is surrounded by Hadamards, as shown in Fig. 11.1. The upper
register is set to |0in and the lower qubit to |1i. This is the same circuit as for the Deutsch-Josza
algorithm, see problem 11.1.
n xn n xn n
0 H H a
n n
Uf
1 H H 1
Figure 11.1: Circuit diagram for the Bernstein-Vazirani algorithm. In the final state the upper (input)
register contains |ai while the lower (output) qubit reverts to its initial state |1i. The desired value of
a can therefore be read o↵ by measuring the upper register.
Acting with H on |0i gives an equal linear superposition of the two basis states. Similarly acting
with H ⌦n on |0in gives an equal superposition of the 2n basis states. Hence, including the lower
register, the state inputted to Uf is
2 1 n
⌦n 1 X |0i |1i
H |0in ⌦ H|1i = p |xin ⌦ p . (11.7)
n
2 x=0 2
For each term in the superposition, the function Uf acts in the same way as for the Deutsch algorithm
described in Chapter 10. The lower qubit is flipped if f (x) = 1, which is the same as changing the sign
of the state. If f (x) = 0 there is no change. Hence each term in the superposition acquires a factor of
( 1)f (x) , so the state of the system immediately after the action of Uf is
2 1n
1 X ( |0i |1i )
p ( 1)f (x) |xin ⌦ p . (11.8)
n
2 x=0 2
where x · y is the bitwise inner product with moduloP2n addition defined in Eq. (11.2), and we have
1
used the fact that we only need to know whether j=0 xj yj is even or odd. All the amplitudes,
P
cy ⌘ (1/2 )( 1) , are equal in magnitude and the sign is +1 if nj=01 xj yj is even (note that each
n x·y
P
term in the sum is 1 or 0) and the sign is 1 if nj=01 xj yj is odd.
Hence, combining Eqs. (11.8) and (11.10), the amplitude to find the upper register in state |yin ⌘
|yn 1 i · · · |y1 i|y0 i is
2 1n
1 X
cy = n ( 1)f (x)+x·y
2
x=0
2 3
n 1 1
1 Y 4X
= n ( 1)(aj +yj )xj 5 . (11.11)
2
j=0 xj =0
Let us evaluate this for the state where yj = aj for all j, in which case P aj + yj = 2 or 0. If xj = 0
then ( 1)(aj +yj )xj = 1 and if xj = 1 we also get ( 1)(aj +yj )xj = 1, so 1xj =0 ( 1)(aj +yj )xj = 2, i.e. the
two terms add up in phase. Hence, from Eq. (11.11), we have ca = 1. Since the total probability must
add up to 1 this means that all the other amplitudes must be zero. To see that this is indeed the case,
note that for each qubit where yj 6= aj , aj + yj = 1 and so the sum over xj for these qubits gives zero.
The final result in Eq. (11.11) is a product over terms for each qubit and so we get zero, as required.
Including the lower (output) qubit, the final state is
and a measurement of the upper register in Fig. 11.1 gives a, with probability one, even though we
made just one call to the function.
Since a classical computation of a requires n function calls, we have obtained a “quantum speedup”
of n. Note that the procedure is analogous to Deutsch’s algorithm. The first set of Hadamards
generates a superposition of inputs to the gate Uf which “evaluates”2 the function for all 2n inputs
using quantum parallelism, and then the second set of Hadamards destroys all the outputs apart from
a, using quantum interference.
a = 11010
x4
x3
Uf = x2
x1
x0
y X X X y + a x
Figure 11.2: A circuit diagram for n = 5 to implement the function f (x) = a · x with a = 11010,
i.e. f (x) = x1 + x3 + x4 mod 2. The circuit flips the output qubit, the lowest one, initialized to y,
whenever x1 x3 x4 = 1. (Note that flipping y is equivalent to adding 1 to y mod 2.) Hence the
final value of the output qubit is y (a · x) = y x1 x3 x4 as required.
x4 0 H H 0 X 1
x3 0 H H 0 X 1
x2 0 H H 0 0
x1 0 H H = 0 X 1
x0 0 H H 0 0
y 1 H X X X H 1 1
Figure 11.3: Sandwiching the circuit for Uf in Fig. 11.2 between Hadamards, and realizing that the
e↵ect of the Hadamards is to interchange the control and target qubits in the CNOT (control-X) gates,
we see immediately that the final state of the upper (input) register contains a = 11010.
except for the one in which the upper (input) register is in the state |ai. The second
suggests a specific mechanism for representing the subroutine that executes Uf and then
shows that sandwiching such a mechanism between Hadamards automatically (my italics)
imprints a on the upper register. Interestingly, quantum mechanics appears in the second
method only because it allows the reversal of the control and target qubits of a cNOT
operation solely by means of 1-qubit (Hadamard) gates.”
(I have used the conventional spelling of “qubit” rather than Mermin’s idiosyncratic “Qbit”.)
Problems
11.1. The Deutsch-Josza Algorithm
This is an extension of the Deutsch algorithm discussed in class. Recall that in Deutsch’s
algorithm the input is one bit and the output is also one bit. In the Deutsch-Josza algorithm,
the output is still one bit but the input has n bits, so there are 2n distinct inputs. We are told
that either the function is “constant” (in which case the function outputs the same value for all
2n inputs) or is “balanced” (in which case an equal number of inputs give the results 1 and 0).
11.2. AN ALTERNATIVE DERIVATION 101
Clearly this is a very artificially constructed problem but it will be our first quantum algorithm
with more than a one-bit input. Note that it is precisely the Deutsch algorithm for n = 1.
The circuit for the Deutsch-Josza is almost identical to that for the Deutsch algorithm except
that the upper qubit in the Deutsch algorithm (sometimes called the “input” qubit) is replaced
by a n-qubit register. The circuit is shown in the figure below.
ψ ψ ψ
0 1 2
0 x nn xn
n H H n y
n
Uf
1 H H 1
The function Uf acts as follows on computational basis states |xin and |zi:
where x is an n-bit integer, |xi is the state of the n-qubit upper register in the figure, z and f (x)
are 1-bit integers, and |zi is the lower qubit in the figure.
As in the Deutsch algorithm, the lower qubit is initialized to |1i. In the Deutsch algorithm, the
upper qubit is initialized to |0i. Here the single qubit is replaced by an n-qubit register which,
by analogy, is initialized to |0in .
The fact that the value of f (x) only changes the overall sign of the state is called the phase
kickback trick by Vathsan.
(iii) Show that after the action of the second set of Hadamards on the n-qubit register, the state
of that register is
2n 1
⌦n 1 X
| 2 in = H | 1 in = n ( 1)[f (x)+x·y] |yin , (11.16)
2
x,y=0
x · y = x 0 y0 x 1 y1 ... xn 1 yn 1 . (11.17)
(iv) The upper register is then measured, and an n-bit integer y is obtained. Show that if
the function is a constant then y = 0 with probability 1. Show also that if the function
is balanced then one must get a non-zero value of y. Hence the Deutsch-Josza algorithm
succeeds with just one function call.
102 CHAPTER 11. THE BERNSTEIN-VAZIRANI ALGORITHM
(v) How does this compare with a classical approach? The only thing one can do classically is
keep computing f (x) for di↵erent values of x and seeing if one gets more than one value
for the output. If the function is balanced, one would probably get di↵erent outputs quite
quickly. If the function is constant one would need to evaluate half the inputs (plus 1),
i.e. 2n 1 + 1, to be 100% sure that the function is not balanced. This is exponentially (in
n) worse than the quantum algorithm.
However, this is arguably not fair. We may well be content to establish that the function is
constant with some high probability3 , a bit less than one. If the function is constant, how
many function calls would you need classically to rule out the possibility that it is balanced
with a probability of error of no more than (i) 10 3 and (ii) 10 6 .
Note: For simplicity, assume that the number of function calls is much less than 2n/2 , the
number of values of x which give the same result if the function is balanced.
11.2. Consider the Deutsch-Josza algorithm for n = 2, and assume a constant function f (x) = 0 for
all x, i.e. x = 0, 1, 2, 3. Compute explicitly the state of the system at each stage and show that
you get the state |yi = |00i in the upper register at the end.
Hint: Evaluate explicitly Eqs. (11.14)–(11.16) for this situation.
11.3. Consider again the Deutsch-Josza algorithm for n = 2 but this time assume that f (00) = f (01) =
0, f (10) = f (11) = 1. Show that at least one of the two qubits in the upper register ends up as
1.
Hint: See the hint for Qu. 11.2.
x x
y y
z z + xy
The target qubit z is flipped if both the control qubits, x and y, are 1 and is otherwise unchanged.
x x
y X X y
z V V V
3
For later quantum algorithms we will only be able to solve the problem with high probability. Since we have to give
up 100% certainty in the quantum case, we we should not insist on 100% certainty here from the classical algorithm.
11.2. AN ALTERNATIVE DERIVATION 103
Show that it acts with V 2 on |zi if both x and y are 1 and otherwise does nothing.
Hint: One possible way of approaching this question (though not the only way) is to consider
separately what happens for the four possible input values of the control qubits x and y,
namely 00, 01, 10, and 11.
Another, more elegant, way is to note that the e↵ect of a Ctrl-V gate in which |zi is the
target and |xi is the contol is V |xi|zi ! |xiV x |zi.
(ii) Now take V to be the following 1-qubit gate:
( + i X)
V = (1 i) . (11.18)
2
Show that V † V = , and hence V is unitary. Show also that V 2 = X and hence the above
circuit is a quantum To↵oli gate.
Note: One sometimes says that V is the “square root of X”.
104 CHAPTER 11. THE BERNSTEIN-VAZIRANI ALGORITHM