# Using External Libraries¶

It is straightforward to make use of external libraries written in other language. For example, the following codes import modules from the OpenFermion and construct a qubit Hamiltonian for molecules as quantum blocks in Yao.

In [ ]:
using Yao
using LinearAlgebra
using PyCall


Install OpenFermion and PySCF if you don't have them

In [ ]:
pip = pyimport("pip._internal.main")
pip.main(["install", "pyscf", "openfermion", "openfermionpyscf"])


First we import hamiltonians from OpenFermion and PySCF

In [ ]:
of_hamil = pyimport("openfermion.hamiltonians")
of_trsfm = pyimport("openfermion.transforms")
of_pyscf = pyimport("openfermionpyscf")


following the OpenFermion and PySCF tutorial, we define molecules from data.

In [ ]:
diatomic_bond_length = 1.0
geometry = [("H", (0., 0., 0.)), ("H", (0., 0., diatomic_bond_length))]
basis = "sto-3g"
multiplicity = 1
charge = 0
description = string(diatomic_bond_length)

molecule = of_hamil.MolecularData(geometry, basis, multiplicity, charge, description)
molecule = of_pyscf.run_pyscf(molecule,run_scf=1,run_fci=1)


Then obtain its Hamiltonian

In [ ]:
m_h = molecule.get_molecular_hamiltonian()
nbits = m_h.n_qubits

jw_h = of_trsfm.jordan_wigner(of_trsfm.get_fermion_operator(m_h))


now, let's construct the corresponding Hamiltonian in Yao

In [ ]:
function yao_hamiltonian(nbits, jw_h)
gates = Dict("X"=>X, "Y"=>Y, "Z"=>Z)
for (k, v) in jw_h.terms
op = v*put(nbits, 1=>I2)
for t in k
site, opname = t
op = op*put(nbits, site+1=>gates[opname])
end
push!(h, op)
end
h
end


Yah!

In [ ]:
yao_h = yao_hamiltonian(nbits, jw_h)


now let's try to calculate its eigen values

In [ ]:
w, v = eigen(Matrix(yao_h))


This notebook was generated using Literate.jl.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6593492,"math_prob":0.9889107,"size":1593,"snap":"2020-45-2020-50","text_gpt3_token_len":512,"char_repetition_ratio":0.12838262,"word_repetition_ratio":0.0,"special_character_ratio":0.26553673,"punctuation_ratio":0.19117647,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99929845,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-12-02T19:18:52Z\",\"WARC-Record-ID\":\"<urn:uuid:02d67651-aaf0-4f40-9070-56d1744f6cff>\",\"Content-Length\":\"23266\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e7eebfa7-cbbc-40e5-9008-267e4fb6b3c1>\",\"WARC-Concurrent-To\":\"<urn:uuid:291c055c-a8c4-4c7d-ba23-60bb762793d0>\",\"WARC-IP-Address\":\"104.28.8.110\",\"WARC-Target-URI\":\"https://nbviewer.jupyter.org/github/QuantumBFS/tutorials/blob/gh-pages/dev/generated/developer-guide/3.extern-libraries/main.ipynb\",\"WARC-Payload-Digest\":\"sha1:57XF7WMBZOHNWYG53H55LAMDBX64KBU7\",\"WARC-Block-Digest\":\"sha1:UNWK362RB5KI36RF3M323WHDWB7D7VIC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141715252.96_warc_CC-MAIN-20201202175113-20201202205113-00135.warc.gz\"}"}