# density_matrix_expectation_value

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

```python
Graph.density_matrix_expectation_value(density_matrix, operator, *, name=None)
```

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

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

### Parameters
* **density_matrix** (*np.ndarray* *or* [*Tensor*](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Tensor.md#boulderopal.graph.Tensor)) – The density matrix. It must be of shape `(..., D, 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.expectation_value`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/expectation_value.md#boulderopal.graph.Graph.expectation_value)
: Expectation value of an operator with respect to a pure state.

[`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 density matrix $\rho=\sum_i p_i |\psi_i\rangle\langle\psi_i|$ is defined as

$$
{\mathrm{Tr}}(A\rho) = {\mathrm{Tr}}(A\sum_i p_i |\psi_i\rangle\langle\psi_i|)
= \sum_i p_i \langle\psi_i|A|\psi_i\rangle .
$$

For more information about the density matrix expectation value, see
[density matrix](https://en.wikipedia.org/wiki/Density_matrix) on Wikipedia.

## Examples

```pycon
>>> graph.density_matrix_expectation_value(
...     np.array([[0.9, 0.], [0., 0.1]]), np.array([[1., 0.], [0., -1.]]),
...     name="expectation",
... )
<Tensor: name="expectation", operation_name="density_matrix_expectation_value", shape=()>
>>> result = bo.execute_graph(graph=graph, output_node_names="expectation")
>>> result["output"]["expectation"]["value"]
0.8
>>> graph.density_matrix_expectation_value(
...     np.array([[0.9, 0.], [0., 0.1]]),
...     np.array([[[0., 1.], [1., 0.]], [[0., -1.j], [1.j, 0.]], [[1., 0.], [0., -1.]]]),
...     name="expectation2"
... )
<Tensor: name="expectation2", operation_name="expectation_value", shape=(3,)>
>>> result = bo.execute_graph(graph=graph, output_node_names="expectation2")
>>> result["output"]["expectation2"]["value"]
array([0. +0.j, 0. +0.j, 0.8+0.j])
```
