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

# Function Loading

#### Haiqu.function\_loading(num\_qubits, func, interval\_start, interval\_end, num\_layers=2, truncation\_cutoff=1e-06, fine\_tuning\_iterations=20, max\_time=900, name=None)

Generate a quantum circuit that prepares the values of a single-variable function in its amplitudes.

Given a function `f(x)`, this method creates a Data Loading job that runs in the Haiqu cloud. The result of this
job is a circuit gate which prepares a state whose amplitudes are the function values, L2-normalized as a quantum state.
The resulting gate can be used to supply the function to a quantum algorithm for processing.

The function is provided as a [SymPy](https://www.sympy.org) expression or as a string, and must depend on the
single variable `x` only (e.g. `"sin(x)"`, `"exp(-x**2)"`, `"x**2 + 1"`). The variable `x` is real-valued
and ranges over the real interval `[interval_start, interval_end]`. The function values may be complex even though
`x` is real; the imaginary unit can be written Python-style as `1j` or SymPy-style as `I`
(e.g. `"exp(1j*x)"` or `"exp(I*x)"`).

The function is discretized on a grid of `2**num_qubits` points: it is evaluated at the center (midpoint) of each
bin of the interval, and the resulting values are normalized as a quantum state. Singularities and non-finite values
(`nan`, `+/-inf`) are nullified (set to 0).

The complexity and quality of the generated circuit can be controlled by the `num_layers`, `truncation_cutoff`,
and `fine_tuning_iterations` parameters.

* **Parameters:**
  * **num\_qubits** (*int*) -- The number of qubits in the generated circuit (from 1 to 1000 qubits).
  * **func** (*str* *|* *sympy.Expr*) -- The function to encode, given as a SymPy expression or a string in the single
    variable `x`.
  * **interval\_start** (*Real*) -- The beginning of the interval on which the function is sampled.
  * **interval\_end** (*Real*) -- The end of the interval on which the function is sampled.
  * **num\_layers** (*int*) -- The number of layers in the generated circuit (from 1 to 100 layers).
    More layers can improve the quality of the output
    function at the cost of a deeper circuit. Defaults to 2.
  * **truncation\_cutoff** (*Real*) -- The entanglement cutoff for later layers. Increasing this threshold may result in a smaller
    (but more approximate) circuit. Defaults to `1e-6`.
  * **fine\_tuning\_iterations** (*int*) -- The maximum number of fine-tuning iterations to perform after each layer is added.
    Increasing this limit may improve the quality of the circuit by using more classical
    resources. Defaults to 20, maximal is 500.
  * **max\_time** (*int* *|* *float*) -- Soft time limit for the job (in seconds).
    The data loading job will first always produce the initial result and then limit the fine-tuning
    stage by the remaining time left. If time limit exceeds during the fine-tuning - the best
    current result will be returned. Defaults to 900
    (15 min). Max allowed job time is 15 min.
    The job can take more wall clock time than user specified max\_time due to latency,
    initialization overheads or if the initial result already takes more time.
  * **name** (*str* *|* *None*) -- The name for the job and the produced circuit. If `None` (default), a name will be automatically
    generated.
* **Returns:**
  The Data Loading job that will generate the circuit for the function.
  : Call `job.result()` to retrieve a Qiskit-compatible gate (`HaiquCircuitGate`) that prepares the function
  values. `job.quality` is the achieved state fidelity vs. the ideal target function; `job.info` exposes
  loader metadata (`fidelity`).
  Run `help(job.result)` for the full description of result and `info` contents.
* **Return type:**
  DataLoadingJobModel

#### Examples

Encoding a Gaussian given as a SymPy expression:

```python theme={null}
>>> import sympy
>>> x = sympy.Symbol("x")
>>> job = haiqu.function_loading(num_qubits=6, func=sympy.exp(-x**2), interval_start=-3, interval_end=3)
>>> fl_gate = job.result()  # fl_gate is a Qiskit-compatible gate
>>> print(f"Function was loaded with fidelity {job.quality:.6f}")
Function was loaded with fidelity 0.999518
```

The same function given as a string:

```python theme={null}
>>> job = haiqu.function_loading(num_qubits=6, func="exp(-x**2)", interval_start=-3, interval_end=3)
```

Encoding a sine wave:

```python theme={null}
>>> job = haiqu.function_loading(num_qubits=6, func="sin(x)", interval_start=-5, interval_end=5)
```

Encoding a complex-valued function (`1j` and `I` are equivalent):

```python theme={null}
>>> job = haiqu.function_loading(num_qubits=6, func="exp(1j*x)", interval_start=0, interval_end=10)
```
