# expectation_value

Source: https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/expectation_value

```python
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*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Tensor.md#boulderopal.graph.Tensor)) – The state. It must be a vector of shape `(..., D)`.
* **operator** (*np.ndarray* *or* [*Tensor*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Tensor.md#boulderopal.graph.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](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Tensor.md#boulderopal.graph.Tensor)

### SEE ALSO
[`Graph.density_matrix_expectation_value`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/density_matrix_expectation_value.md#boulderopal.graph.Graph.density_matrix_expectation_value)
: Expectation value of an operator with respect to a density matrix.

[`Graph.inner_product`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/inner_product.md#boulderopal.graph.Graph.inner_product)
: Inner product of two vectors.

[`Graph.outer_product`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/outer_product.md#boulderopal.graph.Graph.outer_product)
: Outer product of two vectors.

[`Graph.trace`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/trace.md#boulderopal.graph.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](https://en.wikipedia.org/wiki/Expectation_value_(quantum_mechanics)) on Wikipedia.

## Examples

```pycon
>>> 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]])
```
