```python
!pip install qiskit

```

    Collecting qiskit
      Downloading https://files.pythonhosted.org/packages/6f/61/cb7506e17a2566dc8a31a3e1924d91ac0bdd8ff07c71ec698c06647b6306/qiskit-0.26.2.tar.gz
    Collecting qiskit-terra==0.17.4
      Downloading https://files.pythonhosted.org/packages/b3/0c/3c7a8dd451dae0907263e9de9e3e34909e15e18c88a589b44581972c8511/qiskit_terra-0.17.4-cp37-cp37m-manylinux2010_x86_64.whl (6.0MB)
         |████████████████████████████████| 6.0MB 5.2MB/s
    Collecting qiskit-aer==0.8.2
      Downloading https://files.pythonhosted.org/packages/c2/d2/6ff15c370b5465b32529b528bf3f4ce1e01f74498be16203aa1c04b67022/qiskit_aer-0.8.2-cp37-cp37m-manylinux2010_x86_64.whl (18.0MB)
         |████████████████████████████████| 18.0MB 215kB/s
    Collecting qiskit-ibmq-provider==0.13.1
      Downloading https://files.pythonhosted.org/packages/be/99/74bbb901f88603a7d850d4889abc06d81ba702e4227151f4a5b66f2631fe/qiskit_ibmq_provider-0.13.1-py3-none-any.whl (228kB)
         |████████████████████████████████| 235kB 47.1MB/s
    Collecting qiskit-ignis==0.6.0
      Downloading https://files.pythonhosted.org/packages/54/be/a13c828e457e09d979667a61bddbd8c7246aafa94e2501b6a9154429cbea/qiskit_ignis-0.6.0-py3-none-any.whl (207kB)
         |████████████████████████████████| 215kB 42.6MB/s
    Collecting qiskit-aqua==0.9.1
      Downloading https://files.pythonhosted.org/packages/88/79/392c57b978decbb24b902344b536af52c40a751aed0ebbaefa8bc2964cb5/qiskit_aqua-0.9.1-py3-none-any.whl (2.1MB)
         |████████████████████████████████| 2.1MB 31.4MB/s
    Requirement already satisfied: scipy>=1.4 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra==0.17.4->qiskit) (1.4.1)
    Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra==0.17.4->qiskit) (2.6.0)
    Requirement already satisfied: python-dateutil>=2.8.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra==0.17.4->qiskit) (2.8.1)
    Collecting fastjsonschema>=2.10
      Downloading https://files.pythonhosted.org/packages/d1/fb/ea090e917b18320f79be31d754bbe496b715175e865603cfce1eaed2e774/fastjsonschema-2.15.1-py3-none-any.whl
    Collecting ply>=3.10
      Downloading https://files.pythonhosted.org/packages/a3/58/35da89ee790598a0700ea49b2a66594140f44dec458c07e8e3d4979137fc/ply-3.11-py2.py3-none-any.whl (49kB)
         |████████████████████████████████| 51kB 5.7MB/s
    Requirement already satisfied: sympy>=1.3 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra==0.17.4->qiskit) (1.7.1)
    Requirement already satisfied: dill>=0.3 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra==0.17.4->qiskit) (0.3.3)
    Requirement already satisfied: psutil>=5 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra==0.17.4->qiskit) (5.4.8)
    Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra==0.17.4->qiskit) (1.19.5)
    Collecting python-constraint>=1.4
      Downloading https://files.pythonhosted.org/packages/37/8b/5f1bc2734ca611943e1d6733ee244238679f6410a10cd45ede55a61a8402/python-constraint-1.4.0.tar.bz2
    Collecting retworkx>=0.8.0
      Downloading https://files.pythonhosted.org/packages/9e/cd/70d436f170aa1ead2ac9e4c19c8838633355d48b530f09455eab0af2f98e/retworkx-0.9.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.4MB)
         |████████████████████████████████| 1.5MB 27.3MB/s
    Collecting pybind11>=2.6
      Downloading https://files.pythonhosted.org/packages/8d/43/7339dbabbc2793718d59703aace4166f53c29ee1c202f6ff5bf8a26c4d91/pybind11-2.6.2-py2.py3-none-any.whl (191kB)
         |████████████████████████████████| 194kB 28.1MB/s
    Collecting websockets>=8
      Downloading https://files.pythonhosted.org/packages/84/64/78c2b3fe37730b30dca3c93d1f7f4a4286767f86e7c04cf3571b39bc2fb7/websockets-9.1-cp37-cp37m-manylinux2010_x86_64.whl (103kB)
         |████████████████████████████████| 112kB 53.2MB/s
    Requirement already satisfied: nest-asyncio!=1.1.0,>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-ibmq-provider==0.13.1->qiskit) (1.5.1)
    Requirement already satisfied: requests>=2.19 in /usr/local/lib/python3.7/dist-packages (from qiskit-ibmq-provider==0.13.1->qiskit) (2.23.0)
    Collecting requests-ntlm>=1.1.0
      Downloading https://files.pythonhosted.org/packages/03/4b/8b9a1afde8072c4d5710d9fa91433d504325821b038e00237dc8d6d833dc/requests_ntlm-1.1.0-py2.py3-none-any.whl
    Requirement already satisfied: urllib3>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from qiskit-ibmq-provider==0.13.1->qiskit) (1.24.3)
    Requirement already satisfied: setuptools>=40.1.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-ignis==0.6.0->qiskit) (56.1.0)
    Requirement already satisfied: h5py<=3.1.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-aqua==0.9.1->qiskit) (3.1.0)
    Requirement already satisfied: scikit-learn<=0.24.1,>=0.20.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-aqua==0.9.1->qiskit) (0.22.2.post1)
    Requirement already satisfied: pandas<=1.2.3 in /usr/local/lib/python3.7/dist-packages (from qiskit-aqua==0.9.1->qiskit) (1.1.5)
    Collecting yfinance<=0.1.55
      Downloading https://files.pythonhosted.org/packages/7a/e8/b9d7104d3a4bf39924799067592d9e59119fcfc900a425a12e80a3123ec8/yfinance-0.1.55.tar.gz
    Collecting quandl<=3.6.0
      Downloading https://files.pythonhosted.org/packages/c2/58/9f0e69d836045e3865d263e9ed49f42b23a58526fdabb30f74c430baee3f/Quandl-3.6.0-py2.py3-none-any.whl
    Collecting dlx<=1.0.4
      Downloading https://files.pythonhosted.org/packages/54/c0/b8fb5bb727e983b6f5251433ef941b48f38c65bb0bd6ec509e9185bcd406/dlx-1.0.4.tar.gz
    Collecting docplex<=2.20.204; sys_platform != "darwin"
      Downloading https://files.pythonhosted.org/packages/87/99/6f7c219b39fd58c84688ad0713eb932bfcf6be81fc74519e43ea9c915b56/docplex-2.20.204.tar.gz (611kB)
         |████████████████████████████████| 614kB 31.8MB/s
    Requirement already satisfied: fastdtw<=0.3.4 in /usr/local/lib/python3.7/dist-packages (from qiskit-aqua==0.9.1->qiskit) (0.3.4)
    Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.8.0->qiskit-terra==0.17.4->qiskit) (1.15.0)
    Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.7/dist-packages (from sympy>=1.3->qiskit-terra==0.17.4->qiskit) (1.2.1)
    Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.19->qiskit-ibmq-provider==0.13.1->qiskit) (2020.12.5)
    Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.19->qiskit-ibmq-provider==0.13.1->qiskit) (2.10)
    Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.19->qiskit-ibmq-provider==0.13.1->qiskit) (3.0.4)
    Collecting ntlm-auth>=1.0.2
      Downloading https://files.pythonhosted.org/packages/ff/84/97c550164b54942b0e908c31ef09d9469f3ba4cd7332a671e2125732f63b/ntlm_auth-1.5.0-py2.py3-none-any.whl
    Collecting cryptography>=1.3
      Downloading https://files.pythonhosted.org/packages/b2/26/7af637e6a7e87258b963f1731c5982fb31cd507f0d90d91836e446955d02/cryptography-3.4.7-cp36-abi3-manylinux2014_x86_64.whl (3.2MB)
         |████████████████████████████████| 3.2MB 32.7MB/s
    Requirement already satisfied: cached-property; python_version < "3.8" in /usr/local/lib/python3.7/dist-packages (from h5py<=3.1.0->qiskit-aqua==0.9.1->qiskit) (1.5.2)
    Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn<=0.24.1,>=0.20.0->qiskit-aqua==0.9.1->qiskit) (1.0.1)
    Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas<=1.2.3->qiskit-aqua==0.9.1->qiskit) (2018.9)
    Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.7/dist-packages (from yfinance<=0.1.55->qiskit-aqua==0.9.1->qiskit) (0.0.9)
    Collecting lxml>=4.5.1
      Downloading https://files.pythonhosted.org/packages/30/c0/d0526314971fc661b083ab135747dc68446a3022686da8c16d25fcf6ef07/lxml-4.6.3-cp37-cp37m-manylinux2014_x86_64.whl (6.3MB)
         |████████████████████████████████| 6.3MB 28.8MB/s
    Requirement already satisfied: more-itertools in /usr/local/lib/python3.7/dist-packages (from quandl<=3.6.0->qiskit-aqua==0.9.1->qiskit) (8.7.0)
    Collecting inflection>=0.3.1
      Downloading https://files.pythonhosted.org/packages/59/91/aa6bde563e0085a02a435aa99b49ef75b0a4b062635e606dab23ce18d720/inflection-0.5.1-py2.py3-none-any.whl
    Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.7/dist-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibmq-provider==0.13.1->qiskit) (1.14.5)
    Requirement already satisfied: pycparser in /usr/local/lib/python3.7/dist-packages (from cffi>=1.12->cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibmq-provider==0.13.1->qiskit) (2.20)
    Building wheels for collected packages: qiskit, python-constraint, yfinance, dlx, docplex
      Building wheel for qiskit (setup.py) ... done
      Created wheel for qiskit: filename=qiskit-0.26.2-cp37-none-any.whl size=10491 sha256=f918bb498cb8dbde768f9544d24f28176928a4a92eaa20342c52edbb0938f4dd
      Stored in directory: /root/.cache/pip/wheels/89/89/34/524839952d5a58a7be9789e580bfc1ca883bf6579152444568
      Building wheel for python-constraint (setup.py) ... done
      Created wheel for python-constraint: filename=python_constraint-1.4.0-py2.py3-none-any.whl size=24079 sha256=35b086ebd96ea7a2891fb5e52f5485c24ae251f78fec828a2895254c5038edb3
      Stored in directory: /root/.cache/pip/wheels/34/31/15/7b070b25d0a549d20ce2e9fe6d727471c2c61ef904720fd40c
      Building wheel for yfinance (setup.py) ... done
      Created wheel for yfinance: filename=yfinance-0.1.55-py2.py3-none-any.whl size=22616 sha256=5f1fe2d1709cbbfaf38cdf03a09dc41d437133e2554f1b41c2392074f45c9943
      Stored in directory: /root/.cache/pip/wheels/04/98/cc/2702a4242d60bdc14f48b4557c427ded1fe92aedf257d4565c
      Building wheel for dlx (setup.py) ... done
      Created wheel for dlx: filename=dlx-1.0.4-cp37-none-any.whl size=5712 sha256=1154e839748f8d28680d5d7c922c454909954f6b3a334a5b7633ba059156b7e1
      Stored in directory: /root/.cache/pip/wheels/bb/ba/15/fdd0deb104df3254912998150ba9245668db06b00af5912d1a
      Building wheel for docplex (setup.py) ... done
      Created wheel for docplex: filename=docplex-2.20.204-cp37-none-any.whl size=675362 sha256=9494f9dd2dec35c5b52e88a18051305c0b7307ab107312f9b0d514a546535200
      Stored in directory: /root/.cache/pip/wheels/ae/2c/e2/a099ebb6fda8adeba9c5fc2e25659d195ad2f5c6cc5fb75fd4
    Successfully built qiskit python-constraint yfinance dlx docplex
    ERROR: qiskit-aqua 0.9.1 has requirement retworkx<=0.8.0,>=0.7.0, but you'll have retworkx 0.9.0 which is incompatible.
    Installing collected packages: fastjsonschema, ply, python-constraint, retworkx, qiskit-terra, pybind11, qiskit-aer, websockets, ntlm-auth, cryptography, requests-ntlm, qiskit-ibmq-provider, qiskit-ignis, lxml, yfinance, inflection, quandl, dlx, docplex, qiskit-aqua, qiskit
      Found existing installation: lxml 4.2.6
        Uninstalling lxml-4.2.6:
          Successfully uninstalled lxml-4.2.6
    Successfully installed cryptography-3.4.7 dlx-1.0.4 docplex-2.20.204 fastjsonschema-2.15.1 inflection-0.5.1 lxml-4.6.3 ntlm-auth-1.5.0 ply-3.11 pybind11-2.6.2 python-constraint-1.4.0 qiskit-0.26.2 qiskit-aer-0.8.2 qiskit-aqua-0.9.1 qiskit-ibmq-provider-0.13.1 qiskit-ignis-0.6.0 qiskit-terra-0.17.4 quandl-3.6.0 requests-ntlm-1.1.0 retworkx-0.9.0 websockets-9.1 yfinance-0.1.55

