Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
This tutorial presents the functionality of Classiq’s Chemistry application module. The application is based on the OpenFermion package [1], which is a comprehensive library for defining and analyzing Fermionic systems, in particular quantum chemistry problems. It provides efficient tools for transforming Fermionic operators to Pauli operators, which are then can be used with Qmod to define quantum algorithms (for more details see their OpenFermion intro tutorial [2]). The different classes and functions of Classiq’s chemistry application is demonstrated below by implementing a Variational Quantum Eigensolver (VQE). A collection of concrete and concise VQE and QPE examples for chemistry can be found in the chemistry application directory.

The FermionHamiltonianProblem Class: Defining an Electronic Structure Problem.

There are two ways of defining an electronic structure problem, either providing MolecularData of a molecule, or by directly defining a Fermionic Hamiltonian together with the number of spin up/down (α/β\alpha/\beta) particles. Below we demonstrate the former, which is a more practical usecase. For the direct definition see this example.

Defining a Molecule

We start with defining a molecule, specifying its geometry (elements and their 3D position), multiplicity (2(total spin)+12\cdot(\text{total spin})+1), basis, and an optional string for its description. In this tutorial we focus on the LiH molecule. There are several ways to get geometry of molecules, typical way involves using the SMILES (Simplified Molecular Input Line Entry System) of a molecule and use a chemical package such as RDkit to extract the geometry as an xyz file (a code example is given in the Appendix A of this notebook). For simplicity, we store the geometry in advance in the lih.xyz file and load it. Comment: For complex molecules it is possible to call directly from openfermion.chem import geometry_from_pubchem
Output:
Next, we run a pyscf plugin for calculating various objects for our molecule problem, such as the second quantized Hamiltonian that is at the core of the VQE algorithm. For small problems, we can also get the Full Configuration Interaction (FCI), which calculates classically the ground state energy, i.e., for validating our quantum approach. Comment: For complex problems running pyscf can take time, it is possible to run it only once, and load the data later on, using the save and load methods.
Now we can get several properties of our molecular problem. The electronic structure problem is described as a second quantized Hamiltonian H=h0+p,q=02N1hpqapaq+12p,q,r,s=02N1hpqrsapaqaras,(1)\Large H = h_0 + \sum_{p,q=0}^{2N-1} h_{pq}\, a^\dagger_p a_q + \frac{1}{2} \sum_{p,q,r,s=0}^{2N-1} h_{pqrs} \, a^\dagger_p a^\dagger_q a_r a_s, \tag{1} where h0h_0 is a constant nuclear repulsion energy, and hpqh_{pq} and hpqrsh_{pqrs} are the well-known one-body and two-body molecular integrals, respectively. The sum is over all spin orbitals, which is twice the number of spatial orbitals NN, as for each spatial orbital we have a spin up and spin down space (also known, and refer hereafter, as α\alpha and β\beta particles). This, together with the number of free electrons that can occupy those orbitals, define the electronic structure problem.
Output:

Defining a Reduced Problem (Active Space/Freeze Core)

