# Deutsch-Jozsa and Bernstein-Vazirani Algorithms

@[section]({
    "id": "deutsch_jozsa__overview",
    "title": "Overview"
})

This kata introduces you to Deutsch-Jozsa algorithm - one of the most famous algorithms in quantum computing. The problem it solves has little practical value, but the algorithm itself is one of the earliest examples of a quantum algorithm that is exponentially faster than any possible deterministic algorithm for the same problem. It is also relatively simple to explain and illustrates several very important concepts (such as quantum oracles). As such, Deutsch–Jozsa algorithm is part of almost every introductory course on quantum computing.

**This kata covers the following topics:**

- The problem solved by Deutsch-Jozsa algorithm and the classical solution to it
- Multi-qubit phase oracles (for a more detailed introduction to phase oracles, see Oracles kata)
- Deutsch-Jozsa algorithm
- Implementing oracles and end-to-end Deutsch-Jozsa algorithm in Q#
- Bernstein-Vazirani algorithm and the problem solved by it

**What you should know to start working on this kata:**

- Basic single-qubit gates
- Quantum measurements
- Deutsch algorithm - the single-qubit variant of Deutsch-Jozsa algorithm

@[section]({
    "id": "deutsch_jozsa__problem",
    "title": "The Problem"
})

You are given a classical function that takes an $N$-bit string as an input and returns one bit: $f(x): \\{0, 1\\}^N \to \\{0, 1\\}$. You are guaranteed that the function $f$ is

- either *constant* (returns the same value for all inputs) 
- or *balanced* (returns value $0$ for half of the inputs and $1$ for the other half of the inputs). 

The task is to figure out whether the function is constant or balanced.

**Examples**

- $f(x) \equiv 0$ or $f(x) \equiv 1$ are constant functions (and they are actually the only constant functions in existence).
- $f(x) = x \bmod 2$ (the least significant bit of $x$) or $f(x) = 1 \text{ if the binary notation of }x \text{ has odd number of 1s and 0 otherwise}$ are examples of multi-bit balanced functions. Indeed, for both these functions you can check that for every possible input $x$ for which $f(x) = 0$ there exists an input $x^\prime$ (equal to $x$ with the least significant bit flipped) such that $f(x^\prime) = 1$, and vice versa, which means that the function is balanced.

If we solve this problem classically, how many calls to the given function will we need? 

The first call will give us no information - regardless of whether it returns $0$ or $1$, the function could still be constant or balanced.
In the best case scenario the second call will return a different value and we'll be able to conclude that the function is balanced in just $2$ calls. 
However, if we get the same value for the first two calls, we'll have to keep querying the function until either we get a different value or until we do $2^{N-1}+1$ queries that will return the same value - only in this case we'll know for certain that the function is constant.

What about the quantum scenario?


@[section]({
    "id": "deutsch_jozsa__oracles",
    "title": "Multi-Qubit Oracles"
})

In the quantum scenario, the classical function we're working with is implemented as a quantum oracle - a "black box" operation used as input to another algorithm. This operation is implemented in a way which allows to perform calculations not only on individual inputs, but also on superpositions of inputs. 

To enable the oracle to act on quantum states instead of classical values, the integer input $x$ is represented in binary $x = (x_{0}, x_{1}, \dots, x_{N-1})$, 
and encoded into an $N$-qubit register: $|\vec{x} \rangle = |x_{0} \rangle \otimes |x_{1} \rangle \otimes \cdots \otimes |x_{N-1} \rangle$.
The phase oracle $U_f$ for this function is defined as follows:

$$U_f |\vec{x} \rangle = (-1)^{f(x)} |\vec{x} \rangle$$

The function $f$ can return only two values, 0 or 1, which result in no phase change or multiplication by a relative phase $-1$, respectively.

The effect of such an oracle on any single basis state is not particularly interesting: it just adds a global phase which is not something you can observe. However, if you apply this oracle to a *superposition* of basis states, its effect becomes noticeable. 
Remember that quantum operations are linear: if you define the effect of an operation on the basis states, you'll be able to deduce its effect on superposition states (which are just linear combinations of the basis states) using its linearity.

