Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions src/openfermion/chem/molecular_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ def geometry_from_file(file_name):
def antisymtei(two_body_integrals):
"""
Args:
two_body_integrals : Numpy array of two-electron integrals with OpenFermion
Ordering.
two_body_integrals: Numpy array of two-electron integrals with
OpenFermion Ordering.

Returns:
antisymints : Numpy array of anti-symmetrized integrals
<ij||kl> = <ij|kl> - <ij|lk> (physicist ordering).
antisymints : Numpy array of anti-symmetrized integrals
<ij||kl> = <ij|kl> - <ij|lk> (physicist ordering).
"""
symints = numpy.copy(two_body_integrals.transpose(0, 1, 3, 2), order='C')
antisymints = symints - two_body_integrals
Expand All @@ -339,12 +339,12 @@ def antisymtei(two_body_integrals):
def j_mat(two_body_integrals):
"""
Args:
two_body_integrals : Numpy array of two-electron integrals with OpenFermion
Ordering.
two_body_integrals: Numpy array of two-electron integrals with
OpenFermion Ordering.

Returns:
j_matr : Numpy array of the coulomb integrals J_{p,q} = (pp|qq)
(in chemist notation).
j_matr : Numpy array of the coulomb integrals J_{p,q} = (pp|qq)
(in chemist notation).
"""
chem_ordering = numpy.copy(two_body_integrals.transpose(0, 3, 1, 2), order='C')
return numpy.einsum('iijj -> ij', chem_ordering)
Expand All @@ -353,12 +353,12 @@ def j_mat(two_body_integrals):
def k_mat(two_body_integrals):
"""
Args:
two_body_integrals : Numpy array of two-electron integrals with OpenFermion
Ordering.
two_body_integrals: Numpy array of two-electron integrals with
OpenFermion Ordering.

Returns:
k_matr : Numpy array of the exchange integrals K_{p,q} = (pq|qp)
(in chemist notation).
k_matr : Numpy array of the exchange integrals K_{p,q} = (pq|qp)
(in chemist notation).
"""
chem_ordering = numpy.copy(two_body_integrals.transpose(0, 3, 1, 2), order='C')
return numpy.einsum('ijji -> ij', chem_ordering)
Expand Down Expand Up @@ -391,9 +391,9 @@ def spinorb_from_spatial(one_body_integrals, two_body_integrals):
two_body_coefficients[2 * p, 2 * q, 2 * r, 2 * s] = two_body_integrals[
p, q, r, s
]
two_body_coefficients[
2 * p + 1, 2 * q + 1, 2 * r + 1, 2 * s + 1
] = two_body_integrals[p, q, r, s]
two_body_coefficients[2 * p + 1, 2 * q + 1, 2 * r + 1, 2 * s + 1] = (
two_body_integrals[p, q, r, s]
)

