# Quantum Phase Estimation (QPE) for Solving Molecular Energies

Quantum Phase Estimation (QPE) is a key algorithm in quantum computing, allowing one to estimate the phase (or eigenvalue) of an eigenvector of a unitary operation. The algorithm is designed so, that for a given Hamiltonian $$H$$, and an eigenvalue $${|\psi\rangle}$$, the output that will be obtained is $$\epsilon$$ where

$$U{|\psi\rangle} = e^{2\pi i\epsilon}{|\psi\rangle} , U = e^{2\pi iH}$$ .

Therefore, by measuring the phase accumulated, the QPE algorithm allows calculating the energies relating to the chosen initial state. When using QPE for chemistry problems, it is common to search for the lowest energy of a given molecule. As the molecule can be written in the form of a Hamiltonian (Hermitian matrix which meaning is the energetic forces of the structure), usually symbolized by "H", one only need to insert the ground eigenvector in order to obtain the minimal energy value using QPE. However, obtaining the ground state is not a trivial problem. In order to overcome it, it is sufficient to use a state with big overlap with the ground state.

We define a state $${|v\rangle}$$ which will be chosen as the algorithm's initial state. Let us define {$$\psi_i$$} to be the set of (unknown) eigenvalues of $$H$$. Generally, any vector can be rewritten as a superposition of any basis set, thus -

$${|v\rangle} = \sum_i a_i{|\psi_i\rangle}$$

and -

$$U{|v\rangle} = \sum_i a_i e^{2\pi i\epsilon_i}{|\psi_i\rangle}$$.

where $${\epsilon_i}$$ are the eigenvalues of H, i.e. the span of energies relating to the molecule. Using execution with enough shots, one will obtain this set of $$\theta_i$$, i.e., a subset of the Hamiltonian's eigenvalues. As we are specifically interested in $$\epsilon_0$$, the ground state of H, it is important that there is a large overlap between $${\psi_0}$$ and $${|v\rangle}$$ so the probability to measure $${\epsilon_0}$$ is high, i.e.

$P(\epsilon_0) = |\langle v|\psi_0\rangle|^2 > \epsilon$.

How large is $$\epsilon$$? After execution, we will obtain a set of $${E_i}$$. If we have done 1000 shots of execution, and $$P(\epsilon_0)>1$$, it should be measured is about 10 times to get a good statistical variance.

A common choice for $${|v\rangle}$$ (the initial state) is the Hartree-Fock (HF) state, which with good probability have a large overlap with the ground state. However, other guesses for the initial state are possibly good or even better fit, and choosing the right initial state is a sort of art and an active field of research.

For further reading about QPE we recommend [1].

#### What are the benefits of using QPE algorithm for finding a molecule's ground state?

The two most prominent methods to solve ground energy for molecules are quantum variational algorithm (VQE) and QPE. Those promise better scalability compared to classical counterparts as the molecules becomes more complex, with larger number of electrons manifesting as more degrees of freedom for the problem.

While so, the number of parameters in VQE is closely related to the number of electrons. This may create inherent difficulty achieving high-precision calculations through sampling statistical estimators, and paraphs even not converge for very large systems. While so, the number of parameters in QPE is a flexible value which is directly related to the resolution of the problem, but is not bonded with the number of electrons.

Furthermore, it is known that advanced quantum algorithms based on QPE can perform electronic structure calculations in sub-exponential time with accuracy that rivals exact diagonalization methods. This guarantee of simultaneously achieving high accuracy, efficiency, and generality is a feat that is believed to be impossible for classical algorithms. For those reasons, VQE is applicable in the near term (NISQ) era, while QPE is suited for fault-tolerant design.

#### During this tutorial, we will follow the QPE algorithms steps as following:

a. Define a molecule and convert it into a Hamiltonian H, manifested as a pauli list.

b. Prepare the Hamiltonian for QPE: including normalization and choosing the resolution.

c. Initializing the state for the HF state.

d. Preparing the quantum circuit.

e. Executing the circuit to find the related phases and analyzing the results to find the ground state.

f. Comparing the QPE results with exact solution for the moleculeâ€™s ground state.

## 0. Pre-requirments

The model is using several Classiq's libraries in addition to IBM's simulating tool.

# !pip install -U classiq

# import classiq
# classiq.authenticate()

import matplotlib.pyplot as plt
import numpy as np
import scipy
from numpy import linalg as LA

# for state preperation and phase estimation
from classiq import Constraints, Preferences

# for chemistry
from classiq.applications.chemistry import Molecule, MoleculeProblem, PauliOperator


## 1. Create Your Molecule

### XYZ cartesian coordinates

# build your molecule

# hydrogen
molecule_H2 = Molecule(atoms=[("H", (0.0, 0.0, 0)), ("H", (0.0, 0.0, 0.735))])
# oxygen
molecule_O2 = Molecule(atoms=[("O", (0.0, 0.0, 0)), ("O", (0.0, 0.0, 1.16))])
# lithium hydride
molecule_LiH = Molecule(atoms=[("H", (0.0, 0.0, 0.0)), ("Li", (0.0, 0.0, 1.596))])
# water
molecule_H2O = Molecule(
atoms=[("O", (0.0, 0.0, 0.0)), ("H", (0, 0.586, 0.757)), ("H", (0, 0.586, -0.757))]
)
# beryllium hydride
molecule_BeH2 = Molecule(
atoms=[("Be", (0.0, 0.0, 0.0)), ("H", (0, 0, 1.334)), ("H", (0, 0, -1.334))]
)
# carbon dioxide
molecule_CO2 = Molecule(
atoms=[("C", (0.0, 0.0, 0.0)), ("O", (0, 0, 1.1693)), ("O", (0, 0, -1.1693))]
)
# ethyne, acetylene
molecule_C2H2 = Molecule(
atoms=[
("C", (0, 0, -0.5977)),
("C", (0, 0, 0.5977)),
("H", (0, 0, -1.6692)),
("H", (0, 0, 1.6692)),
]
)
# chloroform
molecule_CH3Cl = Molecule(
atoms=[
("C", (0, 0, -1.1401)),
("Cl", (0, 0, 0.6645)),
("H", (0, 1.0343, -1.4855)),
("H", (0.8957, -0.5171, -1.4855)),
("H", (-0.8957, -0.5171, -1.4855)),
]
)
# ethylene
molecule_C2H4 = Molecule(
atoms=[
("C", (0, 0, 0.6673)),
("C", (0, 0, -0.6673)),
("H", (0, 0.9239, 1.2411)),
("H", (0, -0.9239, 1.2411)),
("H", (0, -0.9239, -1.2411)),
("H", (0, 0.9239, -1.2411)),
]
)

# define your molecule
# molecule = molecule_H2
# molecule = molecule_O2
# molecule = molecule_LiH

molecule = molecule_BeH2

# molecule = molecule_H2O

# molecule = molecule_CO2
#  The exponentiation constraints are not satisfiable.
#  Minimal max_depth is 36476.
# molecule = molecule_CH3Cl
#  The exponentiation constraints are not satisfiable.
#  Minimal max_depth is 57965.
# molecule = molecule_C2H2
#  The exponentiation constraints are not satisfiable.
#  Minimal max_depth is 15115.
# molecule = molecule_C2H4
#  The exponentiation constraints are not satisfiable.
#  Minimal max_depth is 24471.


# Results for BeH2:

Your qubit solution is [{'phase': 0.46875}: 135341]