```python
!pip install qiskit-nature

```

    Collecting qiskit-nature
      Downloading https://files.pythonhosted.org/packages/4c/54/2562684ccf73215d3d85b65174ea06ee3f1b184422cfd81241a8a13491cd/qiskit_nature-0.1.3-py3-none-any.whl (1.5MB)
         |████████████████████████████████| 1.5MB 6.5MB/s
    Requirement already satisfied: h5py in /usr/local/lib/python3.7/dist-packages (from qiskit-nature) (3.1.0)
    Requirement already satisfied: scipy>=1.4 in /usr/local/lib/python3.7/dist-packages (from qiskit-nature) (1.4.1)
    Requirement already satisfied: scikit-learn>=0.20.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-nature) (0.22.2.post1)
    Requirement already satisfied: setuptools>=40.1.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-nature) (56.1.0)
    Requirement already satisfied: psutil>=5 in /usr/local/lib/python3.7/dist-packages (from qiskit-nature) (5.4.8)
    Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.7/dist-packages (from qiskit-nature) (1.19.5)
    Requirement already satisfied: qiskit-terra>=0.17.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-nature) (0.17.4)
    Requirement already satisfied: cached-property; python_version < "3.8" in /usr/local/lib/python3.7/dist-packages (from h5py->qiskit-nature) (1.5.2)
    Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from scikit-learn>=0.20.0->qiskit-nature) (1.0.1)
    Requirement already satisfied: python-dateutil>=2.8.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (2.8.1)
    Requirement already satisfied: dill>=0.3 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (0.3.3)
    Requirement already satisfied: retworkx>=0.8.0 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (0.9.0)
    Requirement already satisfied: ply>=3.10 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (3.11)
    Requirement already satisfied: python-constraint>=1.4 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (1.4.0)
    Requirement already satisfied: sympy>=1.3 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (1.7.1)
    Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (2.6.0)
    Requirement already satisfied: fastjsonschema>=2.10 in /usr/local/lib/python3.7/dist-packages (from qiskit-terra>=0.17.0->qiskit-nature) (2.15.1)
    Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.8.0->qiskit-terra>=0.17.0->qiskit-nature) (1.15.0)
    Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.7/dist-packages (from sympy>=1.3->qiskit-terra>=0.17.0->qiskit-nature) (1.2.1)
    Installing collected packages: qiskit-nature
    Successfully installed qiskit-nature-0.1.3