Let's see how to implement several examples of multi-bit constant and balanced functions as phase oracles in Q#.

1. $f(x) \equiv 0$

This is the easiest function to implement: if $f(x) \equiv 0$, 

$$U_f |x\rangle \equiv (-1)^0 |x\rangle = |x\rangle$$

This means that $U_f$ is an identity - a transformation which does absolutely nothing! 

2. $f(x) \equiv 1$

The second constant function is slightly trickier: if $f(x) \equiv 1$

$$U_f |x\rangle \equiv (-1)^1 |x\rangle = - |x\rangle$$

Now $U_f$ is a negative identity, i.e., a transformation which applies a global phase of $-1$ to the state. 
A lot of algorithms just ignore the global phase accumulated in them, since it is not observable. 
However, if we want to be really meticulous, we can use the $R$ gate which performs a given rotation around the given axis. 
When called with `PauliI` axis, this operation applies a global phase to the given qubit. 
We can use any qubit to apply this gate, for example, `qs[0]`.

3. $f(x) = x \bmod 2$

The binary representation of $x$ is $x = (x_{0}, x_{1}, \dots, x_{N-1})$, with the least significant bit encoded in the last bit (stored in the last qubit of the input array). Then we can rewrite the function as

$$f(x) = x_{N-1}$$

Let's use this in the oracle effect expression:

$$U_f |x\rangle = (-1)^{f(x)} |x\rangle = (-1)^{x_{N-1}} |x\rangle = |x_{0} \rangle \otimes \cdots \otimes |x_{N-2} \rangle \otimes (-1)^{x_{N-1}} |x_{N-1}\rangle$$

This means that we only need to use the last qubit in the implementation: do nothing if it is $|0\rangle$ and apply a phase of $-1$ if it is $|1\rangle$. This is exactly the effect of the $Z$ gate!

We can write out the oracle unitary as follows:

$$U_f = \mathbb{1} \otimes \cdots \otimes \mathbb{1} \otimes Z$$

In this demo we will see how to implement three multi-bit functions as quantum oracles, and their effect on a quantum state.
After that, you'll try to implement the oracles for two more functions on your own!

@[example]({"id": "deutsch_jozsa__oracle_implementations", "codePath": "./examples/OracleImplementationDemo.qs"})

@[exercise]({
    "id": "deutsch_jozsa__msb_oracle",
    "title": "Oracle for f(x) = most significant bit of x",
    "path": "./msb_oracle/",
    "qsDependencies": [
        "../KatasLibrary.qs"
    ]
})

@[exercise]({
    "id": "deutsch_jozsa__parity_oracle",
    "title": "Oracle for f(x) = parity of the number of 1 bits in x",
    "path": "./parity_oracle/",
    "qsDependencies": [
        "../KatasLibrary.qs"
    ]
})


@[section]({
    "id": "deutsch_jozsa__algorithm",
    "title": "Solving the Problem: Deutsch-Jozsa Algorithm"
})

Now let's return to the problem of figuring out whether the given function is constant or balanced.
We'll present the algorithm in detail step-by-step and summarize it in the end.

### Inputs

You are given the number of bits in the oracle input $N$ and the oracle itself - a "black box" operation $U_f$ that implements a classical function $f(x)$. You are guaranteed that the function implemented by the oracle is either constant or balanced.

### The starting state

The algorithm starts with $N$ qubits in the $|0...0\rangle = |0\rangle^{\otimes N}$ state.

### Step 1. Apply Hadamard transform to each qubit

Applying the $H$ gate to one qubit in the $|0\rangle$ state converts it to the $\frac{1}{\sqrt2} \big(|0\rangle + |1\rangle \big)$ state, which is an equal superposition of both basis states on one qubit. 

If we apply the $H$ gate to each of the two qubits in the $|00\rangle$ state, we'll get 

