Equality of `SparsePauliOp`s with different orders
See original GitHub issueWhat is the expected enhancement?
Two SparsePauliOps 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
equivfor 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 Free
Top 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

I tries a dict-based
simplifywith 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.equivSo, I’m thinking of the following plan. Do you have any suggestion?SparsePauliOp.__eq__as is (entrywise equality).SparsePauliOp.equivasnp.allclose((a - b).simplify().coeffs, [0])(numerical equivalence)If necessary, I will add
rtolandatolofnp.allclosetoSparsePauliOp.equiv. FYISparsePauliOp.__eq__uses the defaultrtolandatolofnp.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.equivfor equivalence checkPauliSumOp.equalsusedSparsePauliOp.__eq__for equivalence check. It was a wrong algorithm theoretically, but it happened to work fine becauseSparsePauliOp.simplifysorted the operators (but, the API doc does not guarantee it). So I replace that part with the newSparsePauliOp.equivin #7656 because I updatedSparsePauliOp.simplifywith a faster algorithm without sort. These changes do not break unit tests and they are also consistent with the API docs.