# expectation_value

Graph.expectation_value(state, operator, *, name=None)

Calculate the expectation value of an operator with respect to a state.

The last dimension of the state must be equal to the last two dimensions of the operator and their batch shapes must be broadcastable.

Parameters:
• state (np.ndarray or Tensor) – The state. It must be a vector of shape (..., D).

• operator (np.ndarray or Tensor) – The operator. It must be of shape (..., D, D).

• name (str or None, optional) – The name of the node.

Returns:

The expectation value with shape (...).

Return type:

Tensor

See also

Graph.density_matrix_expectation_value

Expectation value of an operator with respect to a density matrix.

Graph.inner_product

Inner product of two vectors.

Graph.outer_product

Outer product of two vectors.

Graph.trace

Trace of an object.

Notes

The expectation value of an operator $$\mathbf{A}$$ with respect to a vector $$\mathbf{x}$$ is defined as

$\mathbf{x}^\dagger \mathbf{A} \mathbf{x} = \langle x \vert \mathbf{A} \vert x \rangle = \sum_{ij} x^\ast_{i} A_{ij} x_{j} .$

For more information about the expectation value, see expectation value on Wikipedia.

Examples

>>> graph.expectation_value(np.array([1j, 1j]), np.eye(2), name="expectation")
<Tensor: name="expectation", operation_name="expectation_value", shape=()>
>>> result = bo.execute_graph(graph=graph, output_node_names="expectation")
>>> result["output"]["expectation"]["value"]
2.+0.j
>>> graph.expectation_value(np.ones([3,1,4]), np.ones([2,4,4]), name="expectation)
<Tensor: name="expectation", operation_name="expectation_value", shape=(3, 2)>
>>> result = bo.execute_graph(graph=graph, output_node_names="expectation")
>>> result["output"]["expectation"]["value"]
array([[16, 16], [16, 16], [16, 16]])