---
title: QfFermionOperator (latest version)
description: API reference for QfFermionOperator in the latest version of qiskit-fermions-c
source: https://quantum.cloud.ibm.com/docs/en/api/qiskit-fermions-c/qf-ferm-op
---

# QfFermionOperator

### QfFermionOperator

`struct QfFermionOperator`

A spin-less fermionic operator.

> **Note**
>
> This is an opaque data structure to the C API whose internals are implemented entirely in Rust. The remainder of this page describes the design and related functions to work with this struct.

***

## Definition

This operator is defined by a linear combination of products of fermionic creation and annihilation operators acting on spin-less fermionic modes. That is to say, the individual terms fulfill the following anti-commutation relations: [\[1\]](#id2)

$$
\left\{a^\dagger_i, a^\dagger_j\right\} =
\left\{a_i, a_j\right\} = 0,~~\text{and}~~
\left\{a_i, a^\dagger_j\right\} = \delta_{ij} \, ,
$$

where $i$ and $j$ do not distinguish the spin species of the fermionic modes they are indexing.

This makes the definition of the entire operator the following:

$$
\text{\texttt{QfFermionOperator}} = \sum_i c_i \bigotimes_j \hat{A_j} \, ,
$$

where $\hat{A_j} \in \{ a_j, a^\dagger_j \}$ and $c_i$ is the (complex) coefficient making up the linear combination of products. The index $j$ can take any value between 0 and the number of fermionic modes acted upon by the operator minus 1.

***

## Implementation

This struct stores the terms and coefficients in multiple sparse vectors, akin to the [compressed sparse row format](https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_\(CSR,_CRS_or_Yale_format\)) commonly used for sparse matrices. More concretely, a single operator contains 4 arrays:

|              |                                                                                   |
| ------------ | --------------------------------------------------------------------------------- |
| `coeffs`     | A vector of complex coefficients consisting of two 64-bit floating point numbers. |
| `actions`    | A vector of booleans storing the nature of the second-quantization actions.       |
| `modes`      | A vector of 32-bit integers storing the fermionic mode indices acted upon.        |
| `boundaries` | A vector of integers indicating the boundaries in `actions` and `modes`.          |

Entries in `actions` indicate creation (annihilation) operators by `True` (`False`). Fermionic modes indexed by `modes` are considered spinless.

> **Note**
>
> You can access **read-only copies** of these internal arrays via their respective functions:
>
> - [`qf_ferm_op_get_coeffs()`](#qf_ferm_op_get_coeffs "qf_ferm_op_get_coeffs")
> - [`qf_ferm_op_get_actions()`](#qf_ferm_op_get_actions "qf_ferm_op_get_actions")
> - [`qf_ferm_op_get_modes()`](#qf_ferm_op_get_modes "qf_ferm_op_get_modes")
> - [`qf_ferm_op_get_boundaries()`](#qf_ferm_op_get_boundaries "qf_ferm_op_get_boundaries")

This data structure allows for very efficient construction and manipulation of operators. However, it implies that duplicate terms might be contained in an operator at any moment. These must be resolved manually through the use of [`qf_ferm_op_simplify()`](#qf_ferm_op_simplify "qf_ferm_op_simplify").

### Construction

A new operator can be constructed directly by specifying the corresponding arrays outlined above. Alternatively, an empty [`QfFermionOperator`](#qffermionoperator "QfFermionOperator") can be initialized with [`qf_ferm_op_zero()`](#qf_ferm_op_zero "qf_ferm_op_zero") and terms can be added iteratively via [`qf_ferm_op_add_term()`](#qf_ferm_op_add_term "qf_ferm_op_add_term").

|                                                                       |                                                     |
| --------------------------------------------------------------------- | --------------------------------------------------- |
| [`qf_ferm_op_new()`](#qf_ferm_op_new "qf_ferm_op_new")                | Constructs a new operator from the provided arrays. |
| [`qf_ferm_op_zero()`](#qf_ferm_op_zero "qf_ferm_op_zero")             | Constructs the additive identity operator.          |
| [`qf_ferm_op_one()`](#qf_ferm_op_one "qf_ferm_op_one")                | Constructs the multiplicative identity operator.    |
| [`qf_ferm_op_add_term()`](#qf_ferm_op_add_term "qf_ferm_op_add_term") | Adds a term to an existing `QfFermionOperator`.     |

> **Note**
>
> A [`QfFermionOperator`](#qffermionoperator "QfFermionOperator") can be freed with [`qf_ferm_op_free()`](#qf_ferm_op_free "qf_ferm_op_free").

### Arithmetics

The following functions provide arithmetic manipulation:

|                                                                    |                                           |
| ------------------------------------------------------------------ | ----------------------------------------- |
| [`qf_ferm_op_add()`](#qf_ferm_op_add "qf_ferm_op_add")             | Adds two operators together.              |
| [`qf_ferm_op_mul()`](#qf_ferm_op_mul "qf_ferm_op_mul")             | Multiplies an operator by a scalar.       |
| [`qf_ferm_op_compose()`](#qf_ferm_op_compose "qf_ferm_op_compose") | Composes two operators with each other.   |
| [`qf_ferm_op_adjoint()`](#qf_ferm_op_adjoint "qf_ferm_op_adjoint") | Returns the Hermitian conjugate operator. |

### Manipulation

The following functions provide operator manipulation logic:

|                                                                                         |                                                           |
| --------------------------------------------------------------------------------------- | --------------------------------------------------------- |
| [`qf_ferm_op_ichop()`](#qf_ferm_op_ichop "qf_ferm_op_ichop")                            | Removes terms with small coefficient magnitudes.          |
| [`qf_ferm_op_simplify()`](#qf_ferm_op_simplify "qf_ferm_op_simplify")                   | Returns an equivalent but simplified operator.            |
| [`qf_ferm_op_normal_ordered()`](#qf_ferm_op_normal_ordered "qf_ferm_op_normal_ordered") | Returns an equivalent operator with normal ordered terms. |
| [`qf_ferm_op_relabel_modes()`](#qf_ferm_op_relabel_modes "qf_ferm_op_relabel_modes")    | Relabels the modes of an operator.                        |

### Properties

The following functions exist to check certain properties of an operator.

|                                                                                                                          |                                                            |
| ------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
| [`qf_ferm_op_is_hermitian()`](#qf_ferm_op_is_hermitian "qf_ferm_op_is_hermitian")                                        | Returns whether an operator is Hermitian.                  |
| [`qf_ferm_op_max_rank()`](#qf_ferm_op_max_rank "qf_ferm_op_max_rank")                                                    | Returns the maximum rank of the terms in this operator.    |
| [`qf_ferm_op_min_num_modes()`](#qf_ferm_op_min_num_modes "qf_ferm_op_min_num_modes")                                     | Returns the minimum number of modes of this operator.      |
| [`qf_ferm_op_conserves_particle_number()`](#qf_ferm_op_conserves_particle_number "qf_ferm_op_conserves_particle_number") | Returns whether an operator is particle-number conserving. |

***

\[[1](#id1)]

[https://en.wikipedia.org/wiki/Second\_quantization#Fermion\_creation\_and\_annihilation\_operators](https://en.wikipedia.org/wiki/Second_quantization#Fermion_creation_and_annihilation_operators)

## Members

### qf\_ferm\_op\_new

`QfFermionOperator *qf_ferm_op_new(uint64_t num_terms, uint64_t num_actions, const QkComplex64 *coeffs, const bool *actions, const uint32_t *modes, const uint32_t *boundaries)`

Constructs a new operator.

Any of the pointer arguments can be `NULL` if and only if their corresponding length is zero.

#### Example

```c
1uint64_t num_terms = 3;
2uint64_t num_actions = 4;
3bool actions[4] = {true, false, true, false};
4uint32_t modes[4] = {0, 1, 2, 3};
5QkComplex64 coeffs[3] = {{1.0, 0.0}, {-1.0, 0.0}, {0.0, -1.0}};
6uint32_t boundaries[4] = {0, 0, 2, 4};
7QfFermionOperator *op = qf_ferm_op_new(num_terms, num_actions, coeffs,
8                                       actions, modes, boundaries);
```

**Parameters**

- **num\_terms** – The number of terms in the operator.
- **num\_actions** – The number of actions summed over all terms.
- **coeffs** – A pointer to an array of term coefficients. The length of this array should be `num_terms`.
- **actions** – A pointer to an array of actions over all terms. The length of this array should be `num_actions`.
- **modes** – A pointer to an array of action modes over all terms. The length of this array should be `num_actions`.
- **boundaries** – A pointer to an array of the boundaries between terms. The length of this array should be `num_terms + 1`.

### qf\_ferm\_op\_free

`void qf_ferm_op_free(QfFermionOperator *op)`

Frees an existing operator.

#### Example

```c
1QfFermionOperator *op = qf_ferm_op_one();
2qf_ferm_op_free(op);
```

**Parameters**

- **op** – A pointer to the fermionic operator to be freed.

### qf\_ferm\_op\_get\_coeffs

`void qf_ferm_op_get_coeffs(const QfFermionOperator *op, QkComplex64 **coeffs_out, uint64_t *coeffs_len)`

Provides read-only access to the operator’s coefficients.

> **See also**
>
> The explanation of the internal data structure, [here](#qf-ferm-op-implementation).

#### Example

```c
 1uint64_t num_terms = 2;
 2uint64_t num_actions = 0;
 3QkComplex64 coeffs[2] = {{1.0, 0.0}, {0.0, -1.0}};
 4uint32_t boundaries[3] = {0, 0, 0};
 5QfFermionOperator *op =
 6    qf_ferm_op_new(num_terms, num_actions, coeffs, NULL, NULL, boundaries);
 7
 8QkComplex64 *coeffs_out;
 9uint64_t *coeffs_len;
10
11qf_ferm_op_get_coeffs(op, &coeffs_out, &coeffs_len);
12
13assert(coeffs_len == 2);
```

> **Note**
>
> This function returns a **copy** of the internal data.

**Parameters**

- **op** – A pointer to the fermionic operator whose coefficients to access.
- **coeffs\_out** – A pointer to the array of complex values into which to write the coefficients.
- **coeffs\_len** – A pointer to the integer into which to write the length of the output array.

### qf\_ferm\_op\_get\_actions

`void qf_ferm_op_get_actions(const QfFermionOperator *op, bool **actions_out, uint64_t *actions_len)`

Provides read-only access to the operator’s actions.

> **See also**
>
> The explanation of the internal data structure, [here](#qf-ferm-op-implementation).

#### Example

```c
 1uint64_t num_terms = 2;
 2uint64_t num_actions = 2;
 3bool actions[2] = {true, false};
 4uint32_t modes[2] = {0, 1};
 5QkComplex64 coeffs[2] = {{1.0, 0.0}, {0.0, -1.0}};
 6uint32_t boundaries[3] = {0, 0, 2};
 7QfFermionOperator *op =
 8    qf_ferm_op_new(num_terms, num_actions, coeffs, actions, modes, boundaries);
 9
10QkComplex64 *actions_out;
11uint64_t *actions_len;
12
13qf_ferm_op_get_actions(op, &actions_out, &actions_len);
14
15assert(actions_len == 2);
```

> **Note**
>
> This function returns a **copy** of the internal data.

**Parameters**

- **op** – A pointer to the fermionic operator whose actions to access.
- **actions\_out** – A pointer to the array of boolean values into which to write the actions.
- **actions\_len** – A pointer to the integer into which to write the length of the output array.

### qf\_ferm\_op\_get\_modes

`void qf_ferm_op_get_modes(const QfFermionOperator *op, uint32_t **modes_out, uint64_t *modes_len)`

Provides read-only access to the operator’s acted-upon mode indices.

> **See also**
>
> The explanation of the internal data structure, [here](#qf-ferm-op-implementation).

#### Example

```c
 1uint64_t num_terms = 2;
 2uint64_t num_actions = 2;
 3bool actions[2] = {true, false};
 4uint32_t modes[2] = {0, 1};
 5QkComplex64 coeffs[2] = {{1.0, 0.0}, {0.0, -1.0}};
 6uint32_t boundaries[3] = {0, 0, 2};
 7QfFermionOperator *op =
 8    qf_ferm_op_new(num_terms, num_actions, coeffs, actions, modes, boundaries);
 9
10QkComplex64 *modes_out;
11uint64_t *modes_len;
12
13qf_ferm_op_get_modes(op, &modes_out, &modes_len);
14
15assert(modes_len == 2);
```

> **Note**
>
> This function returns a **copy** of the internal data.

**Parameters**

- **op** – A pointer to the fermionic operator whose modes to access.
- **modes\_out** – A pointer to the array of boolean values into which to write the modes.
- **modes\_len** – A pointer to the integer into which to write the length of the output array.

### qf\_ferm\_op\_get\_boundaries

`void qf_ferm_op_get_boundaries(const QfFermionOperator *op, size_t **boundaries_out, uint64_t *boundaries_len)`

Provides read-only access to the indices indicating the boundaries between operator terms.

> **See also**
>
> The explanation of the internal data structure, [here](#qf-ferm-op-implementation).

#### Example

```c
 1uint64_t num_terms = 2;
 2uint64_t num_actions = 2;
 3bool actions[2] = {true, false};
 4uint32_t modes[2] = {0, 1};
 5QkComplex64 coeffs[2] = {{1.0, 0.0}, {0.0, -1.0}};
 6uint32_t boundaries[3] = {0, 0, 2};
 7QfFermionOperator *op =
 8    qf_ferm_op_new(num_terms, num_actions, coeffs, actions, modes, boundaries);
 9
10QkComplex64 *boundaries_out;
11uint64_t *boundaries_len;
12
13qf_ferm_op_get_boundaries(op, &boundaries_out, &boundaries_len);
14
15assert(boundaries_len == 3);
```

> **Note**
>
> This function returns a **copy** of the internal data.

**Parameters**

- **op** – A pointer to the fermionic operator whose boundaries to access.
- **boundaries\_out** – A pointer to the array of boolean values into which to write the boundaries.
- **boundaries\_len** – A pointer to the integer into which to write the length of the output array.

### qf\_ferm\_op\_num\_support

`uint32_t qf_ferm_op_num_support(const QfFermionOperator *op)`

Gets the size of the support of an operator.

Use this to size the output buffer of [`qf_ferm_op_get_support()`](#qf_ferm_op_get_support "qf_ferm_op_get_support"), which does not report a length of its own.

#### Example

```c
1bool actions[3] = {true, false, true};
2uint32_t modes[3] = {2, 0, 2};
3QkComplex64 coeffs[2] = {{1.0, 0.0}, {1.0, 0.0}};
4uint32_t boundaries[3] = {0, 2, 3};
5QfFermionOperator *op = qf_ferm_op_new(2, 3, coeffs, actions, modes, boundaries);
6
7uint32_t num_support = qf_ferm_op_num_support(op);
8
9assert(num_support == 2);
```

**Parameters**

- **op** – A pointer to the fermionic operator whose support size to get.

**Returns**

The number of distinct mode indices acted upon by the operator.

### qf\_ferm\_op\_get\_support

`void qf_ferm_op_get_support(const QfFermionOperator *op, uint32_t *support_out)`

Gets the support of an operator, i.e. the mode indices acted upon by the operator.

The indices are written in **ascending** order, and every index appears exactly once no matter how many terms act upon it.

#### Example

```c
 1bool actions[3] = {true, false, true};
 2uint32_t modes[3] = {2, 0, 2};
 3QkComplex64 coeffs[2] = {{1.0, 0.0}, {1.0, 0.0}};
 4uint32_t boundaries[3] = {0, 2, 3};
 5QfFermionOperator *op = qf_ferm_op_new(2, 3, coeffs, actions, modes, boundaries);
 6
 7uint32_t num_support = qf_ferm_op_num_support(op);
 8uint32_t *support_out = malloc(num_support * sizeof(uint32_t));
 9
10qf_ferm_op_get_support(op, support_out);
11
12assert(support_out[0] == 0);
13assert(support_out[1] == 2);
14
15free(support_out);
```

> **Note**
>
> Unlike the other getters, this does not hand out a pointer into the operator: the support is computed on demand rather than stored, so the caller provides the output buffer. Query its required length with [`qf_ferm_op_num_support()`](#qf_ferm_op_num_support "qf_ferm_op_num_support") first.

**Parameters**

- **op** – A pointer to the fermionic operator whose support to get.
- **support\_out** – A pointer to the integer array into which to write the support. It must be sized to at least :c:func:`qf_ferm_op_num_support` elements.

### qf\_ferm\_op\_min\_num\_modes

`uint32_t qf_ferm_op_min_num_modes(const QfFermionOperator *op)`

Gets the smallest number of modes that can host an operator.

This is what determines, for example, the number of qubits a mapping requires. Note that it is not the number of distinct modes, which [`qf_ferm_op_num_support()`](#qf_ferm_op_num_support "qf_ferm_op_num_support") returns.

#### Example

```c
1bool actions[3] = {true, false, true};
2uint32_t modes[3] = {2, 0, 2};
3QkComplex64 coeffs[2] = {{1.0, 0.0}, {1.0, 0.0}};
4uint32_t boundaries[3] = {0, 2, 3};
5QfFermionOperator *op = qf_ferm_op_new(2, 3, coeffs, actions, modes, boundaries);
6
7uint32_t min_num_modes = qf_ferm_op_min_num_modes(op);
8
9assert(min_num_modes == 3);
```

**Parameters**

- **op** – A pointer to the fermionic operator whose minimum number of modes to get.

**Returns**

One more than the largest mode index acted upon by the operator, or 0 if it acts on no mode.

### qf\_ferm\_op\_zero

`QfFermionOperator *qf_ferm_op_zero(void)`

Constructs the additive identity operator.

Adding the operator that is constructed by this method to another one has no effect.

#### Example

```c
1QfFermionOperator *zero = qf_ferm_op_zero();
2
3QfFermionOperator *op_plus_zero = qf_ferm_op_add(op, zero);
4
5assert(qf_ferm_op_equal(op, op_plus_zero));
```

**Returns**

A pointer to the created operator.

### qf\_ferm\_op\_one

`QfFermionOperator *qf_ferm_op_one(void)`

Constructs the multiplicative identity operator.

Composing the operator that is constructed by this method with another one has no effect.

#### Example

```c
1QfFermionOperator *one = qf_ferm_op_one();
2
3QfFermionOperator *op_times_one = qf_ferm_op_compose(op, one);
4
5assert(qf_ferm_op_equal(op, op_times_one));
```

**Returns**

A pointer to the created operator.

### qf\_ferm\_op\_has\_groups

`bool qf_ferm_op_has_groups(const QfFermionOperator *op)`

Checks whether this operator tracks group indices.

> **See also**
>
> The explanation on [Group operator terms: use the operator structure](/docs/addons/qiskit-fermions/guides/grouping#grouping-explanation).

#### Example

```c
1QfFCIDump *fcidump = NULL;
2qf_fcidump_from_file("molecule.fcidump", &fcidump);
3QfFermionOperator *op = qf_ferm_op_from_fcidump(fcidump);
4
5bool has_groups = qf_ferm_op_has_groups(op);
6
7assert(!has_groups);
```

**Parameters**

- **op** – A pointer to the fermionic operator to be checked.

**Returns**

Whether the provided operator has a `groups` attribute.

### qf\_ferm\_op\_num\_groups

`uint32_t qf_ferm_op_num_groups(const QfFermionOperator *op)`

Gets the number of groups from an operator.

> **See also**
>
> The explanation on [Group operator terms: use the operator structure](/docs/addons/qiskit-fermions/guides/grouping#grouping-explanation).

#### Example

```c
1QfFCIDump *fcidump = NULL;
2qf_fcidump_from_file("molecule.fcidump", &fcidump);
3QfFermionOperator *op = qf_ferm_op_from_fcidump(fcidump);
4
5uint32_t num_groups = qf_ferm_op_num_groups(op);
```

> **Note**
>
> The number of groups is evaluated lazily as the largest occurring group index plus 1.

**Parameters**

- **op** – A pointer to the fermionic operator whose number of groups to get.

**Returns**

The number of group indices from the operator’s `groups` attribute.

### qf\_ferm\_op\_get\_groups

`void qf_ferm_op_get_groups(const QfFermionOperator *op, uint32_t **groups_out, uint64_t *groups_len)`

Gets the group indices for all operator terms.

> **See also**
>
> The explanation on [Group operator terms: use the operator structure](/docs/addons/qiskit-fermions/guides/grouping#grouping-explanation).

#### Example

```c
1QfFermionOperator *op = ...;
2uint32_t *groups_out;
3uint32_t groups_len;
4
5qf_ferm_op_get_groups(op, &groups_out, &groups_len);
```

**Parameters**

- **op** – A pointer to the fermionic operator whose group indices to get.
- **groups\_out** – A pointer to the integer array into which to write the group indices.
- **groups\_len** – A pointer to the integer into which to write the length of the output array.

### qf\_ferm\_op\_set\_groups

`QfExitCode qf_ferm_op_set_groups(QfFermionOperator *op, const uint32_t *groups_in, uint64_t groups_len)`

Sets the `groups` attribute of the provided operator.

The length is validated here because nothing downstream re-checks it: too few indices would silently drop the trailing terms wherever terms are iterated together with their groups, and too many would make [`qf_ferm_op_num_groups()`](#qf_ferm_op_num_groups "qf_ferm_op_num_groups") report groups that no term carries.

> **See also**
>
> The explanation on [Group operator terms: use the operator structure](/docs/addons/qiskit-fermions/guides/grouping#grouping-explanation).

#### Example

```c
1QfFermionOperator *op = ...;
2
3uint32_t num_terms = 4;
4uint32_t groups_in[4] = {0, 1, 0, 1};
5qf_ferm_op_set_groups(op, groups_in, num_terms);
```

**Parameters**

- **op** – A pointer to the fermionic operator whose `groups` attribute to write.
- **groups\_in** – A pointer to the `groups` integer array to write into the operator.
- **groups\_len** – The number of terms in the `groups_in` array.

**Returns**

An exit code.

- `QfExitCode_Success` upon success
- `QfExitCode_ValueError` if `groups_len` differs from the number of terms in the operator

### qf\_ferm\_op\_del\_groups

`void qf_ferm_op_del_groups(QfFermionOperator *op)`

Deletes the `groups` attribute from the provided operator.

> **See also**
>
> The explanation on [Group operator terms: use the operator structure](/docs/addons/qiskit-fermions/guides/grouping#grouping-explanation).

#### Example

```c
1QfFermionOperator *op = ...;
2
3qf_ferm_op_del_groups(op);
```

**Parameters**

- **op** – A pointer to the fermionic operator whose `groups` attribute to delete.

### qf\_ferm\_op\_split\_out\_groups

`void qf_ferm_op_split_out_groups(const QfFermionOperator *op, const uint32_t *group_indices, uint64_t num_indices, QfFermionOperator **group_ops_out)`

Splits this operator into a list of new operators based on its `groups` attribute.

A duplicate index in `group_indices` is written once per occurrence in `group_ops_out`. Requesting only a small number of groups out of a much larger total is significantly cheaper than requesting all of them, since terms belonging to a group that is not requested are skipped rather than appended anywhere.

> **See also**
>
> The explanation on [Group operator terms: use the operator structure](/docs/addons/qiskit-fermions/guides/grouping#grouping-explanation).

#### Example

```c
 1uint64_t num_terms = 4;
 2uint64_t num_actions = 8;
 3bool actions[8] = {true, false, true, false, true, false, true, false};
 4uint32_t modes[8] = {0, 1, 2, 3, 1, 0, 3, 2};
 5QkComplex64 coeffs[4] = {{1.0, 0.0}, {1.0, 0.0}, {1.0, 0.0}, {1.0, 0.0}};
 6uint32_t boundaries[5] = {0, 2, 4, 6, 8};
 7QfFermionOperator *op =
 8    qf_ferm_op_new(num_terms, num_actions, coeffs, actions, modes, boundaries);
 9
10uint32_t groups_in[4] = {0, 1, 0, 1};
11qf_ferm_op_set_groups(op, groups_in, num_terms);
12
13// build every group, in index order
14QfFermionOperator *group_ops[2];
15qf_ferm_op_split_out_groups(op, NULL, 0, group_ops);
16
17// build only group 1
18uint32_t group_indices[1] = {1};
19QfFermionOperator *group_op[1];
20qf_ferm_op_split_out_groups(op, group_indices, 1, group_op);
```

**Parameters**

- **op** – A pointer to the fermionic operator whose `groups` to split out.
- **group\_indices** – A pointer to the array of group indices for which to build operators, in the desired output order. May be `NULL`, in which case every group is built, in index order (equivalent to passing every index from `0` to :c:func:`qf_ferm_op_num_groups` `- 1`).
- **num\_indices** – The number of indices in the `group_indices` array. Ignored if `group_indices` is `NULL`.
- **group\_ops\_out** – A pointer to the array of :c:struct:`QfFermionOperator` into which to write the operators for each requested group. Must be sized to `num_indices` when `group_indices` is non-`NULL`, or to :c:func:`qf_ferm_op_num_groups` when it is `NULL`.

### qf\_ferm\_op\_add\_term

`void qf_ferm_op_add_term(QfFermionOperator *op, uint64_t num_actions, const bool *actions, const uint32_t *modes, const QkComplex64 *coeff)`

Adds a term to an existing operator.

Any of the pointer arguments can be `NULL` if and only if their corresponding length is zero.

> **Caution**
>
> This function resets the operator’s `groups` attribute to `NULL`.

#### Example

```c
 1QfFermionOperator *one = qf_ferm_op_one();
 2
 3QfFermionOperator *op = qf_ferm_op_zero();
 4bool actions[0] = {};
 5uint32_t modes[0] = {};
 6QkComplex64 coeff = {1.0, 0.0};
 7
 8qf_ferm_op_add_term(op, 0, actions, modes, &coeff);
 9
10assert(qf_ferm_op_equal(op, one));
```

**Parameters**

- **op** – A pointer to the fermionic operator to be modified.
- **num\_actions** – The length of the actions array.
- **actions** – A pointer to an array of actions. The length of this array should be `num_actions`.
- **modes** – A pointer to an array of action modes. The length of this array should be `num_actions`.
- **coeff** – A pointer to the complex coefficient.

### qf\_ferm\_op\_add

`QfFermionOperator *qf_ferm_op_add(const QfFermionOperator *left, const QfFermionOperator *right)`

Adds two operators together.

#### Example

```c
1QfFermionOperator *one = qf_ferm_op_one();
2QfFermionOperator *zero = qf_ferm_op_zero();
3
4QfFermionOperator *result = qf_ferm_op_add(one, zero);
5
6assert(qf_ferm_op_equal(result, one));
```

**Parameters**

- **left** – A pointer to the left operator.
- **right** – A pointer to the right operator.

**Returns**

A pointer to the resulting operator.

### qf\_ferm\_op\_scaled\_add

`QfFermionOperator *qf_ferm_op_scaled_add(const QfFermionOperator *left, const QfFermionOperator *right, const QkComplex64 *factor)`

Adds two operators together, scaling the coefficients of the right one.

This fuses the scaling into the addition, which avoids building a fully scaled copy of `right` just to append it. Passing a factor of `-1` therefore subtracts, which is why no separate subtraction function is provided.

#### Example

```c
1QfFermionOperator *one = qf_ferm_op_one();
2QfFermionOperator *other = qf_ferm_op_one();
3
4// Subtracting via a factor of -1 leaves the two terms canceling each other out.
5QkComplex64 factor = {-1.0, 0.0};
6QfFermionOperator *result = qf_ferm_op_scaled_add(one, other, &factor);
7
8QfFermionOperator *simplified = qf_ferm_op_simplify(result, 1e-10);
9assert(qf_ferm_op_len(simplified) == 0);
```

> **Note**
>
> Like [`qf_ferm_op_add()`](#qf_ferm_op_add "qf_ferm_op_add"), this appends the terms of `right` without combining them with those of `left`, so the result tracks no group indices. Call [`qf_ferm_op_simplify()`](#qf_ferm_op_simplify "qf_ferm_op_simplify") to collect equal terms afterwards.

**Parameters**

- **left** – A pointer to the left operator.
- **right** – A pointer to the right operator.
- **factor** – A pointer to the factor to scale the right operator’s coefficients with.

**Returns**

A pointer to the resulting operator, `left + factor * right`.

### qf\_ferm\_op\_add\_inplace

`void qf_ferm_op_add_inplace(QfFermionOperator *left, const QfFermionOperator *right)`

Adds an operator into another one, in place.

The in-place counterpart of [`qf_ferm_op_add()`](#qf_ferm_op_add "qf_ferm_op_add"), which saves copying `left` into a freshly allocated result.

> **Caution**
>
> This function resets the operator’s `groups` attribute to `NULL`.

#### Example

```c
1QfFermionOperator *left = qf_ferm_op_one();
2QfFermionOperator *right = qf_ferm_op_one();
3
4qf_ferm_op_add_inplace(left, right);
5
6assert(qf_ferm_op_len(left) == 2);
```

**Parameters**

- **left** – A pointer to the operator to add into.
- **right** – A pointer to the operator to add.

### qf\_ferm\_op\_scaled\_add\_inplace

`void qf_ferm_op_scaled_add_inplace(QfFermionOperator *left, const QfFermionOperator *right, const QkComplex64 *factor)`

Adds an operator into another one in place, scaling the coefficients of the right one.

The in-place counterpart of [`qf_ferm_op_scaled_add()`](#qf_ferm_op_scaled_add "qf_ferm_op_scaled_add"), computing `left + factor * right` without allocating a result. As there, a factor of `-1` subtracts.

> **Caution**
>
> This function resets the operator’s `groups` attribute to `NULL`.

#### Example

```c
1QfFermionOperator *left = qf_ferm_op_one();
2QfFermionOperator *right = qf_ferm_op_one();
3
4// Subtract, leaving two terms that cancel each other out.
5QkComplex64 factor = {-1.0, 0.0};
6qf_ferm_op_scaled_add_inplace(left, right, &factor);
7
8QfFermionOperator *simplified = qf_ferm_op_simplify(left, 1e-10);
9assert(qf_ferm_op_len(simplified) == 0);
```

**Parameters**

- **left** – A pointer to the operator to add into.
- **right** – A pointer to the operator to add.
- **factor** – A pointer to the factor to scale the right operator’s coefficients with.

### qf\_ferm\_op\_mul\_inplace

`void qf_ferm_op_mul_inplace(QfFermionOperator *op, const QkComplex64 *scalar)`

Multiplies an operator by a scalar, in place.

The in-place counterpart of [`qf_ferm_op_mul()`](#qf_ferm_op_mul "qf_ferm_op_mul"), which saves copying the operator into a freshly allocated result.

#### Example

```c
1QfFermionOperator *op = qf_ferm_op_one();
2QkComplex64 scalar = {2.0, 0.0};
3
4qf_ferm_op_mul_inplace(op, &scalar);
```

> **Note**
>
> Unlike the two in-place additions above, this **preserves** the `groups` attribute: scaling the coefficients leaves the number of terms, and hence the one-index-per-term invariant, untouched.

**Parameters**

- **op** – A pointer to the operator to scale.
- **scalar** – A pointer to the scalar.

### qf\_ferm\_op\_mul

`QfFermionOperator *qf_ferm_op_mul(const QfFermionOperator *op, const QkComplex64 *scalar)`

Multiplies an operator by a scalar.

#### Example

```c
 1QfFermionOperator *one = qf_ferm_op_one();
 2QkComplex64 coeff = {2.0, 0.0};
 3QfFermionOperator *result = qf_ferm_op_mul(one, &coeff);
 4
 5QfFermionOperator *expected = qf_ferm_op_zero();
 6bool actions[0] = {};
 7uint32_t modes[0] = {};
 8qf_ferm_op_add_term(expected, 0, actions, modes, &coeff);
 9
10assert(qf_ferm_op_equal(result, expected));
```

**Parameters**

- **op** – A pointer to the operator.
- **scalar** – A pointer to the scalar.

**Returns**

A pointer to the resulting operator.

### qf\_ferm\_op\_compose

`QfFermionOperator *qf_ferm_op_compose(const QfFermionOperator *left, const QfFermionOperator *right)`

Composes two operators with each other.

#### Example

```c
1QfFermionOperator *one = qf_ferm_op_one();
2QfFermionOperator *zero = qf_ferm_op_zero();
3
4QfFermionOperator *result = qf_ferm_op_compose(one, zero);
5
6assert(qf_ferm_op_equal(result, zero));
```

> **Note**
>
> The composition of two operators tracks no groups, even when both operands do.

**Parameters**

- **left** – A pointer to the left operator.
- **right** – A pointer to the right operator.

**Returns**

A pointer to `left.compose(right)`, which equals the operator `result = right @ left` in terms of the matrix multiplication `@`. In other words, `right` is applied first. To obtain `left @ right`, swap the arguments.

### qf\_ferm\_op\_adjoint

`QfFermionOperator *qf_ferm_op_adjoint(const QfFermionOperator *op)`

Returns the Hermitian conjugate (or adjoint) of an operator.

This affects the terms and coefficients as follows:

- the actions in each term reverse their order and flip between creation and annihilation
- the coefficients are complex conjugated

#### Example

```c
 1QfFermionOperator *op = qf_ferm_op_zero();
 2bool actions[0] = {};
 3uint32_t modes[0] = {};
 4QkComplex64 coeff = {0.0, 1.0};
 5qf_ferm_op_add_term(op, 0, actions, modes, &coeff);
 6
 7QfFermionOperator *adjoint = qf_ferm_op_adjoint(op);
 8
 9QfFermionOperator *expected = qf_ferm_op_zero();
10QkComplex64 coeff_adj = {0.0, -1.0};
11qf_ferm_op_add_term(expected, 0, actions, modes, &coeff_adj);
12
13assert(qf_ferm_op_equal(adjoint, expected));
```

**Parameters**

- **op** – A pointer to the operator.

**Returns**

A pointer to the created operator.

### qf\_ferm\_op\_ichop

`void qf_ferm_op_ichop(QfFermionOperator *op, double atol)`

Removes terms whose coefficient magnitude lies below the provided threshold.

> **Caution**
>
> This functions truncates coefficients greedily! If the acted upon operator might contain separate coefficients for duplicate terms consider calling [`qf_ferm_op_simplify()`](#qf_ferm_op_simplify "qf_ferm_op_simplify") instead!

#### Example

```c
 1QfFermionOperator *op = qf_ferm_op_zero();
 2bool actions[0] = {};
 3uint32_t modes[0] = {};
 4QkComplex64 coeff = {1e-8};
 5qf_ferm_op_add_term(op, 0, actions, modes, &coeff);
 6
 7qf_ferm_op_ichop(op, 1e-6);
 8
 9QfFermionOperator *expected = qf_ferm_op_zero();
10
11assert(qf_ferm_op_equal(op, expected));
```

**Parameters**

- **op** – A pointer to the operator.
- **atol** – The absolute tolerance for coefficient truncation.

### qf\_ferm\_op\_simplify

`QfFermionOperator *qf_ferm_op_simplify(const QfFermionOperator *op, double atol)`

Returns an equivalent but simplified operator.

The simplification process first sums all coefficients that belong to equal terms and then only retains those whose total coefficient exceeds the specified tolerance (just like [`qf_ferm_op_ichop()`](#qf_ferm_op_ichop "qf_ferm_op_ichop")).

When an operator has been arithmetically manipulated or constructed in a way that does not guarantee unique terms, this method should be called before applying any method that filters numerically small coefficients to avoid loss of information. See the example below which showcases how [`qf_ferm_op_ichop()`](#qf_ferm_op_ichop "qf_ferm_op_ichop") can truncate terms that sum to a total coefficient magnitude which should not be truncated:

```c
 1uint64_t num_terms = 100000;
 2uint64_t num_actions = 0;
 3bool actions[0] = {};
 4uint32_t modes[0] = {};
 5QkComplex64 coeffs[100000];
 6uint32_t boundaries[100001];
 7for (int i = 0; i < 100000; i++) {
 8  coeffs[i].re = 1e-5;
 9  coeffs[i].im = 0.0;
10  boundaries[i] = 0;
11}
12boundaries[100000] = 0;
13QfFermionOperator *op = qf_ferm_op_new(num_terms, num_actions, coeffs,
14                                       actions, modes, boundaries);
15
16QfFermionOperator *canon = qf_ferm_op_simplify(op, 1e-4);
17
18QfFermionOperator *one = qf_ferm_op_one();
19bool canon_is_equal = qf_ferm_op_equiv(canon, one, 1e-6);
20
21qf_ferm_op_ichop(op, 1e-4);
22
23QfFermionOperator *zero = qf_ferm_op_zero();
24bool ichop_is_equal = qf_ferm_op_equiv(op, zero, 1e-6);
```

**Parameters**

- **op** – A pointer to the fermionic operator to be simplified.
- **atol** – The absolute tolerance for coefficient truncation.

**Returns**

An equivalent but simplified operator.

### qf\_ferm\_op\_normal\_ordered

`QfFermionOperator *qf_ferm_op_normal_ordered(const QfFermionOperator *op, const bool *sandwich)`

Returns an equivalent operator with normal ordered terms.

The normal order of an operator term is defined such that all creation actions appear before all annihilation actions. Within each group, the acted-upon modes are ordered lexicographically. Whether their order is ascending or descending depends upon the value of the `sandwich` argument:

- `NULL`: both groups are ordered lexicographically descending (e.g. `+_1 +_0 -_1 -_0`)
- `True`: larger indices appear towards the middle, i.e. creation actions are lexicographically ascending while annihilation ones are descending (e.g. `+_0 +_1 -_1 -_0`)
- `False`: smaller indices appear towards the middle, i.e. creation actions are lexicographically descending while annihilation ones are ascending (e.g. `+_1 +_0 -_0 -_1`)

#### Example

```c
 1QfFermionOperator *op = qf_ferm_op_zero();
 2bool actions[4] = {false, true, false, true};
 3uint32_t modes[4] = {1, 1, 0, 0};
 4QkComplex64 coeff = {1.0, 0.0};
 5qf_ferm_op_add_term(op, 4, actions, modes, &coeff);
 6
 7QfFermionOperator *normal_ordered = qf_ferm_op_normal_ordered(op, NULL);
 8
 9uint64_t num_terms = 4;
10uint64_t num_actions = 8;
11bool actions_exp[8] = {true, false, true, false, true, true, false, false};
12uint32_t modes_exp[8] = {0, 0, 1, 1, 1, 0, 1, 0};
13QkComplex64 coeffs_exp[4] = {
14    {1.0, 0.0}, {-1.0, 0.0}, {-1.0, 0.0}, {-1.0, 0.0}};
15uint32_t boundaries_exp[5] = {0, 0, 2, 4, 8};
16QfFermionOperator *expected =
17    qf_ferm_op_new(num_terms, num_actions, coeffs_exp, actions_exp,
18                   modes_exp, boundaries_exp);
19
20assert(qf_ferm_op_equal(normal_ordered, expected));
```

> **Note**
>
> When a term is being reordered, the anti-commutation relations have to be taken into account, $a_i a^\dagger_j = \delta_{ij} - a^\dagger_j a^i$, implying that the number of terms might change.

**Parameters**

- **op** – A pointer to the operator.
- **sandwich** – A pointer to a boolean value. This pointer can be `NULL`.

**Returns**

A pointer to the created operator.

### qf\_ferm\_op\_is\_hermitian

`bool qf_ferm_op_is_hermitian(const QfFermionOperator *op, double atol)`

Checks whether an operator is Hermitian.

#### Example

```c
 1QfFermionOperator *op = qf_ferm_op_zero();
 2bool actions1[2] = {true, false};
 3uint32_t modes1[2] = {0, 1};
 4QkComplex64 coeff1 = {0.0, 1.00001};
 5qf_ferm_op_add_term(op, 2, actions1, modes1, &coeff1);
 6bool actions2[2] = {true, false};
 7uint32_t modes2[2] = {1, 0};
 8QkComplex64 coeff2 = {0.0, -1};
 9qf_ferm_op_add_term(op, 2, actions2, modes2, &coeff2);
10
11assert(qf_ferm_op_is_hermitian(op, 1e-4));
12assert(!qf_ferm_op_is_hermitian(op, 1e-8));
```

> **Note**
>
> This check is implemented using [`qf_ferm_op_equiv()`](#qf_ferm_op_equiv "qf_ferm_op_equiv") on the [`qf_ferm_op_normal_ordered()`](#qf_ferm_op_normal_ordered "qf_ferm_op_normal_ordered") difference of `op` and its [`qf_ferm_op_adjoint()`](#qf_ferm_op_adjoint "qf_ferm_op_adjoint") and [`qf_ferm_op_zero()`](#qf_ferm_op_zero "qf_ferm_op_zero").

**Parameters**

- **op** – A pointer to the fermionic operator to be checked.
- **atol** – The absolute tolerance upto which coefficients are considered equal.

**Returns**

Whether the provided operator is Hermitian.

### qf\_ferm\_op\_max\_rank

`uint32_t qf_ferm_op_max_rank(const QfFermionOperator *op)`

Checks the maximum rank of an operator.

#### Example

```c
1QfFermionOperator *op = qf_ferm_op_zero();
2bool actions[4] = {true, false, true, false};
3uint32_t modes[4] = {0, 1, 2, 3};
4QkComplex64 coeff = {1.0, 0.0};
5qf_ferm_op_add_term(op, 4, actions, modes, &coeff);
6
7assert(qf_ferm_op_max_rank(op), 4);
```

> **Note**
>
> The length of the longest term can depend on the operator’s form which means that (for example) operator simplification or normal-ordering can result in a different maximum rank.

**Parameters**

- **op** – A pointer to the fermionic operator to be checked.

**Returns**

The maximum rank of the operator.

### qf\_ferm\_op\_conserves\_particle\_number

`bool qf_ferm_op_conserves_particle_number(const QfFermionOperator *op)`

Checks whether an operator is particle-number conserving.

This is the special case of [`qf_ferm_op_conserves_sector()`](#qf_ferm_op_conserves_sector "qf_ferm_op_conserves_sector") that treats all modes as a single block. Use that function to additionally require conservation *within* mode blocks, for example to check conservation of the spin z-component alongside the particle number.

#### Example

```c
 1// A hopping term conserves the particle number, ...
 2QfFermionOperator *op = qf_ferm_op_zero();
 3bool actions[2] = {true, false};
 4uint32_t modes[2] = {0, 2};
 5QkComplex64 coeff = {1.0, 0.0};
 6qf_ferm_op_add_term(op, 2, actions, modes, &coeff);
 7
 8assert(qf_ferm_op_conserves_particle_number(op));
 9
10// ... whereas a bare creation operator does not.
11QfFermionOperator *cre = qf_ferm_op_zero();
12bool cre_actions[1] = {true};
13uint32_t cre_modes[1] = {0};
14qf_ferm_op_add_term(cre, 1, cre_actions, cre_modes, &coeff);
15
16assert(!qf_ferm_op_conserves_particle_number(cre));
```

**Parameters**

- **op** – A pointer to the fermionic operator to be checked.

**Returns**

Whether the provided operator is particle-number conserving.

### qf\_ferm\_op\_conserves\_sector

`bool qf_ferm_op_conserves_sector(const QfFermionOperator *op, const uint32_t *block_sizes, uint64_t num_blocks)`

Checks whether an operator conserves the particle number within each mode block.

The `block_sizes` partition the mode range into consecutive, non-overlapping blocks: block `b` spans the modes `[start_b, start_b + block_sizes[b])` where `start_b` is the sum of all preceding block sizes. A term conserves the sector if and only if, in *every* block, its number of creation operators equals its number of annihilation operators. A term acting on a mode beyond the last block never conserves the sector, because there is no block whose count it could balance.

For a spin-orbital layout of `norb` spatial orbitals, `[norb]` checks the particle number of a spinless system, while `[norb, norb]` requires the alpha block `[0, norb)` and the beta block `[norb, 2 * norb)` to be conserved *separately*, i.e. strict conservation of both the particle number and the z-component of the spin.

#### Example

```c
 1// This term hops an electron from the beta block into the alpha block.
 2QfFermionOperator *op = qf_ferm_op_zero();
 3bool actions[2] = {true, false};
 4uint32_t modes[2] = {0, 2};
 5QkComplex64 coeff = {1.0, 0.0};
 6qf_ferm_op_add_term(op, 2, actions, modes, &coeff);
 7
 8// The total particle number is conserved, ...
 9uint32_t one_block[1] = {4};
10assert(qf_ferm_op_conserves_sector(op, one_block, 1));
11assert(qf_ferm_op_conserves_sector(op, NULL, 0));
12
13// ... but the two spin sectors are not conserved individually.
14uint32_t spin_blocks[2] = {2, 2};
15assert(!qf_ferm_op_conserves_sector(op, spin_blocks, 2));
```

> **Note**
>
> Passing `NULL` (with `num_blocks` set to `0`) treats all modes as one unbounded block, making this equivalent to [`qf_ferm_op_conserves_particle_number()`](#qf_ferm_op_conserves_particle_number "qf_ferm_op_conserves_particle_number").

**Parameters**

- **op** – A pointer to the fermionic operator to be checked.
- **block\_sizes** – A pointer to the array of block sizes partitioning the mode range. May be `NULL` to treat all modes as a single block.
- **num\_blocks** – The number of entries in the `block_sizes` array.

**Returns**

Whether the provided operator conserves the particle number within every block.

### qf\_ferm\_op\_equal

`bool qf_ferm_op_equal(const QfFermionOperator *left, const QfFermionOperator *right)`

Compare two operators for equality.

#### Example

```c
1QfFermionOperator *one = qf_ferm_op_one();
2QfFermionOperator *zero = qf_ferm_op_zero();
3
4assert(qf_ferm_op_equal(one, one));
5assert(!qf_ferm_op_equal(one, zero));
```

**Parameters**

- **left** – A pointer to the left operator.
- **right** – A pointer to the right operator.

**Returns**

Whether the two operators are equal.

### qf\_ferm\_op\_equiv

`bool qf_ferm_op_equiv(const QfFermionOperator *left, const QfFermionOperator *right, double atol)`

Compare two operators for equivalence.

Equivalence in this context means approximate equality up to the specified absolute tolerance. To be more precise, this method returns `True`, when all the absolute values of the coefficients in the difference `other - self` are below the specified threshold `atol`.

#### Example

```c
 1QfFermionOperator *zero = qf_ferm_op_zero();
 2
 3QfFermionOperator *op = qf_ferm_op_zero();
 4bool actions[0] = {};
 5uint32_t modes[0] = {};
 6QkComplex64 coeff = {1e-7, 0.0};
 7qf_ferm_op_add_term(op, 0, actions, modes, &coeff);
 8
 9assert(qf_ferm_op_equiv(op, zero, 1e-6));
10assert(!qf_ferm_op_equiv(op, zero, 1e-8));
```

**Parameters**

- **left** – A pointer to the left operator.
- **right** – A pointer to the right operator.
- **atol** – The absolute tolerance for coefficient equivalence.

**Returns**

Whether the two operators are equivalent.

### qf\_ferm\_op\_len

`size_t qf_ferm_op_len(const QfFermionOperator *op)`

Returns the length (or number of terms) of the provided operator.

#### Example

```c
1QfFermionOperator *op = qf_ferm_op_zero();
2bool actions[4] = {true, false, true, false};
3uint32_t modes[4] = {0, 1, 2, 3};
4QkComplex64 coeff = {1.0, 0.0};
5qf_ferm_op_add_term(op, 4, actions, modes, &coeff);
6
7assert(qf_ferm_op_len(op) == 1);
```

**Parameters**

- **op** – A pointer to the fermionic operator.

**Returns**

The length (or number of terms) of the operator.

### qf\_ferm\_op\_relabel\_modes

`QfExitCode qf_ferm_op_relabel_modes(QfFermionOperator *op, uint64_t num_modes, const uint32_t *permutation)`

Relabels the indices of the provided operator.

#### Example

```c
 1QfFermionOperator *op = qf_ferm_op_zero();
 2bool actions[4] = {true, false, true, false};
 3uint32_t indices[4] = {0, 1, 2, 3};
 4QkComplex64 coeff = {1.0, 0.0};
 5qf_ferm_op_add_term(op, 4, actions, indices, &coeff);
 6
 7uint32_t permutation[4] = {3, 2, 1, 0};
 8
 9QfExitCode exit = qf_ferm_op_relabel_modes(op, 4, permutation);
10
11assert(exit == QfExitCode_Success);
```

**Parameters**

- **op** – A pointer to the fermionic operator.
- **num\_modes** – The number of mode indices in the provided permutation list.
- **permutation** – The index permutation list.

**Returns**

An exit code.

- `QfExitCode_Success` upon success
- `QfExitCode_DuplicateIndexError` if duplicate indices were found in the permutation
- `QfExitCode_IndexError` for any other index errors, such as invalid indices.
