pyqpanda_alg.QSVD¶
Submodules¶
Classes¶
This class provides a framework for the Quantum Singular Value Decomposition (QSVD) algorithm, |
Package Contents¶
- class pyqpanda_alg.QSVD.SVD(matrix_in=None, depth=8)¶
This class provides a framework for the Quantum Singular Value Decomposition (QSVD) algorithm, which estimates the singular values of an input matrix via parameterized quantum circuits.
- Parameters
matrix_in :
array_like, optionalThe input real-valued matrix to be decomposed. The matrix will be zero-padded to the nearest power-of-two shape.
depth :
int, optional (default=8)The number of variational layers used in the parameterized quantum circuits. Must be in the range [2, 10].
- Attributes
matrix :
ndarrayThe input matrix converted into a 2D NumPy array.
q0 :
intNumber of qubits required to encode the number of matrix rows.
q1 :
intNumber of qubits required to encode the number of matrix columns.
q_matrix :
ndarrayThe zero-padded version of the input matrix to fit a 2^n × 2^m dimension.
normal_value :
floatNormalization factor (Frobenius norm of the input matrix).
iter_depth :
intDepth of the variational quantum circuit layers.
parameter :
ndarrayInitial random parameters used for variational optimization.
- Methods
- cir(qlist, para)
Builds a parameterized variational quantum circuit based on the input qubit list and parameter vector.
- loss(ls, return_type=True)
Computes the loss function between the diagonal of the original matrix and that of the decomposed one. If return_type is False, returns the reconstructed matrix and its dominant singular vector index.
- QSVD_min()
Optimizes the variational parameters using classical optimization (SLSQP) to minimize the loss function.
- return_diag(par)
Returns the diagonal-approximated matrix reconstructed from the optimized parameters.
- max_eig(return_mat=’0’, par=None, max_index=0)
Extracts the principal eigenvector of the reconstructed left or right unitary matrix from QSVD based on the given return_mat flag (‘0’ for left unitary, ‘1’ for right unitary), using the inverse quantum circuit.
Note
This implementation follows a hybrid quantum-classical approach using amplitude encoding, variational circuits, and classical optimization to extract approximated singular values of the input matrix.
- Requirements
This class depends on pyQPanda as the quantum programming interface and NumPy for numerical operations.
- Examples:
from pyqpanda_alg import QSVD import numpy as np matrix = np.random.random(64).reshape([8, 8]) para = QSVD.SVD(matrix_in=matrix).QSVD_min() qeig = QSVD.SVD(matrix_in=matrix).return_diag(para) l1 = QSVD.SVD(matrix_in=matrix).max_eig('0', para, 0) l2 = QSVD.SVD(matrix_in=matrix).max_eig('1', para, 0) print(l1) print(l2) print(np.sort(np.diag(qeig))) u, s, v = np.linalg.svd(matrix) print(u[:, 0]) print(v[0]) print(np.sort(s))
- matrix¶
- q0¶
- q1¶
- q_matrix¶
- normal_value¶
- iter_depth = 8¶
- parameter¶
- cir(qlist, para)¶
- loss(ls, return_type=True)¶
- QSVD_min()¶
- return_diag(par)¶
- max_eig(return_mat='0', par=None, max_index=0)¶