```python
!pip install pyscf
```

    Collecting pyscf
      Downloading https://files.pythonhosted.org/packages/ef/d6/af4ce5035977cb011e4dbe9979bf254129a36d48cb569b86e57b5a72c5b1/pyscf-1.7.6.post1-cp37-cp37m-manylinux1_x86_64.whl (29.7MB)
         |████████████████████████████████| 29.7MB 145kB/s
    Requirement already satisfied: numpy!=1.16,!=1.17,>1.8 in /usr/local/lib/python3.7/dist-packages (from pyscf) (1.19.5)
    Requirement already satisfied: scipy!=1.5.0,!=1.5.1 in /usr/local/lib/python3.7/dist-packages (from pyscf) (1.4.1)
    Requirement already satisfied: h5py>2.2 in /usr/local/lib/python3.7/dist-packages (from pyscf) (3.1.0)
    Requirement already satisfied: cached-property; python_version < "3.8" in /usr/local/lib/python3.7/dist-packages (from h5py>2.2->pyscf) (1.5.2)
    Installing collected packages: pyscf
    Successfully installed pyscf-1.7.6.post1

```python
from qiskit_nature.drivers import PySCFDriver #Importing pyscf module to enable simulation of chemical molecules

molecule = 'Li 0.0 0.0 0.0; H 0.0 0.0 1.5474'#These represent the coordinates of the atoms in the molcule
driver = PySCFDriver(atom=molecule)
qmolecule = driver.run()
```

