Then let's translate it to Openfermmion and see if two are equal. Don't blame me for slow code. This is just a prove of concept. Currently has no plan to optimize the performance.
max_len=max(A.shape)max_power=np.ceil(np.log2(max_len)).astype(int)paulis=[]coefs=[]lookup=["I+Z","X+iY","X-iY","I-Z",]lookup_bin=[((0.5,0),(0.5,2)),((0.5,1),(0.5j,3)),((0.5,1),(-0.5j,3)),((0.5,0),(-0.5,2)),]paulis_encoding=[]OPENFERMION=Trueforl,rinnp.vstack(A.nonzero()).T.copy():coefs.append(A[l,r])ifOPENFERMION:# openfermion is counting reversely# here we reverse the binary stringifl:_str=format(l,"b")trailing_zeros=max_power-len(_str)print("l",trailing_zeros,_str)l=int(_str[::-1],base=2)<<trailing_zerosifr:_str=format(r,"b")trailing_zeros=max_power-len(_str)print("r",trailing_zeros,_str)r=int(_str[::-1],base=2)<<trailing_zeros# After this line, compatible with normal integer orderingj=0current_pauli=[]_encoding=[]# Translate coordinate from fine to coarse to Pauli Tensorwhilej<max_power:lr=((l&1)<<1)+(r&1)current_pauli.append(lookup[lr])_encoding.append(lr)l>>=1r>>=1j+=1paulis.append(current_pauli)paulis_encoding.append(_encoding)pauliscoefspaulis_encoding
Here you can see that 4 nonzero component translated into length=16 linear combination of Pauli Tensor. After aggregate, it remains 12. I wonder if there is a general algorithm to do this without the need to aggregate terms.
fromopenfermionimportQubitOperatorasQOfromopenfermionimportget_sparse_operator# Translate result to Openfermionpauli_str=("I","X","Z","Y")QO1=QO()forpauli,coefinqo.items():QO1+=QO(tuple((j,pauli_str[p])forj,pinenumerate(pauli)ifp),coef)QO1QO_openfermion_format=get_sparse_operator(QO1).todense().round(1)QO_openfermion_format
And you can see that the resulting matrix is exactly the original one. Hence, this prove that we can dump all kinds of matrix into quantum computer and do some computations!