diff --git a/docs/source/index.rst b/docs/source/index.rst index 5d9b5dcb..63eadfc9 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -31,6 +31,7 @@ See the `contributing page = nqbits: + raise Exception( + "One or more of the qubits indicies is out of range. " + "They must each be less than nqbits." + ) self.operator = operator self.qubits = qubits @@ -90,6 +95,21 @@ def __repr__(self): [f"{op}{q}" for op, q in zip(self.operator, self.qubits)] ) + def copy(self): + """ + Return a copy. + """ + return self.__class__(self.operator, self.qubits, self.nqbits, self.coef) + + def __mul__(self, other): + op = self.copy() + op *= other + return op + + def __imul__(self, other): + self.coef *= float(other) + return self + def _check_operator(op_list): for op in op_list: diff --git a/qse/operator/operators.py b/qse/operator/operators.py index 0c5a5184..8bf9a5ab 100644 --- a/qse/operator/operators.py +++ b/qse/operator/operators.py @@ -69,7 +69,8 @@ def extend(self, other): other: Operators or Operator The operators to be added to the current object. """ - if other.nqbits != self.nqbits: + # We only run this check when nterms > 0 + if (other.nqbits != self.nqbits) and self.nterms: raise Exception("other must have the same number of qubits.") if isinstance(other, Operator): @@ -79,6 +80,12 @@ def extend(self, other): else: raise Exception("other must be Operators or Operator.") + def copy(self): + """ + Return a copy. + """ + return self.__class__(self.operator_list) + def __add__(self, other): op = self.copy() op += other @@ -100,6 +107,8 @@ def __getitem__(self, i): @property def nqbits(self): + if self.nterms == 0: + return 0 return self[0].nqbits @property