```python
n_mo = qmolecule.num_molecular_orbitals
print(n_mo)#This prints the total number of molecular orbitals which is 6 here

from qiskit_nature.problems.second_quantization.electronic import ElectronicStructureProblem #To produce list of fermionic operators
from qiskit_nature.transformers import FreezeCoreTransformer #Freezing the core to reduce the number of qubits

#In order to find out which orbitals to freeze, I checked the corresponding exact ground energy value by freezing all the orbitals in the range (0,5) and chose the ones which were closer to the threshold value
tr = FreezeCoreTransformer(True,[3,4])#Freezing orbitals 3 and 4
problem = ElectronicStructureProblem(driver,[tr])

second_q_ops = problem.second_q_ops()#Generating the second-quantized operators

main_op = second_q_ops[0]#Hamiltonian

```

    6

```python
from qiskit_nature.mappers.second_quantization import ParityMapper, BravyiKitaevMapper, JordanWignerMapper
from qiskit_nature.converters.second_quantization.qubit_converter import QubitConverter

#Setting up the mapper and qubit converter
#Defining mapping for representing molecular orbitals using qubits
mapper = ParityMapper()

converter = QubitConverter(mapper=mapper, two_qubit_reduction=True)#,z2symmetry_reduction=[-1,1])#Uncomment to apply z2symmetry
# The fermionic operators are mapped to qubit operators
num_particles = (problem.molecule_data_transformed.num_alpha,
             problem.molecule_data_transformed.num_beta)
qubit_op = converter.convert(main_op, num_particles=num_particles)
print(qubit_op.z2_symmetries)#This prints the symmetries present in the molecule . I didn't apply z2symmetry because I already reduce the number of qubits to 4
```

    Z2 symmetries:
    Symmetries:
    IIIZII
    ZIIIII
    Single-Qubit Pauli X:
    IIIXII
    XIIIII
    Cliffords:
    0.7071067811865475 * IIIZII
    + 0.7071067811865475 * IIIXII
    0.7071067811865475 * ZIIIII
    + 0.7071067811865475 * XIIIII
    Qubit index:
    [2, 5]
    Tapering values:
    [-1, 1]

