-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
205 lines (180 loc) · 7.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright 2022 The Quket Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# limitations under the License.
import os
### To avoid conflict
os.environ["OMP_NUM_THREADS"] = "1" ### Initial setting
os.environ["MKL_NUM_THREADS"] = "1" ### Initial setting
os.environ["NUMEXPR_NUM_THREADS"] = "1" ### Initial setting
import inspect
import datetime
from quket import _version
from quket import config as cf
from quket.mpilib import mpilib as mpi
from quket.vqe import VQE_driver
from quket.qite import QITE_driver
from quket.fileio import error, prints, printmat, print_geom, print_grad, tstamp
from quket.fileio import read_input, set_config
from quket.quket_data import QuketData
from quket.utils import get_func_kwds
prints(f"///////////////////////////////////////////////////////////////////////////////////", opentype="w")
prints(f"/// ///")
prints(f"/// ///")
prints(f"/// QQQ UUU UUU KKK KK EEEEEEE TTTTTTT ///")
prints(f"/// Q Q u U K K E E T T T ///")
prints(f"/// Q Q U U K K E E T ///")
prints(f"/// Q Q U U KKK EEEE T ///")
prints(f"/// Q QQQ Q U U K K E E T ///")
prints(f"/// Q Q U U K K E E T ///")
prints(f"/// QQQ QQ UUUU KKK KK EEEEEEE TTT ///")
prints(f"/// ///")
prints(f"/// Quantum Computing Simulator Ver {_version.__version__:10} ///")
prints(f"/// ///")
prints(f"/// Copyright 2019-2022 ///")
prints(f"/// The Quket Developers ///")
prints(f"/// All rights Reserved. ///")
prints(f"/// ///")
prints(f"///////////////////////////////////////////////////////////////////////////////////")
tstamp()
from quket._sys import get_user_api
cf._user_api = get_user_api()
cf.mem, cf.cpu = mpi.mem_proc_dict()
cf.nprocs_my_node = cf.cpu[mpi.name]
prints(f"{mpi.nprocs} processes x {cf.nthreads} = "
f"Total {mpi.nprocs*int(cf.nthreads)} cores\n"
f"API for qulacs: {cf._user_api}\n"
f"Number of cpus in the main node ({mpi.name}): {cf.nprocs_my_node}")
#prints('user_api', cf.user_api)
#prints('Number of total CPU = ', cf.ntotal_cpu)
######################################
### Start reading input file ###
######################################
kwds_list = read_input()
for job_no, kwds in enumerate(kwds_list, 1):
prints("+-------------+")
prints("| Job # %3d |" % job_no)
prints("+-------------+")
try:
prints(kwds["comment"])
except:
pass
# Get kwds for initialize QuketData
init_dict = get_func_kwds(QuketData.__init__, kwds)
Quket = QuketData(**init_dict)
##############
# Set config #
##############
set_config(kwds, Quket)
#######################
# Construct QuketData #
#######################
Quket.initialize(**kwds)
if cf.debug:
tstamp('QuketData initialized')
if Quket.method != 'mbe':
# Transform Jordan-Wigner Operators to Qulacs Format
Quket.openfermion_to_qulacs()
### Tweaking orbitals...
if Quket.alter_pairs != []:
## Switch orbitals
Quket.alter(Quket.alter_pairs)
if Quket.local != []:
## Localize orbitals
Quket.boys(*Quket.local)
#
Quket.get_pauli_list()
# Set projection parameters
Quket.set_projection()
# Saving input
Quket._init_dict = init_dict
Quket._kwds = kwds
if Quket.model == "chemical":
prints(f"NBasis = {Quket.mo_coeff.shape[0]}")
Quket.print_mo_energy()
if Quket.cf.print_level > 1 or cf.debug:
if cf.debug:
format = '18.14f'
else:
format = '11.7f'
printmat(Quket.mo_coeff, eig=Quket.orbital_energies, name="MO coeff", format=format)
if cf.debug:
printmat(Quket.overlap_integrals, name="Overlap", format=format)
if Quket.cf.do_taper_off or Quket.symmetry:
Quket.tapering.run(mapping=Quket.cf.mapping)
if Quket.cf.do_taper_off and Quket.method != 'mbe':
### Create excitation-pauli list, and transform relevant stuff by unitary
Quket.transform_all(reduce=True)
elif Quket.get_allowed_pauli_list:
Quket.get_allowed_pauli_list()
if Quket.run_qubitfci:
Quket.fci2qubit()
if Quket.ansatz is None or Quket.maxiter == 0:
prints(f"\n Skipping job since maxiter = {Quket.maxiter}.\n")
Quket.energy = Quket.qulacs.Hamiltonian.get_expectation_value(Quket.state)
else:
############
# VQE part #
############
if Quket.method in ("vqe", "mbe"):
VQE_driver(Quket,
Quket.cf.kappa_guess,
Quket.cf.theta_guess,
Quket.cf.mix_level,
Quket.cf.opt_method,
Quket.cf.opt_options,
Quket.cf.print_level,
Quket.cf.maxiter,
Quket.cf.Kappa_to_T1)
if Quket.cf.oo:
if Quket.cf.do_taper_off:
Quket.transform_all(backtransform=True)
from quket.orbital.oo import oo
oo(Quket, Quket.oo_maxiter, Quket.oo_gtol, Quket.oo_ftol)
# post VQE for excited states
for istate in range(Quket.nexcited):
prints(f"Performing VQE for excited states: "
f"{istate+1}/{Quket.nexcited} states")
Quket.vqd(det=Quket.excited_states[istate])
VQE_driver(Quket,
"zero",
Quket.cf.theta_guess,
Quket.cf.mix_level,
Quket.cf.opt_method,
Quket.cf.opt_options,
Quket.cf.print_level,
Quket.cf.maxiter,
False)
#############
# QITE part #
#############
elif Quket.method == "qite":
QITE_driver(Quket)
if Quket.method != 'mbe':
if Quket.cf.do_taper_off:
# Back to the original space
Quket.transform_all(backtransform=True)
Quket.prop()
#################
# Post-VQE part #
#################
# Nuclear gradient and/or Geometry optimization
if Quket.do_grad or Quket.geom_opt:
from quket.post import grad
Quket.nuclear_grad = grad.nuclear_grad(Quket)
if Quket.geom_opt:
grad.geomopt(Quket,init_dict,kwds)
# Everything is fine.
if mpi.main_rank and os.path.exists(cf.tmp):
os.remove(cf.tmp)
# This job is done. Go to the next job.
Quket.save(cf.qkt)
prints(f"Normal termination of quket at {datetime.datetime.now()}")