Liking cljdoc? Tell your friends :D

org.soulspace.qclojure.application.algorithm.hhl

HHL (Harrow-Hassidim-Lloyd) Algorithm

The HHL algorithm is a quantum algorithm for solving linear systems of equations of the form Ax = b, where A is a Hermitian matrix. It provides exponential speedup over classical algorithms for certain classes of linear systems.

The algorithm works by:

  1. Encoding the vector b as a quantum state |b⟩
  2. Using quantum phase estimation to find eigenvalues of A
  3. Performing conditional rotation to compute A^(-1)
  4. Amplitude amplification to extract the solution

This implementation is designed for production use with:

  • General n×n Hermitian matrices
  • Integration with existing quantum phase estimation
  • Proper error handling and validation
  • Configurable precision and success probability

Key functions:

  • matrix-encoding-unitary: Encode Hermitian matrix as unitary evolution
  • vector-preparation-circuit: Prepare |b⟩ state from classical vector
  • hhl-circuit: Build complete HHL quantum circuit
  • hhl-algorithm: Execute HHL algorithm with backend
HHL (Harrow-Hassidim-Lloyd) Algorithm

The HHL algorithm is a quantum algorithm for solving linear systems of equations 
of the form Ax = b, where A is a Hermitian matrix. It provides exponential speedup 
over classical algorithms for certain classes of linear systems.

The algorithm works by:
1. Encoding the vector b as a quantum state |b⟩
2. Using quantum phase estimation to find eigenvalues of A
3. Performing conditional rotation to compute A^(-1)
4. Amplitude amplification to extract the solution

This implementation is designed for production use with:
- General n×n Hermitian matrices
- Integration with existing quantum phase estimation
- Proper error handling and validation
- Configurable precision and success probability

Key functions:
- matrix-encoding-unitary: Encode Hermitian matrix as unitary evolution
- vector-preparation-circuit: Prepare |b⟩ state from classical vector
- hhl-circuit: Build complete HHL quantum circuit
- hhl-algorithm: Execute HHL algorithm with backend
raw docstring

conditional-rotation-circuitclj

(conditional-rotation-circuit precision-qubits ancilla-qubit C)

Create circuit for conditional rotation to implement A^(-1).

This implements the key step of HHL: conditional rotation based on eigenvalues to achieve matrix inversion. For eigenvalue λ, we rotate an ancilla qubit by angle θ ∝ 1/λ.

The rotation implements |λ⟩|0⟩ → |λ⟩(cos(θ)|0⟩ + sin(θ)|1⟩) where θ = C/λ for some constant C.

For this to work, we need meaningful rotations that actually flip the ancilla qubit with reasonable probability.

Parameters:

  • precision-qubits: Number of qubits used for eigenvalue estimation
  • ancilla-qubit: Index of ancilla qubit for conditional rotation
  • C: Scaling constant for rotation angle

Returns: Function that takes a circuit and applies conditional rotations

Create circuit for conditional rotation to implement A^(-1).

This implements the key step of HHL: conditional rotation based on
eigenvalues to achieve matrix inversion. For eigenvalue λ,
we rotate an ancilla qubit by angle θ ∝ 1/λ.

The rotation implements |λ⟩|0⟩ → |λ⟩(cos(θ)|0⟩ + sin(θ)|1⟩)
where θ = C/λ for some constant C.

For this to work, we need meaningful rotations that actually flip
the ancilla qubit with reasonable probability.

Parameters:
- precision-qubits: Number of qubits used for eigenvalue estimation
- ancilla-qubit: Index of ancilla qubit for conditional rotation
- C: Scaling constant for rotation angle

Returns:
Function that takes a circuit and applies conditional rotations
sourceraw docstring

estimate-condition-numberclj

(estimate-condition-number matrix)

Estimate the condition number κ(A) = λ_max / λ_min of a Hermitian matrix.

The condition number affects the precision required for the HHL algorithm. For well-conditioned matrices (κ ≈ 1), fewer precision qubits are needed. For ill-conditioned matrices (κ >> 1), more precision qubits are required.

This uses Frobenius norm estimation. For higher accuracy, the algorithm can be extended with proper eigenvalue decomposition techniques.

Parameters:

  • matrix: Hermitian matrix

Returns: Estimated condition number (positive real number)

Estimate the condition number κ(A) = λ_max / λ_min of a Hermitian matrix.

The condition number affects the precision required for the HHL algorithm.
For well-conditioned matrices (κ ≈ 1), fewer precision qubits are needed.
For ill-conditioned matrices (κ >> 1), more precision qubits are required.

This uses Frobenius norm estimation. For higher accuracy, the algorithm
can be extended with proper eigenvalue decomposition techniques.

Parameters:
- matrix: Hermitian matrix

Returns:
Estimated condition number (positive real number)
sourceraw docstring

hhl-algorithmclj

(hhl-algorithm backend matrix b-vector options)

Execute the HHL algorithm to solve Ax = b.

This is the main entry point for the HHL algorithm. It builds the quantum circuit, executes it on the specified backend, and extracts the solution vector.

Parameters:

  • backend - Quantum backend to use
  • matrix: Hermitian matrix A (n×n)
  • b-vector: Input vector b (length n)
  • options: Algorithm options map with keys:
    • :precision-qubits - Number of qubits for eigenvalue precision (default: 4)
    • :ancilla-qubits - Number of ancilla qubits (default: 1)
    • :shots - Number of measurement shots (default: 1000)