In some cases, we can “freeze” some of the orbitals, occupying them with both spin up and spin down electrons. This will be orbitals with very low energy, such as the core orbitals, which are expected to be “classically” (with both spin up and down) occupied. In addition, we can exclude orbitals with very high energies, as they are unlikely to contribute significantly to the ground state. In other words, we can choose the active space for our molecular problem --- the spatial orbitals that are relevant to the quantum problem. This of-course reduces the problem we need to tackle. Below we define a FermionHamiltonianProblem for the LiH molecule, freezing its core (0th0^{\rm th}) orbital. The updated number of spatial orbitals and electrons are a property of the class.
Output:
Let us look at several terms of our Fermionic operator:
Output:
We can see one-body terms ((i,1),(j,0))((i,1),(j,0)) that refer to aiaja_i^{\dagger}a_j, and two-body terms ((i,1),(j,1),(k,0),(l,0))((i,1),(j,1),(k,0),(l,0)) that corresponds to aiajakala_i^{\dagger}a_j^{\dagger}a_ka_l.
Orbital labeling: For NN spatial orbitals we have Nα(spin up)+Nβ(spin down)=2NN_\alpha (\text{spin up})+N_\beta (\text{spin down})=2N electron orbitals. The Classiq object for the electronic structure problem is defined according to block spin labeling (0,1,,(N1),0,1,(N1)(0_\uparrow, 1_\uparrow, \dots, (N-1)_\uparrow, 0_\downarrow,1_\downarrow\dots, (N-1)_\downarrow). This is opposed to the OpenFermion conventions, that has alternating spin labeling (0,0,1,1,,(N1),(N1)(0_\uparrow, 0_\downarrow, 1_\uparrow, 1_\downarrow,\dots, (N-1)_\uparrow, (N-1)_\downarrow). When transforming the problem to a Qubit Hamiltonian, described by Pauli strings, different labeling conventions can result in different Hamiltonians, which in turn, might lead to different quantum circuits in terms of depth or cx-counts.

The FermionToQubitMapper Class: From Fock Space to Qubit Space


Transforming to Qubit Hamiltonian (Pauli Strings)

Typically, when dealing with Fock space operators we need to transform the creation/annihilation operators to Pauli operators, suitable for quantum algorithms. There are several known transforms, such as Jordan Wigner (JW) and Bravyi Kitaev (BK) transforms.
Output:

Hartree Fock State

Once we have a problem and a mapper in hand, we can construct some quantum primitives. One example is the Hartree Fock state, typically used as an initial condition for ground state solvers. For the Jordan Wigner or the Bravyi Kitaev transforms, the Hartree Fock state, which is an elementary basis state in the Fock space, is mapped into a single computational basis state. This state can be determined using the get_hf_state function.
Output:
HF state under the JW transform: : Working with the JW transform, there is a simple relation between the original Fock (occupation number) and transformed (computational) basis states: the state 00k1100|\underbrace{0\dots 0}_{k-1}10\dots 0\rangle corresponds to occupation of the kk-th spin orbital in both spaces. Therefore, the Hartree Fock state under this transformation is the string 11Nα00011Nβ00|\underbrace{1\dots 1}_{N_{\alpha}}00\dots 0\underbrace{1\dots 1}_{N_{\beta}}0\dots 0\rangle. (This is as opposed to the BK transform, that gives a different computational basis state).

The Z2SymTaperMapper Class: From Fock Space to Reduced Qubit Space by Using Symmetries


Using symmetries of the second quantized Hamiltonian, one can reduce the number of qubits representing the problem by removing (tapering) qubits. The theory of qubit tapering is broad and complex, see for example Refs [3] and [4]. The Z2SymTaperMapper defines a mapper that includes qubit tapering. It can be initialized by providing Z2\mathbb{Z}_2 symmetries data (set of generators and Pauli XX operators) explicitly, or by providing a Fermionic Hamiltonian problem. In the latter case, that is introduced below, the Z2\mathbb{Z}_2 symmetries are deduced from the problem Hamiltonian. This is done via the from_problem method. **In the following section we provide some mathematical explanation of what is happening behind the secens when defining the Z2SymTaperMapper. All the logic presented below is incorporated as part of this class. The advanced reader is encouraged to follow this part, while less experienced readers may choose to skip ahead to the Constructing a VQE section without loss of continuity.**

Reducing the Problem Size with Z2\mathbb{Z}_2 Symmetries (Qubit Tapering)

The main steps of qubit tapering is as follows (see some technical details in Appendix B at the end of this notebook):
  1. Find generators {g(i)}i=1k\left\{g^{(i)}\right\}^k_{i=1} for a group of operators that commute with the Hamiltonian HH: for all gg(1),g(k)g\in \langle g^{(1)},\dots g^{(k)}\rangle, [H,g]=0\left[H, g\right] = 0.
That means that there is a basis in which both HH and such gg operators are diagonal. These operators are assumed to be a single Pauli string, typically containing only Pauli ZZ operators.
  1. Find a unitary transformation UU that diagonalizes all g(i)g^{(i)}, such that each generator operates trivially on all qubits except one, e.g., they transform to operators of the form XlX_{l} for some qubit number ll. It can be shown that such unitary can be constructed as Πi=1k12(Xm(i)+g(i))\Pi^k_{i=1}\frac{1}{\sqrt{2}}\left(X_{m^{(i)}}+g^{(i)}\right), where Xm(i)X_{m^{(i)}} is operating on a some single qubit m(i)m^{(i)}.
  2. Apply the transformation UHUU^{\dagger} H U, whose eigenspace will be identical to those of UgiUU^{\dagger} g_i U.
That means that on some qubits the transformed Hamiltonian is acting trivially, returning ±1\pm 1 (thus is the name Z2\mathbb{Z}_2 symmetries), and we can taper them off.
  1. Taper off qubits from the transformed Hamiltonian.
Let us define a qubit tapering mapper and inspect its properties:
We can verify that the generators are indeed commuting with the Hamiltonian
Output:
The generators for the symmetry group {g(i)}\left\{g^{(i)}\right\} are accompained by {Xm(i)}\left\{X_{m^{(i)}}\right\} operators, such that g(i)Xm(j)=(1)δijXm(j)g(i)g^{(i)} X_{m^{(j)}}= (-1)^{\delta_{ij}} X_{m^{(j)}}g^{(i)} . We can verify this as well:
Output:
Intuition for conserved quantities under the JW transform: In electronic structure problems we have, for example, particle number conservation, spin conservation, and number of particles with fixed spin orientation. The latter corresponds to the two Fermionic operators:
>Total number of spin-up/down particles operator:N=iaiai,>N=iaiai.>>\text{Total number of spin-up/down particles operator:} \qquad N_{\uparrow} = \sum_i a^{\dagger}_{i\uparrow} a_{i\uparrow}, \qquad >N_{\downarrow} = \sum_i a^{\dagger}_{i\downarrow} a_{i\downarrow}. >
As explained in the previous info box, working with the JW transform gives that Fock basis state are trasformed to the computational basis states. In particular, there is a relation between the orbital number operator and the ZZ operators: niaiai=12(1Zi)n_i \equiv a_i^{\dagger}a_i = \frac{1}{2}\left(1-Z_i\right). We cannot use the transformation of N()N_{\uparrow(\downarrow)} as our symmetry generators, since they correspond to a sum of Pauli strings rather than a single string. However, we can use any function of those, >for example g()=eπiN^()g_{\uparrow(\downarrow)} = e^{\pi i \hat{N}_{\uparrow(\downarrow)}}, which, up to a global phase, gives the generators
>g=Πk=0N/21Zk,g=Πk=N/2N1Zk.>>g_{\uparrow} = \Pi^{N/2-1}_{k=0}Z_{k}, \qquad g_{\downarrow} = \Pi^{N-1}_{k=N/2}Z_{k}. >
We can see that gg_{\uparrow} is indeed a generator in the example above.
Next, we can define a transformation from the generators the the Pauli XX operators, which means that it block-diagonalizes the Hamiltonian according to symmetry subspaces. This unitary is given by U=Πi=1k12(Xm(i)+g(i))U = \Pi^k_{i=1}\frac{1}{\sqrt{2}}\left(X_{m^{(i)}}+g^{(i)}\right).
Let us verify, for example, that indeed this diagonalizing operator map each generator into a single computational basis subspace:
Output:
Next, let us examine the block-diagonalized Hamiltonian—
  • We shall see that after transformation, the Hamiltonian acts trivially on some of the qubits, with the identity or with the {Xm(i)}\left\{X_{m^{(i)}}\right\} operators found above. Thus, we can reduce it by going to one of the two eigenspaces of these operators, with eigenvalues ±1\pm 1.
Output:
We can see that on the 0th^{th} qubit we have only XX operations, therefore, we know that the eigenstates of our Hamiltonian will be of the form: ψ10=±1ψˉ9.|\psi\rangle_{10} = |\pm 1\rangle |\bar{\psi}\rangle_{9}. That is, the first qubit is either at state +|+\rangle or |-\rangle (the eigenvectors of the Puali XX matrix). The same is true for all the other Pauli XX operators in x_ops. We shall choose a sector, i.e., the +1+1 or 1-1 subspace, for each of the subspaces X(mi)X_{(m_i)} operations. Which eigenspace to choose? The answer to this question depends on the problem at hand. If we would like to find the minimal energy of the Hamiltonian, then we shall take the subspace containing the minimal energy. One possibility is to solve multiple (242^4) problems on all sectors. However, another approach is to fix the sector according to the HF state, which is assumed to be in the optimal sector with minimal energy. This is the default sector defined in Z2SymTaperMapper when initializing with the .from_problem method. To emphasize the effect of choosing different sectors, we construct 242^4 tapered operators, each for the subspaces (sectors) ±1±±1±1\pm 1 \otimes \pm \otimes \pm 1 \otimes \pm 1, and classically calculate the ground state for each tapered Hamiltonian.
Output:

Constructing a VQE Model with Classiq


Next, we use all the classical pre-processing and definitions from the previous sections to build, synthesize, and execute a VQE model. We will take the following steps:
  1. Defining the transformed and tapered-off Hartree Fock state, which serves as an initial condition for the problem.
  2. Constructing the transformed and tapered-off UCC ansatz.
  3. Defining, synthesizing, and executing the full model
As a preliminary step, we define the Hamiltonian of the VQE problem. Since this is the final Hamiltonian (after a series of transformation, from second quantized Hamiltonian, tapering, etc.), let us trim small values according to some rough threshold.
Output:
Next, we use Classiq built-in functions to get the Hartree Fock state and the UCC ansatz Hamiltonians, given the problem and mapper.
Moving to symmetry subspaces: The Hartree Fock and the UCC operators that are defined below do not necessarily have the same symmetries of the molecular Hamiltonian. Thus, after the block-diagonalization, the resulting operators are not restricted to the symmetries’ subspaces. We take the following approach: we remove terms which do not satisfy the symmetry relation, i.e., commute with symmetry generators. This is done automatically by calling the corresponding functions.

  1. Hartree Fock in the Tapered-Off Space
We have already calculated the HF state under the JW transform, let us find the HF state after qubit tapering:
Output:

  1. UCC Ansatz
The Unitary Coupled Cluster ansatz assumes that the HF state is initially occupied. Then, it includes excitations from the occupied to un-occupied states, where the former is defined by the HF state. In this tutorial we focus on the UCCSD ansatz, in which only singlet and doublet excitation are taken. The corresponding Fermionic operator reads: UUCCSDeTT,T=T1+T2\large U_{\text{UCCSD}} \equiv e^{T - T^\dagger}, \qquad T = T_1 + T_2 where: T1=ioccavirttiaaaai,T2=i<jocca<bvirttijabaaabajai,\large T_1 = \sum_{i \in \text{occ}} \sum_{a \in \text{virt}} t_i^a a_a^\dagger a_i, \qquad T_2 = \sum_{i<j \in \text{occ}} \sum_{a<b \in \text{virt}} t_{ij}^{ab} a_a^\dagger a_b^\dagger a_j a_i, and the tt terms are parametric angles. The function for obtaining the UCC Hamiltonians takes into account spin conservation.

  1. Running a VQE
The VQE is constructed by preparing the HF state (with the prepare_basis_state function) and evolving a parametric UCCSD Hamiltonian with Suzuki Trotter.
Output:
To get a quick execution, we run on a statevector simulator.
We run simple optimization using the minimize method of ExecutionSession
Output:
output

Appendix A

  • Loading Molecule Geometry

Appendix B

  • Techical Details on Qubit Tapering
Below we provide some technical details concerning qubit tapering and Z2\mathbb{Z}_2 symmetries. It is a well-known fact in linear algebra that if two operators commute, [A,B]=0[A,B]=0, then they can be mutually diagonalized. In particular, if v|v\rangle is an eigenvector of BB with an eigenvalue λ\lambda, we have [A,B]=0    AB=BA    ABv=BAv    λ(Av)=B(Av).[A,B]=0\implies AB = BA \implies AB|v\rangle = BA|v\rangle \implies \lambda \left(A|v\rangle\right) = B\left(A|v\rangle\right). That is, AvA|v\rangle is also an eigenvector of BB with eigenvalue λ\lambda. Thus, AvA|v\rangle must be in the eigenspace Vλ{u,Bu=λu}V_{\lambda} \equiv \left\{|u\rangle, B|u\rangle = \lambda|u\rangle\right\}. Now, Refs. [3] and [4] show, and this is implemented explicitly in Appendix B, that we can find a Clifford transformation UU, such that the transformed Hamiltonian H=UHUH'=U^{\dagger} H U commutes with the transformed symmetries Xm(i)=UgiUX_{m^{(i)}} = U^{\dagger} g_i U. We know how the eigenspaces of Xm(i)X_{m^{(i)}} look like. For example, X0X_{0} has two eigenspaces that correspond to the eigenvalues ±1\pm 1: V±={uN,X0uN=±uN}={±u~N1,u~N1 some state on N1 qubits}V_{\pm} = \left\{|u\rangle_N, X_{0}|u\rangle_N = \pm |u\rangle_N\right\} = \left\{|\pm\rangle \otimes |\tilde{u}\rangle_{N-1},\, |\tilde{u}\rangle_{N-1} \text{ some state on } N-1 \text{ qubits}\right\} . From the arguments above we get that H(±u)V±,H'\cdot \left(|\pm\rangle |u\rangle\right) \in V_{\pm}, which means that HH' must acts with X0X_0 or the Identity on the first qubit.

References

[1]: McClean et. al. Quantum Sci. Technol. 5 034014 (2020). OpenFermion: the electronic structure package for quantum computers. [2]: Introduction to OpenFermion. [3]: [Bravyi et. al., arXiv preprint arXiv:1701.08213 (2017). Tapering off qubits to simulate fermionic Hamiltonians. ](https://arxiv.org/abs/1701.08213) [4]: Kanav et al. J. Chem. Theo. Comp. 16 10 (2020). Reducing qubit requirements for quantum simulations using molecular point group symmetries.