<h1>Grover's Algorithm</h1>


```
%load_ext sympyprinting
```


```
from sympy import sqrt, symbols, Rational
from sympy import expand, Eq, Symbol, simplify, exp, sin
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
from sympy.physics.quantum.gate import *
from sympy.physics.quantum.grover import *
from sympy.physics.quantum.qft import QFT, IQFT, Fourier
from sympy.physics.quantum.circuitplot import circuit_plot
```


```
nqubits = 3

```

Grover's algorithm is a quantum algorithm which searches an unordered database (inverts a function).<br/>
Provides polynomial speedup over classical brute-force search ($O(\sqrt{N}) vs. O(N))$ 

Define a black box function that returns True if it is passed the state we are searching for.


```
def black_box(qubits):
    return True if qubits == IntQubit(1, qubits.nqubits) else False

```

Build a uniform superposition state to start the search.


```
psi = superposition_basis(nqubits); psi

```


```
v = OracleGate(nqubits, black_box)

```

Perform two iterations of Grover's algorithm.  Each iteration, the amplitude of the target state increases.


```
iter1 = qapply(grover_iteration(psi, v)); iter1

```


```
iter2 = qapply(grover_iteration(iter1, v)); iter2

```

A single shot measurement is performed to retrieve the target state.


```
measure_all_oneshot(iter2)

```

ee4df0fb95235dffb785a17d65260f154e8ece11
ipynb
Jupyter Notebook
docs/examples/notebooks/grovers.ipynb
tinyclues/ipython
71e32606b0242772b81c9be0d40751ba47d95f2c
BSD-3-Clause-Clear
2016-05-26T10:57:18.000Z
2016-05-26T10:57:18.000Z
docs/examples/notebooks/grovers.ipynb
adgaudio/ipython
a924f50c0f7b84127391f1c396326258c2b303e2
BSD-3-Clause-Clear
docs/examples/notebooks/grovers.ipynb
adgaudio/ipython
a924f50c0f7b84127391f1c396326258c2b303e2
BSD-3-Clause-Clear
Qwen/Qwen-72B
1. YES
2. YES