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 of an operator is computed
- as if
x
is a ket or bra , - as if
x
is a density matrix .
Warning
The returned array is complex-valued. If the operator corresponds to a
physical observable, it is Hermitian: , 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)