1

I'm trying to calculate the expectation value of a quantum operator in function of time. I obtained yes the states for every istance

t_l = tf.constant( np.linspace(0, 4 / gam_q, 1000, dtype = np.complex128) )
dt = t_l[1] - t_l[0]

H_t = tf.tensordot(- 1j * t_l, H, 0)
H_t = tfla.expm(H_t)

psi0 = tf.constant( ((qtp.fock(2, 0)).unit()).full() )

psi_t = tf.tensordot(H_t, psi0, [[2], [0]])

where H is the Hamiltonian (sigma_z in this case). However, I need to know the time dependence of the expectation value of an operator S_op. I tried this

pre = tf.tensordot( tf.math.conj(psi_t), S_op, [[1], [0]]) #psi_t was not transposed, so I put [[1], [0]]
expect = tf.tensordot(pre, psi_t, [[2], [1]])

which returns a tensor of shape (1000, 1, 1000, 1), while I need a 1D array of shape (1000).

EDIT

I solved by introducing the einsum

pre = tf.tensordot(tfmt.conj(psi_t), S_op, [[1], [0]])
expect = tfmt.real( tf.tensordot(pre, psi_t, [[2], [1]]) )
expect = tf.einsum("ijij->i", expect)

1 Answers1

1

For the benefit of community providing solution in answer section

I solved by introducing the einsum

pre = tf.tensordot(tfmt.conj(psi_t), S_op, [[1], [0]])
expect = tfmt.real( tf.tensordot(pre, psi_t, [[2], [1]]) )
expect = tf.einsum("ijij->i", expect)

(paraphrased from Alberto Mercurio)