Skip to content

Commit

Permalink
black style
Browse files Browse the repository at this point in the history
  • Loading branch information
liwt31 committed Jul 13, 2023
1 parent c451117 commit 2ecf6b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
3 changes: 3 additions & 0 deletions tencirchem/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def water(bond_angle=104.45, bond_length=0.9584, basis="sto3g"):

N2 = n2 = nitrogen = lambda d=1.09: M(atom=[["N", 0, 0, 0], ["N", 0, 0, d]])


def get_ch4_coord(d):
x = d / np.sqrt(3)
ch4_coord = [
Expand All @@ -123,6 +124,8 @@ def get_ch4_coord(d):
["H", x, x, -x],
]
return ch4_coord


CH4 = ch4 = methane = lambda x=1.09: M(atom=get_ch4_coord(x))


Expand Down
2 changes: 1 addition & 1 deletion tencirchem/static/hea.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def get_circuit(params):

# allow setting these attributes for features such as calculating RDM
# could make it a function for customized mapping
self.mapping: str = None # fermion-to-qubit mapping
self.mapping: str = None # fermion-to-qubit mapping
self.int1e = None
self.int2e = None
self.n_elec = None
Expand Down
31 changes: 19 additions & 12 deletions tencirchem/static/ucc.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ def __init__(
else:
raise ValueError(f"Unknown initialization method: {init_method}")


# circuit related
self._init_state = None
self.ex_ops = None
Expand Down Expand Up @@ -1334,7 +1333,6 @@ def param_ids(self, v):


def compute_fe_t2(no, nv, int1e, int2e):

n_orb = no + nv

def translate_o(n):
Expand All @@ -1349,8 +1347,8 @@ def translate_v(n):
else:
return n // 2 + no

t2 = np.zeros((2*no, 2*no, 2*nv, 2*nv))
for i, j, k, l in product(range(2*no), range(2*no), range(2*nv), range(2*nv)):
t2 = np.zeros((2 * no, 2 * no, 2 * nv, 2 * nv))
for i, j, k, l in product(range(2 * no), range(2 * no), range(2 * nv), range(2 * nv)):
# spin not conserved
if i % 2 != k % 2 or j % 2 != l % 2:
continue
Expand Down Expand Up @@ -1398,23 +1396,32 @@ def _compute_e_diff(r, s, b, a, int1e, int2e, n_orb, no):
new_a.append(i % n_orb)

diag1e = np.diag(int1e)
diagj = np.einsum('iijj->ij', int2e)
diagk = np.einsum('ijji->ij', int2e)
diagj = np.einsum("iijj->ij", int2e)
diagk = np.einsum("ijji->ij", int2e)

e_diff_1e = diag1e[new_a].sum() + diag1e[new_b].sum() - diag1e[old_a].sum() - diag1e[old_b].sum()
e_diff_j = _compute_j_outer(diagj, inert_a, inert_b, new_a, new_b) - _compute_j_outer(diagj, inert_a, inert_b, old_a, old_b)
e_diff_k = _compute_k_outer(diagk, inert_a, inert_b, new_a, new_b) - _compute_k_outer(diagk, inert_a, inert_b, old_a, old_b)
# fmt: off
e_diff_j = _compute_j_outer(diagj, inert_a, inert_b, new_a, new_b) \
- _compute_j_outer(diagj, inert_a, inert_b, old_a, old_b)
e_diff_k = _compute_k_outer(diagk, inert_a, inert_b, new_a, new_b) \
- _compute_k_outer(diagk, inert_a, inert_b, old_a, old_b)
# fmt: on
return e_diff_1e + 1 / 2 * (e_diff_j - e_diff_k)


def _compute_j_outer(diagj, inert_a, inert_b, outer_a, outer_b):
# fmt: off
v = diagj[inert_a][:, outer_a].sum() + diagj[outer_a][:, inert_a].sum() + diagj[outer_a][:, outer_a].sum() \
+ diagj[inert_a][:, outer_b].sum() + diagj[outer_a][:, inert_b].sum() + diagj[outer_a][:, outer_b].sum() \
+ diagj[inert_b][:, outer_a].sum() + diagj[outer_b][:, inert_a].sum() + diagj[outer_b][:, outer_a].sum() \
+ diagj[inert_b][:, outer_b].sum() + diagj[outer_b][:, inert_b].sum() + diagj[outer_b][:, outer_b].sum()
+ diagj[inert_a][:, outer_b].sum() + diagj[outer_a][:, inert_b].sum() + diagj[outer_a][:, outer_b].sum() \
+ diagj[inert_b][:, outer_a].sum() + diagj[outer_b][:, inert_a].sum() + diagj[outer_b][:, outer_a].sum() \
+ diagj[inert_b][:, outer_b].sum() + diagj[outer_b][:, inert_b].sum() + diagj[outer_b][:, outer_b].sum()
# fmt: on
return v


def _compute_k_outer(diagk, inert_a, inert_b, outer_a, outer_b):
# fmt: off
v = diagk[inert_a][:, outer_a].sum() + diagk[outer_a][:, inert_a].sum() + diagk[outer_a][:, outer_a].sum() \
+ diagk[inert_b][:, outer_b].sum() + diagk[outer_b][:, inert_b].sum() + diagk[outer_b][:, outer_b].sum()
+ diagk[inert_b][:, outer_b].sum() + diagk[outer_b][:, inert_b].sum() + diagk[outer_b][:, outer_b].sum()
# fmt: on
return v
2 changes: 1 addition & 1 deletion tests/static/test_hea.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_qiskit_circuit():

@pytest.mark.parametrize("mapping", ["jordan-wigner", "bravyi-kitaev"])
def test_mapping(mapping):
uccsd= UCCSD(h2)
uccsd = UCCSD(h2)
hea = HEA.ry(uccsd.int1e, uccsd.int2e, uccsd.n_elec, uccsd.e_core, 3, mapping=mapping)
e = hea.kernel()
np.testing.assert_allclose(e, uccsd.e_fci)
Expand Down

0 comments on commit 2ecf6b1

Please sign in to comment.