Skip to content

Commit

Permalink
Merge branch 'CISD' of ssh://github.com/UCL-CCS/symmer into CISD
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisRalli committed Jan 17, 2023
2 parents 4da556a + 31230b1 commit 79dc4c7
Show file tree
Hide file tree
Showing 14 changed files with 7,670 additions and 6,287 deletions.
59 changes: 27 additions & 32 deletions notebooks/CS-VQE for Electronic Structure.ipynb

Large diffs are not rendered by default.

12,305 changes: 6,412 additions & 5,893 deletions notebooks/Chemistry usage.ipynb

Large diffs are not rendered by default.

221 changes: 160 additions & 61 deletions notebooks/H4_CISD_tapering.ipynb

Large diffs are not rendered by default.

428 changes: 387 additions & 41 deletions notebooks/PauliwordOp usage.ipynb

Large diffs are not rendered by default.

572 changes: 444 additions & 128 deletions notebooks/QuantumState usage.ipynb

Large diffs are not rendered by default.

128 changes: 45 additions & 83 deletions notebooks/Tapering the Heisenberg Hamiltonian.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 74,
"id": "072ceab2",
"metadata": {},
"outputs": [
Expand All @@ -25,26 +25,26 @@
"text": [
"The heisenberg Hamiltonian is\n",
"\n",
"-0.325+0.000j IIXX +\n",
"-0.325+0.000j IXXI +\n",
"-0.325+0.000j XXII +\n",
"-0.174+0.000j IIYY +\n",
"-0.174+0.000j IYYI +\n",
"-0.174+0.000j YYII +\n",
"-0.064+0.000j IIZZ +\n",
"-0.064+0.000j IZZI +\n",
"-0.064+0.000j ZZII\n",
"-0.482+0.000j IIXX +\n",
"-0.482+0.000j IXXI +\n",
"-0.482+0.000j XXII +\n",
"-0.471+0.000j IIYY +\n",
"-0.471+0.000j IYYI +\n",
"-0.471+0.000j YYII +\n",
"-0.032+0.000j IIZZ +\n",
"-0.032+0.000j IZZI +\n",
"-0.032+0.000j ZZII\n",
"\n",
"with ground state energy -1.055675535453756 and corresponding eigenvector\n",
"with ground state energy -2.0743038562181226 and corresponding eigenvector\n",
"\n",
" 0.173+0.000j |0000> +\n",
" 0.281+0.000j |0011> +\n",
" 0.455+0.000j |0101> +\n",
" 0.429+0.000j |0110> +\n",
" 0.429+0.000j |1001> +\n",
" 0.455+0.000j |1010> +\n",
" 0.281+0.000j |1100> +\n",
" 0.173+0.000j |1111>\n"
"-0.005+0.000j |0000> +\n",
"-0.231+0.000j |0011> +\n",
"-0.495+0.000j |0101> +\n",
"-0.448+0.000j |0110> +\n",
"-0.448+0.000j |1001> +\n",
"-0.495+0.000j |1010> +\n",
"-0.231+0.000j |1100> +\n",
"-0.005+0.000j |1111>\n"
]
}
],
Expand Down Expand Up @@ -98,7 +98,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 75,
"id": "a1738822",
"metadata": {
"scrolled": true
Expand Down Expand Up @@ -146,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 76,
"id": "d0560d49",
"metadata": {},
"outputs": [
Expand All @@ -156,26 +156,26 @@
"text": [
"We rotate the Hamiltonian accordingly:\n",
"\n",
"-0.064+0.000j IIZZ +\n",
"-0.325+0.000j IIXX +\n",
"-0.174+0.000j IIYY +\n",
" 0.064-0.000j IXZI +\n",
" 0.325-0.000j IXXX +\n",
" 0.064-0.000j XIZZ +\n",
" 0.174-0.000j XIZX +\n",
" 0.325-0.000j XXIX +\n",
"-0.174+0.000j XXYY\n",
"-0.032+0.000j IIZZ +\n",
"-0.482+0.000j IIXX +\n",
"-0.471+0.000j IIYY +\n",
" 0.032-0.000j IXZI +\n",
" 0.482-0.000j IXXX +\n",
" 0.032-0.000j XIZZ +\n",
" 0.471-0.000j XIZX +\n",
" 0.482-0.000j XXIX +\n",
"-0.471+0.000j XXYY\n",
"\n",
"and observe that qubit positions [1 0] consist only of Pauli I, X operators.\n",
"\n",
"These may therefore be removed to yield a 2-qubit reduced Hamiltonian:\n",
"\n",
"-0.064+0.000j ZI +\n",
"-0.128+0.000j ZZ +\n",
" 0.325+0.000j IX +\n",
"-0.174+0.000j ZX +\n",
"-0.649+0.000j XX +\n",
"-0.349+0.000j YY\n"
"-0.032+0.000j ZI +\n",
"-0.065+0.000j ZZ +\n",
" 0.482+0.000j IX +\n",
"-0.471+0.000j ZX +\n",
"-0.965+0.000j XX +\n",
"-0.942+0.000j YY\n"
]
}
],
Expand All @@ -189,28 +189,6 @@
"print(H_taper)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f58a7949",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
" 1 ZZZZ \n",
" 1 XXXX"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"QT.stabilizers"
]
},
{
"cell_type": "markdown",
"id": "a8de483a",
Expand All @@ -221,24 +199,24 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 78,
"id": "db6d9dec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The ground state energy of the Hamiltonian is -1.055675535453756\n",
"and for the 2-qubit tapered Hamiltonian it is -1.0556755354537553;\n",
"the energy error is 6.661338147750939e-16.\n",
"The ground state energy of the Hamiltonian is -2.0743038562181226\n",
"and for the 2-qubit tapered Hamiltonian it is -2.0743038562181217;\n",
"the energy error is 8.881784197001252e-16.\n",
"\n",
"The tapered ground state is:\n",
"\n",
" 0.244-0.000j |00> +\n",
"-0.606+0.000j |01> +\n",
"-0.644+0.000j |10> +\n",
" 0.398+0.000j |11>\n"
" 0.007+0.000j |00> +\n",
"-0.634+0.000j |01> +\n",
"-0.701+0.000j |10> +\n",
" 0.327+0.000j |11>\n"
]
}
],
Expand All @@ -252,22 +230,6 @@
"print('The tapered ground state is:\\n')\n",
"print(gs_psi_tap)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32126b3e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "628ae709",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -286,7 +248,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.13"
"version": "3.8.13"
},
"vscode": {
"interpreter": {
Expand Down
53 changes: 43 additions & 10 deletions symmer/chemistry/build_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pyscf.scf.addons import get_ghf_orbspin
from openfermion import get_fermion_operator, jordan_wigner, FermionOperator
from typing import Tuple, List
import numpy as np

def list_to_xyz(geometry: List[Tuple[str, Tuple[float, float, float]]]) -> str:
""" Convert the geometry stored as a list to xyz string
Expand All @@ -26,9 +27,13 @@ def __init__(self,
charge=0,
basis='STO-3G',
spin=0,
run_fci = True,
run_mp2 = True,
run_cisd = True,
run_ccsd = True,
run_fci = True,
qubit_mapping_str = 'jordan_wigner',
hf_method = 'RHF',
CI_ansatz = 'CISD',
symmetry = False,
print_info = True) -> None:
"""
Expand All @@ -46,9 +51,16 @@ def __init__(self,
self.qubit_mapping_str = qubit_mapping_str
self.symmetry = symmetry
self.print_info = print_info
self.calculate(run_fci=run_fci, hf_method=hf_method)
self.CI_ansatz = CI_ansatz
self.calculate(
run_mp2=run_mp2,run_cisd=run_cisd,run_ccsd=run_ccsd,run_fci=run_fci,
hf_method=hf_method)
self.n_particles = self.pyscf_obj.pyscf_hf.mol.nelectron
self.n_alpha, self.n_beta = self.pyscf_obj.pyscf_hf.nelec
if hf_method.find('RHF') != -1:
n_electron = self.pyscf_obj.pyscf_hf.mol.nelectron
self.n_alpha = self.n_beta = n_electron//2
else:
self.n_alpha, self.n_beta = self.pyscf_obj.pyscf_hf.nelec
orbspin = get_ghf_orbspin(
self.pyscf_obj.pyscf_hf.mo_energy,
self.pyscf_obj.pyscf_hf.mo_occ,
Expand All @@ -72,14 +84,19 @@ def __init__(self,

self.H = get_fermion_operator(self.H_fermion.fermionic_molecular_hamiltonian)
self.T = self.T_fermion.fermionic_cc_operator

self.CI, self.total_CI_energy, self.psi_CI = self.H_fermion.get_fermionic_CI_ansatz(
S=spin/2, method=CI_ansatz
)
if run_cisd and self.CI_ansatz == 'CISD':
assert(np.isclose(self.total_CI_energy,self.cisd_energy)), 'Manual CISD calculation does not match PySCF'
# map to QubitOperator via fermion -> qubit mapping and convert to PauliwordOp
self.H_q = fermion_to_qubit_operator(self.H, self.qubit_mapping_str, N_qubits=self.n_qubits)
if len(self.T.terms)==0:
self.T_q = PauliwordOp.empty(self.n_qubits)
else:
self.T_q = fermion_to_qubit_operator(self.T, self.qubit_mapping_str, N_qubits=self.n_qubits)

self.CI_q = fermion_to_qubit_operator(self.CI, self.qubit_mapping_str, N_qubits=self.n_qubits)
self.UCC_q = self.T_q - self.T_q.dagger
self.UCC_q.coeff_vec = self.UCC_q.coeff_vec.imag
self.SOR_q = self.second_order_response()
Expand Down Expand Up @@ -110,9 +127,22 @@ def calculate(self,
)
self.pyscf_obj.run_pyscf()

if self.print_info:
print('HF converged? ', self.pyscf_obj.pyscf_hf.converged)
print('CCSD converged?', self.pyscf_obj.pyscf_ccsd.converged)
if run_mp2:
self.mp2_energy = self.pyscf_obj.pyscf_mp2.e_tot
else:
self.mp2_energy = None
if run_cisd:
if self.print_info:
print('CISD converged? ', self.pyscf_obj.pyscf_cisd.converged)
self.cisd_energy = self.pyscf_obj.pyscf_cisd.e_tot
else:
self.cisd_energy = None
if run_ccsd:
if self.print_info:
print('FCI converged? ', self.pyscf_obj.pyscf_ccsd.converged)
self.ccsd_energy = self.pyscf_obj.pyscf_ccsd.e_tot
else:
self.ccsd_energy = None
if run_fci:
if self.print_info:
print('FCI converged? ', self.pyscf_obj.pyscf_fci.converged)
Expand All @@ -121,14 +151,13 @@ def calculate(self,
self.fci_energy = None

self.hf_energy = self.pyscf_obj.pyscf_hf.e_tot
self.mp2_energy = self.pyscf_obj.pyscf_mp2.e_tot
self.ccsd_energy = self.pyscf_obj.pyscf_ccsd.e_tot


if self.print_info:
print()
print(f'HF energy: {self.hf_energy}')
print(f'MP2 energy: {self.mp2_energy}')
print(f'CCSD energy: {self.ccsd_energy}')
print(f'CISD energy: {self.cisd_energy}')
print(f'FCI energy: {self.fci_energy}')
print()

Expand Down Expand Up @@ -242,6 +271,10 @@ def data_dictionary(self):
mol_data['calculated_properties']['CCSD'] = {
'energy':self.ccsd_energy, 'converged':bool(self.pyscf_obj.pyscf_ccsd.converged)}
mol_data['auxiliary_operators']['UCCSD_operator'] = safe_PauliwordOp_to_dict(self.UCC_q)
if self.pyscf_obj.run_cisd:
mol_data['calculated_properties'][self.CI_ansatz] = {
'energy':self.total_CI_energy, 'converged':bool(self.pyscf_obj.pyscf_cisd.converged)}
mol_data['auxiliary_operators'][f'{self.CI_ansatz}_operator'] = safe_PauliwordOp_to_dict(self.CI_q)
if self.pyscf_obj.run_fci:
mol_data['calculated_properties']['FCI'] = {
'energy':self.fci_energy, 'converged':bool(self.pyscf_obj.pyscf_fci.converged)}
Expand Down
Loading

0 comments on commit 79dc4c7

Please sign in to comment.