qml.math.expectation_value¶
-
expectation_value
(operator_matrix, state_vector, check_state=False, check_operator=False, c_dtype='complex128')[source]¶ Compute the expectation value of an operator with respect to a pure state.
The expectation value is the probabilistic expected result of an experiment. Given a pure state, i.e., a state which can be represented as a single vector \(\ket{\psi}\) in the Hilbert space, the expectation value of an operator \(A\) can computed as
\[\langle A \rangle_\psi = \bra{\psi} A \ket{\psi}\]- Parameters
operator_matrix (tensor_like) – operator matrix with shape
(2**N, 2**N)
or(batch_dim, 2**N, 2**N)
.state_vector (tensor_like) – state vector with shape
(2**N)
or(batch_dim, 2**N)
.check_state (bool) – if True, the function will check the validity of the state vector via its shape and the norm.
check_operator (bool) – if True, the function will check the validity of the operator via its shape and whether it is hermitian.
c_dtype (str) – complex floating point precision type.
- Returns
Expectation value of the operator for the state vector.
- Return type
float
Example
The expectation value for any operator can obtained by passing their matrix representation as an argument. For example, for a 2 qubit state, we can compute the expectation value of the operator \(Z \otimes I\) as
>>> import pennylane as qml >>> import numpy as np >>> state_vector = [1 / np.sqrt(2), 0, 1 / np.sqrt(2), 0] >>> operator_matrix = qml.matrix(qml.PauliZ(0), wire_order=[0, 1]) >>> qml.math.expectation_value(operator_matrix, state_vector) tensor(-2.23711432e-17+0.j, requires_grad=True)
See also