2

I am trying to compute the 2-electron reduced-density matrix (2-RDM) for a given quantum state with openfermion. The code is as follow

two_rdm = np.zeros((n_qubits,) * 4, dtype=complex)
psi =  = openfermion.haar_random_vector(2 ** n_qubits, random_seed).astype(numpy.complex64)
for i, j, k, l in itertools.product(range(n_qubits), repeat=4):
    transformed_operator = jordan_wigner(
            FermionOperator(((i, 1), (j, 1), (k, 0), (l, 0))))
    two_rdm[i, j, k, l] = openfermion.linalg.expectation(transformed_operator, psi)

The problem is with the last line, which gives the following exception

TypeError: can only concatenate str (not "ABCMeta") to str

After examning the source code, I figured out that the problem is with the [operator * state][1]. Can anybody help to fix this?

ironmanaudi
  • 809
  • 4
  • 10

1 Answers1

1

From the openfermion documentation (see here), it says the operator must be a scipy.sparse matrix. So, replacing transformed_operator with get_sparse_matrix(transformed_operator, n_qubits=n_qubits) in the final line will give you a 2**n_qubits x 2**n_qubits dimensional sparse matrix, which should remove the error.

Solarflare0
  • 528
  • 2
  • 7