$$(H \otimes H) |00\rangle = \big(H |0\rangle \big) \otimes \big(H |0\rangle\big) = \left(\frac{1}{\sqrt2} \big(|0\rangle + |1\rangle \big)\right) \otimes \left(\frac{1}{\sqrt2} \big(|0\rangle + |1\rangle \big)\right) = \frac{1}{2} \big(|00\rangle + |01\rangle + |10\rangle + |11\rangle \big)$$

This is just an equal superposition of all basis states on two qubits! 
We can extend the same thinking to applying the $H$ gate to each of the $N$ qubits in the $|0...0\rangle$ state to conclude that this transforms them into a state that is an equal superposition of all basis states on $N$ qubits.

Mathematically the transformation "apply $H$ gate to each of the $N$ qubits" can be denoted as $H^{\otimes N}$. After applying this transformation we'll get the following state:

$$H^{\otimes N} |0\rangle^{\otimes N} = \big( H|0\rangle \big)^{\otimes N} = \left( \frac{1}{\sqrt2} \big(|0\rangle + |1\rangle \big) \right)^{\otimes N} = \frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} |x\rangle$$


### Step 2. Apply the oracle

This step is the only step in which we use the knowledge of the classical function, given to us as the quantum oracle. 
This step will keep the amplitudes of the basis states for which $f(x) = 0$ unchanged, and multiply the amplitudes of the basis states for which $f(x) = 1$ by $-1$.

Mathematically the results of oracle application can be written as follows:

$$U_f \left(\frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} |x\rangle \right) = \frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} U_f|x\rangle = \frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} (-1)^{f(x)} |x\rangle$$

### Step 3. Apply Hadamard transform to each qubit again

In this step, let's not worry about the whole expression for the state of the qubits after applying the $H$ gates to them; instead let's calculate only the resulting amplitude of the basis state $|0\rangle^{\otimes N}$.

Consider one of the basis states $|x\rangle$ in the expression $\sum_{x=0}^{2^N-1} (-1)^{f(x)} |x\rangle$.  
It can be written as $|x\rangle = |x_{0} \rangle \otimes \cdots \otimes |x_{N-1}\rangle$, where each $|x_k\rangle$ is either $|0\rangle$ or $|1\rangle$.  
When we apply the $H$ gates to $|x\rangle$, we'll get $H^{\otimes N} |x\rangle = H|x_{0} \rangle \otimes \cdots \otimes H|x_{N-1}\rangle$, where each term of the tensor product is either $H|0\rangle = \frac{1}{\sqrt2}\big(|0\rangle + |1\rangle \big) = |+\rangle$ or $H|1\rangle = \frac{1}{\sqrt2}\big(|0\rangle - |1\rangle \big) = |-\rangle$. 
If we open the brackets in this tensor product, we'll get a superposition of all $N$-qubit basis states, each of them with amplitude $\frac{1}{\sqrt{2^N}}$ or $-\frac{1}{\sqrt{2^N}}$ — and, since the amplitude of the $|0\rangle$ state in both $|+\rangle$ and $|-\rangle$ is positive, we know that the amplitude of the basis state $|0\rangle^{\otimes N}$ will end up positive, i.e., $\frac{1}{\sqrt{2^N}}$.

Now we can calculate the amplitude of the $|0\rangle^{\otimes N}$ state in the expression $H^{\otimes N} \left( \frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} (-1)^{f(x)} |x\rangle \right)$: in each of the $2^N$ terms of the sum its amplitude is $\frac{1}{\sqrt{2^N}}$; therefore, we get the total amplitude

$$\frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} (-1)^{f(x)} \frac{1}{\sqrt{2^N}} = \frac{1}{2^N} \sum_{x=0}^{2^N-1} (-1)^{f(x)}$$

### Step 4. Perform measurements and interpret the result

So far we did not use the fact that the function we are given is constant or balanced. Let's see how this affects the amplitude of the $|0\rangle^{\otimes N}$ state.

