Skip to content

dq.expect

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

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

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

  • as \(\braket{O}=\braket{\psi|O|\psi}\) if x is a ket \(\ket\psi\) or bra \(\bra\psi\),
  • as \(\braket{O}=\tr{O\rho}\) if x is a density matrix \(\rho\).
Warning

The returned array is complex-valued. If the operator \(O\) corresponds to a physical observable, it is Hermitian: \(O^\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 (array_like of shape (nO?, n, n))

    Arbitrary operator or list of nO operators.

  • x (array_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.

Raises

  • ValueError

    If x is not a ket, bra or density matrix.

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)