Skip to main content

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 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:
The same function given as a string:
Encoding a sine wave:
Encoding a complex-valued function (1j and I are equivalent):