question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Equality of `SparsePauliOp`s with different orders

See original GitHub issue

What 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 equiv for equivalence check

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
t-imamichicommented, Mar 7, 2022

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 equivalence equiv, e.g., quantum_info.Pauli.equiv So, I’m thinking of the following plan. Do you have any suggestion?

  • Leave SparsePauliOp.__eq__ as is (entrywise equality).
  • Add SparsePauliOp.equiv as np.allclose((a - b).simplify().coeffs, [0]) (numerical equivalence)

If necessary, I will add rtol and atol of np.allclose to SparsePauliOp.equiv. FYI SparsePauliOp.__eq__ uses the default rtol and atol of np.allclose. https://github.com/Qiskit/qiskit-terra/blob/77219b5c7b7146b1545c5e5190739b36f4064b2f/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py#L133

1reaction
t-imamichicommented, Mar 14, 2022

I made #7656 so that it won’t break changes The summary is

  • Leave SparsePauliOp.__eq__ as is (elementary-wise comparison)
  • Add SparsePauliOp.equiv for equivalence check

PauliSumOp.equals used SparsePauliOp.__eq__ for equivalence check. It was a wrong algorithm theoretically, but it happened to work fine because SparsePauliOp.simplify sorted the operators (but, the API doc does not guarantee it). So I replace that part with the new SparsePauliOp.equiv in #7656 because I updated SparsePauliOp.simplify with a faster algorithm without sort. These changes do not break unit tests and they are also consistent with the API docs.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found