> ## 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.

# Pretrain

#### Haiqu.pretrain(problem, \*, max\_time=60, seed=42, initial\_parameters=None, name=None, description=None)

Pretrain parameters for a variational quantum circuit to minimize the expectation value of input observable.

Accepts either a linear `VariationalProblem` (minimize a single observable's expectation) or a
`NonlinearVariationalProblem` (minimize a sympy objective over several named observables, whose
terms may include the `0`/`1` projector symbols). A linear problem is treated internally as the
trivial objective `"x"` over its single observable.

* **Parameters:**
  * **problem** ([*VariationalProblem*](variational.md#haiqu.sdk.qml.problem.VariationalProblem) *|* [*NonlinearVariationalProblem*](variational.md#haiqu.sdk.qml.problem.NonlinearVariationalProblem)) -- problem instance containing the
    ansatz circuit and either a single observable (linear) or a loss expression with named
    observables (nonlinear).
  * **max\_time** (*float*) -- maximal time (in seconds) the pretraining can take. If this time exceeds (not counting
    initialization and other overheads), then the current best result is returned. Defaults to 1 minute.
    Current maximal pretraining time is 15 minutes.
  * **seed** (*int* \*|\**None*) -- a seed for initial random initialization of weights. They are chosen from uniform
    distribution in the interval \[-π,π). Defaults to 42.
  * **initial\_parameters** (*list* \*\[\**float* *]* \*|\**None*) -- if specified, then these weights are used instead of random ones.
    Defaults to None.
  * **name** (*str* \*|\**None*) -- optional name of the job. If not set, then automatic will be generated.
  * **description** (*str* \*|\**None*) -- optional description of the job. If not set, then automatic will be generated.
* **Returns:**
  Job handle to track pretraining progress and retrieve results.
  : Call `job.result()` to retrieve the pretrained ansatz parameters as a `list[float]` (one entry per
  parameter in the input `VariationalProblem.ansatz`), suitable for use as `parameters` in
  [`run()`](../index.md#haiqu.sdk.quantum_haiqu.Haiqu.run) or as `initial_parameters` in [`variational_optimization()`](../index.md#haiqu.sdk.quantum_haiqu.Haiqu.variational_optimization).
  Run `help(job.result)` for the full description of result and `info` contents.
* **Return type:**
  PretrainingJobModel

#### Examples

```python theme={null}
>>> from qiskit import QuantumCircuit
>>> from qiskit.circuit.library import efficient_su2
>>> from qiskit.quantum_info import SparsePauliOp
>>> from haiqu.sdk.qml import VariationalProblem
>>> pqc = QuantumCircuit(5)
>>> pqc.compose(efficient_su2(num_qubits=pqc.num_qubits, reps=1), inplace=True)
>>> loss = SparsePauliOp(["ZIIII", "IIZXI"])
>>> problem = VariationalProblem(pqc, loss)
>>> job = haiqu.pretrain(problem, max_time=10)
>>> pretrained_params = job.result()  # accessing the pretrained parameters
>>> haiqu.run([problem.ansatz], observables=[problem.observable], parameters=[pretrained_params],
...           device=haiqu.get_device("aer_simulator")).result()  # checking the result
[[[-2.0]]]  # result may vary. -2 is the optimal loss for two independent pauli strings
```

Nonlinear objective over several observables (terms may include the `0`/`1` projectors):

```python theme={null}
>>> from haiqu.sdk.qml import NonlinearVariationalProblem
>>> problem = NonlinearVariationalProblem(
...     pqc, "1 - x/y", {"x": [("ZIIII", 1.0)], "y": [("0IIII", 0.5), ("1IIZI", -0.5)]}
... )
>>> job = haiqu.pretrain(problem, max_time=10)
```
