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 must be of shape (..., D, D).

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

Returns

The expectation value with shape (...).

Return type

Tensor

See also

inner_product

Inner product of two vectors.

outer_product

Outer product of two vectors.

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 = qctrl.functions.calculate_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 = qctrl.functions.calculate_graph(graph=graph, output_node_names=["expectation"])
>>> result.output["expectation"]["value"]
array([[16, 16], [16, 16], [16, 16]])