From 1e62db939b129ceea8847adb0ed574e944c2b10c Mon Sep 17 00:00:00 2001 From: James Nelson Date: Wed, 11 Mar 2026 11:17:37 +0000 Subject: [PATCH 1/2] add --- qse/operator/operator.py | 20 ++++++++++++++++++++ qse/operator/operators.py | 11 ++++++++++- tests/qse/qbits_methods_test.py | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/qse/operator/operator.py b/qse/operator/operator.py index 509fafaf..756af3b9 100644 --- a/qse/operator/operator.py +++ b/qse/operator/operator.py @@ -47,6 +47,11 @@ def __init__(self, operator, qubits, nqbits, coef=1.0): raise Exception( "The number of passed qubits must equal the number of passed operators." ) + if max(qubits) >= 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 diff --git a/tests/qse/qbits_methods_test.py b/tests/qse/qbits_methods_test.py index 43b4cf70..8207ab4c 100644 --- a/tests/qse/qbits_methods_test.py +++ b/tests/qse/qbits_methods_test.py @@ -472,7 +472,7 @@ def test_reciprocal_cell(cell, expected): def test_compute_interaction_hamiltonian(): - """Test ompute_interaction_hamiltonian method.""" + """Test compute_interaction_hamiltonian method.""" # To create a ZZ Hamiltonian for only nearest neighbour qubits spacing = 1.0 qbits = qse.lattices.chain(spacing, 4) From dc72f78a9212a91c36e8ba4a855053b32d6b7412 Mon Sep 17 00:00:00 2001 From: James Nelson Date: Wed, 11 Mar 2026 11:18:38 +0000 Subject: [PATCH 2/2] nb --- docs/source/index.rst | 1 + .../tutorials/working_with_operators.ipynb | 274 ++++++++++++++++++ 2 files changed, 275 insertions(+) create mode 100644 docs/source/tutorials/working_with_operators.ipynb 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