The trek goes on

0028

Understanding the Bernstein–Vazirani algorithm

Posted at 23:13 on 9 October 2022, and revised at 15:14 on 10 October 2022

After writing 0027, which discussed the Deutsch–Jozsa algorithm, I saw a closely related Bernstein-Vazirani algorithm in Carette et al’s QPL 2021 paper. (By the way, this paper appears to be the most recent and complete development in graphical approaches to quantum algorithms. In particular, the paper treats Grover’s algorithm in full, with a graphical representation of iterations and probabilistic reasoning — I’ll have to catch up.) The Bernstein–Vazirani problem gives us an oracle corresponding to the function (s):2n2, which is the dot-product function () partially applied to s:2n: given an input x=x1x2xn:2n where each xi:2,

(s)(x):=xs:=(x1s1)(x2s2)(xnsn)

The secret s is unknown to us, and we are required to recover s. Following the approach taken in 0027, I tried to lift (s) to a linear map (s) and evaluate it on the X-basis, but this time it was rather difficult, and eventually I had to fall back on the more conventional understanding of the oracle construction, which is about encoding function outputs as phases. It’s still easier to talk about this encoding purely in linear algebra though.

First, let us recap how the X-basis elements are expressed relative to the Z-basis, for example,

(1)|-+-=|000|001+|010|011|100+|101|110+|111

This is expanded from

(2)|-+-=(|0|1)(|0+|1)(|0|1)

using the linearity of tensor product, so, for example, the term |001 in Equation 1 comes from |0|0|1 (drawing a summand from each of the three terms in Equation 2), and |111 from |1|1|1. Think of -+- as selecting the first and third bits to participate in the decision of the sign of a term; to actually decide the sign of a term |x1x2x3 in |-+-, we count the number of 1 among the selected bits x1 and x3: if it’s even, that means we multiply an even number of 1, and the term is positive; otherwise, the term is negative. As a formula:

|-+-=x1,x2,x3:2(1)x1x3|x1x2x3

Note the correspondence between -+- and the exponent expression x1x3, where x1 and x3 are turned ‘on’ (participating in the decision of the sign) by -, and x2 is turned ‘off’ (not appearing in the expression) by +. This turning on and off can be expressed more generally as a dot product

x1x3=(x11)(x20)(x31)=x101

If we (somewhat informally) use H to denote the conversion(s) between 0/1 and +/- and generalise, we can write

(3)|H(s)=x:2n(1)xs|x

where all the outputs of (s) are encoded in the coefficients, which are called ‘phases’ in quantum theory. (Now we know where the function (s) in the Bernstein–Vazirani problem comes from, and see that the problem is actually no less artificial —but perhaps more delicately designed— than Deutsch’s problem.)

The Bernstein–Vazirani algorithm can be understood as (i) preparing a superposition state in Equation 3, (ii) measuring this state with respect to the X-basis, and (iii) applying H1 to convert the resulting string of + and - back to a string of 0 and 1, which is s. Since such a state is necessarily one of the X-basis elements, which can be perfectly distinguished from the rest of the X-basis, the measurement will return that element with certainty. To a beginner, it may be surprising that we should prepare such an intricate superposition state, which seems difficult to handle, but in this case the measurement with respect to the X-basis acts as a powerful pattern-matching operation that recognises such superposition states and decodes them back to a string, so there is no difficulty.

It turns out that, in general, we can encode a function f:2n2 in phases quite easily using f¯T. The lifting of f to f¯ can be defined by

f¯:=x:2n|f(x)x|

whose transpose is

f¯T=x:2n|xf(x)|

Now, if we apply f¯T to |0, we can select all the summands where f(x)=0; we can also apply f¯T to |1 to select all the summands where f(x)=1, and put a minus sign in the front. And the sum of the two applications —which is the same as just one application to |-— is the state we want!

(4)f¯T|-=f¯T|0f¯T|1=(x:2nf(x)=0|x)(x:2nf(x)=1|x)=x:2n(1)f(x)|x

So actually what we need to do in the quantum part of the algorithm is exactly the same as Deutsch–Jozsa; the rest of the development is the same as in 0027.

The Deutsch–Jozsa algorithm can also be explained —probably more concisely— using the phase encoding: if f is constant, then all the terms in Equation 4 will concentrate in either f¯T|0 or f¯T|1, and the resulting state will be |++ (ignoring the sign); if f is balanced, then the terms will distribute evenly in f¯T|0 and f¯T|1, and the resulting state will be orthogonal to |++. Well, I suppose that the phase-encoding explanation is favoured traditionally for a reason. What’s still different here is that we can talk about how and why the algorithm works purely in linear algebra, and separately from its quantum implementation.