-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
450 lines (370 loc) · 15.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import datetime
import json
import importlib
import click
import numpy as np
from qiskit_ibm_runtime import QiskitRuntimeService, Estimator, Sampler, Session, Options
from qiskit.primitives import BackendEstimator, BackendSampler
from qiskit.providers.fake_provider import FakeLagos
from qiskit_nature.drivers.second_quantization import PySCFDriver
from qiskit_nature.drivers import UnitsType
from qiskit_nature.problems.second_quantization.electronic import ElectronicStructureProblem
from qiskit_nature.transformers.second_quantization.electronic import ActiveSpaceTransformer
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import ParityMapper, JordanWignerMapper, BravyiKitaevMapper
from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector
from qiskit_nature.circuit.library import *
from qiskit.circuit.library import *
from qiskit.algorithms.optimizers import SPSA, COBYLA
from qiskit.ignis.verification.tomography import state_tomography_circuits, StateTomographyFitter
from qiskit.compiler import transpile
import mthree
from zne import zne, ZNEStrategy
from zne.extrapolation import *
# TODO: turn start/end logging into a decorator
# Global variables
RUNTIME = False
XYZ_FILE = None
CHARGE = None
SPIN = None
BASIS = None
NUM_ACTIVE_ELECTRONS = None
NUM_ACTIVE_ORBITALS = None
ACTIVE_ORBITALS = None
MAPPER = None
ANSATZ = None
OPTIMIZER = None
BACKEND = None
SHOTS = None
OPTIMIZATION_LEVEL = None
RESILIENCE_LEVEL = None
LAST_MTHREE_CALIBRATION = None
############################################
# Utility functions
############################################
def get_counts(result, num_qbits):
shots = result.metadata[0]['shots']
quasi_dists = result.quasi_dists
counts = []
for qd in quasi_dists:
d = {}
for k, v in qd.items():
k = bin(k)[2:].zfill(num_qbits)
v = int(v * shots)
d[k] = v
counts.append(d)
return counts
def get_qd(counts):
qd = []
for count in counts:
d = {}
for k, v in count.items():
k = int(k, base=2)
d[k] = v
qd.append(d)
return qd
def parse_xyz_file(xyz_file):
xyz = []
with open(xyz_file) as fp:
n = 1
for line in fp:
line = line.strip()
if line.isdigit():
n = int(line) + 1
continue
xyz.append(line)
n -= 1
if n == 0:
break
atom_str = xyz[1:]
return atom_str
def parse_settings(settings_file):
print(f'Parsing settings at {datetime.datetime.now().isoformat()}', flush=True)
with open(settings_file, 'r') as fp:
settings = json.load(fp)
global XYZ_FILE
XYZ_FILE = settings['XYZ_FILE']
global CHARGE
CHARGE = settings['CHARGE']
global SPIN
SPIN = settings['SPIN']
global BASIS
BASIS = settings['BASIS']
global NUM_ACTIVE_ELECTRONS
NUM_ACTIVE_ELECTRONS = settings['NUM_ACTIVE_ELECTRONS']
global NUM_ACTIVE_ORBITALS
NUM_ACTIVE_ORBITALS = settings['NUM_ACTIVE_ORBITALS']
global ACTIVE_ORBITALS
ACTIVE_ORBITALS = settings['ACTIVE_ORBITALS']
global MAPPER
module = importlib.import_module('qiskit_nature.mappers.second_quantization')
MAPPER = getattr(module, settings['MAPPER'])()
global ANSATZ
ANSATZ = settings['ANSATZ']
global OPTIMIZER
module = importlib.import_module('qiskit.algorithms.optimizers')
if 'MAXITER' not in settings:
OPTIMIZER = getattr(module, settings['OPTIMIZER'])()
else:
OPTIMIZER = getattr(module, settings['OPTIMIZER'])(maxiter=settings['MAXITER'])
global BACKEND
if RUNTIME == True:
BACKEND = settings['BACKEND']
else:
module = importlib.import_module('qiskit.providers.fake_provider')
BACKEND = getattr(module, settings['BACKEND'])()
global SHOTS
SHOTS = settings['SHOTS']
global OPTIMIZATION_LEVEL
OPTIMIZATION_LEVEL = settings['OPTIMIZATION_LEVEL']
global RESILIENCE_LEVEL
RESILIENCE_LEVEL = settings['RESILIENCE_LEVEL']
print(f'Settings parsed at {datetime.datetime.now().isoformat()}', flush=True)
############################################
# Ansatz
############################################
def make_hwe_ansatz(num_qubits, depth):
"""
Implements https://www.nature.com/articles/nature23879.
Code taken from https://github.com/lockwo/Paper-Review/blob/main/HEA-VQE/hea_vqe.ipynb.
:param num_qubits: Number of qubits (qubit_op.num_qubits)
"""
num_q = num_qubits
circuit = QuantumCircuit(num_q)
params = ParameterVector("theta", length=num_q * (3 * depth + 2))
counter = 0
for q in range(num_q):
circuit.rx(params[counter], q)
counter += 1
circuit.rz(params[counter], q)
counter += 1
for d in range(depth):
for q in range(num_q - 1):
circuit.cx(q, q + 1)
for q in range(num_q):
circuit.rz(params[counter], q)
counter += 1
circuit.rx(params[counter], q)
counter += 1
circuit.rz(params[counter], q)
counter += 1
return circuit
def make_esu2_ansatz(num_qubits, su2_gates=['ry', 'rz']):
"""
Ansatz alternates between su2_gates layer and entanglement layer.
Notes:
- Recommended to toggle between ['ry'] and ['ry', 'rz'] for su2_gates.
:param num_qubits: Number of qubits (qubit_op.num_qubits)
"""
# Should keep entanglement as linear and reps as 1 to minimize depth
ansatz = EfficientSU2(num_qubits, su2_gates=su2_gates, entanglement='linear', reps=1)
return ansatz
def make_excitation_preserving_ansatz(num_qubits, initial_state = None):
"""
Excitation preserving ansatz.
:param num_qubits: Number of qubits (qubit_op.num_qubits)
:param initial_state: Initial state of ansatz (ie. Hartree-Fock initial state)
"""
ansatz = ExcitationPreserving(
num_qubits,
entanglement = 'linear',
initial_state = initial_state,
reps = 1,
)
ansatz._preferred_init_points = np.zeros(ansatz.num_parameters)
return ansatz
############################################
# main
############################################
@click.command()
@click.option('--runtime', type=bool, default=False, help='Set True to run with Qiskit runtime.')
@click.option('--channel', help='IBM Quantum credentials: see https://qiskit.org/documentation/partners/qiskit_ibm_runtime/getting_started.html')
@click.option('--token', help='IBM Quantum credentials: see https://qiskit.org/documentation/partners/qiskit_ibm_runtime/getting_started.html')
@click.option('--settings', required=True, help='Settings file for the VQE algorithm.')
def main(runtime, channel, token, settings):
start = datetime.datetime.now()
print(f'Start at {start.isoformat()}', flush=True)
set_credentials(runtime, channel, token)
run(settings)
end = datetime.datetime.now()
print(f'End at {end.isoformat()}', flush=True)
print(f'Runtime is {(end - start).seconds}s', flush=True)
print('DONE', flush=True)
def set_credentials(runtime, channel, token):
"""
Set the credentials for the IBM Quantum account.
:param runtime: Set True to run with Qiskit runtime.
:param channel: IBM Quantum credentials: see https://qiskit.org/documentation/partners/qiskit_ibm_runtime/getting_started.html
:param token: IBM Quantum credentials: see https://qiskit.org/documentation/partners/qiskit_ibm_runtime/getting_started.html
"""
print(f'Setting credentials at {datetime.datetime.now().isoformat()}', flush=True)
if runtime == True:
print('Running with Qiskit runtime')
QiskitRuntimeService.save_account(channel=channel, token=token, overwrite=True)
global RUNTIME
RUNTIME = True
else:
print('Running locally')
print(f'Credentials set at {datetime.datetime.now().isoformat()}', flush=True)
def run(settings_file):
"""
Run the VQE algorithm with the given settings.
:param settings_file: Settings file for the VQE algorithm.
"""
parse_settings(settings_file)
print(f'PYSCF calculation start at {datetime.datetime.now().isoformat()}', flush=True)
driver = PySCFDriver(atom=parse_xyz_file(XYZ_FILE), unit=UnitsType.ANGSTROM, charge=CHARGE, spin=SPIN, basis=BASIS)
problem = ElectronicStructureProblem(driver, [ActiveSpaceTransformer(
NUM_ACTIVE_ELECTRONS,
NUM_ACTIVE_ORBITALS,
ACTIVE_ORBITALS,
)])
second_q_ops = problem.second_q_ops()
converter = QubitConverter(mapper=MAPPER, two_qubit_reduction=True)
qubit_op = converter.convert(
second_q_op=second_q_ops['ElectronicEnergy'],
num_particles=problem.num_particles,
)
ee_property = problem.grouped_property_transformed.get_property('ElectronicEnergy')
nuclear_repulsion_energy = ee_property.nuclear_repulsion_energy
active_space_energy_shift = 0
if hasattr(ee_property, '_shift') and 'ActiveSpaceTransformer' in ee_property._shift:
active_space_energy_shift = np.real(ee_property._shift['ActiveSpaceTransformer'])
print(f'nuclear_repulsion_energy={nuclear_repulsion_energy}')
print(f'active_space_energy_shift={active_space_energy_shift}')
print(f'PYSCF calculation end at {datetime.datetime.now().isoformat()}', flush=True)
global ANSATZ
if ANSATZ.startswith('hwe-'):
depth = int(ANSATZ.split('-')[1])
ANSATZ = make_hwe_ansatz(qubit_op.num_qubits, depth)
elif ANSATZ.startswith('esu2-'):
su2_gates = ANSATZ.split('-')[1].split(',')
ANSATZ = make_esu2_ansatz(qubit_op.num_qubits, su2_gates)
elif ANSATZ == 'excitation_preserving':
initial_state = HartreeFock(
problem.num_spin_orbitals,
problem.num_particles,
converter,
)
ANSATZ = make_excitation_preserving_ansatz(qubit_op.num_qubits, initial_state)
else:
raise NotImplementedError(f'Ansatz {ANSATZ} not implemented')
initial_point = np.random.uniform(-np.pi, np.pi, len(ANSATZ.parameters))
# if hasattr(ANSATZ, '_preferred_init_points'):
# initial_point = ANSATZ._preferred_init_points
def vqe(estimator, sampler, zne_strategy = None):
with open('output.txt', 'w') as fp:
fp.write('iteration\tenergy\ttime\tparameters\n')
def get_energy():
iteration = 1
if RESILIENCE_LEVEL == 6:
mit = mthree.M3Mitigation(sampler)
def energy(theta):
nonlocal iteration
nonlocal mit
nonlocal estimator
nonlocal zne_strategy
if RUNTIME == False and RESILIENCE_LEVEL == 2:
result = estimator.run(circuits=ANSATZ, observables=qubit_op, parameter_values=[theta], shots=SHOTS, zne_strategy=zne_strategy).result().values[0]
elif RESILIENCE_LEVEL < 4:
result = estimator.run(circuits=ANSATZ, observables=qubit_op, parameter_values=[theta], shots=SHOTS).result().values[0]
elif RESILIENCE_LEVEL == 5 or RESILIENCE_LEVEL == 6:
num_qubits = sampler.backend.configuration().num_qubits
circuit = ANSATZ.assign_parameters(theta)
tomography_circuits = state_tomography_circuits(circuit, circuit.qubits)
job = sampler.run(circuits=tomography_circuits, shots=SHOTS)
result = job.result()
if RESILIENCE_LEVEL == 6:
global LAST_MTHREE_CALIBRATION
# mthree recalibrate every 300s
if LAST_MTHREE_CALIBRATION is None or (datetime.datetime.now() - LAST_MTHREE_CALIBRATION).seconds > 300:
LAST_MTHREE_CALIBRATION = datetime.datetime.now()
mit.cals_from_system()
counts = get_counts(result, num_qubits)
qd = get_qd(mit.apply_correction(counts, range(num_qubits)))
for i in range(len(result.quasi_dists)):
result.quasi_dists[i] = qd[i]
fitter = StateTomographyFitter(result, tomography_circuits)
density_matrix = fitter.fit()
eigenvalues, eigenvectors = np.linalg.eig(density_matrix)
# The imaginary parts are very small due to precision errors
eigenvalues = [np.real(ev) for ev in eigenvalues]
psi = eigenvectors[:, np.argmax(eigenvalues)]
psi.reshape((len(psi),))
H = qubit_op.to_matrix()
# np.inner doesn't do complex conjugate
result = np.real(np.inner(psi.conjugate(), H.dot(psi)))
else:
pass
# logging
with open('output.txt', 'a') as fp:
parameters = str(theta).replace('\n', '')
fp.write(f'{iteration}\t{result}\t{datetime.datetime.now().isoformat()}\t{parameters}\n')
iteration += 1
return result
return energy
energy = get_energy()
OPTIMIZER.minimize(energy, initial_point)
if RUNTIME == True:
service = QiskitRuntimeService()
with Session(service=service, backend=BACKEND):
if RESILIENCE_LEVEL == 2:
options = Options(optimization_level=OPTIMIZATION_LEVEL, resilience_level=RESILIENCE_LEVEL)
options.resilience.noise_factors = [1, 3, 5]
options.resilience.extrapolator = "LinearExtrapolator"
estimator = Estimator(options=options)
elif RESILIENCE_LEVEL < 4:
estimator = Estimator(options=Options(
optimization_level=OPTIMIZATION_LEVEL,
resilience_level=RESILIENCE_LEVEL,
))
sampler = None
elif RESILIENCE_LEVEL == 4:
raise NotImplementedError('Matrix-free not implemented.')
elif RESILIENCE_LEVEL == 5:
estimator = None
sampler = Sampler(options=Options(
optimization_level=OPTIMIZATION_LEVEL,
resilience_level=0,
))
elif RESILIENCE_LEVEL == 6:
estimator = None
sampler = Sampler(options=Options(
optimization_level=OPTIMIZATION_LEVEL,
resilience_level=1,
))
else:
raise NotImplementedError(f'Resilience level {RESILIENCE_LEVEL} does not exist.')
vqe(estimator, sampler)
else:
if RESILIENCE_LEVEL == 2:
zne_strategy = ZNEStrategy(noise_factors=[1, 3, 5], extrapolator=PolynomialExtrapolator(degree=1))
zne_estimator = zne(BackendEstimator)
estimator = zne_estimator(backend=BACKEND)
sampler = None
elif RESILIENCE_LEVEL < 4:
estimator = BackendEstimator(backend=BACKEND, options={
'optimization_level': OPTIMIZATION_LEVEL,
'resilience_level': RESILIENCE_LEVEL,
})
sampler = None
elif RESILIENCE_LEVEL == 4:
raise NotImplementedError('Matrix-free not implemented.')
elif RESILIENCE_LEVEL == 5:
estimator = None
sampler = BackendSampler(backend=BACKEND, options={
'optimization_level': OPTIMIZATION_LEVEL,
'resilience_level': 0,
})
elif RESILIENCE_LEVEL == 6:
estimator = None
sampler = BackendSampler(backend=BACKEND, options={
'optimization_level': OPTIMIZATION_LEVEL,
'resilience_level': 1,
})
else:
raise NotImplementedError(f'Resilience level {RESILIENCE_LEVEL} does not exist.')
vqe(estimator, sampler, zne_strategy)
if __name__ == '__main__':
main()