```python
from qiskit_nature.circuit.library import HartreeFock
#To implement hartreefrock state as the initial state
num_particles = (problem.molecule_data_transformed.num_alpha,
             problem.molecule_data_transformed.num_beta)
num_spin_orbitals = 2 * problem.molecule_data_transformed.num_molecular_orbitals
init_state = HartreeFock(num_spin_orbitals, num_particles, converter)
print(init_state)
```

         ┌───┐
    q_0: ┤ X ├
         ├───┤
    q_1: ┤ X ├
         └───┘
    q_2: ─────

    q_3: ─────

```python
from qiskit.circuit.library import TwoLocal
#Choosing a quantum circuit to approximate ground state

#Parameters for q-UCC antatze
num_particles = (problem.molecule_data_transformed.num_alpha,
            problem.molecule_data_transformed.num_beta)
num_spin_orbitals = 2 * problem.molecule_data_transformed.num_molecular_orbitals

#Using TwoLocal ansatz with the following parameters because it uses least number of CNOTs found out by Hit and Trial
#Single qubit rotations that are placed on all qubits with independent parameters
rotation_blocks = ['ry', 'rz']
#Entangling gates
entanglement_blocks = 'cx'
#How the qubits are entangled
entanglement = 'linear'
#Repetitions of rotation_blocks + entanglement_blocks with independent parameters
repetitions = 1
# Not skipping the final rotation_blocks layer
skip_final_rotation_layer = False
ansatz = TwoLocal(qubit_op.num_qubits, rotation_blocks, entanglement_blocks, reps=repetitions,
                      entanglement=entanglement, skip_final_rotation_layer=skip_final_rotation_layer)

print(ansatz)
```

         ┌──────────┐┌──────────┐     ┌──────────┐┌───────────┐             »
    q_0: ┤ RY(θ[0]) ├┤ RZ(θ[4]) ├──■──┤ RY(θ[8]) ├┤ RZ(θ[12]) ├─────────────»
         ├──────────┤├──────────┤┌─┴─┐└──────────┘└┬──────────┤┌───────────┐»
    q_1: ┤ RY(θ[1]) ├┤ RZ(θ[5]) ├┤ X ├─────■───────┤ RY(θ[9]) ├┤ RZ(θ[13]) ├»
         ├──────────┤├──────────┤└───┘   ┌─┴─┐     └──────────┘├───────────┤»
    q_2: ┤ RY(θ[2]) ├┤ RZ(θ[6]) ├────────┤ X ├──────────■──────┤ RY(θ[10]) ├»
         ├──────────┤├──────────┤        └───┘        ┌─┴─┐    ├───────────┤»
    q_3: ┤ RY(θ[3]) ├┤ RZ(θ[7]) ├─────────────────────┤ X ├────┤ RY(θ[11]) ├»
         └──────────┘└──────────┘                     └───┘    └───────────┘»
    «
    «q_0: ─────────────
    «
    «q_1: ─────────────
    «     ┌───────────┐
    «q_2: ┤ RZ(θ[14]) ├
    «     ├───────────┤
    «q_3: ┤ RZ(θ[15]) ├
    «     └───────────┘

