qiskit-nature
qiskit-nature copied to clipboard
Delete FermionicOp.to_matrix
What is the expected enhancement?
Apparently, this method returns a matrix representation under the Jordan-Wigner transformation, but with the qubit ordering reversed. This is pretty confusing. In any case, this can already be achieved by performing the Jordan-Wigner transform, and then reversing the qubit order, so this method is redundant. The name is also ambiguous; there are many different ways to turn a FermionicOp into a matrix (and "Jordan-Wigner transform with qubits reversed" is a peculiar one to single out).
import numpy as np
from qiskit_nature.second_q.mappers import JordanWignerMapper
from qiskit_nature.second_q.operators import FermionicOp
op = FermionicOp({"+_0": 1, "-_1": 1})
mat = op.to_matrix().todense()
jw = JordanWignerMapper().map(op).primitive
print(np.allclose(mat, jw.to_matrix(), atol=1e-8)) # prints False
for pauli in jw.paulis:
pauli.x = pauli.x[::-1]
pauli.z = pauli.z[::-1]
print(np.allclose(mat, jw.to_matrix(), atol=1e-8)) # prints True
False
True
Apparently, this method returns a matrix representation under the Jordan-Wigner transformation
This is not necessarily a correct interpretation of what this method does.
As explained in the docstring of FermionicOp.to_matrix, the method
Converts to a matrix representation over the full fermionic Fock space in the occupation number basis.
One could draw a correlation between this and the interpretation of the Jordan-Wigner mapping, but that is a conclusion, not an initial assumption.
The name is also ambiguous; there are many different ways to turn a FermionicOp into a matrix
Yes, but given my previous quote from the docstring as well as the following line quoted from the docstring, the documentation is very clear on what to expect as an output from this method.
The basis states are ordered in increasing bitstring order as 0000, 0001, ..., 1111.
This last piece of information, also informs you, why you are seeing the "reversed" ordering compared to the JW + QubitOp.to_matrix approach with which you are comparing the result. Essentially, this is an artifact of the little- vs big-endian ordering which was already discussed multiple times.
The existence of this method is useful to inspect operators in a matrix representation directly in the fermionic space. Arguably, now that our operators are sparse and therefore no longer have the endianness mismatch with Terra, this method could be updated to say:
The basis states are ordered in increasing bitstring order as 0000, 0001, ..., 1111, in little-endian notation.
Or however one can make that more clear, since this means that 0001 in little endian would be stored in a list as [1, 0, 0, 0].
Doing this, will make the to_matrix result match with what one obtains from the QubitOp.to_matrix method after applying a JW mapping.
In conclusion, I do not think that we should remove this method. However, the change above could be done.
When doing so, I would also update the method to use sparse matrices instead of scipy.sparse so that our public API does not mix two different sparse array types.
Thanks for quoting the docstring. It describes exactly the Jordan-Wigner transform with qubit ordering reversed, just in different words. In any case, the method is still redundant, so I question whether it's worth keeping around due to the maintenance burden. For example, it needs more work to support sparse matrices and parameters. If you really want to keep it around, perhaps it could be rewritten to simply use the existing Jordan-Wigner functionality. That way it would automatically benefit from improvements in those parts of the code base; for example, with #891 it would support parameters.