pyqpanda_alg.QSEncode¶
Submodules¶
Classes¶
Quantum Sparse State Encoding Class. |
Package Contents¶
- class pyqpanda_alg.QSEncode.QSpare_Code(prob_list=None, cut_length=None, mode='walsh')¶
Quantum Sparse State Encoding Class.
This class implements a quantum state preparation technique that encodes a sparse approximation of a probability distribution using either Walsh-Hadamard or Fourier basis. It allows for amplitude encoding and quantum circuit construction to simulate and analyze sparse distributions efficiently.
Parameters¶
- prob_listlist[float] or np.ndarray
A list of probabilities representing the distribution to encode.
- cut_lengthint, optional
The number of dominant components (in the transformed basis) to retain for sparse encoding. If not specified, defaults to
2 * log2(len(prob_list)).- modestr, optional
The basis transformation method, one of {‘walsh’, ‘fourier’}. Default is ‘walsh’.
Attributes¶
- probnp.ndarray
Normalized probability distribution (padded to power-of-2 length).
- qubits_numint
Number of qubits required for the state.
- ampnp.ndarray
Square root of probabilities, used for amplitude encoding.
- cutint
Number of top components retained after transformation.
- modestr
Basis transform mode used for sparse encoding.
Methods¶
- select_top_n_complex_numbers(arr, n)
Selects top-n complex values by magnitude from the input array and zeroes out the rest.
- Transform(amp)
Applies Walsh-Hadamard or Fourier transform to the amplitude vector based on the mode.
- quantum_cir(qubits)
Constructs a quantum circuit that prepares the sparse encoded quantum state.
- Quantum_Res()
Simulates the quantum circuit and returns the output probability distribution.
Examples
>>> import numpy as np >>> import matplotlib.pyplot as plt >>> from pyqpanda_alg.QSEncode import QSpare_Code >>> mu = 0 >>> sigma = 1 >>> x = np.linspace(-3, 3, 2 ** 10) >>> pdf_normal = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)) >>> # pdf_normal = (1 / (x * sigma * np.sqrt(2 * np.pi))) * np.exp(-(np.log(x) - mu) ** 2 / (2 * sigma ** 2)) >>> ini = pdf_normal / np.linalg.norm(pdf_normal) >>> # res = QSEncode.QSpare_Code(ini ** 2, mode='walsh', cut_length=80).Quantum_Res() >>> res = QSpare_Code(ini ** 2, mode='fourier', cut_length=20).Quantum_Res()
>>> plt.plot(x, res) >>> plt.plot(x, ini ** 2) >>> plt.show()
- prob¶
- qubits_num¶
- amp¶
- cut = None¶
- mode = 'walsh'¶
- select_top_n_complex_numbers(arr, n)¶
Selects top-n elements by magnitude from the input complex array.
Parameters¶
- arrnp.ndarray
The input array of complex or real values.
- nint
The number of largest-magnitude elements to keep.
Returns¶
- np.ndarray
A new array with only top-n magnitudes retained, rest set to zero.
- Transform(amp)¶
Applies the selected basis transformation to the input amplitude vector.
Parameters¶
- ampnp.ndarray
Input amplitude vector.
Returns¶
- np.ndarray
Transformed amplitude vector in the selected basis.