```python
from qiskit import Aer #To specify the simulator for the algorithm to run on
backend = Aer.get_backend('statevector_simulator')
```

```python
from qiskit.algorithms.optimizers import SLSQP
optimizer = SLSQP(maxiter=2000)#This optimiser gave the correct energy value for the chosen ansatz
```

```python
from qiskit_nature.algorithms.ground_state_solvers.minimum_eigensolver_factories import NumPyMinimumEigensolverFactory
from qiskit_nature.algorithms.ground_state_solvers import GroundStateEigensolver
import numpy as np
#Trying to solve the problem using exact diagonalization of the Hamiltonian

def exact_diagonalizer(problem, converter):
    solver = NumPyMinimumEigensolverFactory()
    calc = GroundStateEigensolver(converter, solver)
    result = calc.solve(problem)
    return result

result_exact = exact_diagonalizer(problem, converter)
exact_energy = np.real(result_exact.eigenenergies[0])
print("Exact electronic energy", exact_energy)
print(result_exact)

# The targeted electronic energy for LiH is -1.086 Ha

```

    Exact electronic energy -1.0887060157347406
    === GROUND STATE ENERGY ===

    * Electronic ground state energy (Hartree): -8.907396311316
      - computed part:      -1.088706015735
      - FreezeCoreTransformer extracted energy part: -7.818690295581
    ~ Nuclear repulsion energy (Hartree): 1.025934879643
    > Total ground state energy (Hartree): -7.881461431673

    === MEASURED OBSERVABLES ===

      0:  # Particles: 2.000 S: 0.000 S^2: 0.000 M: 0.000

    === DIPOLE MOMENTS ===

    ~ Nuclear dipole moment (a.u.): [0.0  0.0  2.92416221]

      0:
      * Electronic dipole moment (a.u.): [0.0  0.0  4.76300889]
        - computed part:      [0.0  0.0  4.76695575]
        - FreezeCoreTransformer extracted energy part: [0.0  0.0  -0.00394686]
      > Dipole moment (a.u.): [0.0  0.0  -1.83884668]  Total: 1.83884668
                     (debye): [0.0  0.0  -4.67388163]  Total: 4.67388163

