From eaf968954425cf5983abdafdd6be5392330548c3 Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Thu, 30 Jun 2022 12:01:56 -0400 Subject: [PATCH 1/2] add atol parameter to SparsePauliOp.equiv --- .../quantum_info/operators/symplectic/sparse_pauli_op.py | 8 +++++--- .../notes/sparse-pauli-equiv-atol-58f5dfe7f39b70ee.yaml | 4 ++++ .../operators/symplectic/test_sparse_pauli_op.py | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/sparse-pauli-equiv-atol-58f5dfe7f39b70ee.yaml diff --git a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py index 93416dbb80bd..ea54d93a9738 100644 --- a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py +++ b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py @@ -15,7 +15,7 @@ from collections import defaultdict from numbers import Number -from typing import Dict +from typing import Dict, Optional import numpy as np import retworkx as rx @@ -139,7 +139,7 @@ def __eq__(self, other): and self.paulis == other.paulis ) - def equiv(self, other): + def equiv(self, other, atol: Optional[float] = None): """Check if two SparsePauliOp operators are equivalent. Args: @@ -150,7 +150,9 @@ def equiv(self, other): """ if not super().__eq__(other): return False - return np.allclose((self - other).simplify().coeffs, [0]) + if atol is None: + atol = self.atol + return np.allclose((self - other).simplify().coeffs, 0.0, atol=atol) @property def settings(self) -> Dict: diff --git a/releasenotes/notes/sparse-pauli-equiv-atol-58f5dfe7f39b70ee.yaml b/releasenotes/notes/sparse-pauli-equiv-atol-58f5dfe7f39b70ee.yaml new file mode 100644 index 000000000000..2652bc45f8e9 --- /dev/null +++ b/releasenotes/notes/sparse-pauli-equiv-atol-58f5dfe7f39b70ee.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Adds `atol` parameter to :meth:`.SparsePauliOp.equiv`. \ No newline at end of file diff --git a/test/python/quantum_info/operators/symplectic/test_sparse_pauli_op.py b/test/python/quantum_info/operators/symplectic/test_sparse_pauli_op.py index 5f1ef8f89200..4a7338f3345c 100644 --- a/test/python/quantum_info/operators/symplectic/test_sparse_pauli_op.py +++ b/test/python/quantum_info/operators/symplectic/test_sparse_pauli_op.py @@ -592,6 +592,13 @@ def test_equiv(self, num_qubits): if not spp_op2.equiv(zero): self.assertFalse(spp_op2.equiv(spp_op2 + spp_op2)) + def test_equiv_atol(self): + """Test equiv method with atol.""" + op1 = SparsePauliOp.from_list([("X", 1), ("Y", 2)]) + op2 = op1 + 1e-7 * SparsePauliOp.from_list([("I", 1)]) + self.assertFalse(op1.equiv(op2)) + self.assertTrue(op1.equiv(op2, atol=1e-7)) + def test_eq_equiv(self): """Test __eq__ and equiv methods with some specific cases.""" with self.subTest("shuffled"): From 433d494167f16df922efe1380b408a8f0903deba Mon Sep 17 00:00:00 2001 From: "Kevin J. Sung" Date: Thu, 30 Jun 2022 14:47:48 -0400 Subject: [PATCH 2/2] lint --- qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py index ea54d93a9738..d7e9cfe6fb25 100644 --- a/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py +++ b/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py @@ -144,6 +144,7 @@ def equiv(self, other, atol: Optional[float] = None): Args: other (SparsePauliOp): an operator object. + atol: Absolute numerical tolerance for checking equivalence. Returns: bool: True if the operator is equivalent to ``self``.