Equality of `SparsePauliOp`s with different orders
See original GitHub issueWhat is the expected enhancement?
Two SparsePauliOp
s with different orders are judges as “not equal” as follows.
from qiskit.quantum_info import SparsePauliOp
a = SparsePauliOp.from_list([('X', 1), ('Y', 2)])
b = SparsePauliOp.from_list([('Y', 2), ('X', 1)])
print(a == b)
output
False
Is it better to return True
for this comparison or mention the order dependency in the docsting of __eq__
?
@ikkoham also told me another case as follow. A redundant operator (coeff is 0) makes the comparison “not equal”.
from qiskit.quantum_info import SparsePauliOp
a = SparsePauliOp.from_list([('X', 1)])
b = SparsePauliOp.from_list([('X', 1), ('Y', 0)])
print(a == b)
False
If we need to compare two operator with different order and/or redundant operators, I think we need to call as follows. Is there any simple alternatives?
np.allclose((a - b).simplify().coeffs, [0])
Update
I updated SparsePauliOp
in #7656 as follows.
- Leave
__eq__
as is (elementary-wise comparison) - Add
equiv
for equivalence check
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
qiskit.quantum_info.SparsePauliOp
It can be used for performing operator arithmetic for hundred of qubits if the number of non-zero Pauli basis terms is sufficiently small....
Read more >arXiv:2210.15438v1 [quant-ph] 25 Oct 2022
Therefore in order to have a well functioning VQE one also need a sufficient optimiser. There is a plethora of different classical optimisers ......
Read more >Clarification on the usage of class UCC of qiskit_nature
This I believe is due to the reps parameter of UCC which by default is equal to one. Then,. 4. Why is there...
Read more >Qiskit Pocket Guide
The draw() method draws a quantum circuit in various formats. ... Obtains the list of classical bits in the order that the registers...
Read more >Newest 'qiskit' Questions - Stack Overflow
If I have two independent (unentangled) qubits, let's say one in state |1> and the other one in some superposition state with equal...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I tries a dict-based
simplify
with rust and it succeeded to improve the performance. So, I want to move it forward. Then, I need to deal with this issue #7657 to pass the unit test. As Julien suggested, there is a method to check the equivalenceequiv
, e.g.,quantum_info.Pauli.equiv
So, I’m thinking of the following plan. Do you have any suggestion?SparsePauliOp.__eq__
as is (entrywise equality).SparsePauliOp.equiv
asnp.allclose((a - b).simplify().coeffs, [0])
(numerical equivalence)If necessary, I will add
rtol
andatol
ofnp.allclose
toSparsePauliOp.equiv
. FYISparsePauliOp.__eq__
uses the defaultrtol
andatol
ofnp.allclose
. https://github.com/Qiskit/qiskit-terra/blob/77219b5c7b7146b1545c5e5190739b36f4064b2f/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py#L133I made #7656 so that it won’t break changes The summary is
SparsePauliOp.__eq__
as is (elementary-wise comparison)SparsePauliOp.equiv
for equivalence checkPauliSumOp.equals
usedSparsePauliOp.__eq__
for equivalence check. It was a wrong algorithm theoretically, but it happened to work fine becauseSparsePauliOp.simplify
sorted the operators (but, the API doc does not guarantee it). So I replace that part with the newSparsePauliOp.equiv
in #7656 because I updatedSparsePauliOp.simplify
with a faster algorithm without sort. These changes do not break unit tests and they are also consistent with the API docs.