pyqpanda_alg.HHL.HHL¶
Functions¶
|
Use HHL algorithm to solve the target linear systems of equations \(Ax = b\) |
|
Extending linear equations to \(N\) dimension, \(N = 2^n\) |
|
Build the quantum circuit for HHL algorithm to solve the target linear systems of equations \(Ax = b\) |
Module Contents¶
- pyqpanda_alg.HHL.HHL.HHL_solve_linear_equations(matrix_A: List[complex], data_b: List[float], precision_cnt: int = 0)¶
Use HHL algorithm to solve the target linear systems of equations \(Ax = b\)
- Parameters
matrix_A :
listParameter matrix_A for a unitary matrix or Hermitian \(N*N\) matrix with \(N = 2^n\)
data_b :
listParameter matrix_A for a given vector
precision_cnt :
integerParameter precision_cnt for the count of digits after the decimal point, default is 0, indicates that there are only integer solutions.
- Return
QStat :
listThe solution of equation, i.e.x for \(Ax = b\)
- Example
Run a HHL algorithm circuit of solving problem \(Ax = b\) with \(A = [[1, 0], [0, 1]]\) and \(b = [0.6, 0.8]^T\).
#!/usr/bin/env python import numpy as np from pyqpanda_alg.HHL import HHL_solve_linear_equations if __name__ == "__main__": A=[1,0,0,1] b=[0.6,0.8] result = HHL_solve_linear_equations(A,b,1) # print HHL result for key in result: print(key)
The output result should be identical to the right-hand side vector, which is \([0.6,0.8]\), with the imaginary component set to 0. Small perturbations may introduce minor errors.
(0.5999999999999998+0j) (0.7999999999999994+0j)
Note
The higher the precision is, the more qubit number and circuit - depth will be, for example: 1 - bit precision, 4 additional qubits are required, for 2 - bit precision, we need 7 additional qubits, and so on.
- pyqpanda_alg.HHL.HHL.expand_linear_equations(matrix_A: List[complex], data_b: List[float])¶
Extending linear equations to \(N\) dimension, \(N = 2^n\)
- Parameters
matrix_A :
listParameter matrix_A for a source matrix, which will be extend to \(N*N\) with \(N = 2^n\)
data_b :
listParameter matrix_A for a given vector, which will be extend to \(2^n\)
- Return
Result :
listThe list of matrix and vector, which are extended
- Example
Run Extending linear equations to \(N\) dimension, \(N = 2^n\) with \(A = [[2.0, 1.0, 0], [1.0, 2.0, 1.0], [0, 1.0, 2.0]]\) and \(b = [1.0, 1.0, 1.0]^T\).
#!/usr/bin/env python from pyqpanda_alg.HHL import expand_linear_equations if __name__ == "__main__": A = [2.0, 1.0, 0, 1.0, 2.0, 1.0, 0, 1.0, 2.0] b = [1.0, 1.0, 1.0] result = expand_linear_equations(A, b) print(result[0]) print(result[1])
The codes above would give results like:
[(2+0j), (1+0j), 0j, 0j, (1+0j), (2+0j), (1+0j), 0j, 0j, (1+0j), (2+0j), 0j, 0j, 0j, 0j, 0j] [1.0, 1.0, 1.0, 0.0]
- pyqpanda_alg.HHL.HHL.build_HHL_circuit(matrix_A: List[complex], data_b: List[float], precision_cnt: int = 0)¶
Build the quantum circuit for HHL algorithm to solve the target linear systems of equations \(Ax = b\)
- Parameters
matrix_A :
listParameter matrix_A for a unitary matrix or Hermitian \(N*N\) matrix with \(N = 2^n\)
data_b :
listParameter matrix_A for a given vector
precision_cnt :
integerParameter precision_cnt for the count of digits after the decimal point, default is 0, indicates that there are only integer solutions
- Return
QCircuit :
QCircuitThe whole quantum circuit for HHL algorithm
- Example
Run a build HHL algorithm circuit of solving problem \(Ax = b\) with \(A = [[1, 0], [0, 1]]\) and \(b = [0.6, 0.8]^T\).
#!/usr/bin/env python from pyqpanda_alg.HHL import build_HHL_circuit if __name__ == "__main__": A=[1.0, 0, 0, 1.0] b=[0.6, 0.8] prog = build_HHL_circuit(A, b, 1) print(prog)
The output result should be identical to the right-hand side vector, which is \([0.6,0.8]\), with the imaginary component set to 0. Small perturbations may introduce minor errors.
┌──────────────┐ ┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ > q_0: |0>─┤RY(1.85459044)├ ┤ORACLE├ ┤ORACLE├ ┤ORACLE├ ┤ORACLE├ ┤ORACLE├ ┤ORACLE├ ─────── ──────────────────── ─────────────────────────── > ├─┬────────────┘ └───┬──┘ └───┬──┘ └───┬──┘ └───┬──┘ └───┬──┘ └───┬──┘ ┌─────┐ > q_1: |0>─┤H├───────────── ────┼─── ────┼─── ────┼─── ────┼─── ────┼─── ────*─── ┤H.dag├ ──────────*───────── ─────────────────*───────── > ├─┤ │ │ │ │ │ └─────┘ ┌─────────┴────────┐ ┌─────┐ │ > q_2: |0>─┤H├───────────── ────┼─── ────┼─── ────┼─── ────┼─── ────*─── ──────── ─────── ┤CP(1.57079633).dag├ ┤H.dag├──────────┼───────── > ├─┤ │ │ │ │ └──────────────────┘ └─────┘┌─────────┴────────┐ > q_3: |0>─┤H├───────────── ────┼─── ────┼─── ────┼─── ────*─── ──────── ──────── ─────── ──────────────────── ───────┤CP(0.78539816).dag├ > ├─┤ │ │ │ └──────────────────┘ > q_4: |0>─┤H├───────────── ────┼─── ────┼─── ────*─── ──────── ──────── ──────── ─────── ──────────────────── ─────────────────────────── > ├─┤ │ │ > q_5: |0>─┤H├───────────── ────┼─── ────*─── ──────── ──────── ──────── ──────── ─────── ──────────────────── ─────────────────────────── > ├─┤ │ > q_6: |0>─┤H├───────────── ────*─── ──────── ──────── ──────── ──────── ──────── ─────── ──────────────────── ─────────────────────────── > └─┘ > q_7: |0>───────────────── ──────── ──────── ──────── ──────── ──────── ──────── ─────── ──────────────────── ─────────────────────────── > > c : / ════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ ... ...Note
The higher the precision is, the more qubit number and circuit - depth will be, for example: 1 - bit precision, 4 additional qubits are required, for 2 - bit precision, we need 7 additional qubits, and so on. The final solution = (HHL result) * (normalization factor for b) * (1 << ceil(log2(pow(10, precision_cnt)))).