-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_hamiltonian.py
55 lines (48 loc) · 2.24 KB
/
get_hamiltonian.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
# Qiskit imports
from qiskit_nature.properties.second_quantization.electronic import (
ElectronicStructureDriverResult,
ElectronicEnergy,
ParticleNumber,
)
from qiskit_nature.properties.second_quantization.electronic.bases import ElectronicBasis
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import JordanWignerMapper, ParityMapper
def get_hamiltonian(frag, nelecas_sub, ncas_sub, h1, h2):
if frag is None:
num_alpha = nelecas_sub[0]
num_beta = nelecas_sub[1]
n_so = ncas_sub*2
else:
# Get alpha and beta electrons from LAS
num_alpha = nelecas_sub[frag][0]
num_beta = nelecas_sub[frag][1]
n_so = ncas_sub[frag]*2
h1 = h1[frag]
h2 = h2[frag]
# For QPE, need second_q_ops
# Hacking together an ElectronicStructureDriverResult to create second_q_ops
# Lines below stolen from qiskit's FCIDump driver and modified
particle_number = ParticleNumber(
num_spin_orbitals=n_so,
num_particles=(num_alpha, num_beta),
)
# Assuming an RHF reference for now, so h1_b, h2_ab, h2_bb are created using
# the corresponding spots from h1_frag and just the aa term from h2_frag
electronic_energy = ElectronicEnergy.from_raw_integrals(
# Using MO basis here for simplified conversion
ElectronicBasis.MO, h1, h2
)
# QK NOTE: under Python 3.6, pylint appears to be unable to properly identify this case of
# nested abstract classes (cf. https://github.com/Qiskit/qiskit-nature/runs/3245395353).
# However, since the tests pass I am adding an exception for this specific case.
# pylint: disable=abstract-class-instantiated
driver_result = ElectronicStructureDriverResult()
driver_result.add_property(electronic_energy)
driver_result.add_property(particle_number)
second_q_ops = driver_result.second_q_ops()
# Choose fermion-to-qubit mapping
qubit_converter = QubitConverter(mapper = JordanWignerMapper(), two_qubit_reduction=False)
# This just outputs a qubit op corresponding to a 2nd quantized op
qubit_ops = [qubit_converter.convert(op) for op in second_q_ops]
hamiltonian = qubit_ops[0]
return hamiltonian