-
Notifications
You must be signed in to change notification settings - Fork 392
Added functionality to compute one-norm of qubit/majorana Hamiltonian from molecular integrals #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
938cb45
1dae314
62e86ce
c203f67
a0a16a9
96deaff
120f4e2
2b91f2d
8f130e4
5757cce
1b5056c
cd9bba0
7e2b5e8
1c2a000
9ca67fa
dcaeb56
693c841
c58df07
7507d9c
ab9352d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# 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 | ||
# | ||
# http://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. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Function to calculate the 1-Norm of a molecular Hamiltonian after | ||
fermion-to-qubit transformation from a MolecularData class. | ||
See https://arxiv.org/abs/2103.14753 | ||
""" | ||
|
||
import numpy as np | ||
|
||
from openfermion import MolecularData | ||
|
||
|
||
def get_one_norm(mol_or_int, return_constant=True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be explicit with inputs. Use default None and allow the user to specify. This partially documents the function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will also give you freedom to calculate norms for 1-body ops only. or 2-body only. Is there a difficulty extending this to arbitrary interactions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm, this is only valid in the first place for two-body tensors which have real orbital symmetries. I'm not sure how to extend this, as an n-body interaction will not be a molecular system. There is probably a way to write an arbitrary n-body Fermionic Hamiltonian in Majorana representation in terms of the original coefficients though? (I just wouldn't know how) |
||
r""" | ||
|
||
Returns the 1-Norm of the Hamiltonian described in | ||
https://arxiv.org/abs/2103.14753 after a fermion-to-qubit | ||
transformation given nuclear constant, one-body (2D np.array) | ||
and two-body (4D np.array) integrals. | ||
|
||
Parameters | ||
---------- | ||
|
||
mol_or_int : Tuple of (constant, one_body_integrals, two_body_integrals) | ||
constant : Nuclear repulsion or adjustment to constant shift in Hamiltonian | ||
from integrating out core orbitals | ||
one_body_integrals : An array of the one-electron integrals having | ||
shape of (n_orb, n_orb). | ||
two_body_integrals : An array of the two-electron integrals having | ||
shape of (n_orb, n_orb, n_orb, n_orb). | ||
|
||
-----OR---- | ||
|
||
mol_or_int : MolecularData class object | ||
|
||
----------- | ||
|
||
return_constant (optional) : If False, do not return the constant term in | ||
in the (majorana/qubit) Hamiltonian | ||
|
||
Returns | ||
------- | ||
one_norm : 1-Norm of the Qubit Hamiltonian | ||
""" | ||
if isinstance(mol_or_int, MolecularData): | ||
return _get_one_norm_mol(mol_or_int, return_constant) | ||
else: | ||
if return_constant: | ||
constant, one_body_integrals, two_body_integrals = mol_or_int | ||
return _get_one_norm(constant, one_body_integrals, | ||
two_body_integrals) | ||
else: | ||
_, one_body_integrals, two_body_integrals = mol_or_int | ||
return _get_one_norm_woconst(one_body_integrals, two_body_integrals) | ||
|
||
|
||
def _get_one_norm_mol(molecule, return_constant): | ||
if return_constant: | ||
return _get_one_norm(molecule.nuclear_repulsion, | ||
molecule.one_body_integrals, | ||
molecule.two_body_integrals) | ||
else: | ||
return _get_one_norm_woconst(molecule.one_body_integrals, | ||
molecule.two_body_integrals) | ||
|
||
|
||
def _get_one_norm(constant, one_body_integrals, two_body_integrals): | ||
n_orb = one_body_integrals.shape[0] | ||
|
||
htilde = constant | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be htilde = abs(constant), right? Otherwise we can wind up with negative 1 norms... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it is actually correct like this. The reason is that you have in principle two (or three if you consider an active space) contributions to the constant term in majorana operators: The nuclear repulsion (always positive), the mean field energy of the frozen orbitals if you consider a CAS (always negative) and the ((1 / 2 * g_[ppqq]) - (1 / 4 *g_[pqpq])) term from rearranging the majorana operators. It is important to actually take the absolute value after counting them up together as they can cancel eachother. I take the absolute value of htilde on the line below, so the 1-norm can never be negative. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok, thanks for the clarification! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no I think this goes back to the comment I made in the "issue" about this. It depends on how you are defining and accessing the LCU. There is a way to represent the LCU such that what @Emieeel is saying is true. And it is a clever trick that he's pointed out there. But under the simplest way to simulate the LCU, you don't get that subtraction from using the Majorana operators. |
||
for p in range(n_orb): | ||
htilde += one_body_integrals[p, p] | ||
for q in range(n_orb): | ||
htilde += ((1 / 2 * two_body_integrals[p, q, q, p]) - | ||
(1 / 4 * two_body_integrals[p, q, p, q])) | ||
|
||
htildepq = np.zeros(one_body_integrals.shape) | ||
for p in range(n_orb): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ncrubin will know this better than I, but for the purposes of speed could this possibly be improved by using np.einsum? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the restriction on the sums I didn't know how to employ einsum here. There could be a way to do that, it would certainly make it faster... In the calculations of the paper I actually used numba http://numba.pydata.org/ to speed it up, which helped a lot, but I didn't include it here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not antisymmeterize the integrals and then do the summation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also it seems like there is no spin-function information. |
||
for q in range(n_orb): | ||
htildepq[p, q] = one_body_integrals[p, q] | ||
for r in range(n_orb): | ||
htildepq[p, q] += ((two_body_integrals[p, r, r, q]) - | ||
(1 / 2 * two_body_integrals[p, r, q, r])) | ||
|
||
one_norm = abs(htilde) + np.sum(np.absolute(htildepq)) | ||
|
||
for p in range(n_orb): | ||
for q in range(n_orb): | ||
for r in range(n_orb): | ||
for s in range(n_orb): | ||
if p > q and r > s: | ||
one_norm += 1 / 2 * abs(two_body_integrals[p, q, r, s] - | ||
two_body_integrals[p, q, s, r]) | ||
one_norm += 1 / 4 * abs(two_body_integrals[p, q, r, s]) | ||
|
||
return one_norm | ||
|
||
|
||
def _get_one_norm_woconst(one_body_integrals, two_body_integrals): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still needs a doc string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given my above comments, I think this function should be exposed to the user, in which case the docstring from 'get_one_norm_int' can be copied down with minor adjustments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, in quantum computing 1-norms should almost always be reported without the constant term. I'd suggest calling this function 'get_one_norm', and making it clear in the docs that the constant term is not added. ( @ncrubin , let me know if you have worries about this.) |
||
n_orb = one_body_integrals.shape[0] | ||
|
||
htildepq = np.zeros(one_body_integrals.shape) | ||
for p in range(n_orb): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. n_orbs or spin-orbitals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is all in spatial orbital basis There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So can you add this as a doc string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last thing. See above. Just document input with "parameters" like the other functions since this is now a public function. |
||
for q in range(n_orb): | ||
htildepq[p, q] = one_body_integrals[p, q] | ||
for r in range(n_orb): | ||
htildepq[p, q] += ((two_body_integrals[p, r, r, q]) - | ||
(1 / 2 * two_body_integrals[p, r, q, r])) | ||
|
||
one_norm = np.sum(np.absolute(htildepq)) | ||
|
||
for p in range(n_orb): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use itertools product or convert to contraction over antisymmeterized integrals. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converted to contraction over antisymmetrized integrals. Thank you for the suggestion! |
||
for q in range(n_orb): | ||
for r in range(n_orb): | ||
for s in range(n_orb): | ||
if p > q and r > s: | ||
one_norm += 1 / 2 * abs(two_body_integrals[p, q, r, s] - | ||
two_body_integrals[p, q, s, r]) | ||
one_norm += 1 / 4 * abs(two_body_integrals[p, q, r, s]) | ||
return one_norm |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 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 | ||
# | ||
# http://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. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Tests for get_one_norm.""" | ||
|
||
import os | ||
import pytest | ||
from openfermion import get_one_norm, MolecularData, jordan_wigner | ||
from openfermion.config import DATA_DIRECTORY | ||
|
||
filename = os.path.join(DATA_DIRECTORY, "H1-Li1_sto-3g_singlet_1.45.hdf5") | ||
molecule = MolecularData(filename=filename) | ||
molecular_hamiltonian = molecule.get_molecular_hamiltonian() | ||
qubit_hamiltonian = jordan_wigner(molecular_hamiltonian) | ||
|
||
|
||
def test_one_norm_from_molecule(): | ||
assert qubit_hamiltonian.induced_norm() == pytest.approx( | ||
get_one_norm(molecule)) | ||
|
||
|
||
def test_one_norm_from_ints(): | ||
ints = (molecule.nuclear_repulsion, molecule.one_body_integrals, | ||
molecule.two_body_integrals) | ||
assert qubit_hamiltonian.induced_norm() == pytest.approx(get_one_norm(ints)) | ||
|
||
|
||
def test_one_norm_woconst(): | ||
one_norm_woconst = (qubit_hamiltonian.induced_norm() - | ||
abs(qubit_hamiltonian.constant)) | ||
ints = (molecule.nuclear_repulsion, molecule.one_body_integrals, | ||
molecule.two_body_integrals) | ||
assert one_norm_woconst == pytest.approx( | ||
get_one_norm(molecule, return_constant=False)) | ||
assert one_norm_woconst == pytest.approx( | ||
get_one_norm(ints, return_constant=False)) |
Uh oh!
There was an error while loading. Please reload this page.