```python
from qiskit.algorithms import VQE
from IPython.display import display, clear_output
#Importing VQE to run the algorithim

def callback(eval_count, parameters, mean, std):
    display("Evaluation: {}, Energy: {}, Std: {}".format(eval_count, mean, std))
    clear_output(wait=True)
    counts.append(eval_count)
    values.append(mean)
    params.append(parameters)
    deviation.append(std)

counts = []
values = []
params = []
deviation = []

#Default fixed small displacement
try:
    initial_point = [0.01] * len(ansatz.ordered_parameters)
except:
    initial_point = [0.01] * ansatz.num_parameters

algorithm = VQE(ansatz,
                optimizer=optimizer,
                quantum_instance=backend,
                callback=callback,
                initial_point=initial_point)

result = algorithm.compute_minimum_eigenvalue(qubit_op)

print(result)
```

    {   'aux_operator_eigenvalues': None,
        'cost_function_evals': 851,
        'eigenstate': array([-1.38503080e-03-3.73975129e-05j,  4.83798144e-03+6.36745268e-05j,
           -2.66978013e-02-3.06676159e-04j,  9.91386592e-01+1.39272516e-03j,
            5.38283495e-02+5.40890327e-04j,  3.28152532e-04+9.93615636e-06j,
           -9.07220622e-04+4.32502418e-06j,  2.39114676e-02-3.84502735e-04j,
            2.72891302e-03+4.22182794e-05j,  9.36230793e-06+1.71620611e-06j,
           -5.91311396e-06-6.28705625e-06j, -2.73155829e-04+2.34338973e-04j,
           -1.13726193e-01+2.30850218e-04j, -4.47959858e-04-1.66081270e-05j,
            5.62790023e-04-7.93456918e-06j, -2.66622417e-04+1.18701780e-05j]),
        'eigenvalue': -1.0863663677292887,
        'optimal_parameters': {   ParameterVectorElement(θ[4]): -0.004768579123950326,
                                  ParameterVectorElement(θ[12]): -0.010069423154352004,
                                  ParameterVectorElement(θ[5]): -0.0037985015266241207,
                                  ParameterVectorElement(θ[6]): -0.005513500988743045,
                                  ParameterVectorElement(θ[7]): -0.0010895487469465151,
                                  ParameterVectorElement(θ[8]): 0.05313368644547072,
                                  ParameterVectorElement(θ[9]): -0.00977072471667363,
                                  ParameterVectorElement(θ[10]): 0.047986900543765254,
                                  ParameterVectorElement(θ[13]): -0.011810269141820076,
                                  ParameterVectorElement(θ[14]): -0.017507464711468492,
                                  ParameterVectorElement(θ[15]): -0.012680710973539674,
                                  ParameterVectorElement(θ[1]): 0.005744855621419048,
                                  ParameterVectorElement(θ[11]): 0.44182324189775946,
                                  ParameterVectorElement(θ[0]): -2.8892613849526074,
                                  ParameterVectorElement(θ[3]): -0.44239241452227135,
                                  ParameterVectorElement(θ[2]): -3.1410072751603506},
        'optimal_point': array([-2.88926138e+00,  4.79869005e-02,  4.41823242e-01, -1.00694232e-02,
           -1.18102691e-02, -1.75074647e-02, -1.26807110e-02,  5.74485562e-03,
           -3.14100728e+00, -4.42392415e-01, -4.76857912e-03, -3.79850153e-03,
           -5.51350099e-03, -1.08954875e-03,  5.31336864e-02, -9.77072472e-03]),
        'optimal_value': -1.0863663677292887,
        'optimizer_evals': 851,
        'optimizer_time': 9.800698041915894}

