<h1>Grover's Algorithm</h1>

https://en.wikipedia.org/wiki/Grover%27s_algorithm

## Imports


```python
%matplotlib inline
```


```python
from IPython.display import display
```


```python
from sympy import init_printing
init_printing(use_latex=True)
```


```python
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
```

## Grover's algorithm for 3 qubits


```python
nqubits = 3
```

Grover's algorithm is a quantum algorithm which searches an unordered database (inverts a function). It 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.


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

Build a uniform superposition state to start the search.


```python
psi = superposition_basis(nqubits)
psi
```


```python
v = OracleGate(nqubits, black_box)
```

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


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


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

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


```python
measure_all_oneshot(iter2)
```

d4674c26f8152f4f8e2ff3d7a8e6ef4a16f03421
ipynb
Jupyter Notebook
notebooks/grovers.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-17T22:44:27.000Z
2022-03-28T06:26:46.000Z
notebooks/grovers.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-09T05:16:41.000Z
2018-09-22T03:08:29.000Z
notebooks/grovers.ipynb
gvvynplaine/quantum_notebooks
58783823596465fe2d6c494c2cc3a53ae69a9752
BSD-3-Clause
2017-10-09T04:22:19.000Z
2022-03-28T06:25:21.000Z
Qwen/Qwen-72B
1. YES
2. YES