Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qubo fault detection #9

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
0208f34
Adding and setup changes for QC hackathon 2023 for Team 8, and making…
akashkthkr Apr 28, 2023
1634505
Merge pull request #1 from qcswat/first_commit
akashkthkr Apr 28, 2023
0c8ed56
Added code to import graph,visualize it and compute centralitiy of no…
YaserAlOsh Apr 29, 2023
308b002
Added random 0s and 1s
YaserAlOsh Apr 29, 2023
3929192
Added the QML code for debugging
Apr 29, 2023
dd24030
qubo fault detection
Basant-Abdelaal Apr 30, 2023
ec401f0
added WDN animation
selindoga Apr 30, 2023
e67ab57
Update README.md
AnasMM19 Apr 30, 2023
883499f
Updated README.md with QML part
rathilakshika Apr 30, 2023
6ce3b4d
Update README.md
selindoga Apr 30, 2023
3f12ba9
added requiremens.txt and qso.py
AnasMM19 Apr 30, 2023
41b2e55
Added the code for leak localisation - QML
rathilakshika Apr 30, 2023
267ba20
Delete QML_debugging.ipynb
rathilakshika Apr 30, 2023
b85a133
Add files via upload
rathilakshika Apr 30, 2023
dc9bd58
Delete qml_debugging.py
rathilakshika Apr 30, 2023
0af9231
Add files via upload
rathilakshika Apr 30, 2023
5b89a22
use pyqubo
Basant-Abdelaal Apr 30, 2023
dc1f099
update cost function
Basant-Abdelaal Apr 30, 2023
87429c7
update cost function
Basant-Abdelaal Apr 30, 2023
8aa8f0f
defiend MagnetoHydroSensor class
AnasMM19 Apr 30, 2023
2695fee
Merge pull request #5 from qcswat/QML_leakage_detection
akashkthkr Apr 30, 2023
da3f53d
Adding QML and changes to github
akashkthkr Apr 30, 2023
d8aef1a
Merge branch 'QML_code'
akashkthkr Apr 30, 2023
4c8858c
Create new WDN animation.gif
selindoga Apr 30, 2023
7ea8c44
Docs
akashkthkr Apr 30, 2023
34c1c61
add data
qdevpsi3 Apr 30, 2023
1d23d7a
annealer call
Basant-Abdelaal Apr 30, 2023
e35e751
add docs conf
qdevpsi3 Apr 30, 2023
78c19f4
Merge branch 'main' into AnasMM19-patch-1
vtomole Apr 30, 2023
d3f1765
Update README.md
vtomole Apr 30, 2023
d141230
Update README.md
vtomole Apr 30, 2023
ebb954d
Merge pull request #2 from qcswat/AnasMM19-patch-1
vtomole Apr 30, 2023
b715312
Merge branch 'main' into selindoga-patch-1
vtomole Apr 30, 2023
53ef4e1
Merge pull request #3 from qcswat/selindoga-patch-1
vtomole Apr 30, 2023
173a092
Merge branch 'main' into qso_branch
vtomole Apr 30, 2023
a5424a0
Merge pull request #4 from qcswat/qso_branch
vtomole Apr 30, 2023
8a44e50
Remove directory
vtomole Apr 30, 2023
d9dcde4
Merge branch 'main' into Qubo_Fault_Detection
vtomole Apr 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.vscode/
node_modules/

Expand Down
10,881 changes: 10,881 additions & 0 deletions Graph_viz.ipynb

Large diffs are not rendered by default.

498 changes: 498 additions & 0 deletions QML_rancode.ipynb

Large diffs are not rendered by default.

