Skip to content

Commit

Permalink
Merge branch 'ChitambarLab:main' into feature/PPT
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bhatia authored Aug 1, 2024
2 parents 268d62c + b516495 commit f12986b
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 63 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/build_documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ammaraskar/sphinx-action@master
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.4.1
with:
docs-folder: "docs/"
access_token: ${{ github.token }}
- uses: actions/checkout@v2
- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .
pip install -r docs/requirements.txt
- name: Build Documentation
run: |
sphinx-build -b html docs/source/ docs/build/html
- name: Commit documentation changes
run: |
git clone https://github.com/ChitambarLab/qNetVO.git --branch gh-pages --single-branch gh-pages
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/check_documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ammaraskar/sphinx-action@master
with:
docs-folder: "docs/"
- uses: actions/checkout@v2
- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .
pip install -r docs/requirements.txt
- name: Build Documentation
run: |
sphinx-build -b html docs/source/ docs/build/html
2 changes: 1 addition & 1 deletion .github/workflows/formatting_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.11

- name: Install dependencies
run: pip install black
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ jobs:

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Set up Python 3.8
- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# qNetVO: The Quantum Network Variational Optimizer
# qNetVO: Quantum Network Variational Optimizer

*Simulate and optimize quantum communication networks using quantum computers.*

Expand Down Expand Up @@ -32,7 +32,7 @@ $ pip install qnetvo
Install PennyLane:

```
$ pip install pennylane==0.29
$ pip install pennylane==0.37
```

Import packages:
Expand All @@ -48,7 +48,7 @@ Note
</p>
<p>
For optimal use, qNetVO should be used with PennyLane.
QNetVO is currently compatible with PennyLane v0.29.
QNetVO is currently compatible with PennyLane v0.37.
</p>
</div>

Expand Down
8 changes: 4 additions & 4 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.
sphinx>=3.0
sphinx
Pygments>=2.7.4
m2r2==0.3.3
sphinxcontrib-email==0.3.5
sphinx-mdinclude>=0.6
sphinxcontrib-email==0.3.6
furo
pennylane==0.28
pennylane==0.37
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"sphinx.ext.mathjax",
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
"m2r2",
"sphinx_mdinclude",
"sphinxcontrib.email",
]
source_suffix = [".rst", ".md"]
Expand Down
8 changes: 3 additions & 5 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ name: qnetvo-dev
channels:
- defaults
dependencies:
- python=3.8
- black
- python=3.11
- sphinx
- black
- pytest
- notebook
- matplotlib
- pip
- pip:
- -r requirements.txt
- furo
- m2r2==0.3
- sphinxcontrib-email==0.3.5
- -r docs/requirements.txt
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pennylane~=0.29
qiskit==0.34
pennylane-qiskit==0.20
pennylane==0.37
qiskit>=1
pennylane-qiskit==0.37
tensorflow>=2.0,<3.0
dask[delayed]==2022.5
tensornetwork>=0.3,<0.4
flaky==3.7
pytest-rerunfailures
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = qNetVO
version = 0.4.3
version = 0.4.4
author = Brian Doolittle and Tom Bromley
author_email = [email protected]
description = The Quantum Network Variational Optimizer
Expand All @@ -18,9 +18,9 @@ classifiers =
package_dir =
= src
packages = find:
python_requires = >=3.8
python_requires = >=3.9
install_requires =
pennylane~=0.29
pennylane==0.37