Returns: Map containing:

  • :success - Boolean indicating if algorithm succeeded
  • :solution-vector - Estimated solution x (when successful)
  • :circuit - The quantum circuit used
  • :measurements - Raw measurement results
  • :condition-number - Estimated condition number of matrix
Execute the HHL algorithm to solve Ax = b.

This is the main entry point for the HHL algorithm. It builds the
quantum circuit, executes it on the specified backend, and extracts
the solution vector.

Parameters:
- backend - Quantum backend to use
- matrix: Hermitian matrix A (n×n)
- b-vector: Input vector b (length n)
- options: Algorithm options map with keys:
  - :precision-qubits - Number of qubits for eigenvalue precision (default: 4)
  - :ancilla-qubits - Number of ancilla qubits (default: 1)
  - :shots - Number of measurement shots (default: 1000)

Returns:
Map containing:
- :success - Boolean indicating if algorithm succeeded
- :solution-vector - Estimated solution x (when successful)
- :circuit - The quantum circuit used
- :measurements - Raw measurement results
- :condition-number - Estimated condition number of matrix
sourceraw docstring

hhl-circuitclj

(hhl-circuit matrix b-vector precision-qubits ancilla-qubits)

Build the complete HHL quantum circuit.

This constructs a functional HHL algorithm circuit that actually works:

  1. Vector preparation (encoding |b⟩)
  2. Quantum phase estimation to find eigenvalues
  3. Conditional rotation for matrix inversion
  4. Proper qubit management and meaningful operations

This implementation focuses on actually working rather than theoretical completeness.

Parameters:

  • matrix: Hermitian matrix A
  • b-vector: Input vector b
  • precision-qubits: Number of qubits for eigenvalue precision
  • ancilla-qubits: Number of ancilla qubits (typically 1)

Returns: Complete quantum circuit implementing HHL algorithm

Build the complete HHL quantum circuit.

This constructs a functional HHL algorithm circuit that actually works:
1. Vector preparation (encoding |b⟩)
2. Quantum phase estimation to find eigenvalues  
3. Conditional rotation for matrix inversion
4. Proper qubit management and meaningful operations

This implementation focuses on actually working rather than
theoretical completeness.

Parameters:
- matrix: Hermitian matrix A
- b-vector: Input vector b
- precision-qubits: Number of qubits for eigenvalue precision
- ancilla-qubits: Number of ancilla qubits (typically 1)

Returns:
Complete quantum circuit implementing HHL algorithm
sourceraw docstring

matrix-encoding-unitaryclj

(matrix-encoding-unitary matrix time-scale)

Create a unitary operation that encodes the Hermitian matrix A.

For HHL, we need to encode A as a unitary evolution U = e^(iAt) for some time t. This function returns a function that applies controlled-U^(2^k) operations needed for quantum phase estimation.

Parameters:

  • matrix: Hermitian matrix A to encode
  • time-scale: Time parameter t for evolution e^(iAt)

Returns: Function that takes (circuit, control-qubit, power, target-qubits) and applies controlled-U^power to the circuit

Create a unitary operation that encodes the Hermitian matrix A.

For HHL, we need to encode A as a unitary evolution U = e^(iAt) for some time t.
This function returns a function that applies controlled-U^(2^k) operations
needed for quantum phase estimation.

Parameters:
- matrix: Hermitian matrix A to encode
- time-scale: Time parameter t for evolution e^(iAt)

Returns:
Function that takes (circuit, control-qubit, power, target-qubits) and applies
controlled-U^power to the circuit
sourceraw docstring

validate-hermitian-matrixclj

(validate-hermitian-matrix matrix)

Validate that a matrix is Hermitian (A = A†).

For real matrices, this means the matrix is symmetric. For complex matrices, this means A[i,j] = conj(A[j,i]).

Parameters:

  • matrix: 2D vector representing the matrix

Returns: Boolean indicating if matrix is Hermitian

Example: (validate-hermitian-matrix [[1 2] [2 3]]) ;=> true (validate-hermitian-matrix [[1 2] [3 4]]) ;=> false

Validate that a matrix is Hermitian (A = A†).

For real matrices, this means the matrix is symmetric.
For complex matrices, this means A[i,j] = conj(A[j,i]).

Parameters:
- matrix: 2D vector representing the matrix

Returns:
Boolean indicating if matrix is Hermitian

Example:
(validate-hermitian-matrix [[1 2] [2 3]]) ;=> true
(validate-hermitian-matrix [[1 2] [3 4]]) ;=> false
sourceraw docstring

vector-preparation-circuitclj

(vector-preparation-circuit b-vector num-qubits)

Create a quantum circuit to prepare the state |b⟩ from classical vector b.

This function takes a classical vector b and creates a quantum circuit that prepares the corresponding quantum state |b⟩ = Σᵢ bᵢ|i⟩.

For HHL to work properly, we need accurate amplitude encoding. This implementation uses a simplified but effective approach.

Parameters:

  • b-vector: Classical vector b (will be normalized)
  • num-qubits: Number of qubits needed to represent the vector

Returns: Quantum circuit that prepares |b⟩ when applied to |0...0⟩

Create a quantum circuit to prepare the state |b⟩ from classical vector b.

This function takes a classical vector b and creates a quantum circuit
that prepares the corresponding quantum state |b⟩ = Σᵢ bᵢ|i⟩.

For HHL to work properly, we need accurate amplitude encoding.
This implementation uses a simplified but effective approach.

Parameters:
- b-vector: Classical vector b (will be normalized)
- num-qubits: Number of qubits needed to represent the vector

Returns:
Quantum circuit that prepares |b⟩ when applied to |0...0⟩
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close