Skip to content

Find a secret number with O(1) complexity using Quantum Computing instead of classical O(n) approach. The repository is an application interface to use the Bernstein-Vazirani algorithm.

License

electricalgorithm/BernsteinVaziraniAlgorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Bernstein-Vazirani Algorithm using Qiskit

The Bernstein-Vazirani algorithm is a quantum algorithm used for finding a hidden integer within a range, with the help of a quantum oracle. The algorithm uses a single query to the oracle and is exponentially faster than classical algorithms that use multiple queries. The basic idea behind the algorithm is to create a superposition of all possible states of the hidden integer and then use the oracle to determine the state that corresponds to the correct value.

The algorithm starts by initializing a register of qubits to the state |0⟩, then applying a Hadamard gate to each qubit, creating a superposition of all possible states. Next, the algorithm interacts with the oracle, which is a black box that can perform a specific operation on the qubits. The oracle is designed to mark the correct state of the hidden integer, by flipping the value of a single qubit. Finally, by measuring the state of the qubits, the algorithm can determine the value of the hidden integer with high probability.

The quantum circuit schemetic can be found in below.

     ┌───┐      ░ ┌─────────────────────┐ ░ ┌───┐ ░ ┌─┐   
q_0: ┤ H ├──────░─┤0                    ├─░─┤ H ├─░─┤M├───
     ├───┤      ░ │                     │ ░ ├───┤ ░ └╥┘┌─┐
q_1: ┤ H ├──────░─┤1                    ├─░─┤ H ├─░──╫─┤M├
     ├───┤┌───┐ ░ │                     │ ░ └───┘ ░  ║ └╥┘
q_2: ┤ X ├┤ H ├─░─┤2 SecretNumberOracle ├─░───────░──╫──╫─
     └───┘└───┘ ░ │                     │ ░       ░  ║  ║ 
c_0: ═════════════╡0                    ╞════════════╩══╬═
                  │                     │               ║ 
c_1: ═════════════╡1                    ╞═══════════════╩═
                  └─────────────────────┘                 

Usage

It is recommended to use a virtual environment to run the program. To install the required dependencies, run the following command:

$ pip3 install -r requirements.txt

To run the program interface, simply run the following command:

$ python3 bernsteinvazirani.py

Application Interface

from bernsteinvazirani import BVAlgorithm

# The integer which will be found.
secret_no = 11

# Create the black-box oracle from the number.
black_box = BVAlgorithm.create_oracle_from_number(secret_no)

# Run the algorithm.
result = BVAlgorithm.simulate(black_box)

# Print the result.
print(result["result"])

About

Find a secret number with O(1) complexity using Quantum Computing instead of classical O(n) approach. The repository is an application interface to use the Bernstein-Vazirani algorithm.

Topics

Resources

License

Stars

Watchers

Forks

Languages