lynkos/grovers-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PythonJupyterQiskitShellCode+Editor
LicenseLast CommitCommit ActivityRepo SizeAsk DeepWiki

Important

This repository has accompanying blog posts on my blog!

These blog posts are much more readable and user-friendly than this repository by itself and . If you're interested in sharing or reading this repository as a blog post, please feel free to refer to the aforementioned links!

Tip

If you have trouble deciding between Anaconda and Miniconda, please refer to the table below

AnacondaMiniconda
New to conda and/or PythonFamiliar with conda and/or Python
Not familiar with using terminal and prefer GUIComfortable using terminal
Like the convenience of having Python and 1,500+ scientific packages automatically installed at onceWant 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 packageDon'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 Gist) 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
    conda env remove -n test_env && rm -rf $(conda info --base)/envs/test_env

The shortcut has 80.8% less characters!

  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) to be cloned
    • POSIX
      cd ~/path/to/directory
    • Windows
      cd C:\Users\user\path\to\directory
  4. Clone the repository (grovers-algorithm), then enter (i.e. cd command) grovers-algorithm directory
    git clone https://.com/lynkos/grovers-algorithm.git && cd grovers-algorithm
  5. Create a conda virtual environment from 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
      

Tip

If you're unsure about which subsection(s) to follow, please refer to the table below

Visual Studio CodeCommand Line
Beginner/User-friendlyRecommended if familiar with using terminals/shells/CLIs
GUICLI
Click a button to run programExecute a command in terminal/shell/CLI to run program

Note

Although they both perform the same function, there's a discernable difference between grovers_algorithm.py and grovers_algorithm.ipynb. The former is a Python script, the latter is a Jupyter notebook.

Run grovers_algorithm.py

python src/grovers_algorithm.py
Command Line Arguments
OptionTypeDescriptionDefault
-H, --helpShow help message and exit
-T, --title <title>strWindow title"Grover's Algorithm"
-n, --n-qubits <n_qubits>intNumber of qubits5
-s, --search <search>intNonnegative integers to search for11 9 0 3
(i.e., { 11, 9, 0, 3 })
-S, --shots <shots>intNumber of simulations1000
-f, --font-size <font_size>intHistogram's font size10
-p, --printboolWhether or not to print quantum circuit(s)False
-c, --combineboolWhether to combine all non-winning states into 1 bar labeled "Others" or notFalse
  1. Open grovers_algorithm.py
  2. Run grovers_algorithm.py: Click (i.e. Play button) in the upper-right corner
  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
  5. Confirm grovenv is the selected kernel
  6. Run grovers_algorithm.ipynb by clicking Run All
  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 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
  1. [Optional] Deactivate the virtual environment (grovenv) to clean up and remove it
    conda deactivate
    
  2. Close the terminal

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

Generated by grovers_algorithm.ipynb (i.e. Jupyter notebook)

Oracle circuit
Diffuser circuit
Grover circuit

Generated by grovers_algorithm.ipynb (i.e. Jupyter notebook)

Bloch sphere
City plot
Hinton plot
Q-Sphere

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

Generated by 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

Generated by 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

Special thanks to Simanraj Sadana for "Grover's search algorithm for n qubits with optimal number of iterations", which has been a helpful reference and an informative read

Distributed under the MIT License, Copyright © 2023 – 2025 Kiran Brahmatewari

About

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

Topics

Resources

License

Stars

Watchers

Forks