* If the function is constant, $f(x) = C$ (either always $0$ or always $1$), we get  
  $$\frac{1}{2^N} \sum_{x=0}^{2^N-1} (-1)^{f(x)} = \frac{1}{2^N} \sum_{x=0}^{2^N-1} (-1)^{C} = \frac{1}{2^N} \cdot 2^N (-1)^C = (-1)^C$$
  Since the sum of squares of amplitudes of all basis states always equals $1$, the amplitudes of the rest of the basis states have to be 0 - this means that the state of the qubits after step 3 *is* $|0\rangle^{\otimes N}$.

* If the function is balanced, i.e., returns $0$ for exactly half of the inputs and $1$ for the other half of the inputs, exactly half of the terms in the sum $\frac{1}{2^N} \sum_{x=0}^{2^N-1} (-1)^{f(x)}$ will be $1$ and the other half of the terms will be $-1$, and they will all cancel out, leaving the amplitude of $|0\rangle^{\otimes N}$ equal to $0$.

Now, what happens when we measure all qubits? (Remember that the probability of getting a certain state as a result of measurement equals to the square of the amplitude of this state.)

If the function is constant, the only measurement result we can get is all zeros - the probability of getting any other result is $0$. If the function is balanced, the probability of getting all zeros is $0$, so we'll get any measurement result except this.

This is exactly the last step of the algorithm: **measure all qubits, if all measurement results are 0, the function is constant, otherwise it is balanced**.

### Summary

In the end the algorithm is very straightforward:

1. Apply the $H$ gate to each qubit.
2. Apply the oracle.
3. Apply the $H$ gate to each qubit again.
4. Measure all qubits.
5. If all qubits were measured in $|0\rangle$ state, the function is constant, otherwise it is balanced.

Note that this algorithm requires only $1$ oracle call, and always produces the correct result!

@[exercise]({
    "id": "deutsch_jozsa__implement_dj",
    "title": "Implement Deutsch-Jozsa Algorithm",
    "path": "./implement_dj/",
    "qsDependencies": [
        "../KatasLibrary.qs"
    ]
})


@[section]({
    "id": "deutsch_jozsa__e2e",
    "title": "Running Deutsch-Jozsa Algorithm End to End"
})

The last demo in this kata shows you how to combine the oracles you've seen so far and the Deutsch-Jozsa algorithm you've implemented into an end-to-end application that will check whether each oracle implements a constant or a balanced function.

@[example]({"id": "deutsch_jozsa__e2edemo", "codePath": "./examples/DeutschJozsaAlgorithmDemo.qs"})


@[section]({
    "id": "deutsch_jozsa__bernstein-vazirani",
    "title": "Bernstein-Vazirani Algorithm"
})

To wrap up our discussion in this kata, let's take a look at a problem solved using a similar approach - the Bernstein-Vazirani algorithm.
In this problem, you are also given an oracle implementing an $N$-bit function $f(x): \\{0, 1\\}^N \to \\{0, 1\\}$.
However, this time the function is guaranteed to be a *scalar product function*, that is, there exists an $N$-bit string $s$
that allows the following representation ($\cdot$ is bitwise inner product of integers modulo $2$):

$$f(x) = x \cdot s = \sum_{k=0}^{N-1} x_k s_k \bmod 2$$

The task is to recover the hidden bit string $s$.

**Examples**

- $f(x) \equiv 0$ is an example of such a function with $s = 0, \dots, 0$.
- $f(x) = 1 \text{ if x has odd number of 1s, and } 0 \text{ otherwise }$ is another example of such a function, with $s = 1, \dots, 1$.

If we solve this problem classically, how many calls to the given function will we need? 
We'd need to use one query to recover each bit of $s$ (the query for $k$-th bit can be a bit string with $1$ in the $k$-th bit and zeros in all other positions), for a total of $N$ queries.

What about the quantum scenario?
It turns out that the algorithm that allows us to solve this problem looks just like Deutsch-Jozsa algorithm, 
except for the way we interpret the measurement results on the last step. To see this, we'll need to take another look 
at the math involved in applying Hadamard gates to multiple qubits.

