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

# Distribution loading

haiqu.distribution\_loading()

This notebook demonstrates how to use `haiqu.distribution_loading()` to efficiently prepare a probability distribution in a quantum state.

Loading a log-normal distribution on 12 qubits traditionally requires 4083 CNOT gates and a two-qubit gate depth of 4083. With Haiqu, the same distribution is prepared using only 21 CNOT gates (\~200x improvement) and a two-qubit gate depth of 11 (>350x improvement).

haiqu.distribution\_loading()

What does it do? Distribution loading prepares a quantum state whose measurement statistics match a desired probability density function. How do I use it? Pass a scipy distribution name, its parameters and intervals, and size of the quantum register to haiqu.distribution\_loading(). This will create a data loading job. The results can be retrieve with job.result().

What are the options? num\_layers and truncation\_cutoff for controling circut synthesis. Which option do you recommend? Start with the default settings. num\_layer = 2 is usually more than enought for most distributions.

Having a bug or an issue?

Submit feedback

Initialize the benchmark

Import the necessary libraries, initialize the Haiqu SDK.

```python theme={null}
import qiskit
import numpy as np
import pandas as pd
from haiqu.sdk import haiqu
import matplotlib.pyplot as plt
from qiskit_finance.circuit.library import LogNormalDistribution

haiqu.login()
haiqu.init("Distribution Loading Tutorial")
```

Run benchmark scenarios

Prepare log-normal distribution with traditional and Haiqu methods.

```python theme={null}
# set up the distribution
# support interval to encode
interval_start = 0
interval_end = 10
# amount of qubits to encode, creates 2^num_qubits grid points
num_qubits = 12
# parameters of the corresponding normal distribution
mu = 0  
sigma = 1

# Scenario 1: standard method
# We consider a method proposed in Qiskit, and available for Financial computations
circuit_qiskit = LogNormalDistribution(num_qubits, mu=mu, sigma=sigma**2, bounds=(interval_start, interval_end))
circuit_qiskit.name = "Qiskit"

# Scenario 2: Haiqu distribution loading
circuit_haiqu = qiskit.QuantumCircuit(num_qubits, name="Haiqu")
distribution_gate, fidelity = haiqu.distribution_loading(distribution_name="lognorm", num_qubits=num_qubits, s=sigma, scale=np.exp(mu),
                                                         interval_start=interval_start, interval_end=interval_end).result()
circuit_haiqu.compose(distribution_gate, inplace=True)
print(f"Haiqu distribution is loaded with fidelity: {fidelity:.3f}")

# show the distribution we load
plt.plot(circuit_qiskit.values, circuit_qiskit.probabilities)
plt.title("Log-normal distribution")
plt.xlabel("x")
plt.ylabel("p(x)")
```

Haiqu's distribution loading significantly outperforms standard methods as shown in the comparison table below:

```python theme={null}
# we consider an ideal device with all-to-all connectivity
device_ideal = haiqu.get_device("aer_simulator")

circuit_qiskit_transpiled = haiqu.transpile(circuit_qiskit, device=device_ideal)

circuit_haiqu_transpiled = haiqu.transpile(circuit_haiqu, device=device_ideal)

haiqu.compare_metrics(circuit_qiskit_transpiled, circuit_haiqu_transpiled)
```

Get in Touch

Documentation portal [docs.haiqu.ai](https://docs.haiqu.ai)

Contact Support [feedback.haiqu.ai](https://feedback.haiqu.ai)

Follow Us on LinkedIn latest news on [LinkedIn](https://www.linkedin.com/company/haiquai/)

Visit Our Website Learn more about Haiqu Inc. on [haiqu.ai](https://haiqu.ai)

Business Inquiries Contact us at [info@haiqu.ai](mailto:info@haiqu.ai)
