Quantum computing for chemistry #RDKit #psi4 #qiskit

Some years ago I read book about quantum computing written in Japanese. https://www.amazon.co.jp/%E9%87%8F%E5%AD%90%E3%82%B3%E3%83%B3%E3%83%94%E3%83%A5%E3%83%BC%E3%82%BF%E3%81%8C%E4%BA%BA%E5%B7%A5%E7%9F%A5%E8%83%BD%E3%82%92%E5%8A%A0%E9%80%9F%E3%81%99%E3%82%8B-%E8%A5%BF%E6%A3%AE-%E7%A7%80%E7%A8%94/dp/4822251896

It was very exciting for me and I had interested in quantum computer.

Quantum computer has potential to solve many difficult problems for conventional computer. Of course it is useful for chemistry. You know qiskit is one of python library for quantum computing frame work which is developed by IBM.

Fortunately, pythonista can use qiskit just only type pip install qiskit!

Qiskit has driver for several quantum chemistry calculation frame work such as PySCF, Psi4 and Gaussian etc.

Today I tried to use qiskit for chemistry problem with psi4(psikit).

Following code calculate energy of the query molecule with psi4 and optimize hamiltonian with quantum computer (used simulator in following case).

Let’s write code. Import packages for calculation and define querymaker method for qiskit driver object. I used PSI4Driver but other drivers are also available. If reader would like to use pyscf, pyquante and gaussian, please check the document.

 
import qiskit
from qiskit.chemistry import drivers
from qiskit.chemistry import FermionicOperator
from psikit import Psikit

# Defiene query maker for Psi4Driver I used psikit for mol to xyz
def querymaker(smiles, basis='sto-3g'):
    pk = Psikit()
    pk.read_from_smiles(smiles)
    xyz = pk.mol2xyz()
    setting_str = 'molecule mol {' + xyz + '}\n\n'
    setting_str += 'set {\n basis ' + basis + '\n scf_type pk\n}'
    setting_str = setting_str.replace('no_reorient', '').replace('no_com', '')
    return setting_str

Next, I passed the query to the driver and run the 1st step calculation.

 
# make query for quantum calculation

query = querymaker('O')
driver = drivers.PSI4Driver(query.split('\n'))

# run psi4 calculation
mol = driver.run()
print('finish psi4 calc')
num_particles = mol.num_alpha + mol.num_beta
num_spin_orbitals = mol.num_orbitals * 2

After the calculation, make qubit operator. The code is almost same as the original repository’s README.md.

 
# Build qubit operator
# please see eq 5 of the reference
ferOp = FermionicOperator(h1=mol.one_body_integrals, h2=mol.two_body_integrals)

map_type = 'PARITY'
qubitOp = ferOp.mapping(map_type)
qubitOp = qubitOp.two_qubit_reduced_operator(num_particles)
num_qubits = qubitOp.num_qubits
print(f'There are {num_particles} particles ')
print(f'{num_qubits} Qubits will be used for calculation')

Fermionoperator is the function which maps fermionic Hamiltonian to qubit Hamiltonian.

And the final part of this code is quantum computation.

 
# set the backend for the quantum computation
from qiskit import Aer
backend = Aer.get_backend('statevector_simulator')

from qiskit.aqua.components.optimizers import L_BFGS_B
optimizer = L_BFGS_B()

from qiskit.chemistry.aqua_extensions.components.initial_states import HartreeFock
init_state = HartreeFock(num_qubits, num_spin_orbitals, num_particles)
from qiskit.aqua.components.variational_forms import RYRZ
var_form = RYRZ(num_qubits, initial_state=init_state)

from qiskit.aqua.algorithms import VQE
algorithm = VQE(qubitOp, var_form, optimizer)
result = algorithm.run(backend)

print(result)

VQE is a variational eigenvalue solver on a quantum processor. The details are described following article.

https://arxiv.org/pdf/1304.3061.pdf

It is useful for solving large eigenvalue problems. And final output was below.

 
iwatobipen$ python qiskit_test.py 

  Memory set to   3.725 GiB by Python driver.
  Threads set to 4 by Python driver.
finish psi4 calc
There are 10 particles 
12 Qubits will be used for calculation
{'num_optimizer_evals': 1067, 'min_val': -82.1551123486962, 'opt_params': array([ 3.14159265, -3.14159265,  3.14159265, -0.47209231,  3.14159265,
        1.26538018,  3.08659821, -2.03722008,  3.14159265,  1.83786188,
       -1.14116453, -1.22806647,  3.14159265, -2.51141238,  2.76518682,
        0.65038408, -0.5401877 ,  0.94977249,  0.16367673, -2.61606226,
        2.14714991,  1.33265892,  0.14693617, -2.29699039, -0.06600933,
        1.37467628, -3.14159265, -1.58506755, -2.26983475, -0.34581384,
        1.11665802, -0.7554235 ,  1.3765809 , -0.14743813,  2.31945686,
       -0.35074729,  3.13606185, -1.74064297, -1.1052301 ,  1.83308917,
       -0.55939713,  0.58150969, -2.57860126, -2.64458621, -2.38124305,
       -3.03600205,  1.50484991, -3.07866269,  3.14159265, -0.28057194,
        3.14159265, -1.20003684,  0.2060857 ,  0.7300913 ,  0.13547982,
        0.93496467, -2.7768489 ,  2.78637461,  3.06799269,  2.39524209,
        3.14159265, -2.55084639, -2.42941145,  2.57242438,  0.25916875,
        1.85199233,  2.96304219, -0.15247656, -0.09554275,  1.10617151,
        0.83882494,  0.73484547,  0.09034047, -0.95804794, -2.97968157,
       -0.96409293, -1.70094319,  0.719477  ,  0.96564404,  0.65027412,
       -1.65178365, -2.26639198, -1.25597346, -3.01669026, -3.11323418,
        0.82251654,  1.33881735,  0.45800526,  0.32908445, -2.53067172,
        0.36776732, -2.94786506,  0.22166631, -1.00564524, -2.19364914,
       -2.35394437]), 'eval_time': 150.38587403297424, 'eval_count': 1067, 'energy': -82.1551123486962, 'eigvals': array([-82.15511235]), 'min_vector': array([ 1.60736505e-04+1.29185427e-04j, -1.48578723e-03+4.25322038e-04j,
        1.60461226e-05-4.73763479e-06j, ...,
        1.05276114e-04-1.39253104e-04j, -7.71091869e-07+1.48904495e-07j,
       -4.41171486e-06-1.34586907e-05j]), 'eigvecs': array([[ 1.60736505e-04+1.29185427e-04j, -1.48578723e-03+4.25322038e-04j,
         1.60461226e-05-4.73763479e-06j, ...,
         1.05276114e-04-1.39253104e-04j, -7.71091869e-07+1.48904495e-07j,
        -4.41171486e-06-1.34586907e-05j]])}

The result object was dictionary so energy can get by result[‘energy’].

The algorithm of quantum computing is based on physics and quantum chemistry. Now I am learning not only quantum coding but also physics, mathematics, quantum chemistry.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.