Skip to content

Commit

Permalink
Allow empty operator initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mrossinek committed Jun 3, 2021
1 parent af1691d commit 3692340
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
8 changes: 8 additions & 0 deletions qiskit_nature/operators/second_quantization/fermionic_op.py
Expand Up @@ -177,6 +177,14 @@ def __init__(
if isinstance(data, str):
data = [(data, 1)]

if data == []:
if register_length is None:
raise TypeError(
"If you want to initialize an empty operator, you _must_ specify the length of "
"the register!"
)
data = [("I_0", 0)]

if not all(
isinstance(label, str) and isinstance(coeff, (int, float, complex))
for label, coeff in data
Expand Down
17 changes: 10 additions & 7 deletions qiskit_nature/operators/second_quantization/vibrational_op.py
Expand Up @@ -119,6 +119,16 @@ def __init__(
if isinstance(data, str):
data = [(data, 1)]

if isinstance(num_modals, int):
num_modals = [num_modals] * num_modes

self._num_modes = num_modes
self._num_modals = num_modals
self._register_length = sum(self._num_modals)

if data == []:
data = [("I" * self._register_length, 0)]

if not all(
isinstance(label, str) and isinstance(coeff, (int, float, complex))
for label, coeff in data
Expand All @@ -128,13 +138,6 @@ def __init__(
self._coeffs: np.ndarray
self._labels: List[str]

if isinstance(num_modals, int):
num_modals = [num_modals] * num_modes

self._num_modes = num_modes
self._num_modals = num_modals
self._register_length = sum(self._num_modals)

labels, coeffs = zip(*data)
self._coeffs = np.array(coeffs, np.complex128)

Expand Down
6 changes: 2 additions & 4 deletions qiskit_nature/properties/electronic_integrals.py
Expand Up @@ -92,10 +92,8 @@ def to_second_q_op(self) -> FermionicOp:
"""TODO."""
base_ops_labels = self._create_base_ops()

# TODO: allow an empty list as argument to FermionicOp
fac = 2 if self._basis != Basis.SO else 1
initial_label_with_ceoff = ("I" * fac * len(self._matrices[0]), 0)
base_ops_labels.append(initial_label_with_ceoff)
if base_ops_labels == []:
return FermionicOp([], register_length=len(self._matrices[0]))

return FermionicOp(base_ops_labels)

Expand Down

0 comments on commit 3692340

Please sign in to comment.