The final results of the Energy (matrix's eigenvlues) is therefore:

[-5.0500156710895965]

exact result: -3.940331561325765

The results including the error contributed from the resolution (number of qubits participating in the QPE) are: The resolution of result is 3.3666771140597356 the energy solution is between -8.416692785149332 and -1.683338557029861

# Results for H2O:

Your qubit solution is [{'phase': 0.453125}: 89076]

The final results of the Energy (matrix's eigenvlues) is therefore:

[-29.018825909142947]

exact result: -23.54449724044351

The results including the error contributed from the resolution (number of qubits participating in the QPE) are: The resolution of result is 11.607530363657174 the energy solution is between -40.62635627280012 and -17.41129554548577

# define your molecule problem

gs_problem = MoleculeProblem(
molecule=molecule,
basis="sto3g",
mapping="jordan_wigner",  #'bravyi_kitaev'
z2_symmetries=True,
freeze_core=True,
)

operator = gs_problem.generate_hamiltonian()
gs_problem = gs_problem.update_problem(operator.num_qubits)
print("Your Hamiltonian is", operator.show(), sep="\n")

Your Hamiltonian is
-0.702 * IIIIIII
-0.230 * ZIZZZIZ
-0.022 * IIIIIZX
+0.022 * ZIZZZZX
-0.251 * ZIIIZZI
-0.058 * ZIZZZXZ
+0.058 * IIZZIXZ
-0.857 * IIIZIII
-0.857 * IIZIIII
-0.495 * IIIIIIZ
-0.755 * IIIIIZI
-0.230 * ZZZZZII
-0.022 * ZXIIIII
+0.022 * IXZZZII
-0.251 * IIIIZII
+0.058 * YZZZYII
+0.058 * XZZZXII
-0.495 * IZIIIII
-0.755 * ZIIIIII
+0.062 * IIZZIZZ
+0.010 * IIIIIXI
-0.010 * ZIIIZXI
+0.172 * ZIZIZIZ
+0.172 * ZIIZZIZ
+0.159 * ZIZZZII
+0.093 * ZIZZZZZ
+0.012 * ZIIIZIX
-0.012 * IIZZIIX
+0.003 * ZIZZZYY
+0.020 * IIZZIXX
+0.017 * IIZZIYY
+0.017 * IIIIIYY
+0.020 * IIIIIXX
+0.003 * ZIIIZYY
+0.014 * IIIZIZX
-0.014 * ZIZIZZX
+0.014 * IIZIIZX
-0.014 * ZIIZZZX
+0.005 * IIIIIIX
-0.005 * ZIZZZIX
+0.202 * IZIIIIZ
+0.003 * ZZZZZZX
-0.003 * IZIIIZX
+0.041 * ZZIIXZZ
-0.041 * IIZZXZZ
-0.041 * ZZZZXII
+0.041 * IIIIXII
-0.011 * ZZZZXXI
+0.011 * IIIIXXI
-0.011 * IZIIYYZ
+0.011 * ZIZZYYZ
+0.012 * ZZIXIZZ
-0.012 * IIZXZZZ
-0.012 * IZZXZZI
+0.012 * ZIIXIZI
+0.012 * ZZXIIZZ
-0.012 * IIXZZZZ
-0.012 * IZXZZZI
+0.012 * ZIXIIZI
+0.003 * IXZZZIZ
-0.003 * ZXIIIIZ
+0.039 * ZXIIIZX
-0.039 * IXZZZZX
-0.011 * XIZZIZZ
+0.011 * XZIIZZZ
+0.011 * XIIIIII
-0.011 * XZZZZII
+0.014 * XIIIIXI
-0.014 * XZZZZXI
+0.014 * YIZZZYZ
-0.014 * YZIIIYZ
+0.103 * ZIZZIIZ
-0.012 * IIIIZZX
+0.012 * ZIZZIZX
+0.024 * IYIIYZZ
+0.024 * IXIIXZZ
-0.024 * IYZZYII
-0.024 * IXZZXII
-0.015 * IYZZYXI
-0.015 * IXZZXXI
+0.015 * ZYIIXYZ
-0.015 * ZXIIYYZ
-0.001 * XZIIXIZ
-0.001 * YZIIYIZ
-0.018 * YZZZYZX
-0.018 * XZZZXZX
-0.018 * XZIIXZX
-0.018 * YZIIYZX
-0.012 * IYIYIZZ
-0.012 * IXIXIZZ
+0.012 * ZYZYZZI
+0.012 * ZXZXZZI
-0.012 * IYYIIZZ
-0.012 * IXXIIZZ
+0.012 * ZYYZZZI
+0.012 * ZXXZZZI
+0.198 * ZZZZZIZ
-0.036 * YYZZIZZ
-0.036 * XXZZIZZ
+0.036 * YYIIIII
+0.036 * XXIIIII
+0.014 * YYIIIXI
+0.014 * XXIIIXI
-0.014 * XYZZZYZ
+0.014 * YXZZZYZ
+0.107 * IIZZZIZ
-0.009 * ZIIIIZX
+0.009 * IIZZZZX
+0.174 * ZIIZZZI
+0.174 * ZIZIZZI
+0.081 * ZIIIZZZ
+0.182 * ZIIIZII
+0.026 * ZIZIZXZ
-0.026 * IIZIIXZ
+0.026 * ZIIZZXZ
-0.026 * IIIZIXZ
+0.017 * ZIZZZXI
-0.017 * IIZZIXI
+0.103 * IZZZIZI
-0.001 * IZIIIXZ
+0.001 * ZZIIZXZ
+0.024 * IZIIYZY
-0.024 * ZIZZYZY
+0.024 * ZZIIXIX
-0.024 * IIZZXIX
-0.012 * IXIIZZI
+0.012 * ZXZZIZI
+0.018 * IXZZZXZ
-0.018 * ZXIIIXZ
-0.018 * ZXZZIXZ
+0.018 * IXIIZXZ
-0.015 * YIZZZZY
+0.015 * YZIIIZY
-0.015 * XIZZIIX
+0.015 * XZIIZIX
+0.231 * ZIIIIZI
-0.003 * ZIZZIXZ
+0.003 * IIZZZXZ
+0.004 * ZIIXXZI
+0.004 * ZIIYYZI
+0.004 * IIIYYII
+0.004 * IIIXXII
+0.004 * ZIXIXZI
+0.004 * ZIYIYZI
+0.004 * IIYIYII
+0.004 * IIXIXII
-0.021 * ZYIIXZY
+0.021 * ZXIIYZY
+0.021 * IYIIYIX
+0.021 * IXIIXIX
-0.003 * XZZZXZI
-0.003 * YZZZYZI
+0.041 * XZIIXXZ
+0.041 * YZIIYXZ
+0.003 * XZZXZZI
+0.003 * YZZYZZI
+0.003 * YZZYIII
+0.003 * XZZXIII
+0.003 * XZXZZZI
+0.003 * YZYZZZI
+0.003 * YZYZIII
+0.003 * XZXZIII
+0.102 * ZZIIZZI
-0.007 * ZZZZZXZ
+0.007 * IZZZIXZ
+0.024 * XYZZZZY
-0.024 * YXZZZZY
-0.024 * YYZZIIX
-0.024 * XXZZIIX
+0.223 * IIIIZZI
+0.389 * IIZZIII
+0.171 * IIIZIIZ
+0.191 * IIIZIZI
+0.172 * ZZZIZII
-0.012 * ZZIYIIY
+0.012 * IIZYZIY
-0.012 * ZZIXIIX
+0.012 * IIZXZIX
+0.014 * ZXIZIII
-0.014 * IXZIZII
+0.174 * IIIZZII
-0.003 * IIZXYYZ
+0.003 * IIZYXYZ
+0.003 * IIZYYXZ
+0.003 * IIZXXXZ
-0.026 * YZZIYII
-0.026 * XZZIXII
+0.012 * IIXXIII
+0.012 * IIYYIII
-0.013 * IYIXIIY
+0.013 * IXIYIIY
+0.013 * IYIYIIX
+0.013 * IXIXIIX
-0.004 * YZIXIYZ
+0.004 * XZIYIYZ
+0.004 * YZIYIXZ
+0.004 * XZIXIXZ
+0.171 * IZIZIII
+0.191 * ZIIZIII
+0.171 * IIZIIIZ
+0.191 * IIZIIZI
+0.172 * ZZIZZII
-0.012 * ZZYIIIY
+0.012 * IIYZZIY
-0.012 * ZZXIIIX
+0.012 * IIXZZIX
+0.014 * ZXZIIII
-0.014 * IXIZZII
+0.174 * IIZIZII
-0.003 * IIXZYYZ
+0.003 * IIYZXYZ
+0.003 * IIYZYXZ
+0.003 * IIXZXXZ
-0.026 * YZIZYII
-0.026 * XZIZXII
-0.013 * IYXIIIY
+0.013 * IXYIIIY
+0.013 * IYYIIIX
+0.013 * IXXIIIX
-0.004 * YZXIIYZ
+0.004 * XZYIIYZ
+0.004 * YZYIIXZ
+0.004 * XZXIIXZ
+0.171 * IZZIIII
+0.191 * ZIZIIII
+0.074 * IIIIIZZ
-0.036 * ZZZZXYY
+0.036 * IIIIXYY
-0.036 * ZZZZXXX
+0.036 * IIIIXXX
+0.014 * XIIIIYY
-0.014 * XZZZZYY
+0.014 * XIIIIXX
-0.014 * XZZZZXX
+0.102 * IIIIZIZ
-0.024 * IYZZYYY
-0.024 * IXZZXYY
-0.024 * IYZZYXX
-0.024 * IXZZXXX
+0.007 * YZZZYIZ
+0.007 * XZZZXIZ
+0.035 * YYIIIYY
+0.035 * XXIIIYY
+0.035 * YYIIIXX
+0.035 * XXIIIXX
+0.109 * ZIIIIIZ
+0.107 * ZZZZZZI
-0.009 * ZXIIIZI
+0.009 * IXZZZZI
+0.109 * IZIIIZI
+0.062 * ZZZZIII
+0.010 * XIIIXII
+0.010 * YIIIYII
+0.093 * IZZZZII
+0.012 * ZXIIZII
-0.012 * IXZZIII
+0.003 * XYZZYII
+0.020 * XXZZXII
+0.017 * YYZZXII
+0.017 * YYIIXII
+0.020 * XXIIXII
+0.003 * XYIIYII
+0.005 * IXIIIII
-0.005 * ZXZZZII
+0.081 * IZIIZII
-0.017 * YIZZYII
-0.017 * XIZZXII
+0.074 * ZZIIIII

pauli_ops = operator.pauli_list
N = operator.num_qubits
print("Your Pauli list is", pauli_ops, sep="\n")

Your Pauli list is
[('IIIIIII', (-0.7022930768258921+0j)), ('ZIZZZIZ', (-0.23019060454699553+0j)), ('IIIIIZX', (-0.022105135983230773+0j)), ('ZIZZZZX', (0.022105135983230773+0j)), ('ZIIIZZI', (-0.2508359055835329+0j)), ('ZIZZZXZ', (-0.05762054716987136+0j)), ('IIZZIXZ', (0.05762054716987136+0j)), ('IIIZIII', (-0.8572771753840491+0j)), ('IIZIIII', (-0.8572771753840495+0j)), ('IIIIIIZ', (-0.4953913605926791+0j)), ('IIIIIZI', (-0.755422115779769+0j)), ('ZZZZZII', (-0.2301906045469956+0j)), ('ZXIIIII', (-0.022105135983230766+0j)), ('IXZZZII', (0.022105135983230766+0j)), ('IIIIZII', (-0.25083590558353275+0j)), ('YZZZYII', (0.05762054716987136+0j)), ('XZZZXII', (0.05762054716987136+0j)), ('IZIIIII', (-0.49539136059267913+0j)), ('ZIIIIII', (-0.755422115779769+0j)), ('IIZZIZZ', (0.06177536902294072+0j)), ('IIIIIXI', (0.009628629697052186+0j)), ('ZIIIZXI', (-0.009628629697052186+0j)), ('ZIZIZIZ', (0.17213123525686325+0j)), ('ZIIZZIZ', (0.1721312352568633+0j)), ('ZIZZZII', (0.15942306177427767+0j)), ('ZIZZZZZ', (0.09279153113235836+0j)), ('ZIIIZIX', (0.011782782608242606+0j)), ('IIZZIIX', (-0.011782782608242606+0j)), ('ZIZZZYY', (0.002973281387454196+0j)), ('IIZZIXX', (0.020388829108894987+0j)), ('IIZZIYY', (0.017415547721440768+0j)), ('IIIIIYY', (0.017415547721440768+0j)), ('IIIIIXX', (0.020388829108894987+0j)), ('ZIIIZYY', (0.002973281387454196+0j)), ('IIIZIZX', (0.014419981611826721+0j)), ('ZIZIZZX', (-0.014419981611826721+0j)), ('IIZIIZX', (0.014419981611826716+0j)), ('ZIIZZZX', (-0.014419981611826716+0j)), ('IIIIIIX', (0.0048161379430657495+0j)), ('ZIZZZIX', (-0.0048161379430657495+0j)), ('IZIIIIZ', (0.2024043809018113+0j)), ('ZZZZZZX', (0.002565939742358108+0j)), ('IZIIIZX', (-0.002565939742358108+0j)), ('ZZIIXZZ', (0.04108938500992236+0j)), ('IIZZXZZ', (-0.04108938500992236+0j)), ('ZZZZXII', (-0.04108938500992236+0j)), ('IIIIXII', (0.04108938500992236+0j)), ('ZZZZXXI', (-0.011051936749184687+0j)), ('IIIIXXI', (0.011051936749184687+0j)), ('IZIIYYZ', (-0.011051936749184687+0j)), ('ZIZZYYZ', (0.011051936749184687+0j)), ('ZZIXIZZ', (0.01234821063234334+0j)), ('IIZXZZZ', (-0.01234821063234334+0j)), ('IZZXZZI', (-0.01234821063234334+0j)), ('ZIIXIZI', (0.01234821063234334+0j)), ('ZZXIIZZ', (0.01234821063234334+0j)), ('IIXZZZZ', (-0.01234821063234334+0j)), ('IZXZZZI', (-0.01234821063234334+0j)), ('ZIXIIZI', (0.01234821063234334+0j)), ('IXZZZIZ', (0.002565939742358108+0j)), ('ZXIIIIZ', (-0.002565939742358108+0j)), ('ZXIIIZX', (0.03889565589184009+0j)), ('IXZZZZX', (-0.03889565589184009+0j)), ('XIZZIZZ', (-0.011051936749184687+0j)), ('XZIIZZZ', (0.011051936749184687+0j)), ('XIIIIII', (0.011051936749184687+0j)), ('XZZZZII', (-0.011051936749184687+0j)), ('XIIIIXI', (0.01414965096283118+0j)), ('XZZZZXI', (-0.01414965096283118+0j)), ('YIZZZYZ', (0.01414965096283118+0j)), ('YZIIIYZ', (-0.01414965096283118+0j)), ('ZIZZIIZ', (0.10286475403286306+0j)), ('IIIIZZX', (-0.011879666226520792+0j)), ('ZIZZIZX', (0.011879666226520792+0j)), ('IYIIYZZ', (0.023662448834763396+0j)), ('IXIIXZZ', (0.023662448834763396+0j)), ('IYZZYII', (-0.023662448834763396+0j)), ('IXZZXII', (-0.023662448834763396+0j)), ('IYZZYXI', (-0.015307708024581815+0j)), ('IXZZXXI', (-0.015307708024581815+0j)), ('ZYIIXYZ', (0.015307708024581815+0j)), ('ZXIIYYZ', (-0.015307708024581815+0j)), ('XZIIXIZ', (-0.0014233070521324988+0j)), ('YZIIYIZ', (-0.0014233070521324988+0j)), ('YZZZYZX', (-0.018280989412036014+0j)), ('XZZZXZX', (-0.018280989412036014+0j)), ('XZIIXZX', (-0.018280989412036014+0j)), ('YZIIYZX', (-0.018280989412036014+0j)), ('IYIYIZZ', (-0.01185327361540123+0j)), ('IXIXIZZ', (-0.01185327361540123+0j)), ('ZYZYZZI', (0.01185327361540123+0j)), ('ZXZXZZI', (0.01185327361540123+0j)), ('IYYIIZZ', (-0.011853273615401231+0j)), ('IXXIIZZ', (-0.011853273615401231+0j)), ('ZYYZZZI', (0.011853273615401231+0j)), ('ZXXZZZI', (0.011853273615401231+0j)), ('ZZZZZIZ', (0.19831871766611772+0j)), ('YYZZIZZ', (-0.03569653713347678+0j)), ('XXZZIZZ', (-0.03569653713347678+0j)), ('YYIIIII', (0.03569653713347678+0j)), ('XXIIIII', (0.03569653713347678+0j)), ('YYIIIXI', (0.013897967082179451+0j)), ('XXIIIXI', (0.013897967082179451+0j)), ('XYZZZYZ', (-0.013897967082179451+0j)), ('YXZZZYZ', (0.013897967082179451+0j)), ('IIZZZIZ', (0.10694118209518956+0j)), ('ZIIIIZX', (-0.009081829139113701+0j)), ('IIZZZZX', (0.009081829139113701+0j)), ('ZIIZZZI', (0.1744698035079177+0j)), ('ZIZIZZI', (0.1744698035079177+0j)), ('ZIIIZZZ', (0.08104582589502732+0j)), ('ZIIIZII', (0.1820000129718488+0j)), ('ZIZIZXZ', (0.02628674552520143+0j)), ('IIZIIXZ', (-0.02628674552520143+0j)), ('ZIIZZXZ', (0.026286745525201414+0j)), ('IIIZIXZ', (-0.026286745525201414+0j)), ('ZIZZZXI', (0.01726508183549788+0j)), ('IIZZIXI', (-0.01726508183549788+0j)), ('IZZZIZI', (0.10286475403286306+0j)), ('IZIIIXZ', (-0.0014233070521324988+0j)), ('ZZIIZXZ', (0.0014233070521324988+0j)), ('IZIIYZY', (0.023662448834763396+0j)), ('ZIZZYZY', (-0.023662448834763396+0j)), ('ZZIIXIX', (0.023662448834763396+0j)), ('IIZZXIX', (-0.023662448834763396+0j)), ('IXIIZZI', (-0.011879666226520792+0j)), ('ZXZZIZI', (0.011879666226520792+0j)), ('IXZZZXZ', (0.018280989412036014+0j)), ('ZXIIIXZ', (-0.018280989412036014+0j)), ('ZXZZIXZ', (-0.018280989412036014+0j)), ('IXIIZXZ', (0.018280989412036014+0j)), ('YIZZZZY', (-0.015307708024581815+0j)), ('YZIIIZY', (0.015307708024581815+0j)), ('XIZZIIX', (-0.015307708024581815+0j)), ('XZIIZIX', (0.015307708024581815+0j)), ('ZIIIIZI', (0.23075467521596427+0j)), ('ZIZZIXZ', (-0.0025513531024711268+0j)), ('IIZZZXZ', (0.0025513531024711268+0j)), ('ZIIXXZI', (0.0036560013958361025+0j)), ('ZIIYYZI', (0.0036560013958361025+0j)), ('IIIYYII', (0.0036560013958361025+0j)), ('IIIXXII', (0.0036560013958361025+0j)), ('ZIXIXZI', (0.0036560013958361025+0j)), ('ZIYIYZI', (0.0036560013958361025+0j)), ('IIYIYII', (0.0036560013958361025+0j)), ('IIXIXII', (0.0036560013958361025+0j)), ('ZYIIXZY', (-0.020873062000978788+0j)), ('ZXIIYZY', (0.020873062000978788+0j)), ('IYIIYIX', (0.020873062000978788+0j)), ('IXIIXIX', (0.020873062000978788+0j)), ('XZZZXZI', (-0.0025513531024711255+0j)), ('YZZZYZI', (-0.0025513531024711255+0j)), ('XZIIXXZ', (0.04125144772662377+0j)), ('YZIIYXZ', (0.04125144772662377+0j)), ('XZZXZZI', (0.0034405062273071014+0j)), ('YZZYZZI', (0.0034405062273071014+0j)), ('YZZYIII', (0.0034405062273071014+0j)), ('XZZXIII', (0.0034405062273071014+0j)), ('XZXZZZI', (0.0034405062273071014+0j)), ('YZYZZZI', (0.0034405062273071014+0j)), ('YZYZIII', (0.0034405062273071014+0j)), ('XZXZIII', (0.0034405062273071014+0j)), ('ZZIIZZI', (0.10191888789600612+0j)), ('ZZZZZXZ', (-0.006564129606561645+0j)), ('IZZZIXZ', (0.006564129606561645+0j)), ('XYZZZZY', (0.023829211442059527+0j)), ('YXZZZZY', (-0.023829211442059527+0j)), ('YYZZIIX', (-0.023829211442059527+0j)), ('XXZZIIX', (-0.023829211442059527+0j)), ('IIIIZZI', (0.22325146069847254+0j)), ('IIZZIII', (0.3892355930337427+0j)), ('IIIZIIZ', (0.17086285707799778+0j)), ('IIIZIZI', (0.19133630882633235+0j)), ('ZZZIZII', (0.17213123525686325+0j)), ('ZZIYIIY', (-0.01185327361540123+0j)), ('IIZYZIY', (0.01185327361540123+0j)), ('ZZIXIIX', (-0.01185327361540123+0j)), ('IIZXZIX', (0.01185327361540123+0j)), ('ZXIZIII', (0.014419981611826721+0j)), ('IXZIZII', (-0.014419981611826721+0j)), ('IIIZZII', (0.1744698035079177+0j)), ('IIZXYYZ', (-0.0034405062273071014+0j)), ('IIZYXYZ', (0.0034405062273071014+0j)), ('IIZYYXZ', (0.0034405062273071014+0j)), ('IIZXXXZ', (0.0034405062273071014+0j)), ('YZZIYII', (-0.02628674552520143+0j)), ('XZZIXII', (-0.02628674552520143+0j)), ('IIXXIII', (0.012124689610585541+0j)), ('IIYYIII', (0.012124689610585541+0j)), ('IYIXIIY', (-0.012722452161354637+0j)), ('IXIYIIY', (0.012722452161354637+0j)), ('IYIYIIX', (0.012722452161354637+0j)), ('IXIXIIX', (0.012722452161354637+0j)), ('YZIXIYZ', (-0.004138154161062577+0j)), ('XZIYIYZ', (0.004138154161062577+0j)), ('YZIYIXZ', (0.004138154161062577+0j)), ('XZIXIXZ', (0.004138154161062577+0j)), ('IZIZIII', (0.17086285707799778+0j)), ('ZIIZIII', (0.19133630882633235+0j)), ('IIZIIIZ', (0.17086285707799784+0j)), ('IIZIIZI', (0.19133630882633237+0j)), ('ZZIZZII', (0.1721312352568633+0j)), ('ZZYIIIY', (-0.011853273615401231+0j)), ('IIYZZIY', (0.011853273615401231+0j)), ('ZZXIIIX', (-0.011853273615401231+0j)), ('IIXZZIX', (0.011853273615401231+0j)), ('ZXZIIII', (0.014419981611826716+0j)), ('IXIZZII', (-0.014419981611826716+0j)), ('IIZIZII', (0.1744698035079177+0j)), ('IIXZYYZ', (-0.0034405062273071023+0j)), ('IIYZXYZ', (0.0034405062273071023+0j)), ('IIYZYXZ', (0.0034405062273071023+0j)), ('IIXZXXZ', (0.0034405062273071023+0j)), ('YZIZYII', (-0.026286745525201414+0j)), ('XZIZXII', (-0.026286745525201414+0j)), ('IYXIIIY', (-0.012722452161354645+0j)), ('IXYIIIY', (0.012722452161354645+0j)), ('IYYIIIX', (0.012722452161354645+0j)), ('IXXIIIX', (0.012722452161354645+0j)), ('YZXIIYZ', (-0.004138154161062578+0j)), ('XZYIIYZ', (0.004138154161062578+0j)), ('YZYIIXZ', (0.004138154161062578+0j)), ('XZXIIXZ', (0.004138154161062578+0j)), ('IZZIIII', (0.17086285707799784+0j)), ('ZIZIIII', (0.19133630882633237+0j)), ('IIIIIZZ', (0.07373286867690836+0j)), ('ZZZZXYY', (-0.03569653713347678+0j)), ('IIIIXYY', (0.03569653713347678+0j)), ('ZZZZXXX', (-0.03569653713347678+0j)), ('IIIIXXX', (0.03569653713347678+0j)), ('XIIIIYY', (0.013897967082179451+0j)), ('XZZZZYY', (-0.013897967082179451+0j)), ('XIIIIXX', (0.013897967082179451+0j)), ('XZZZZXX', (-0.013897967082179451+0j)), ('IIIIZIZ', (0.10191888789600612+0j)), ('IYZZYYY', (-0.023829211442059527+0j)), ('IXZZXYY', (-0.023829211442059527+0j)), ('IYZZYXX', (-0.023829211442059527+0j)), ('IXZZXXX', (-0.023829211442059527+0j)), ('YZZZYIZ', (0.006564129606561645+0j)), ('XZZZXIZ', (0.006564129606561645+0j)), ('YYIIIYY', (0.03516605240243114+0j)), ('XXIIIYY', (0.03516605240243114+0j)), ('YYIIIXX', (0.03516605240243114+0j)), ('XXIIIXX', (0.03516605240243114+0j)), ('ZIIIIIZ', (0.10889892107933949+0j)), ('ZZZZZZI', (0.10694118209518956+0j)), ('ZXIIIZI', (-0.009081829139113701+0j)), ('IXZZZZI', (0.009081829139113701+0j)), ('IZIIIZI', (0.10889892107933949+0j)), ('ZZZZIII', (0.06177536902294072+0j)), ('XIIIXII', (0.009628629697052186+0j)), ('YIIIYII', (0.009628629697052186+0j)), ('IZZZZII', (0.09279153113235836+0j)), ('ZXIIZII', (0.011782782608242606+0j)), ('IXZZIII', (-0.011782782608242606+0j)), ('XYZZYII', (0.002973281387454196+0j)), ('XXZZXII', (0.020388829108894987+0j)), ('YYZZXII', (0.017415547721440768+0j)), ('YYIIXII', (0.017415547721440768+0j)), ('XXIIXII', (0.020388829108894987+0j)), ('XYIIYII', (0.002973281387454196+0j)), ('IXIIIII', (0.0048161379430657495+0j)), ('ZXZZZII', (-0.0048161379430657495+0j)), ('IZIIZII', (0.08104582589502732+0j)), ('YIZZYII', (-0.01726508183549788+0j)), ('XIZZXII', (-0.01726508183549788+0j)), ('ZZIIIII', (0.07373286867690836+0j))]


## 2. Preparing the Molecule for QPE

### 2.1 Chose the Algorithm's Precision

For QPE algorithms, the precision is set by the number of qubits chosen $$n$$, such that the resolution is $$1/{2^n}$$. In case the matrix needs to be normlized, the resolution will be distorted. In case of normalization, that the span of results in the QPE is strached between the lowest and highest possible phase, thus the reslution will be mapped to $$normalization-coefficient/{2^n} ~\sim 1/{((\lambda_{max}-\lambda_{min})*2^n)}$$.

Here you can chose the precision in the parameter n_qpe, or by setting your desired resolution. If you chose your desired resolution and set the parameter get_recommended_n to be True, the number of qubits will be calculated for you accordingly.

# n_qpe-  The precision of the QPE (the number of qubits to be used in the QPE. Creates resolution of 1/2^n_qpe
n_qpe = 6

# recommanded n_qpe:
get_recommanded_n = False

import math

desired_resolution = 0.02

def get_nqpe(pauli_operator, desired_resolution):
N = pauli_operator.num_qubits
A = 0
for a, b in pauli_operator.pauli_list:
A = A + abs(b)
nqpe = math.log2(2 * N * A / desired_resolution)
return math.ceil(nqpe)

if get_recommanded_n:
n_qpe = get_nqpe(operator, desired_resolution)
print("number of qubits for QPE is", n_qpe)


### 2.3 Normalize the Matrix

As QPE obtains a phase in the form $$e^{2\pi i\theta}$$, there is meaning only for $$\theta \in [0,2\pi)$$. Generally, our matrix M can have any eigenvalue, thus \theta can have any value. In order to fix this discrepancy, the values of the matrix are stretched to be rescaled. We assume $$\theta \in [\lambda_{min}, \lambda_{max}]$$ and use a normalization function in order to map those values into $$[0, 1-1/{2^n}]$$, where n is the number of qubits chosen for the QPE process in section 2.2.

We perform the normalization procedure as following:

a. We evaluate $$\lambda_{min},\lambda_{max}$$ (in the function normalization_params() below). In order to do so we use rough estimation of the absolute max value that can take place by adding together all the pauli coefficients and multiplying by the matrix's dimensions. That will yield us a value $$\lambda$$ (which is referred in the code as normalization_coeff) and we now assume that the domain is $$\theta \in [-\lambda, \lambda]$$. In general, one can build a more accurate assessment, which will decrease the span of solutions and thus achieve a better resolution.

b. We make sure only positive values are available by adding $$\lambda*I^n$$ to the pauli list. Now our evaluated span is $$[0, 2*\lambda]$$.

c. We normlize our matrix by multiplying all of the pauli coefficients by $$(1-1/2^n)/(2*\lambda)$$. Now the span of $$\theta$$ is $$[0, 1-1/2^n]$$, as required for proper QPE process.

The values of the matrix's eigenvalues should be now between $$0$$ to $$1-(1/2^n)$$. The QPE procedure will be performed on this new normalized matrix. After the phases are obtained, the original phases of the pre-normalized matrix will be gathered by performing opposite steps to this normalization procedure.

• Note that in case your matrix's eigenvalues are naturally between the values $$0$$ to $$1-(1/2^n)$$, you may not want to normalize it, as the normalization procedure may enlarge the span, thus lowering the resolution of the agorithm. In that case, you may skip those lines or change the value normalize to False.
from typing import cast

from classiq import Pauli, PauliTerm

# normalizing the operator
## we need to create a matrix such that its normalized version will have eigenvalues of [0,1/2^k] when k is the resolution of the QPE
normalize = True

def normalization_params(pauli_list, N):
A = 0
for a, b in pauli_list:
A = A + abs(b)
return N * A

def normalize_hamiltonian(pauli_list, normalization_coeff, k):
new_pauli_list = []
for a, b in pauli_list:
if a == "I" * N:
new_pauli_list.append(
(
a,
(b + normalization_coeff)
* (1 - 1 / (2**k))
/ (2 * normalization_coeff),
)
)
else:
new_pauli_list.append((a, b * (1 - 1 / (2**k)) / (2 * normalization_coeff)))
return new_pauli_list

pauli_list = pauli_ops
if normalize:
normalization_coeff = normalization_params(pauli_ops, N)
new_pauli_list = normalize_hamiltonian(pauli_ops, normalization_coeff, n_qpe)
pauli_ops = new_pauli_list
print(pauli_ops)

[('IIIIIII', (0.48892810536438785+0j)), ('ZIZZZIZ', (-0.0010683317925043478+0j)), ('IIIIIZX', (-0.00010259158750198228+0j)), ('ZIZZZZX', (0.00010259158750198228+0j)), ('ZIIIZZI', (-0.0011641481769591397+0j)), ('ZIZZZXZ', (-0.0002674212640616372+0j)), ('IIZZIXZ', (0.0002674212640616372+0j)), ('IIIZIII', (-0.003978687415385474+0j)), ('IIZIIII', (-0.003978687415385476+0j)), ('IIIIIIZ', (-0.002299148313610232+0j)), ('IIIIIZI', (-0.0035059704744972017+0j)), ('ZZZZZII', (-0.0010683317925043482+0j)), ('ZXIIIII', (-0.00010259158750198224+0j)), ('IXZZZII', (0.00010259158750198224+0j)), ('IIIIZII', (-0.0011641481769591388+0j)), ('YZZZYII', (0.0002674212640616372+0j)), ('XZZZXII', (0.0002674212640616372+0j)), ('IZIIIII', (-0.002299148313610232+0j)), ('ZIIIIII', (-0.0035059704744972017+0j)), ('IIZZIZZ', (0.00028670410267514636+0j)), ('IIIIIXI', (4.468718974806055e-05+0j)), ('ZIIIZXI', (-4.468718974806055e-05+0j)), ('ZIZIZIZ', (0.0007988739221995865+0j)), ('ZIIZZIZ', (0.0007988739221995868+0j)), ('ZIZZZII', (0.0007398943396800268+0j)), ('ZIZZZZZ', (0.0004306524281429421+0j)), ('ZIIIZIX', (5.468477433874995e-05+0j)), ('IIZZIIX', (-5.468477433874995e-05+0j)), ('ZIZZZYY', (1.3799221043490736e-05+0j)), ('IIZZIXX', (9.462607907840828e-05+0j)), ('IIZZIYY', (8.082685803491742e-05+0j)), ('IIIIIYY', (8.082685803491742e-05+0j)), ('IIIIIXX', (9.462607907840828e-05+0j)), ('ZIIIZYY', (1.3799221043490736e-05+0j)), ('IIIZIZX', (6.69242119310627e-05+0j)), ('ZIZIZZX', (-6.69242119310627e-05+0j)), ('IIZIIZX', (6.692421193106269e-05+0j)), ('ZIIZZZX', (-6.692421193106269e-05+0j)), ('IIIIIIX', (2.2352055991986362e-05+0j)), ('ZIZZZIX', (-2.2352055991986362e-05+0j)), ('IZIIIIZ', (0.0009393738527474021+0j)), ('ZZZZZZX', (1.1908718037412015e-05+0j)), ('IZIIIZX', (-1.1908718037412015e-05+0j)), ('ZZIIXZZ', (0.00019069890548721174+0j)), ('IIZZXZZ', (-0.00019069890548721174+0j)), ('ZZZZXII', (-0.00019069890548721174+0j)), ('IIIIXII', (0.00019069890548721174+0j)), ('ZZZZXXI', (-5.129286410771216e-05+0j)), ('IIIIXXI', (5.129286410771216e-05+0j)), ('IZIIYYZ', (-5.129286410771216e-05+0j)), ('ZIZZYYZ', (5.129286410771216e-05+0j)), ('ZZIXIZZ', (5.730896803991555e-05+0j)), ('IIZXZZZ', (-5.730896803991555e-05+0j)), ('IZZXZZI', (-5.730896803991555e-05+0j)), ('ZIIXIZI', (5.730896803991555e-05+0j)), ('ZZXIIZZ', (5.730896803991555e-05+0j)), ('IIXZZZZ', (-5.730896803991555e-05+0j)), ('IZXZZZI', (-5.730896803991555e-05+0j)), ('ZIXIIZI', (5.730896803991555e-05+0j)), ('IXZZZIZ', (1.1908718037412015e-05+0j)), ('ZXIIIIZ', (-1.1908718037412015e-05+0j)), ('ZXIIIZX', (0.00018051764476372577+0j)), ('IXZZZZX', (-0.00018051764476372577+0j)), ('XIZZIZZ', (-5.129286410771216e-05+0j)), ('XZIIZZZ', (5.129286410771216e-05+0j)), ('XIIIIII', (5.129286410771216e-05+0j)), ('XZZZZII', (-5.129286410771216e-05+0j)), ('XIIIIXI', (6.566958719353869e-05+0j)), ('XZZZZXI', (-6.566958719353869e-05+0j)), ('YIZZZYZ', (6.566958719353869e-05+0j)), ('YZIIIYZ', (-6.566958719353869e-05+0j)), ('ZIZZIIZ', (0.000477403008162358+0j)), ('IIIIZZX', (-5.513441844904338e-05+0j)), ('ZIZZIZX', (5.513441844904338e-05+0j)), ('IYIIYZZ', (0.00010981919278779331+0j)), ('IXIIXZZ', (0.00010981919278779331+0j)), ('IYZZYII', (-0.00010981919278779331+0j)), ('IXZZXII', (-0.00010981919278779331+0j)), ('IYZZYXI', (-7.104421653185207e-05+0j)), ('IXZZXXI', (-7.104421653185207e-05+0j)), ('ZYIIXYZ', (7.104421653185207e-05+0j)), ('ZXIIYYZ', (-7.104421653185207e-05+0j)), ('XZIIXIZ', (-6.605674359651609e-06+0j)), ('YZIIYIZ', (-6.605674359651609e-06+0j)), ('YZZZYZX', (-8.484343757534281e-05+0j)), ('XZZZXZX', (-8.484343757534281e-05+0j)), ('XZIIXZX', (-8.484343757534281e-05+0j)), ('YZIIYZX', (-8.484343757534281e-05+0j)), ('IYIYIZZ', (-5.5011928369130166e-05+0j)), ('IXIXIZZ', (-5.5011928369130166e-05+0j)), ('ZYZYZZI', (5.5011928369130166e-05+0j)), ('ZXZXZZI', (5.5011928369130166e-05+0j)), ('IYYIIZZ', (-5.501192836913017e-05+0j)), ('IXXIIZZ', (-5.501192836913017e-05+0j)), ('ZYYZZZI', (5.501192836913017e-05+0j)), ('ZXXZZZI', (5.501192836913017e-05+0j)), ('ZZZZZIZ', (0.0009204119844437524+0j)), ('YYZZIZZ', (-0.0001656702956102602+0j)), ('XXZZIZZ', (-0.0001656702956102602+0j)), ('YYIIIII', (0.0001656702956102602+0j)), ('XXIIIII', (0.0001656702956102602+0j)), ('YYIIIXI', (6.450150350093854e-05+0j)), ('XXIIIXI', (6.450150350093854e-05+0j)), ('XYZZZYZ', (-6.450150350093854e-05+0j)), ('YXZZZYZ', (6.450150350093854e-05+0j)), ('IIZZZIZ', (0.0004963220153364809+0j)), ('ZIIIIZX', (-4.214944750895218e-05+0j)), ('IIZZZZX', (4.214944750895218e-05+0j)), ('ZIIZZZI', (0.0008097273921596634+0j)), ('ZIZIZZI', (0.0008097273921596634+0j)), ('ZIIIZZZ', (0.0003761397326525242+0j)), ('ZIIIZII', (0.0008446756568395641+0j)), ('ZIZIZXZ', (0.00012199874978090469+0j)), ('IIZIIXZ', (-0.00012199874978090469+0j)), ('ZIIZZXZ', (0.0001219987497809046+0j)), ('IIIZIXZ', (-0.0001219987497809046+0j)), ('ZIZZZXI', (8.012853461743276e-05+0j)), ('IIZZIXI', (-8.012853461743276e-05+0j)), ('IZZZIZI', (0.000477403008162358+0j)), ('IZIIIXZ', (-6.605674359651609e-06+0j)), ('ZZIIZXZ', (6.605674359651609e-06+0j)), ('IZIIYZY', (0.00010981919278779331+0j)), ('ZIZZYZY', (-0.00010981919278779331+0j)), ('ZZIIXIX', (0.00010981919278779331+0j)), ('IIZZXIX', (-0.00010981919278779331+0j)), ('IXIIZZI', (-5.513441844904338e-05+0j)), ('ZXZZIZI', (5.513441844904338e-05+0j)), ('IXZZZXZ', (8.484343757534281e-05+0j)), ('ZXIIIXZ', (-8.484343757534281e-05+0j)), ('ZXZZIXZ', (-8.484343757534281e-05+0j)), ('IXIIZXZ', (8.484343757534281e-05+0j)), ('YIZZZZY', (-7.104421653185207e-05+0j)), ('YZIIIZY', (7.104421653185207e-05+0j)), ('XIZZIIX', (-7.104421653185207e-05+0j)), ('XZIIZIX', (7.104421653185207e-05+0j)), ('ZIIIIZI', (0.0010709496866189421+0j)), ('ZIZZIXZ', (-1.1841020351975455e-05+0j)), ('IIZZZXZ', (1.1841020351975455e-05+0j)), ('ZIIXXZI', (1.6967775606213202e-05+0j)), ('ZIIYYZI', (1.6967775606213202e-05+0j)), ('IIIYYII', (1.6967775606213202e-05+0j)), ('IIIXXII', (1.6967775606213202e-05+0j)), ('ZIXIXZI', (1.6967775606213202e-05+0j)), ('ZIYIYZI', (1.6967775606213202e-05+0j)), ('IIYIYII', (1.6967775606213202e-05+0j)), ('IIXIXII', (1.6967775606213202e-05+0j)), ('ZYIIXZY', (-9.68734401060554e-05+0j)), ('ZXIIYZY', (9.68734401060554e-05+0j)), ('IYIIYIX', (9.68734401060554e-05+0j)), ('IXIIXIX', (9.68734401060554e-05+0j)), ('XZZZXZI', (-1.184102035197545e-05+0j)), ('YZZZYZI', (-1.184102035197545e-05+0j)), ('XZIIXXZ', (0.00019145105066260875+0j)), ('YZIIYXZ', (0.00019145105066260875+0j)), ('XZZXZZI', (1.5967646430117258e-05+0j)), ('YZZYZZI', (1.5967646430117258e-05+0j)), ('YZZYIII', (1.5967646430117258e-05+0j)), ('XZZXIII', (1.5967646430117258e-05+0j)), ('XZXZZZI', (1.5967646430117258e-05+0j)), ('YZYZZZI', (1.5967646430117258e-05+0j)), ('YZYZIII', (1.5967646430117258e-05+0j)), ('XZXZIII', (1.5967646430117258e-05+0j)), ('ZZIIZZI', (0.0004730131727585797+0j)), ('ZZZZZXZ', (-3.0464615889121402e-05+0j)), ('IZZZIXZ', (3.0464615889121402e-05+0j)), ('XYZZZZY', (0.00011059315050655418+0j)), ('YXZZZZY', (-0.00011059315050655418+0j)), ('YYZZIIX', (-0.00011059315050655418+0j)), ('XXZZIIX', (-0.00011059315050655418+0j)), ('IIIIZZI', (0.0010361267075021726+0j)), ('IIZZIII', (0.001806471465812303+0j)), ('IIIZIIZ', (0.0007929872843150064+0j)), ('IIIZIZI', (0.0008880061033849397+0j)), ('ZZZIZII', (0.0007988739221995865+0j)), ('ZZIYIIY', (-5.5011928369130166e-05+0j)), ('IIZYZIY', (5.5011928369130166e-05+0j)), ('ZZIXIIX', (-5.5011928369130166e-05+0j)), ('IIZXZIX', (5.5011928369130166e-05+0j)), ('ZXIZIII', (6.69242119310627e-05+0j)), ('IXZIZII', (-6.69242119310627e-05+0j)), ('IIIZZII', (0.0008097273921596634+0j)), ('IIZXYYZ', (-1.5967646430117258e-05+0j)), ('IIZYXYZ', (1.5967646430117258e-05+0j)), ('IIZYYXZ', (1.5967646430117258e-05+0j)), ('IIZXXXZ', (1.5967646430117258e-05+0j)), ('YZZIYII', (-0.00012199874978090469+0j)), ('XZZIXII', (-0.00012199874978090469+0j)), ('IIXXIII', (5.6271590279399006e-05+0j)), ('IIYYIII', (5.6271590279399006e-05+0j)), ('IYIXIIY', (-5.904585093444126e-05+0j)), ('IXIYIIY', (5.904585093444126e-05+0j)), ('IYIYIIX', (5.904585093444126e-05+0j)), ('IXIXIIX', (5.904585093444126e-05+0j)), ('YZIXIYZ', (-1.9205482609715886e-05+0j)), ('XZIYIYZ', (1.9205482609715886e-05+0j)), ('YZIYIXZ', (1.9205482609715886e-05+0j)), ('XZIXIXZ', (1.9205482609715886e-05+0j)), ('IZIZIII', (0.0007929872843150064+0j)), ('ZIIZIII', (0.0008880061033849397+0j)), ('IIZIIIZ', (0.0007929872843150067+0j)), ('IIZIIZI', (0.0008880061033849398+0j)), ('ZZIZZII', (0.0007988739221995868+0j)), ('ZZYIIIY', (-5.501192836913017e-05+0j)), ('IIYZZIY', (5.501192836913017e-05+0j)), ('ZZXIIIX', (-5.501192836913017e-05+0j)), ('IIXZZIX', (5.501192836913017e-05+0j)), ('ZXZIIII', (6.692421193106269e-05+0j)), ('IXIZZII', (-6.692421193106269e-05+0j)), ('IIZIZII', (0.0008097273921596634+0j)), ('IIXZYYZ', (-1.596764643011726e-05+0j)), ('IIYZXYZ', (1.596764643011726e-05+0j)), ('IIYZYXZ', (1.596764643011726e-05+0j)), ('IIXZXXZ', (1.596764643011726e-05+0j)), ('YZIZYII', (-0.0001219987497809046+0j)), ('XZIZXII', (-0.0001219987497809046+0j)), ('IYXIIIY', (-5.90458509344413e-05+0j)), ('IXYIIIY', (5.90458509344413e-05+0j)), ('IYYIIIX', (5.90458509344413e-05+0j)), ('IXXIIIX', (5.90458509344413e-05+0j)), ('YZXIIYZ', (-1.920548260971589e-05+0j)), ('XZYIIYZ', (1.920548260971589e-05+0j)), ('YZYIIXZ', (1.920548260971589e-05+0j)), ('XZXIIXZ', (1.920548260971589e-05+0j)), ('IZZIIII', (0.0007929872843150067+0j)), ('ZIZIIII', (0.0008880061033849398+0j)), ('IIIIIZZ', (0.00034219975187565625+0j)), ('ZZZZXYY', (-0.0001656702956102602+0j)), ('IIIIXYY', (0.0001656702956102602+0j)), ('ZZZZXXX', (-0.0001656702956102602+0j)), ('IIIIXXX', (0.0001656702956102602+0j)), ('XIIIIYY', (6.450150350093854e-05+0j)), ('XZZZZYY', (-6.450150350093854e-05+0j)), ('XIIIIXX', (6.450150350093854e-05+0j)), ('XZZZZXX', (-6.450150350093854e-05+0j)), ('IIIIZIZ', (0.0004730131727585797+0j)), ('IYZZYYY', (-0.00011059315050655418+0j)), ('IXZZXYY', (-0.00011059315050655418+0j)), ('IYZZYXX', (-0.00011059315050655418+0j)), ('IXZZXXX', (-0.00011059315050655418+0j)), ('YZZZYIZ', (3.0464615889121402e-05+0j)), ('XZZZXIZ', (3.0464615889121402e-05+0j)), ('YYIIIYY', (0.00016320827634266465+0j)), ('XXIIIYY', (0.00016320827634266465+0j)), ('YYIIIXX', (0.00016320827634266465+0j)), ('XXIIIXX', (0.00016320827634266465+0j)), ('ZIIIIIZ', (0.0005054080282183209+0j)), ('ZZZZZZI', (0.0004963220153364809+0j)), ('ZXIIIZI', (-4.214944750895218e-05+0j)), ('IXZZZZI', (4.214944750895218e-05+0j)), ('IZIIIZI', (0.0005054080282183209+0j)), ('ZZZZIII', (0.00028670410267514636+0j)), ('XIIIXII', (4.468718974806055e-05+0j)), ('YIIIYII', (4.468718974806055e-05+0j)), ('IZZZZII', (0.0004306524281429421+0j)), ('ZXIIZII', (5.468477433874995e-05+0j)), ('IXZZIII', (-5.468477433874995e-05+0j)), ('XYZZYII', (1.3799221043490736e-05+0j)), ('XXZZXII', (9.462607907840828e-05+0j)), ('YYZZXII', (8.082685803491742e-05+0j)), ('YYIIXII', (8.082685803491742e-05+0j)), ('XXIIXII', (9.462607907840828e-05+0j)), ('XYIIYII', (1.3799221043490736e-05+0j)), ('IXIIIII', (2.2352055991986362e-05+0j)), ('ZXZZZII', (-2.2352055991986362e-05+0j)), ('IZIIZII', (0.0003761397326525242+0j)), ('YIZZYII', (-8.012853461743276e-05+0j)), ('XIZZXII', (-8.012853461743276e-05+0j)), ('ZZIIIII', (0.00034219975187565625+0j))]


Convert the objects to qmod:

CHAR_TO_STUCT_DICT = {"I": Pauli.I, "X": Pauli.X, "Y": Pauli.Y, "Z": Pauli.Z}

def pauli_str_to_enums(pauli):
return [CHAR_TO_STUCT_DICT[s] for s in pauli]

def pauli_list_to_hamiltonian(pauli_list):
return [
PauliTerm(
pauli=pauli_str_to_enums(pauli), coefficient=cast(complex, coeff).real
)
for pauli, coeff in pauli_list
]


## 3. Creating the Quantum Circuit

We will now create a quantum circuit of the QPE algorithm using the Classiq platform. The user is able to fill in their constraints and preferences as desired.

constraints = Constraints()
preferences = Preferences(timeout_seconds=600)


### 3.1. Create the phase estimation model

from classiq import molecule_problem_to_qmod
from classiq.qmod import (
CInt,
Output,
QArray,
QBit,
QCallable,
QNum,
allocate,
allocate_num,
control,
invert,
qfunc,
repeat,
)
from classiq.qmod.builtins import (
H,
apply_to_all,
exponentiation_with_depth_constraint,
molecule_hartree_fock,
qft,
)
from classiq.qmod.symbolic import log, pi

# this constant will be multipled be a linear factor for each qbit of the qpe, so the
# depth will scale linear with the power of each unitary, on the expense of approximating the
# exponentiation function
exp_max_depth = 2200

# define a Quantum Phase Estimation function that allows specifying each controlled
# power unitary directly
@qfunc
def my_flexible_qpe(
unitary_with_power: QCallable[CInt],
phase: QArray[QBit],
) -> None:
apply_to_all(H, phase)

repeat(
count=phase.len,
iteration=lambda index: control(
ctrl=phase[index],
operand=lambda: unitary_with_power(2**index),
),
)
invert(lambda: qft(phase))

@qfunc
def main(reg: Output[QArray[QBit]], phase: Output[QNum]):
allocate_num(num_qubits=n_qpe, is_signed=False, fraction_digits=n_qpe, out=phase)
allocate(N, reg)

# The state chosen for the initial state is HF, as usually it has good overlap with the with the eigenstate of minimal energy.
molecule_hartree_fock(molecule_problem_to_qmod(gs_problem), reg)

# # Hereby we send the pauli list created for the molecule for creating phase estimation circuit.
my_flexible_qpe(
lambda power: exponentiation_with_depth_constraint(
pauli_list_to_hamiltonian(pauli_ops),
evolution_coefficient=-2 * np.pi * power,
max_depth=exp_max_depth * ((2**0.5) ** log(power, 2)),
qbv=reg,
),
phase,
)


## 3.2. Synthesizing the Circuit

Hereby we syntesize the circuit and show it using the analyzer.

from classiq import create_model
from classiq.execution import ExecutionPreferences

num_shots = 200000

qmod = create_model(
main,
constraints=constraints,
preferences=preferences,
execution_preferences=ExecutionPreferences(num_shots=num_shots),
)

# from classiq import write_qmod

# write_qmod(qmod, name="qpe_for_molecules") # optional

from classiq import show, synthesize

qprog = synthesize(qmod)
show(qprog)

Opening: https://platform.classiq.io/circuit/99f0cfde-38f4-4b77-958b-6991194539a5?version=0.42.1


# 4. Measurment and Analysis

### 4.1. Circuit Execution

The circuit is now sent to execution on a default simulator

from classiq import execute

results = execute(qprog).result()

from classiq.execution import ExecutionDetails

results = results[0].value


### 4.2. Presenting the Result's Count

results.parsed_counts_of_outputs("phase")[:10]

[{'phase': 0.46875}: 135118,
{'phase': 0.484375}: 36276,
{'phase': 0.453125}: 8424,
{'phase': 0.5}: 5425,
{'phase': 0.4375}: 2817,
{'phase': 0.515625}: 2133,
{'phase': 0.421875}: 1332,
{'phase': 0.53125}: 1130,
{'phase': 0.40625}: 859,
{'phase': 0.546875}: 694]


### 4.3. Chosing the Most Probable Solution

Hereby the user will choose the number of eigenvalues they wish to extract from the poll of results. The value number_of_solutions will determine how many results out of qpe_results will be analyzed. The parsed_counts property is already sorted.

We get the result in the phase variable already encoded as fixed point variable in the range [0,1].

number_of_solutions = 1  # to be set

solutions = results.parsed_counts_of_outputs("phase")[:number_of_solutions]
print("Your qubit solution is", solutions, sep="\n")

Your qubit solution is
[{'phase': 0.46875}: 135118]


### 4.4. Translating into Energy value (Phase)

Map the phase back into the original values, i.e. renormalized into it's original span.

def post_process_phase(phase):
return (
(phase * 2 * normalization_coeff) / (1 - (1 / 2**n_qpe))
) - normalization_coeff

# renormalize into the "real" solution -
if normalize:
solution = [post_process_phase(value.state["phase"]) for value in solutions]
else:
solution = phase


The final results of the Energy (matrix's eigenvlues) is therefore:

print(solution)

[-5.050015671089611]


And the results including the error contributed from the resolution (number of qubits participating in the QPE) are:

if normalize:
energy_resolution = (
(1 / (2**n_qpe)) * 2 * normalization_coeff / (1 - (1 / 2**n_qpe))
)
else:
energy_resolution = 1 / (2**n_qpe)

print("the resolution of result is", energy_resolution)

for sol in solution:
print(
"the eneregy solution is beteen",
sol - energy_resolution,
"and",
sol + energy_resolution,
)

###if zero or exceed the normalization range need to add conditions

the resolution of result is 3.3666771140597387
the eneregy solution is beteen -8.41669278514935 and -1.683338557029872


### 4.4. Find Exact Solution for Comparison

mat = operator.to_matrix()
w, v = np.linalg.eig(mat)
print("exact result:", np.real(min(w)))

exact result: -3.940331561325752


### 4.4. Find the Solution's Histogram

import matplotlib.pyplot as plt
import numpy as np

energy_vec = []
energy_prob = []

for value, counts in results.parsed_counts_of_outputs("phase"):
temp = value[1]["phase"]
if normalize:
temp2 = (
temp * 2 * normalization_coeff / (1 - (1 / 2**n_qpe))
) - normalization_coeff
else:
temp2 = temp
energy_vec.append(temp2)
energy_prob.append(counts[1] / num_shots)

plt.plot(energy_vec, energy_prob, ".")
plt.show()


# Results for BeH2:

Your qubit solution is [{'phase': 0.46875}: 135341]

The final results of the Energy (matrix's eigenvlues) is therefore:

[-5.0500156710895965]

exact result: -3.940331561325765

The results including the error contributed from the resolution (number of qubits participating in the QPE) are: The resolution of result is 3.3666771140597356 the energy solution is between -8.416692785149332 and -1.683338557029861

# Results for H2O:

Your qubit solution is [{'phase': 0.453125}: 89076]

The final results of the Energy (matrix's eigenvlues) is therefore:

[-29.018825909142947]

exact result: -23.54449724044351

The results including the error contributed from the resolution (number of qubits participating in the QPE) are: The resolution of result is 11.607530363657174 the energy solution is between -40.62635627280012 and -17.41129554548577