59 changes: 18 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
[<img src="https://qbraid-static.s3.amazonaws.com/logos/Launch_on_qBraid_white.png" width="150">](https://account.qbraid.com?gitHubUrl=https://github.com/qBraid/NYUAD-2023.git)

# NYUAD Hackathon for Social Good in the Arab World: Focusing on Quantum Computing (QC)
# NYUAD Hackathon for Social Good in the Arab World: Focusing on Quantum Computing (QC) and UN Sustainable Development Goals (SDGs).

https://nyuad.nyu.edu/en/events/2022/march/nyuad-hackathon-event.html

March 30 - April 1, 2022

## Technical challenge

_Create a program that applies one or more quantum algorithms to a social good
Expand All @@ -18,48 +14,29 @@ problem of your choice._
- Variational Quantum Eigensolver (VQE)
- Quantum Approximate Optimization Algorithm (QAOA)

**Social good topic examples**:

- Healthcare
- Science (e.g. AI, cryptography, biochemistry)
- Environment (climate)
- Education & Literacy
- Food Securities
- Crisis & Public Safety
- Financial Modeling
- Gaming

**Implementation requirements**:

- Must utilize quantum hardware available through preferably IBM. However, we
set also have AWS devices available as well.
- You are also free to use any technology which allows you to solve the
challenge.
### Qubo Fault Detection
The program is used for fault detection and diagnosis of graph based networks. The implementation is based on qubo explained in [paper](https://arxiv.org/pdf/1406.7601.pdf).

**Bonus requirements**:
**Requirements:**
- Install pyqubo using: pip install pyqubo

- Incorporate noisy simulation through IBM
- Incorporate a hybrid quantum-classical task through IBM
**Input to the program:**
- Sensor readings
- Paths from source to sensor

# qBraid Tutorials

Here, we provide useful tutorials on how to use qBraid-Lab, along with tutorials
on quantum computing, using IBM or Amazon Braket. The ladder were provided by
the [qiskit-tutorials](https://github.com/qiskit/qiskit-tutorials) and the
[amazon-braket-examples](https://github.com/aws/amazon-braket-examples) github
repositories repsectively.
---

The repository is structured as follows:
<p align="center">
<img width="460" height="460" src="https://github.com/qcswat/qatrah/blob/main/WDN%20animation.gif">
</p>

- [Setting up Qiskit environment](qbraid_qiskit_setup/accessing_ibm_hardware.ipynb)
## Replacing Classical Pressure Sensors with Optimized Quantum Sensors

---
Compared to classical pressure sensors, quantum sensors are not invasive. They are also tolerant to the changes in the environment around it while also being more accurate. This improves the ability to detect pipe leakage.

## <a name="qbraid">Setting up Qiskit environment in qBraid</a>
## Leak Detection and Localization

- [**Install Qiskit in qBraid-Lab**](qbraid_qiskit_setup/accessing_ibm_hardware.ipynb)
### Using Quantum Machine Learning
Existing classical literature, suggests the use of machine learning to predict leakage and localise it to a particular pipe using the data from pressure sensors in the WDN at any given point of time. We attempt to solve the same using a quantum machine learning based model.

- [**Enable Qiskit QPU access through qBraid-CLI**](qbraid_qiskit_setup/accessing_ibm_hardware.ipynb)
- [**Example environent setup on qBraid Youtube video**](https://www.youtube.com/watch?v=LyavbzSkvRo) (Please use the code EHNU6626)

---
Specifically, we collect the pressure data from the optimally-placed sensors in a water distribution network to predict leakage in the WDN using a quantum neural network. It is implemented in the Pennylane framework using Jax. The data is fed into the model using Angle encoding. The model is composed of a parametrised quantum circuit with RY, RZ and CNOT gates which are trained over a total of 500 epochs. We use a train to test-set ratio of 4:1 and optimise the model using Rectified adam over the binary cross-entropy loss. At the end we obtain a test accuracy of 87.02% over the dataset of size 650.
Binary file added WDN_animation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
7 changes: 7 additions & 0 deletions docs/_includes/qml_debugging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Debugging module
================

.. automodule:: qml_debugging
:members:
:undoc-members:
:show-inheritance:
68 changes: 68 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = "QatraH"
copyright = "2023, QatraH Dev Eng"
author = "QatraH Dev Eng"
release = "0.0.1"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = []

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

language = "pyhton, jupyter, quantum computing, qiskit, pennylane, jax, qbraid"

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "alabaster"
html_static_path = []

import os
import sys

sys.path.insert(0, os.path.abspath(".."))

# The full version, including alpha/beta/rc tags
release = "0.0.1"

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.

autosummary_generate = True
extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon"]


# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_material"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = []
html_show_sourcelink = False
23 changes: 23 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.. QatraH documentation master file, created by
sphinx-quickstart on Sun Apr 30 14:32:59 2023.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Welcome to QatraH's documentation!
==================================

.. toctree::
:maxdepth: 2
:caption: Contents:

_includes/qml_debugging




Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
103 changes: 103 additions & 0 deletions fault_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from pyqubo import Binary
import neal




def read_input():
sensor_file = open("sensor_readings.txt", "r")
sensor_readings = []
sensor_readings.append(1)
sensor_readings.extend([int(x) for x in next(sensor_file).split(',')])

paths_file = open("paths.txt", "r")
number_of_nodes = int(next(paths_file))
paths = [[]]
for line in paths_file:
paths.append([int(x) for x in line.split(',')])

return sensor_readings, paths, number_of_nodes


def create_optimization_function(sensor_readings, paths, number_of_nodes):
H = 0
number_of_sensors = len(sensor_readings) - 1
lambdaPath = 1
# Add H consistent
for y in range(1, number_of_sensors+1):
l = sensor_readings[y]
# for y in range(1, number_of_sensors+1):
if l == 1:
H+= (lambdaPath * Binary('y'+str(y)) * len(paths[y]))
for x in paths[y]:
H+= (-lambdaPath * Binary('y'+str(y)) * Binary('x'+str(x)))
else:
H+= (lambdaPath*Binary('y'+str(y))*((1-len(paths[y]))**2))
for x in paths[y]:
H+= (lambdaPath*Binary('y'+str(y))*(Binary('x'+str(x))**2) + 2*lambdaPath*Binary('y'+str(y))*(1-len(paths[y]))*Binary('x'+str(x)))
for j in paths[y]:
if x<j:
H+= (2*lambdaPath*Binary('y'+str(y))*Binary('x'+str(x))*Binary('x'+str(j)))

# Add H numFaults
for y in range(1, number_of_sensors+1):
H+= (1-Binary('y'+str(y)))

for x in range(1, number_of_nodes+1):
H+= (1-Binary('x'+str(x)))

return H


def print_variables(best_sample):
Nodes = {}
Sensors = {}
for key in best_sample:
if "*" in key:
if best_sample[key] == 0:
pass
key1, key2 = [x for x in key.split(" * ")]
if "x" in key1:
Nodes[key1] = best_sample[key]
else:
Sensors[key1] = best_sample[key]
if "x" in key2:
Nodes[key2] = best_sample[key]
else:
Sensors[key2] = best_sample[key]
else:
if "x" in key:
Nodes[key] = best_sample[key]
else:
Sensors[key] = best_sample[key]

print("Node Values:")
for key in Nodes:
print(key, ": ", Nodes[key])

print("Sensor Values:")
for key in Sensors:
print(key, ": ", Sensors[key])


if __name__ == '__main__':
# Read the input
sensor_readings, paths, number_of_nodes = read_input()

# Create optimization function
H = create_optimization_function(sensor_readings, paths, number_of_nodes)

# Create model
model = H.compile()
bqm = model.to_bqm()

# Get the best samples
sa = neal.SimulatedAnnealingSampler()
sampleset = sa.sample(bqm, num_reads=10)
decoded_samples = model.decode_sampleset(sampleset)
best_sample = min(decoded_samples, key=lambda x: x.energy)

# Print the binary variables values
# print(best_sample.sample)
print_variables(best_sample.sample)

Loading