Note

This page was generated from tutorials/algorithms/05_iqpe_from_vqe.ipynb.

Run interactively in the IBM Quantum lab.

# IQPE and evolution of VQE output state¶

This notebook demonstrates using an output result state from VQE as the input, starting state for evolution by the IQPE (Iterative Quantum Phase Estimation) algorithm.

This is accomplished by first running VQE and then preparing IQPE’s initial state using the variational form as produced by VQE upon its termination.

```
[1]:
```

```
import numpy as np
from qiskit import BasicAer
from qiskit.aqua import aqua_globals, QuantumInstance
from qiskit.aqua.algorithms import VQE, NumPyMinimumEigensolver, IQPE
from qiskit.aqua.operators import I, X, Z
from qiskit.circuit.library import TwoLocal
from qiskit.aqua.components.optimizers import SPSA
from qiskit.aqua.components.initial_states.var_form_based import VarFormBased
```

First we create a qubit operator for VQE. Here we will use the same operator as used in the algorithms introduction, which was originally computed by Qiskit Chemistry for an H2 molecule.

```
[2]:
```

```
H2_op = (-1.052373245772859 * I ^ I) + \
(0.39793742484318045 * I ^ Z) + \
(-0.39793742484318045 * Z ^ I) + \
(-0.01128010425623538 * Z ^ Z) + \
(0.18093119978423156 * X ^ X)
```

```
/home/runner/work/qiskit/qiskit/.tox/docs/lib/python3.8/site-packages/qiskit/aqua/operators/operator_base.py:46: DeprecationWarning: The package qiskit.aqua.operators is deprecated. It was moved/refactored to qiskit.opflow (pip install qiskit-terra). For more information see <https://github.com/Qiskit/qiskit-aqua/blob/master/README.md#migration-guide>
warn_package('aqua.operators', 'qiskit.opflow', 'qiskit-terra')
```

First we will use the classical NumPyMinimumEigensolver to compute a reference ground state energy value so later we can compare with the VQE result first, then IQPE’s.

```
[3]:
```

```
npme = NumPyMinimumEigensolver()
result = npme.compute_minimum_eigenvalue(operator=H2_op)
ref_value = result.eigenvalue.real
print(f'Reference value: {ref_value:.5f}')
```

```
Reference value: -1.85728
```

```
/home/runner/work/qiskit/qiskit/.tox/docs/lib/python3.8/site-packages/qiskit/aqua/algorithms/minimum_eigen_solvers/minimum_eigen_solver.py:36: DeprecationWarning: The package qiskit.aqua.algorithms.minimum_eigen_solvers is deprecated. It was moved/refactored to qiskit.algorithms.minimum_eigen_solvers (pip install qiskit-terra). For more information see <https://github.com/Qiskit/qiskit-aqua/blob/master/README.md#migration-guide>
warn_package('aqua.algorithms.minimum_eigen_solvers',
/home/runner/work/qiskit/qiskit/.tox/docs/lib/python3.8/site-packages/qiskit/aqua/algorithms/eigen_solvers/eigen_solver.py:36: DeprecationWarning: The package qiskit.aqua.algorithms.eigen_solvers is deprecated. It was moved/refactored to qiskit.algorithms.eigen_solvers (pip install qiskit-terra). For more information see <https://github.com/Qiskit/qiskit-aqua/blob/master/README.md#migration-guide>
warn_package('aqua.algorithms.eigen_solvers',
```

Having established the reference ground energy, we next carry on with our experiment. First we configure a VQE algorithm instance. The idea is that we can set an termination condition such that the VQE instance returns rather quickly with a rough estimation result.

```
[4]:
```

```
seed = 5
aqua_globals.random_seed = seed
var_form = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz', reps=3)
spsa = SPSA(maxiter=10)
backend = BasicAer.get_backend('qasm_simulator')
qi = QuantumInstance(backend, seed_simulator=seed, seed_transpiler=seed)
vqe = VQE(var_form=var_form, optimizer=spsa, quantum_instance=qi)
result_vqe = vqe.compute_minimum_eigenvalue(operator=H2_op)
print(f'VQE estimated the ground energy to be {result_vqe.eigenvalue.real:.5f}')
print(f'Delta from reference energy value is {(result_vqe.eigenvalue.real - ref_value):.5f}')
```

