> ## Documentation Index
> Fetch the complete documentation index at: https://docs.classiq.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Constructing Block Encodings

`BlockEncoding` provides classmethod constructors for common input representations. This page
walks through a selection of them. More constructors are added over time, so check the
[SDK reference](/sdk-reference/applications/block_encoding) for the full, current list. Each
constructor infers `alpha`, `block_size`, and
`data_size` from its input, and (where possible) whether the resulting unitary is Hermitian —
pass `hermitian_be` explicitly to override the inference. This value isn't checked against the
actual unitary, so it's worth getting right (in particular,
[`qubitize`](/user-guide/applications/block-encoding/composing-block-encodings#qubitize) relies
on it being accurate).

All examples on this page import:

```python theme={null}
from classiq.applications.block_encoding import BlockEncoding
```

## `from_matrix`

Block-encodes an arbitrary matrix. This is the most general constructor: use it when your matrix
doesn't have a special structure that a more specific constructor could exploit. Being diagonal,
sparse, or already given as a Pauli sum are examples of such structure.

```python theme={null}
import numpy as np
from classiq.applications.block_encoding import BlockEncoding

matrix = np.array([[0.3, 1.7], [-0.9, 1.2]])
be = BlockEncoding.from_matrix(matrix)
print(be.alpha)
```

```
2.9
```

<Note>
  `be.to_matrix()` may not match `matrix` entrywise even though the encoding is correct. See
  [Verifying with `to_matrix`](/user-guide/applications/block-encoding/composing-block-encodings#to_matrix)
  for details.
</Note>

## `from_sparse_pauli_op`

Block-encodes a sum of weighted Pauli strings, given as a `SparsePauliOp` (see
[Measurements, Observables, and Hermitian Operators](/user-guide/modeling/observables-and-operators)).
This is more general than a Hamiltonian: the coefficients don't need to be real, so
the operator itself doesn't need to be Hermitian. Set `graycode=True` to implement the LCU select
operator with gray-code multiplexed rotations instead of the default unary iteration.

```python theme={null}
from classiq import *
from classiq.applications.block_encoding import BlockEncoding

pauli_op = 0.5 * Pauli.Z(0) * Pauli.Z(1) + 0.8 * Pauli.X(0)
be = BlockEncoding.from_sparse_pauli_op(pauli_op)
print(be.alpha)  # 1.3 -- the Pauli 1-norm sum(|c_j|)
```

```
1.3
```

This block encoding has a scaling factor which is the Pauli 1-norm $\sum_j \vert c_j \vert$, and a block size given by
$\max(\lceil \log_2(\#\text{terms}) \rceil, 1)$.

## `from_constant_diags`

Block-encodes a matrix whose diagonals each carry a single constant value across the whole
diagonal, a common pattern for discretized differential operators. `cyclic=False` (the
default) drops entries that would fall outside the matrix (open boundary); `cyclic=True` wraps
them around the matrix edges (periodic boundary) instead.

```python theme={null}
from classiq.applications.block_encoding import BlockEncoding

be = BlockEncoding.from_constant_diags([(2.0, 0), (0.5, 1), (0.5, -1)], size=2)
print(be.to_matrix().round(3))
```

`be.to_matrix()` prints:

| | 0 | 1 | 2 | 3 |
| - | - | - | - | - |
| 0 | 2.0 | 0.5 | 0.0 | 0.0 |
| 1 | 0.5 | 2.0 | 0.5 | 0.0 |
| 2 | 0.0 | 0.5 | 2.0 | 0.5 |
| 3 | 0.0 | 0.0 | 0.5 | 2.0 |

`size` is the number of data qubits (matrix dimension $2^{\text{size}}$).

## Next: combining encodings

Once you have one or more `BlockEncoding` instances, you can combine and transform them as described in
[Composing and Transforming Block Encodings](/user-guide/applications/block-encoding/composing-block-encodings).