# Truncate.
one_body_coefficients[numpy.absolute(one_body_coefficients) < EQ_TOLERANCE] = 0.0
Expand Down Expand Up @@ -559,7 +559,7 @@ def __init__(
self.init_lazy_properties()

def init_lazy_properties(self):
"""Initializes properties loaded on demand to None"""
"""Initializes properties loaded on demand to None."""

# Molecular orbitals
self._canonical_orbitals = None
Expand Down Expand Up @@ -708,27 +708,27 @@ def save(self):
# Save geometry (atoms and positions need to be separate):
d_geom = f.create_group("geometry")
if not isinstance(self.geometry, basestring):
atoms = [numpy.string_(item[0]) for item in self.geometry]
atoms = [numpy.bytes_(item[0]) for item in self.geometry]
positions = numpy.array([list(item[1]) for item in self.geometry])
else:
atoms = numpy.string_(self.geometry)
atoms = numpy.bytes_(self.geometry)
positions = None
d_geom.create_dataset("atoms", data=(atoms if atoms is not None else False))
d_geom.create_dataset("positions", data=(positions if positions is not None else False))
# Save basis:
f.create_dataset("basis", data=numpy.string_(self.basis))
f.create_dataset("basis", data=numpy.bytes_(self.basis))
# Save multiplicity:
f.create_dataset("multiplicity", data=self.multiplicity)
# Save charge:
f.create_dataset("charge", data=self.charge)
# Save description:
f.create_dataset("description", data=numpy.string_(self.description))
f.create_dataset("description", data=numpy.bytes_(self.description))
# Save name:
f.create_dataset("name", data=numpy.string_(self.name))
f.create_dataset("name", data=numpy.bytes_(self.name))
# Save n_atoms:
f.create_dataset("n_atoms", data=self.n_atoms)
# Save atoms:
f.create_dataset("atoms", data=numpy.string_(self.atoms))
f.create_dataset("atoms", data=numpy.bytes_(self.atoms))
# Save protons:
f.create_dataset("protons", data=self.protons)
# Save n_electrons:
Expand Down Expand Up @@ -824,7 +824,7 @@ def save(self):
key_list = list(self.general_calculations.keys())
f.create_dataset(
"general_calculations_keys",
data=([numpy.string_(key) for key in key_list] if len(key_list) > 0 else False),
data=([numpy.bytes_(key) for key in key_list] if len(key_list) > 0 else False),
)
f.create_dataset(
"general_calculations_values",
Expand Down Expand Up @@ -915,7 +915,7 @@ def load(self):
self.general_calculations = None # pragma: nocover

def get_from_file(self, property_name):
"""Helper routine to re-open HDF5 file and pull out single property
"""Helper routine to re-open HDF5 file and pull out single property.

Args:
property_name: Property name to load from self.filename
Expand Down Expand Up @@ -963,7 +963,7 @@ def get_integrals(self):
return self.one_body_integrals, self.two_body_integrals

def get_active_space_integrals(self, occupied_indices=None, active_indices=None):
"""Restricts a molecule at a spatial orbital level to an active space
"""Restricts a molecule at a spatial orbital level to an active space.

This active space may be defined by a list of active indices and
doubly occupied indices. Note that one_body_integrals and
Expand Down Expand Up @@ -1021,11 +1021,9 @@ def get_molecular_hamiltonian(self, occupied_indices=None, active_indices=None):
one_body_integrals, two_body_integrals = self.get_integrals()
constant = self.nuclear_repulsion
else:
(
core_adjustment,
one_body_integrals,
two_body_integrals,
) = self.get_active_space_integrals(occupied_indices, active_indices)
(core_adjustment, one_body_integrals, two_body_integrals) = (
self.get_active_space_integrals(occupied_indices, active_indices)
)
constant = self.nuclear_repulsion + core_adjustment

one_body_coefficients, two_body_coefficients = spinorb_from_spatial(
Expand Down
10 changes: 5 additions & 5 deletions src/openfermion/circuits/gates/common_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.
import numpy as np
import pytest
from scipy.linalg import expm, kron
from scipy.linalg import expm
import cirq

import openfermion
Expand Down Expand Up @@ -84,8 +84,8 @@ def test_compare_ryxxy_to_cirq_equivalent(rads):
def test_rxxyy_unitary(rads):
X = np.array([[0, 1], [1, 0]])
Y = np.array([[0, -1j], [1j, 0]])
XX = kron(X, X)
YY = kron(Y, Y)
XX = np.kron(X, X)
YY = np.kron(Y, Y)
np.testing.assert_allclose(
cirq.unitary(openfermion.Rxxyy(rads)), expm(-1j * rads * (XX + YY) / 2), atol=1e-8
)
Expand All @@ -97,8 +97,8 @@ def test_rxxyy_unitary(rads):
def test_ryxxy_unitary(rads):
X = np.array([[0, 1], [1, 0]])
Y = np.array([[0, -1j], [1j, 0]])
YX = kron(Y, X)
XY = kron(X, Y)
YX = np.kron(Y, X)
XY = np.kron(X, Y)
np.testing.assert_allclose(
cirq.unitary(openfermion.Ryxxy(rads)), expm(-1j * rads * (YX - XY) / 2), atol=1e-8
)
Expand Down
14 changes: 10 additions & 4 deletions src/openfermion/ops/operators/binary_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# 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.
""" Binary code class for Fermion-qubit mappings (arXiv:1712.07067) """
"""Binary code class for Fermion-qubit mappings (arXiv:1712.07067)."""

import copy

Expand Down Expand Up @@ -45,7 +45,7 @@ def shift_decoder(decoder, shift_constant):


def double_decoding(decoder_1, decoder_2):
"""Concatenates two decodings
"""Concatenates two decodings.

Args:
decoder_1 (iterable): list of BinaryPolynomial
Expand Down Expand Up @@ -215,6 +215,7 @@ def __add__(self, appendix):

def __imul__(self, factor):
"""In-place code concatenation or appendage via *= .

Multiplication with integer will yield appendage, otherwise
concatenation.

Expand Down Expand Up @@ -258,7 +259,8 @@ def __imul__(self, factor):
return self

def __mul__(self, factor):
"""Concatenation of two codes or appendage the same code factor times
"""Concatenation of two codes or appendage the same code factor times.

in case of integer factor.

Args:
Expand Down Expand Up @@ -293,7 +295,11 @@ def __rmul__(self, factor):

def __str__(self):
"""Return an easy-to-read string representation."""
string_return = [list(map(list, self.encoder.toarray()))]

def convert_to_native(value):
return getattr(value, "tolist", lambda: value)()

string_return = [list(map(list, convert_to_native(self.encoder.toarray())))]

dec_str = '['
for term in self.decoder:
Expand Down
6 changes: 4 additions & 2 deletions src/openfermion/ops/representations/interaction_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ def get_tensors_from_integrals(one_body_integrals, two_body_integrals):
n_qubits = 2 * one_body_integrals.shape[0]

# Initialize Hamiltonian coefficients.
one_body_coefficients = numpy.zeros((n_qubits, n_qubits))
two_body_coefficients = numpy.zeros((n_qubits, n_qubits, n_qubits, n_qubits))
one_dtype = one_body_integrals.dtype
two_dtype = two_body_integrals.dtype
one_body_coefficients = numpy.zeros((n_qubits, n_qubits), dtype=one_dtype)
two_body_coefficients = numpy.zeros((n_qubits, n_qubits, n_qubits, n_qubits), dtype=two_dtype)
# Loop through integrals.
for p in range(n_qubits // 2):
for q in range(n_qubits // 2):
Expand Down
Loading