# number_operator

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

```python
Graph.number_operator(dimension, offset=0, *, name=None)
```

Create a number operator in the truncated Fock space.

### Parameters
* **dimension** (*int*) – The size of the state representation in the truncated Fock space.
  By default, the Fock space is truncated at [0, dimension).
  If non-zero offset is passed, the space is then
  truncated at [offset, dimension + offset).
* **offset** (*int* *,* *optional*) – The lowest level of Fock state in the representation. Defaults to 0.
* **name** (*str* *or* *None* *,* *optional*) – The name of the node.
### Returns
A 2D tensor representing the number operator.
### Return type
[Tensor](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Tensor.md#boulderopal.graph.Tensor)

### SEE ALSO
[`Graph.annihilation_operator`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/annihilation_operator.md#boulderopal.graph.Graph.annihilation_operator)
: Create an annihilation operator in the truncated Fock space.

[`Graph.coherent_state`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/coherent_state.md#boulderopal.graph.Graph.coherent_state)
: Create a coherent state (or a batch of them).

[`Graph.creation_operator`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/creation_operator.md#boulderopal.graph.Graph.creation_operator)
: Create a creation operator in the truncated Fock space.

[`Graph.fock_state`](https://docs.q-ctrl.com/references/boulder-opal/boulderopal/graph/Graph/fock_state.md#boulderopal.graph.Graph.fock_state)
: Create a Fock state (or a batch of them).

## Examples

Generate a number operator for a three-level system.

```pycon
>>> graph.number_operator(3, name="n")
<Tensor: name="n", operation_name="number_operator", shape=(3, 3)>
>>> result = bo.execute_graph(graph=graph, output_node_names="n")
>>> result["output"]["n"]["value"]
array([[0.+0.j, 0.+0.j, 0.+0.j],
    [0.+0.j, 1.+0.j, 0.+0.j],
    [0.+0.j, 0.+0.j, 2.+0.j]])
```

Apply a number operator on the second excited state such that
$N|2\rangle = 2|2\rangle$.

```pycon
>>> n = graph.number_operator(3)
>>> state = n @ graph.fock_state(3, 2)[:, None]
>>> state.name = "state"
>>> result = bo.execute_graph(graph=graph, output_node_names="state")
>>> result["output"]["state"]["value"]
array([[0.+0.j],
    [0.+0.j],
    [2.+0.j]])
```

Generate a number operator for a three-level system with an offset.

```pycon
>>> graph.number_operator(3, 1, name="n_offset")
<Tensor: name="n_offset", operation_name="number_operator", shape=(3, 3)>
>>> result = bo.execute_graph(graph=graph, output_node_names="n_offset")
>>> result["output"]["n_offset"]["value"]
array([[1.+0.j, 0.+0.j, 0.+0.j],
    [0.+0.j, 2.+0.j, 0.+0.j],
    [0.+0.j, 0.+0.j, 3.+0.j]])
```

Apply a number operator with an offset such that
$N|3\rangle = 3|3\rangle$.

```pycon
>>> n_offset = graph.number_operator(3, 1)
>>> state_offset = n_offset @ graph.fock_state(3, 3, 1)[:, None]
>>> state_offset.name = "state_offset"
>>> result = bo.execute_graph(graph=graph, output_node_names="state_offset")
>>> result["output"]["state_offset"]["value"]
array([[0.+0.j],
    [0.+0.j],
    [3.+0.j]])
```