```python
#Storing results in a dictionary
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import Unroller

#Transpiling circuit into CNOTs and U gates
pass_ = Unroller(['u', 'cx'])
pm = PassManager(pass_)
ansatz_tp = pm.run(ansatz)
cnots = ansatz_tp.count_ops()['cx']
score = cnots

accuracy_threshold = 4.0 #in mHa
energy = result.optimal_value

result_dict = {
        'optimizer': optimizer.__class__.__name__,
        'mapping': converter.mapper.__class__.__name__,
        'ansatz': ansatz.__class__.__name__,
        'rotation blocks': rotation_blocks,
        'entanglement_blocks': entanglement_blocks,
        'entanglement': entanglement,
        'repetitions': repetitions,
        'skip_final_rotation_layer': skip_final_rotation_layer,
        'energy (Ha)': energy,
        'error (mHa)': (energy-exact_energy)*1000,
        'pass': (energy-exact_energy)*1000 <= accuracy_threshold,
        '# of parameters': len(result.optimal_point),
        'final parameters': result.optimal_point,
        '# of evaluations': result.optimizer_evals,
        'optimizer time': result.optimizer_time,
        '# of qubits': int(qubit_op.num_qubits),
        '# of CNOTs': cnots,
        'score': score}

#Plotting the results
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)
ax.set_xlabel('Iterations')
ax.set_ylabel('Energy')
ax.grid()
fig.text(0.7, 0.75, f'Energy: {result.optimal_value:.3f}\nScore: {score:.0f}')
plt.title(f"{result_dict['optimizer']}-{result_dict['mapping']}\n{result_dict['ansatz']}")
ax.plot(counts, values)
ax.axhline(exact_energy, linestyle='--')
fig_title = f"\
{result_dict['optimizer']}-\
{result_dict['mapping']}-\
{result_dict['ansatz']}-\
Energy({result_dict['energy (Ha)']:.3f})-\
Score({result_dict['score']:.0f})\
.png"
fig.savefig(fig_title, dpi=300)

#Displaying and saving the data
import pandas as pd
import os.path
filename = 'results_h2.csv'
if os.path.isfile(filename):
    result_df = pd.read_csv(filename)
    result_df = result_df.append([result_dict])
else:
    result_df = pd.DataFrame.from_dict([result_dict])
result_df.to_csv(filename)
result_df[['optimizer','ansatz', '# of qubits', '# of parameters','rotation blocks', 'entanglement_blocks',
    'entanglement', 'repetitions', 'error (mHa)', 'pass', 'score']]
```

```python

```