Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoRenaud committed Nov 29, 2024
1 parent f911099 commit fd5987d
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 16 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Python package

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:

build:
name: Build for (${{ matrix.python-version }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ['ubuntu-latest']
python-version: ['3.8']
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Python info
shell: bash -l {0}
run: |
which python3
python3 --version
- name: Upgrade pip and install dependencies
run: |
python3 -m pip install --upgrade pip setuptools
python3 -m pip install .[dev,publishing]
- name: Run unit tests
run: pytest -v
- name: Verify that we can build the package
run: python3 setup.py sdist bdist_wheel
34 changes: 34 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Code coverage

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
coverage:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install tox
run: |
python -m pip install --upgrade pip
pip install tox coveragepy-lcov 'coverage<7'
- name: Run coverage
run: tox -ecoverage
- name: Convert to lcov
run: coveragepy-lcov --output_file_path coveralls.info
- name: Upload report to Coveralls
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
file: coveralls.info
format: lcov
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# HHL linear solvers
Contains an implementation to solve systems of linear equations using the HHL algorithm.
Original implementation by [@anedumla](https://github.com/anedumla) : https://github.com/anedumla/quantum_linear_solvers
![Platform](https://img.shields.io/badge/platform-Linux-blue)
[![Python](https://img.shields.io/badge/Python-3.8-informational)](https://www.python.org/)
[![Qiskit](https://img.shields.io/badge/Qiskit-%E2%89%A5%200.44.2-6133BD)](https://github.com/Qiskit/qiskit)
[![License](https://img.shields.io/github/license/quantumapplicationlab/hhl-prototype?label=License)](https://github.com/quantumapplicationlab/hhl-prototype/blob/main/LICENSE.txt)
[![Code style: Black](https://img.shields.io/badge/Code%20style-Black-000.svg)](https://github.com/psf/black)
[![Tests](https://github.com/quantumapplicationlab/hhl-prototype/actions/workflows/coverage.yml/badge.svg)](https://github.com/quantumapplicationlab/hhl-prototype/actions/workflows/coverage.yml)
[![Coverage Status](https://coveralls.io/repos/github/QuantumApplicationLab/hhl-prototype/badge.svg?branch=main)](https://coveralls.io/github/QuantumApplicationLab/hhl-prototype?branch=main)


# HHL Quantum Linear Solver
The `hhl-prototype` allows to use the HHL algorithm to solve linear system of euqations.
Original implementation was implemented by [@anedumla](https://github.com/anedumla) : https://github.com/anedumla/quantum_linear_solvers

## Installation
```python
Expand All @@ -9,5 +18,26 @@ cd hhl_prototype
pip install -e .
```

## Use

```python
import numpy as np
from hhl_prototype import HHL
from qiskit.primitives import Estimator, Sampler

# create the matrix
A = np.random.rand(4,4)
A = A+A.T

# create the right hand side
b = np.random.rand(4)

# create the solver and solve
hhl = HHL(estimator=Estimator(), sampler=Sampler())
hhl.solve(A, b)
```



## Documentation
Tutorial: https://qiskit.org/textbook/ch-applications/hhl_tutorial.html
6 changes: 3 additions & 3 deletions docs/hhl.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import numpy as np
from hhl_prototype import HHL

A = np.random.rand(4,4)
A = A+A.T
A = np.random.rand(4, 4)
A = A + A.T

b = np.random.rand(4)

hhl = HHL()
hhl.solve(A, b)
hhl.solve(A, b)
1 change: 0 additions & 1 deletion hhl_prototype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@
MatrixFunctional
"""

3 changes: 2 additions & 1 deletion hhl_prototype/observables/absolute_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import numpy as np

from qiskit import QuantumCircuit
#from qiskit.opflow import I, Z, TensoredOp

# from qiskit.opflow import I, Z, TensoredOp
from qiskit.quantum_info import SparsePauliOp as TensoredOp

from qiskit.quantum_info import Statevector
Expand Down
3 changes: 2 additions & 1 deletion hhl_prototype/observables/linear_system_observable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import numpy as np

from qiskit import QuantumCircuit
#from qiskit.opflow import TensoredOp

# from qiskit.opflow import TensoredOp
from qiskit.quantum_info import SparsePauliOp as TensoredOp


Expand Down
3 changes: 2 additions & 1 deletion hhl_prototype/observables/matrix_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
#from qiskit.opflow import I, Z, TensoredOp

# from qiskit.opflow import I, Z, TensoredOp
from qiskit.quantum_info import SparsePauliOp as TensoredOp

from .linear_system_observable import LinearSystemObservable
Expand Down
6 changes: 3 additions & 3 deletions hhl_prototype/solver/hhl.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ def __init__(
estimator: BaseEstimator,
sampler: Optional[Union[BaseSampler, None]] = None,
epsilon: float = 1e-2,
) -> None:
) -> None:
r"""
Args:
estimator: Estimator used for running circuit. Default is BaseEstimator
sampler: Sampler used for running circuit, overrides estimator. Default is None.
sampler: Sampler used for running circuit, overrides estimator. Default is None.
epsilon: Error tolerance of the approximation to the solution, i.e. if :math:`x` is the
exact solution and :math:`\tilde{x}` the one calculated by the algorithm, then
:math:`||x - \tilde{x}|| \le epsilon`.
Expand Down Expand Up @@ -152,7 +152,7 @@ def construct_circuit(
vector = np.array(vector)
nb = int(np.log2(len(vector)))
vector_circuit = QuantumCircuit(nb)
#prepare the vector b in the first quantum register
# prepare the vector b in the first quantum register

isometry = Isometry(vector / np.linalg.norm(vector), 0, 0)
vector_circuit.append(isometry, list(range(nb)))
Expand Down
5 changes: 2 additions & 3 deletions test/test_hhl.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ def create_random_matrix(size: int) -> sparray:
@pytest.mark.parametrize("b", [np.random.rand(size)])
@pytest.mark.parametrize("estimator", [Estimator()])
@pytest.mark.parametrize("sampler", [Sampler()])

def test_hhl_solve_default(A, b, estimator, sampler):
"""Test the hhl solver."""
hhl = HHL(estimator, sampler = sampler)
results = hhl.solve(A,b).solution
hhl = HHL(estimator, sampler=sampler)
results = hhl.solve(A, b).solution
if np.linalg.norm(A.dot(results) - b) > 0.1:
pytest.skip("HHL solution innacurate")
35 changes: 35 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[tox]
minversion = 2.1
envlist = py38, py39, py310, lint, coverage
# CI: skip-next-line
skip_missing_interpreters = true

[testenv]
# CI: skip-next-line
usedevelop = true
install_command = pip install -U {opts} {packages}
setenv =
VIRTUAL_ENV={envdir}
LANGUAGE=en_US
LC_ALL=en_US.utf-8
extras = dev
commands =
pip check
python -m pytest -v --doctest-modules
treon docs --threads 2

[testenv:black]
envdir = .tox/lint
skip_install = true
commands = black .

[testenv:coverage]
basepython = python3
setenv =
{[testenv]setenv}
commands =
coverage3 run --source hhl_prototype --parallel-mode -m pytest --doctest-modules
coverage3 combine
coverage3 report


0 comments on commit fd5987d

Please sign in to comment.