Skip to main content

Haiqu.function_loading(num_qubits, func, interval_start, interval_end, synthesis_options=None, num_layers=None, truncation_cutoff=None, fine_tuning_iterations=None, 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 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 are controlled by synthesis_options.

NOTE

Amplitude indexing (Qiskit little-endian). The prepared state has 2**num_qubits amplitudes. data[i] is the amplitude for computational-basis index i, where the rightmost bit of i is qubit 0 (q_0). For example, with 2 qubits, data = [a, b, c, d] prepares a|00⟩ + b|01⟩ + c|10⟩ + d|11⟩. Apply the returned gate with circuit.append(gate, range(num_qubits)) so gate wire k acts on q_k. Inspect amplitudes after loading with statevector_run().

NOTE

Discretization order. The interval [interval_start, interval_end] is divided into 2**num_qubits equal bins. Bin i (counting from 0) spans increasing position along the interval, and its midpoint value becomes amplitude index i using the same Qiskit little-endian basis indexing as vector_loading() (rightmost bit of i is q_0).
  • 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. The interval width interval_end - interval_start must be at least 1e-08.
    • synthesis_options (dict | DataLoadingSynthesisOptions) — How the circuit preparing the data is synthesized. Pass one of AnalyticalSynthesisOptions, BlockwiseSynthesisOptions or VariationalSynthesisOptions, or an equivalent dict whose "method" key names one of them. Those three classes document every option each method accepts, what it means and what it defaults to. Defaults to None, which this method unwraps into AnalyticalSynthesisOptions(num_layers=2, layer_type="V", bond_dim_layer=2, entanglement_cutoff=1e-6, fine_tuning_iterations=20).
    • num_layers (int) — Deprecated. Pass num_layers inside synthesis_options.
    • truncation_cutoff (Real) — Deprecated. Renamed to entanglement_cutoff inside synthesis_options.
    • fine_tuning_iterations (int) — Deprecated. Pass fine_tuning_iterations inside synthesis_options instead.
    • 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. In a notebook, a link to the job on the Dashboard is also displayed when haiqu.verbosity is "high".
  • Return type: DataLoadingJobModel

Examples

Encoding a Gaussian given as a SymPy expression:
The same function given as a string:
Encoding a sine wave:
Encoding a complex-valued function (1j and I are equivalent):