Skip to content

lynkos/grovers-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Grover's Algorithm

Python Jupyter Qiskit Shell Code+Editor
License Last Commit Commit Activity Repo Size

⭐️⭐️⭐️ Please star this repo if you find it helpful, interesting, or useful! ⭐️⭐️⭐️


Requirements

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) 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% fewer 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) to be cloned
    • POSIX
      cd ~/path/to/directory
    • Windows
      cd C:\Users\user\path\to\directory
  4. Clone the repository (grovers-algorithm), then enter its directory
    git clone https://github.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 (grovers_env)
    conda activate grovers_env
    
  7. Confirm that the virtual environment (grovers_env) is active
    • If active, the virtual environment's name should be in parentheses () or brackets [] before your command prompt, e.g.
      (grovers_env) $
      
    • If necessary, see which environments are available and/or currently active (active environment denoted with asterisk (*))
      conda info --envs
      
      OR
      conda env list
      

Usage

Python Script

Command Line (Recommended)

  1. Run grovers_algorithm.py

    • POSIX
      python grovers_algorithm.py
    • Windows
      python 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
  2. Deactivate the virtual environment (grovers_env) when you're finished

    conda deactivate
    

Visual Studio Code

  1. Open grovers_algorithm.py
  2. Run grovers_algorithm.py: Click (i.e. Play button) in the upper-right corner
  3. Deactivate the virtual environment (grovers_env) when you're finished
    conda deactivate
    

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 (grovers_env)
  4. Open grovers_algorithm.ipynb
  5. Confirm grovers_env is the selected kernel
  6. Run grovers_algorithm.ipynb by clicking Run All
  7. Deactivate the virtual environment (grovers_env) when you're finished
    conda deactivate
    

Command Line

  1. Install ipykernel in the virtual environment (grovers_env)
    conda install -n grovers_env ipykernel
    
  2. Add the virtual environment (grovers_env) as a Jupyter kernel
    python -m ipykernel install --user --name=grovers_env
    
  3. Open grovers_algorithm.ipynb in the currently running notebook server, starting one if necessary
    jupyter notebook grovers_algorithm.ipynb
    
  4. Select the virtual environment (grovers_env) as the kernel before running grovers_algorithm.ipynb
  5. Deactivate the virtual environment (grovers_env) when you're finished
    conda deactivate
    

Resources

License

Distributed under the MIT License, Copyright © 2024 Kiran Brahmatewari