The first quantum algorithm we will look at is the Bernstein-Vazirani algorithm.

Given a secret s $\in \{0,1\}^n$ and the function f : $\{0,1\}^n \rightarrow \{0,1\}$ defined as f(x) = x * s
\* denotes the inner product of two bitstrings here
For Bitstrings x and y of length n the inner product x * y is $x_1y_1 + ... + x_ny_n$ mod 2
The goal is to find the secret s using as few queries of f as possible.
So as few evaluations of f as possible.

We will look at a quantum algorithm that will find s with only one evaluation of f.

![[Pasted image 20260730144713.png]]

The top wire consists of n qubits in state 0.   $\ket{0}^n = \ket{0} \otimes ... \otimes \ket{0}$ 
The bottom wire is in state 1.
Both wires together are in state $\ket{0^n1} = \ket{0}^n \otimes \ket{1}$ 

First, we apply the Hadamard gate on all qubits.
The resulting state is calculated as follows:

![[Pasted image 20260730145119.png]]

We are now in superposition between all classical possibilities on the top wire
and in $\ket{-}$ on the bottom wire.


Next we apply the unitary $U_f$ on both wires.
The Unitary is defined as:
$U_f \ket{x,y} = \ket{x,y \otimes f(x)}$ 
This unitary applies the function f to the bottom wire y.
It can be calculated as follows:

![[Pasted image 20260730153305.png]]

And we can rewrite $U_f(\ket{x} \otimes \ket{-})$ as

![[Pasted image 20260730154444.png]]

The bottom wire has not changed.
The top wire now has f(x) encoded in its quantum state.
The phenomenon that the output of f is encoded as a -1 is called phase kickback.
We need one final step before measuring.


Now, we perform one final hadamard gate on the top wire.
We want the result to be $\ket{s} \otimes \ket{-}$
To check, we calculate $(H^{\otimes n})^\dagger \ket{s} \otimes \ket{-}$ and check if it is equal to $\psi_2$   :

![[Pasted image 20260730155153.png]]

Because $HH^\dagger = I$ we now can measure s on the top wire.