[options.packages.find]
where = src
8 changes: 5 additions & 3 deletions src/qnetvo/gradient_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ def gradient_descent(
if not (isinstance(settings, list)):
settings = [settings]
elif interface == "tf":
# opt.minimize updates settings in place
tf_cost = lambda: cost(*settings)
opt.minimize(tf_cost, settings)
with tf.GradientTape() as tape:
tf_cost = cost(*settings)

gradients = tape.gradient(tf_cost, settings)
opt.apply_gradients(zip(gradients, settings))

elapsed = time.time() - start

Expand Down
4 changes: 3 additions & 1 deletion src/qnetvo/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ def shannon_entropy(probs):
:rtype: float
"""
return -(
math.sum([px * math.log2(px) if px != 0 and not (np.isclose(px, 0)) else 0 for px in probs])
math.sum(
[px * math.log2(px) if px != 0 and not (math.isclose(px, 0)) else 0 for px in probs]
)
)
1 change: 1 addition & 0 deletions src/qnetvo/lazy_dask_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Lazily imports dask when needed.
Throws an error if the user does not have dask[delayed] installed.
"""

try:
import dask

Expand Down
1 change: 1 addition & 0 deletions src/qnetvo/lazy_tensorflow_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Lazily imports tensorflow when needed.
Throws an error if the user does not have tensorflow installed.
"""

try:
import tensorflow

Expand Down
5 changes: 2 additions & 3 deletions test/chsh_gradient_descent_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from flaky import flaky
import pennylane as qml
from pennylane import numpy as np

Expand Down Expand Up @@ -81,7 +80,7 @@ def test_chsh_gradient_descent_inhomogeneous_settings(self):

assert np.isclose(opt_dict["opt_score"], 2 * np.sqrt(2), atol=1e-3)

@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_parallel_chsh_gradient_descent(self):
prepare_nodes = [
qnet.PrepareNode(1, [0, 1], self.bell_state_RY, 2),
Expand All @@ -108,7 +107,7 @@ def test_parallel_chsh_gradient_descent(self):

assert np.isclose(opt_dict["opt_score"], 2 * np.sqrt(2), atol=1e-3)

@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_parallel_chsh_natural_gradient_descent(self):
prepare_nodes = [
qnet.PrepareNode(1, [0, 1], qnet.ghz_state, 0),
Expand Down
17 changes: 14 additions & 3 deletions test/cost/mutual_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,26 @@ class TestShannonEntropy:
def test_shannon_entropy_pure_state(self):
np.random.seed(123)

prep_node = [qnet.PrepareNode(1, [0, 1], qnet.ghz_state, 0)]
meas_node = [qnet.MeasureNode(1, 4, [0, 1], qml.ArbitraryUnitary, 4**2 - 1)]
def test_meas_circ(settings, wires):
qml.CNOT(wires=wires[0:2])
qml.RZ(settings[0], wires=wires[0])
qml.RY(settings[1], wires=wires[0])

prep_node = [qnet.PrepareNode(wires=[0, 1], ansatz_fn=qnet.ghz_state)]
meas_node = [
qnet.MeasureNode(num_out=4, wires=[0, 1], ansatz_fn=test_meas_circ, num_settings=2)
]

ansatz = qnet.NetworkAnsatz(prep_node, meas_node)
shannon_entropy = qnet.shannon_entropy_cost_fn(ansatz)

settings = ansatz.rand_network_settings()
opt_dict = qnet.gradient_descent(
shannon_entropy, settings, step_size=0.08, sample_width=5, num_steps=30
shannon_entropy,
settings,
step_size=0.15,
sample_width=5,
num_steps=35,
)

assert np.isclose(opt_dict["scores"][-1], 0, atol=0.0005)
Expand Down
7 changes: 3 additions & 4 deletions test/cost/nlocal_chain_bell_inequality_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from flaky import flaky
from pennylane import numpy as np
import pennylane as qml

Expand Down Expand Up @@ -63,7 +62,7 @@ def test_nlocal_chain_22_cost_fn(self, parallel_flag):
),
],
)
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_parallel_nlocal_chain_grad_fn(self, natural_grad, prep_nodes, meas_nodes):
chain_ansatz = qnet.NetworkAnsatz(prep_nodes, meas_nodes)

Expand All @@ -81,7 +80,7 @@ def test_parallel_nlocal_chain_grad_fn(self, natural_grad, prep_nodes, meas_node
assert np.allclose(grad, grad_match)

@pytest.mark.parametrize("parallel_flag", [True, False])
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_chain_J22_fn(self, parallel_flag):
prep_nodes = [
qnet.PrepareNode(1, [0, 1], qnet.local_RY, 2),
Expand Down Expand Up @@ -132,7 +131,7 @@ def test_chain_J22_fn(self, parallel_flag):
assert np.isclose(4, J22(*settings))

@pytest.mark.parametrize("parallel_flag", [True, False])
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_chain_I22_fn(self, parallel_flag):
prep_nodes = [
qnet.PrepareNode(1, [0, 1], qnet.local_RY, 2),
Expand Down
23 changes: 11 additions & 12 deletions test/cost/nlocal_star_bell_inequality_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from flaky import flaky
from pennylane import numpy as np
import pennylane as qml

Expand Down Expand Up @@ -39,7 +38,7 @@ def trilocal_star_ry_ansatz(self):
return qnet.NetworkAnsatz(prep_nodes, meas_nodes)

@pytest.mark.parametrize("parallel_flag, nthreads", [(False, 4), (True, 4), (True, 3)])
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_bilocal_star_22_cost(self, parallel_flag, nthreads):
bilocal_star_ansatz = self.bilocal_star_ry_ansatz()
bilocal_22_cost = qnet.nlocal_star_22_cost_fn(
Expand All @@ -55,7 +54,7 @@ def test_bilocal_star_22_cost(self, parallel_flag, nthreads):
assert np.isclose(bilocal_22_cost(*ideal_settings), -(np.sqrt(2)))

@pytest.mark.parametrize("parallel_flag, nthreads", [(False, 4), (True, 4), (True, 5)])
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_trilocal_star_cost(self, parallel_flag, nthreads):
trilocal_star_ansatz = self.trilocal_star_ry_ansatz()
trilocal_22_cost = qnet.nlocal_star_22_cost_fn(
Expand Down Expand Up @@ -86,7 +85,7 @@ def test_trilocal_star_cost(self, parallel_flag, nthreads):
assert np.isclose(trilocal_22_cost(*ideal_settings), -np.sqrt(2))

@pytest.mark.parametrize("parallel_flag, nthreads", [(False, 4), (True, 4), (True, 5)])
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_bilocal_star_22_cost_gradient_descent(self, parallel_flag, nthreads):
bilocal_star_ansatz = self.bilocal_star_ry_ansatz()

Expand All @@ -99,15 +98,15 @@ def test_bilocal_star_22_cost_gradient_descent(self, parallel_flag, nthreads):
num_steps=10,
step_size=2,
sample_width=10,
grad_fn=qnet.parallel_nlocal_star_grad_fn(bilocal_star_ansatz)
if parallel_flag
else None,
grad_fn=(
qnet.parallel_nlocal_star_grad_fn(bilocal_star_ansatz) if parallel_flag else None
),
)

assert np.isclose(opt_dict["opt_score"], np.sqrt(2), atol=0.0001)

@pytest.mark.parametrize("parallel_flag, nthreads", [(False, 4), (True, 4), (True, 5)])
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_trilocal_star_22_cost_gradient_descent(self, parallel_flag, nthreads):
trilocal_star_ansatz = self.trilocal_star_ry_ansatz()

Expand All @@ -118,15 +117,15 @@ def test_trilocal_star_22_cost_gradient_descent(self, parallel_flag, nthreads):
num_steps=8,
step_size=2,
sample_width=10,
grad_fn=qnet.parallel_nlocal_star_grad_fn(trilocal_star_ansatz)
if parallel_flag
else None,
grad_fn=(
qnet.parallel_nlocal_star_grad_fn(trilocal_star_ansatz) if parallel_flag else None
),
)

assert np.isclose(opt_dict["opt_score"], np.sqrt(2), atol=0.0001)

@pytest.mark.parametrize("nthreads", [3, 4])
@flaky(max_runs=5, min_passes=1)
@pytest.mark.flaky(5)
def test_bilocal_star_22_cost_natural_gradient_descent(self, nthreads):
bilocal_star_ansatz = self.bilocal_star_ry_ansatz()

Expand Down
8 changes: 4 additions & 4 deletions test/network_nodes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def circuit(settings, wires, cc_wires):

assert len(tape) == 1
assert tape.wires.tolist() == [0, 1]
assert tape.num_params == 0
assert tape.get_parameters() == []
assert tape.num_params == 1
assert all(tape.get_parameters()[0] == mock_settings)


def test_network_node_defaults():
Expand Down Expand Up @@ -223,5 +223,5 @@ def circuit(settings, wires, cc_wires):

assert len(tape) == 1
assert tape.wires.tolist() == [0, 1]
assert tape.num_params == 0
assert tape.get_parameters() == []
assert tape.num_params == 1
assert all(tape.get_parameters()[0] == mock_settings)
Loading

0 comments on commit f12986b

Please sign in to comment.