Qiskit Aqua: Tutorial to develop an algorithm

The latest version of this notebook is available on https://github.com/Qiskit/qiskit-tutorial.


Contributors

Richard Chen[1], Shaohan Hu[1], Antonio Mezzacapo[1], Peng Liu[1], Marco Pistoia[1], Stephen Wood[1]

Affiliation

  • [1]IBMQ

Introduction

This notebook is part of the simple example of an algorithm for Qiskit Aqua. This samaple, called Evolution Fidelity, illustrates how to implement an algorithm and what steps to take to configure and run it. The implementation of the algorithm itself is located in the evolutionfidelity directory.

Note: This notebook explicitly registers the example algorithm. If instead the register_algorithm is commented and the implementation folder evolutionfidelity moved/copied under qiskit_aqua then the algorithm will be automatically discovered and explicit registration is not needed.

Assuming the above done, we can easily get an instance of the algorithm as well as a random initial state to be used for checking quantum evolution fidelity, as follows.

[Optional] Setup token to run the experiment on a real device

If you would like to run the experiement on a real device, you need to setup your account first.

Note: If you do not store your token yet, use IBMQ.save_accounts() to store it first.


In [1]:
from qiskit import IBMQ
IBMQ.load_accounts()

In [2]:
import numpy as np
from qiskit_aqua.operator import Operator
from qiskit_aqua import get_algorithm_instance
from qiskit_aqua import get_initial_state_instance
from qiskit_aqua import register_algorithm
from evolutionfidelity import EvolutionFidelity

# np.random.seed(2)
num_qubits = 2
temp = np.random.random((2 ** num_qubits, 2 ** num_qubits))
qubitOp = Operator(matrix=temp + temp.T)

# While the algorithm can be automatically discovered if placed in an
# approriate folder, we can manually register it like this. If the sample
# algorithm folder was copied/moved to qiskit_aqua then this 
# line below can be commented out as it will be discovered/registered automatically
register_algorithm(EvolutionFidelity)

# get an instance of Dynamics
ef = get_algorithm_instance('EvolutionFidelity')
ef.setup_quantum_backend(backend='statevector_simulator') # setup the desired backend

state_in = get_initial_state_instance('CUSTOM')
state_in.init_args(num_qubits, state='random')

With the necessary pieces in place, we can then change the expansion_order and run the algorithm to see how the quantum evolution fidelity is affected by the different orders.


In [3]:
import math
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(math.floor(n/10)%10!=1)*(n%10<4)*n%10::4])

for expansion_order in [1, 2, 3, 4]:
    ef.init_args(
        qubitOp, state_in,
        expansion_order=expansion_order
    )
    print('The evolution fidelity under {} order suzuki expansion is {}.'.format(
        ordinal(expansion_order), ef.run()['score']
    ))


The evolution fidelity under 1st order suzuki expansion is 0.9842164185807274.
The evolution fidelity under 2nd order suzuki expansion is 0.9999738006866971.
The evolution fidelity under 3rd order suzuki expansion is 0.9999999990494175.
The evolution fidelity under 4th order suzuki expansion is 0.9999999999999802.