```
<ipython-input-1-6f3d94aa90dc>:2: DeprecationWarning: The variable qiskit.aqua.aqua_globals is deprecated. It was moved/refactored to qiskit.utils.algorithm_globals (pip install qiskit-terra). For more information see <https://github.com/Qiskit/qiskit-aqua/blob/master/README.md#migration-guide>
aqua_globals.random_seed = seed
/home/runner/work/qiskit/qiskit/.tox/docs/lib/python3.8/site-packages/qiskit/aqua/components/optimizers/optimizer.py:49: DeprecationWarning: The package qiskit.aqua.components.optimizers is deprecated. It was moved/refactored to qiskit.algorithms.optimizers (pip install qiskit-terra). For more information see <https://github.com/Qiskit/qiskit-aqua/blob/master/README.md#migration-guide>
warn_package('aqua.components.optimizers',
/home/runner/work/qiskit/qiskit/.tox/docs/lib/python3.8/site-packages/qiskit/aqua/quantum_instance.py:135: DeprecationWarning: The class qiskit.aqua.QuantumInstance is deprecated. It was moved/refactored to qiskit.utils.QuantumInstance (pip install qiskit-terra). For more information see <https://github.com/Qiskit/qiskit-aqua/blob/master/README.md#migration-guide>
warn_class('aqua.QuantumInstance',
/home/runner/work/qiskit/qiskit/.tox/docs/lib/python3.8/site-packages/qiskit/aqua/algorithms/vq_algorithm.py:70: DeprecationWarning: The class qiskit.aqua.algorithms.VQAlgorithm is deprecated. It was moved/refactored to qiskit.algorithms.VariationalAlgorithm (pip install qiskit-terra). For more information see <https://github.com/Qiskit/qiskit-aqua/blob/master/README.md#migration-guide>
warn_class('aqua.algorithms.VQAlgorithm',
```

```
VQE estimated the ground energy to be -1.74063
Delta from reference energy value is 0.11664
```

As previously indicated, the energy estimation result is rather rough–it is far from being an acceptable final estimation figure. But, it is close enough such that the accompanying variational form might be a reasonably good approximation to the ground eigenstate, which means the corresponding wave function can serve as the initial state for the IQPE execution that follows. We next prepare such an initial state.

```
[5]:
```

```
vqe_state = VarFormBased(var_form, result_vqe.optimal_parameters)
```

```
<ipython-input-1-69cc3c051a7d>:1: DeprecationWarning: The VarFormBased class is deprecated as of Aqua 0.9 and will be removed no earlier than 3 months after the release date. Instead, all algorithms and circuits accept a plain QuantumCircuit.
vqe_state = VarFormBased(var_form, result_vqe.optimal_parameters)
```

With the VQE-generated quantum state wave function prepared for the initial state, we now go ahead with configuring and running an IQPE instance.

```
[6]:
```

```
qi = QuantumInstance(backend, shots=100, seed_simulator=seed, seed_transpiler=seed)
iqpe = IQPE(state_in=vqe_state, num_time_slices=1, num_iterations=6,
expansion_mode='suzuki', expansion_order=2, quantum_instance=qi)
result_iqpe = result_vqe = iqpe.compute_minimum_eigenvalue(operator=H2_op)
print(f"Continuing with VQE's result, IQPE estimated the ground energy to be {result_iqpe.eigenvalue.real:.5f}")
print(f'Delta from reference energy value is {(result_iqpe.eigenvalue.real - ref_value):.5f}')
```

```
Continuing with VQE's result, IQPE estimated the ground energy to be -1.84917
Delta from reference energy value is 0.00811
```

As can be seen, the resultant ground state energy estimation, as produced by IQPE, is much more accurate that the intermediate result produced by VQE.

```
[7]:
```

```
import qiskit.tools.jupyter
%qiskit_version_table
%qiskit_copyright
```

### Version Information

Qiskit Software | Version |
---|---|

Qiskit | 0.25.4 |

Terra | 0.17.2 |

Aer | 0.8.2 |

Ignis | 0.6.0 |

Aqua | 0.9.1 |

IBM Q Provider | 0.12.3 |

System information | |

Python | 3.8.10 (default, May 4 2021, 07:16:51) [GCC 9.3.0] |

OS | Linux |

CPUs | 2 |

Memory (Gb) | 6.791343688964844 |

Wed May 05 19:45:40 2021 UTC |

### This code is a part of Qiskit

© Copyright IBM 2017, 2021.

This code is licensed under the Apache License, Version 2.0. You may

obtain a copy of this license in the LICENSE.txt file in the root directory

of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.

Any modifications or derivative works of this code must retain this

copyright notice, and modified files need to carry a notice indicating

that they have been altered from the originals.

```
[ ]:
```

```
```