### Apply Hadamard transform to each qubit: a different view

When we apply an $H$ gate to a single qubit in the basis state $\ket{x}$, we can write the result as the following sum:

$$H\ket{x} = \frac1{\sqrt2} (\ket{0} + (-1)^{x} \ket{1}) = \frac1{\sqrt2} \sum_{z \in {0, 1}} (-1)^{x \cdot z} \ket{z}$$

If we use this representation to spell out the result of applying an $H$ gate to each qubit of an $N$-qubit basis state 
$\ket{x} = \ket{x_0}\ket{x_1} \dots \ket{x_{N-1}}$, we'll get:

$$H\ket{x} = \frac1{\sqrt{2^N}} \sum_{z_k \in {0, 1}} (-1)^{x_0z_0 + \dots + x_{N-1}z_{N-1}} \ket{z_0}\ket{z_1} \dots \ket{z_{N-1}} =$$

$$= \frac1{\sqrt{2^N}} \sum_{z = 0}^{2^N-1} (-1)^{x \cdot z} \ket{z}$$

With this in mind, let's revisit the algorithm and see how we can write the exact quantum state after it.

### Bernstein-Vazirani algorithm

Bernstein-Vazirani algorithm follows the same outline as Deutsch-Jozsa algorithm:

1. Apply the $H$ gate to each qubit.
2. Apply the oracle.
3. Apply the $H$ gate to each qubit again.
4. Measure all qubits.

We know that after the second step the qubits end up in the following state:

$$\frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} (-1)^{f(x)} |x\rangle$$

Now, once we apply the Hadamard gates to each qubit, the system state becomes:

$$\frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} \sum_{z=0}^{2^N-1} (-1)^{f(x) + x \cdot z} |z\rangle$$

> In Deutsch-Jozsa algorithm, we looked at the amplitude of the $\ket{0}$ state in this expression, which was 
> $\frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} (-1)^{f(x)}$.

Now, let's take a look at the amplitude of the $\ket{s}$ state - the state that encodes the hidden bit string we're looking for.
This amplitude is 

$$\frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} (-1)^{f(x) + x \cdot s}$$

Since $f(x) = x \cdot s$, for all values of $x$ $f(x) + x \cdot s = 2 x \cdot s$, and $(-1)^{f(x) + x \cdot s} = 1$.
Overall the amplitude of $\ket{s}$ is 

$$\frac{1}{\sqrt{2^N}} \sum_{x=0}^{2^N-1} 1 = \frac{1}{\sqrt{2^N}} 2^N = 1$$

This means that our state after applying the Hadamard gates is just $\ket{s}$, and measuring it will give us the bit string $s$!
And, same as Deutsch-Jozsa algorithm, Bernstein-Vazirani algorithm takes only one oracle call.

@[exercise]({
    "id": "deutsch_jozsa__implement_bv",
    "title": "Implement Bernstein-Vazirani Algorithm",
    "path": "./implement_bv/",
    "qsDependencies": [
        "../KatasLibrary.qs"
    ]
})


@[section]({
    "id": "deutsch_jozsa__conclusion",
    "title": "Conclusion"
})

Congratulations! In this kata you have learned Deutsch-Jozsa and Bernstein-Vazirani algorithms.

- Deutsch-Jozsa algorithm is the simplest example of a quantum algorithm that is exponentially faster than any possible deterministic algorithm for the same problem.
- Bernstein-Vazirani algorithm is a similar algorithm that extracts information about the hidden bit string of the given function that is known to be a scalar product function. It offers a linear speedup compared to a classical algorithm for the same problem.
- Quantum oracles don't allow you to evaluate the function on all inputs at once! Instead, Deutsch-Jozsa algorithm finds a clever way to aggregate information about all function values into a few bits that indicate whether they are all the same or not. Bernstein-Vazirani algorithm uses a similar approach to encode the information about the hidden bit string into the state of the qubits at the end of the algorithm.
