/qiskit_grover

An implementation of Grover's algorithm on qiskit

Primary LanguagePython

qiskit_grover

An implementation of Grover's algorithm on qiskit.

Prerequisites

It requires the qiskit development kit, version 0.6.0. Take a look at the repo for a full reference, or just install it using

pip install qiskit

Note that, in order to run a simulation on a real device, you also need to get an account from ibmq. The qiskit repository contains all the necessary info.

Usage

Check the grover_test.py file. Run python grover_test.py -h to get an idea on how it works.

As a module

Import the grover_test module in a python interpreter and run the build_and_run(n, x_star, real, backend_name) function.

  • n is the number of qubits (2 or 3)
  • x_star are the marked state (between 0 and 2**n), i.e. the states for which the oracle function returns 1. In general, the oracle should be a black box emitting 1 only on a given set of input states. Obviously, in our case, we have to build the oracle ourself. Note that at the moment multiple input states can be inserted, but the result are not really tested.
  • real is True for a real backend, False (default) for the qasm simulator. If it is set to True, the online option is automatically set to True also.
  • online is True for an online IBMQ device, False (default) otherwise.
  • backend_name is the name of the backend you want to use; omit it if you want a default choice. Most of the time, you want to leave it blank. It makes sense only for an online experiment, because the only local backend is qasm.

If you just want to know some useful infos about the circuit, such as the number of actual gates the circuit gets compiled into, run the build_and_info() function which takes the same number of parameters.

For example, if you want to run an experiment on a real device with 3 qubits and selecting 5 as the x_star of the oracle, just run

import grover_test as gt

gt.build_and_run(3, [5], real=True, online=True)

Note that we can also omit the online parameter, which is automatically True when a real device is used.

To run a local simulation with 2 qubits and 3 as the special x_star state

import grover_test as gt

gt.build_and_run(2, [3])

From command line

You can also run the grover_test.py file from the command line. You can see the full help by running grover_test.py with the -h flag. Here is a brief description

usage: grover_test.py [-h] [-r] [-o] [-i] [-b BACKEND_NAME]
                      [--img_dir IMG_DIR] [--plot]
                      n x_star [x_star ...]

For the same examples as for the case above, we could write

>python src/grover_test.py 3 5 -r
>python src/grover_test.py 2 3

If instead we only want to know how many gates are needed for the ibmq_16_melbourne device to run our first circuit

>python src/grover_test.py 3 5 -r -i -b ibmq_16_melbourne

Results

The results are promising even for real ibmq devices when n is less than 3. However, even in this case, due to the high number of gates used when compiling on ibmqx5 and ibmq_16_melbourne, those quantum devices return wrong results; ibmqx2 and ibmqx4, on the other hand, give the right answer with an high degree of accuracy. For n > 3, the number of gates used is really huge and none of the device can really give a precise answer. The simulation, instead, always return the correct results.

TODO

  • Change code to use add_register(ancillas) function
  • Start porting to v 0.7.0
  • Better test on how it works with multiple x_stars
  • Check circuits w/ other unitary_simulator