Skip to content

dq.expect

expect(O: QArrayLike, x: QArrayLike) -> Array

Returns the expectation value of an operator or list of operators on a ket, bra or density matrix.

The expectation value O\braket{O} of an operator OO is computed

  • as O=ψOψ\braket{O}=\braket{\psi|O|\psi} if x is a ket ψ\ket\psi or bra ψ\bra\psi,
  • as O=Tr ⁣[Oρ]\braket{O}=\tr{O\rho} if x is a density matrix ρ\rho.
Warning

The returned array is complex-valued. If the operator OO corresponds to a physical observable, it is Hermitian: O=OO^\dag=O, and the expectation value is real. One can then keep only the real values of the returned array using dq.expect(O, x).real.

Parameters

  • O (qarray-like of shape (nO?, n, n))

    Arbitrary operator or list of nO operators.

  • x (qarray-like of shape (..., n, 1) or (..., 1, n) or (..., n, n))

    Ket, bra or density matrix.

Returns

(array of shape (nO?, ...)) Complex-valued expectation value.

Examples

>>> O = dq.number(16)
>>> psi = dq.coherent(16, 2.0)
>>> dq.expect(O, psi)
Array(4.+0.j, dtype=complex64)
>>> psis = [dq.fock(16, i) for i in range(5)]
>>> dq.expect(O, psis).shape
(5,)
>>> Os = [dq.position(16), dq.momentum(16)]
>>> dq.expect(Os, psis).shape
(2, 5)