Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lynkos/grovers-algorithm

Python (.py) and Jupyter notebook (.ipynb) implementations of Grover's Algorithm aka Quantum Search Algorithm for n qubits and m targets.
https://github.com/lynkos/grovers-algorithm

algorithm anaconda anaconda3 conda grover grover-algorithm grovers-algorithm jupyter jupyter-notebook latex python python3 qiskit quantum quantum-algorithms quantum-computing quantum-search quantum-search-algorithm

Last synced: 13 days ago
JSON representation

Python (.py) and Jupyter notebook (.ipynb) implementations of Grover's Algorithm aka Quantum Search Algorithm for n qubits and m targets.

Awesome Lists containing this project

README

        


Grover's Algorithm


Python
Jupyter
Qiskit
Shell
Code+Editor


License
Last Commit
Commit Activity
Repo Size

## Requirements
- [x] [Anaconda](https://docs.continuum.io/free/anaconda/install) **OR** [Miniconda](https://docs.conda.io/projects/miniconda/en/latest)

> [!TIP]
> If you have trouble deciding between Anaconda and Miniconda, please refer to the table below
>
>
>
> Anaconda
> Miniconda
>
>
>
>
> New to conda and/or Python
> Familiar with conda and/or Python
>
>
> Not familiar with using terminal and prefer GUI
> Comfortable using terminal
>
>
> Like the convenience of having Python and 1,500+ scientific packages automatically installed at once
> Want fast access to Python and the conda commands and plan to sort out the other programs later
>
>
> Have the time and space (a few minutes and 3 GB)
> Don't have the time or space to install 1,500+ packages
>
>
> Don't want to individually install each package
> Don't mind individually installing each package
>
>
>
>
> Typing out entire Conda commands can sometimes be tedious, so I wrote a shell script ([`conda_shortcuts.sh` on GitHub Gist](https://gist.github.com/lynkos/7a4ce7f9e38bb56174360648461a3dc8)) to define shortcuts for commonly used Conda commands.
>
> Example: Delete/remove a conda environment named test_env
>
> * Shortcut command
> ```
> rmenv test_env
> ```
> * Manually typing out the entire command
> ```sh
> conda env remove -n test_env && rm -rf $(conda info --base)/envs/test_env
> ```
>
> The shortcut has 80.8% less characters!
>

## Installation
1. Verify that conda is installed
```
conda --version
```
2. Ensure conda is up to date
```
conda update conda
```
3. Enter the directory where you want the repository ([`grovers-algorithm`](https://github.com/lynkos/grovers-algorithm)) to be cloned
* POSIX
```sh
cd ~/path/to/directory
```
* Windows
```sh
cd C:\Users\user\path\to\directory
```
4. Clone the repository ([`grovers-algorithm`](https://github.com/lynkos/grovers-algorithm)), then enter (i.e. `cd` command) `grovers-algorithm` directory
```sh
git clone https://github.com/lynkos/grovers-algorithm.git && cd grovers-algorithm
```
5. Create a conda virtual environment from [`environment.yml`](environment.yml)
```
conda env create -f environment.yml
```
6. Activate the virtual environment (`grovenv`)
```
conda activate grovenv
```
7. Confirm that the virtual environment (`grovenv`) is active
* If active, the virtual environment's name should be in parentheses () or brackets [] before your command prompt, e.g.
```
(grovenv) $
```
* If necessary, see which environments are available and/or currently active (active environment denoted with asterisk (*))
```
conda info --envs
```
**OR**
```
conda env list
```

## Usage
> [!TIP]
> If you're unsure about which subsection(s) to follow, please refer to the table below
>
>
>
> Visual Studio Code
> Command Line
>
>
>
>
> Beginner/User-friendly
> Recommended if familiar with using terminals/shells/CLIs
>
>
> GUI
> CLI
>
>
> Click a button to run program
> Execute a command in terminal/shell/CLI to run program
>
>
>

### Python Script
> [!NOTE]
> Although they both perform the same function, there's a discernable difference between [`grovers_algorithm.py`](src/grovers_algorithm.py) and [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb). The former is a Python script, the latter is a Jupyter notebook.

#### Command Line (Recommended)
Run [`grovers_algorithm.py`](src/grovers_algorithm.py)
```sh
python src/grovers_algorithm.py
```

Command Line Arguments



Option
Type
Description
Default




-H, --help

Show help message and exit



-T, --title <title>
str
Window title
"Grover's Algorithm"


-n, --n-qubits <n_qubits>
int
Number of qubits
5


-s, --search <search>
int
Nonnegative integers to search for
11 9 0 3
(i.e., { 11, 9, 0, 3 })


-S, --shots <shots>
int
Number of simulations
1000


-f, --font-size <font_size>
int
Histogram's font size
10


-p, --print
bool
Whether or not to print quantum circuit(s)
False


-c, --combine
bool
Whether to combine all non-winning states into 1 bar labeled "Others" or not
False


#### Visual Studio Code
1. Open [`grovers_algorithm.py`](src/grovers_algorithm.py)
2. Run [`grovers_algorithm.py`](src/grovers_algorithm.py): Click `▷` (i.e. `Play` button) in the upper-right corner

### Jupyter Notebook
#### Visual Studio Code (Recommended)
1. Open the Command Palette with the relevant keyboard shortcut
* Mac
```
⌘ + Shift + P
```
* Windows
```
CTRL + Shift + P
```
2. Search and select `Python: Select Interpreter`
3. Select the virtual environment (`grovenv`)
4. Open [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb)
5. Confirm `grovenv` is the selected [kernel](https://docs.jupyter.org/en/latest/install/kernels.html)
6. Run [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb) by clicking `Run All`

#### Command Line
1. Install `ipykernel` in the virtual environment (`grovenv`)
```
conda install -n grovenv ipykernel
```
2. Add the virtual environment (`grovenv`) as a Jupyter kernel
```
python -m ipykernel install --user --name=grovenv
```
3. Open [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb) in the currently running notebook server, starting one if necessary
```
jupyter notebook src/grovers_algorithm.ipynb
```
4. Select the virtual environment (`grovenv`) as the kernel before running [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb)

## Cleanup
1. [Optional] Deactivate the virtual environment (`grovenv`) to clean up and remove it
```
conda deactivate
```
2. Close the terminal

## Examples
These serve as example outputs/results of the running Grover's algorithm (i.e. files in [`src`](src))

### Quantum Circuits
Generated by [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb) (i.e. Jupyter notebook)

#### Oracle



Oracle circuit



#### Diffuser



Diffuser circuit



#### Grover



Grover circuit



### Figures
Generated by [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb) (i.e. Jupyter notebook)

#### Bloch Sphere



Bloch sphere



#### City Plot



City plot



#### Hinton Plot



Hinton plot



#### Q-Sphere



Q-Sphere



### Simulations
Histograms visualize the outcome/results of 1000 simulations of Grover's algorithm

#### [Python](src/grovers_algorithm.py)
Generated by [`grovers_algorithm.py`](src/grovers_algorithm.py) (i.e. Python script)

> [!TIP]
> Hovering above a bar in the histogram displays that state's frequency



Histogram of the outcome of 1000 simulations of Grover's algorithm

Histogram of the outcome of 1000 simulations of Grover's algorithm






Histogram of the outcome of 1000 simulations of Grover's algorithm, with all non-target states combined into a single bar

Histogram of the outcome of 1000 simulations of Grover's algorithm, with all non-target states combined into a single bar

#### [Jupyter Notebook](src/grovers_algorithm.ipynb)
Generated by [`grovers_algorithm.ipynb`](src/grovers_algorithm.ipynb) (i.e. Jupyter notebook)



Histogram of the outcome of 1000 simulations of Grover's algorithm in Jupyter notebook

Histogram of the outcome of 1000 simulations of Grover's algorithm

## Resources
* [Anaconda Documentation](https://docs.anaconda.com)
* [Conda Documentation](https://docs.conda.io/en/latest)
* [Getting Started with Conda](https://conda.io/projects/conda/en/latest/user-guide/getting-started.html)
* [Jupyter Notebook Documentation](https://jupyter-notebook.readthedocs.io/en/latest)
* [Jupyter Project Documentation](https://docs.jupyter.org/en/latest/index.html) [[PDF](https://buildmedia.readthedocs.org/media/pdf/jupyter/latest/jupyter.pdf)]
* [Technical Support - Jupyter Google Group](https://discourse.jupyter.org)
* [Qiskit Documentation](https://qiskit.org/documentation/index.html)
* [Documentation for Visual Studio Code](https://code.visualstudio.com/docs)
* [Using Jupyter Notebooks in Visual Studio Code](https://code.visualstudio.com/docs/datascience/jupyter-notebooks)

## Credit
Special thanks to [Simanraj Sadana](https://sqmscenter.fnal.gov/profile/simanraj-sadana) for "[Grover's search algorithm for *n* qubits with optimal number of iterations](https://arxiv.org/abs/2011.04051)", which has been a helpful reference and an informative read

## License
Distributed under the [MIT License](LICENSE.md), Copyright © 2024 Kiran Brahmatewari