Skip to content

Commit

Permalink
Merge pull request #149 from sequence-toolbox/RnD
Browse files Browse the repository at this point in the history
Separate Parallel
  • Loading branch information
Alex-Kolar authored Sep 18, 2022
2 parents 1c5fc68 + ff6d89c commit 17107ea
Show file tree
Hide file tree
Showing 39 changed files with 410 additions and 143 deletions.
25 changes: 19 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: main

on:
push:
branches: [master, RnD]
branches: [master, RnD, remove_mpi]
pull_request:
branches: [master. RnD]
branches: [master. RnD, remove_mpi]

jobs:
build:
Expand All @@ -17,20 +17,33 @@ jobs:

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Set up MPI
uses: mpi4py/setup-mpi@v1
with:
mpi: ${{ matrix.mpi }}

- name: Install dependencies
run: |
pip install pytest
make setup
make install
- name: Test with pytest
run: |
make test
- name: Set up MPI
uses: mpi4py/setup-mpi@v1
with:
mpi: ${{ matrix.mpi }}

- name: Set up parallel code
run: |
cd parallel
make install
- name: Test parallel with pytest
run: |
cd parallel
make test_parallel
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Polished observer functionality
- Reworked some protocols to utilize new interface
- Some bug fixes for GUI

## [0.5.1]
### Changed
- Moved all parallel execution code to the parallel directory
- Now installed as separate package `psequence`
- New setup.py file and makefile specified in parallel folder
- Minimum sequence requirement 0.5.1
- Parallel scripts are temporarily broken, will need to be rewritten for new structure

### Removed
- Removed `mpi4py` and `pytest-mpi` requirements for main package
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ jupyter:

test:
pip3 install .
pytest ./tests

test_parallel:
mpiexec -n 2 pytest ./tests_parallel --with-mpi
pytest ./tests
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Xiaoliang Wu, Joaquin Chung, Alexander Kolar, Eugene Wang, Tian Zhong, Rajkumar Kettimuthu, Martin Suchara'

# The full version, including alpha/beta/rc tags
release = '0.5.0'
release = '0.5.1'


# -- General configuration ---------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Welcome to the SeQUeNCe documentation page
references/qkd/top
references/misc/top

.. toctree::
:maxdepth: 1
:caption: Parallel Module References:

references/parallel/top

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

Expand Down
3 changes: 0 additions & 3 deletions docs/source/references/kernel/top.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ The Kernel module provides the discrete event simulator for SeQUeNCe.
process
timeline
quantum_manager
p_timeline
quantum_manager_client
quantum_manager_server
quantum_state
5 changes: 5 additions & 0 deletions docs/source/references/parallel/communication.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Process Communication
=====================

.. automodule:: parallel.src.communication
:members:
5 changes: 5 additions & 0 deletions docs/source/references/parallel/p_quantum_manager.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Parallel Quantum Manager
============================

.. automodule:: parallel.src.p_quantum_manager
:members:
5 changes: 5 additions & 0 deletions docs/source/references/parallel/p_router_net_topo.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Parallel Router Net Topology
============================

.. automodule:: parallel.src.p_router_net_topo
:members:
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Parallel Timeline
=================

.. automodule:: src.kernel.p_timeline
.. automodule:: parallel.src.p_timeline
:members:
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Quantum Manager Client
======================

.. automodule:: src.kernel.quantum_manager_client
.. automodule:: parallel.src.quantum_manager_client
:members:
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Quantum Manager Server
======================

.. automodule:: src.kernel.quantum_manager_server
.. automodule:: parallel.src.quantum_manager_server
:members:
14 changes: 14 additions & 0 deletions docs/source/references/parallel/top.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Parallel
========

The parallel `psequence` package provides a parallel execution implementation for SeQUeNCe.

.. toctree::
:maxdepth: 2

p_timeline
p_router_net_topo
p_quantum_manager
quantum_manager_client
quantum_manager_server
communication
158 changes: 158 additions & 0 deletions example/QCE_demos/modify_script_method_function.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a1107282",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"To modify the `improved_fidelity` static method of entanglement purification protocol (`BBPSSW`).\n",
"\n",
"The modification is aimed at demonstrating entanglement purification under a different error model,\n",
"i.e. only single-qubit bit-flip errors exist. Thus the imperfect entangled states are no longer Werner-type\n",
"but simply a mixture of two Bell states: `desired_state` and its counterpart under single-qubit bit-flip.\n",
"In this case the improved fidelity is different from more general case where Werner states are considered.\n",
"\"\"\"\n",
"\n",
"from sequence.entanglement_management.purification import BBPSSW\n",
"\n",
"\n",
"def test_fid(F):\n",
" \"\"\"Only for testing this approach of modification.\"\"\"\n",
" \n",
" return 0.88\n",
"\n",
"def original_fid(F):\n",
" \"\"\"Only for record of original method in script before modification.\"\"\"\n",
" \n",
" return (F ** 2 + ((1 - F) / 3) ** 2) / (F ** 2 + 2 * F * (1 - F) / 3 + 5 * ((1 - F) / 3) ** 2)\n",
"\n",
"def new_improved_fid(F):\n",
" \"\"\"The improved fidelity corresponding under bit-flip error model.\"\"\"\n",
" \n",
" return F ** 2 / (F ** 2 + (1 - F) ** 2)\n",
"\n",
"BBPSSW.improved_fidelity = new_improved_fid"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af43fb2c",
"metadata": {},
"outputs": [],
"source": [
"\"\"\"To modify the `_set_state_with_fidelity` function defined in SeQUeNCe BSM modele.\n",
"\n",
"This modification is corresponding to the previous modification of purification improved fidelity.\n",
"Previously the physically generated entangled states are of Werner type, to demonstrate the difference\n",
"now they should be mixture of only two Bell states as discussed above.\n",
"\"\"\"\n",
"\n",
"import sequence.components.bsm as bsm_module\n",
"from sequence.kernel.quantum_manager import KET_STATE_FORMALISM, DENSITY_MATRIX_FORMALISM\n",
"\n",
"\n",
"BSM = bsm_module.BSM\n",
"\n",
"def test_method(keys, desired_state, fidelity, rng, qm):\n",
" \"\"\"Only for testing this approach of modification.\"\"\"\n",
" \n",
" possible_states = [BSM._phi_plus, BSM._phi_minus,\n",
" BSM._psi_plus, BSM._psi_minus]\n",
" assert desired_state in possible_states\n",
"\n",
" if qm.formalism == KET_STATE_FORMALISM:\n",
" qm.set(keys, desired_state)\n",
"\n",
" elif qm.formalism == DENSITY_MATRIX_FORMALISM:\n",
" state = outer(desired_state, desired_state)\n",
" qm.set(keys, state)\n",
"\n",
" else:\n",
" raise Exception(\"Invalid quantum manager with formalism {}\".format(qm.formalism))\n",
" \n",
"def _set_state_with_fidelity(keys, desired_state, fidelity, rng, qm):\n",
" \"\"\"Only for record of original method in script before modification.\"\"\"\n",
"\n",
" possible_states = [BSM._phi_plus, BSM._phi_minus,\n",
" BSM._psi_plus, BSM._psi_minus]\n",
" assert desired_state in possible_states\n",
"\n",
" if qm.formalism == KET_STATE_FORMALISM:\n",
" probabilities = [(1 - fidelity) / 3] * 4\n",
" probabilities[possible_states.index(desired_state)] = fidelity\n",
" state_ind = rng.choice(4, p=probabilities)\n",
" qm.set(keys, possible_states[state_ind])\n",
"\n",
" elif qm.formalism == DENSITY_MATRIX_FORMALISM:\n",
" multipliers = [(1 - fidelity) / 3] * 4\n",
" multipliers[possible_states.index(desired_state)] = fidelity\n",
" state = zeros((4, 4))\n",
" for mult, pure in zip(multipliers, possible_states):\n",
" state = add(state, mult * outer(pure, pure))\n",
" qm.set(keys, state)\n",
"\n",
" else:\n",
" raise Exception(\"Invalid quantum manager with formalism {}\".format(qm.formalism))\n",
"\n",
"def new_set_state(keys, desired_state, fidelity, rng, qm):\n",
" \"\"\"The new function to set generated entangled states under a different assumed error model.\"\"\"\n",
"\n",
" possible_states = [BSM._phi_plus, BSM._phi_minus,\n",
" BSM._psi_plus, BSM._psi_minus]\n",
" assert desired_state in possible_states\n",
"\n",
" if qm.formalism == KET_STATE_FORMALISM:\n",
" probabilities = [0] * 4\n",
" probabilities[possible_states.index(desired_state)] = fidelity\n",
" if desired_state == BSM._psi_plus:\n",
" probabilities[possible_states.index(BSM._phi_plus)] = 1 - fidelity\n",
" elif desired_state == BSM._psi_minus:\n",
" probabilities[possible_states.index(BSM._phi_minus)] = 1 - fidelity\n",
" state_ind = rng.choice(4, p=probabilities)\n",
" qm.set(keys, possible_states[state_ind])\n",
"\n",
" elif qm.formalism == DENSITY_MATRIX_FORMALISM:\n",
" multipliers = [0] * 4\n",
" multipliers[possible_states.index(desired_state)] = fidelity\n",
" if desired_state == BSM._psi_plus:\n",
" probabilities[possible_states.index(BSM._phi_plus)] = 1 - fidelity\n",
" elif desired_state == BSM._psi_minus:\n",
" probabilities[possible_states.index(BSM._phi_minus)] = 1 - fidelity\n",
" state = zeros((4, 4))\n",
" for mult, pure in zip(multipliers, possible_states):\n",
" state = add(state, mult * outer(pure, pure))\n",
" qm.set(keys, state)\n",
"\n",
" else:\n",
" raise Exception(\"Invalid quantum manager with formalism {}\".format(qm.formalism))\n",
"\n",
"\n",
"bsm_module._set_state_with_fidelity = new_set_state"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
8 changes: 8 additions & 0 deletions parallel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
install_no_pip:
python3 setup.py install

install:
pip3 install .

test_parallel:
mpiexec -n 2 pytest ./tests_parallel --with-mpi
4 changes: 2 additions & 2 deletions parallel/example/net_with_flow_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from json import load, dump

from sequence.app.request_app import RequestApp
from sequence.topology.router_net_topo import RouterNetTopo
from psequence.p_router_net_topo import ParallelRouterNetTopo


def main(config_file: str, flow_info_file: str, log_path: str):
Expand All @@ -21,7 +21,7 @@ def main(config_file: str, flow_info_file: str, log_path: str):
mpi_rank = MPI.COMM_WORLD.Get_rank()
mpi_size = MPI.COMM_WORLD.Get_size()

topo = RouterNetTopo(config_file)
topo = ParallelRouterNetTopo(config_file)
tl = topo.get_timeline()
tl.stop_time = STOP_TIME

Expand Down
2 changes: 1 addition & 1 deletion parallel/example/p_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from time import time
import numpy as np

from sequence.kernel.p_timeline import ParallelTimeline
from sequence.topology.node import QuantumRouter, BSMNode
from sequence.components.optical_channel import ClassicalChannel, QuantumChannel
from sequence.app.request_app import RequestApp
from psequence.p_timeline import ParallelTimeline
import sequence.utils.log as log


Expand Down
14 changes: 7 additions & 7 deletions parallel/example/rand_flow_by_hop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sequence.kernel.event import Event
from sequence.app.random_request import RandomRequestApp
from sequence.app.request_app import RequestApp
from sequence.topology.router_net_topo import RouterNetTopo
from psequence.p_router_net_topo import ParallelRouterNetTopo
import sequence.utils.log as log

if TYPE_CHECKING:
Expand Down Expand Up @@ -58,13 +58,13 @@ def get_net_qc_graph(config_file: str):
config = load(fh)

graph = {}
for node in config[RouterNetTopo.ALL_NODE]:
if node[RouterNetTopo.TYPE] == RouterNetTopo.QUANTUM_ROUTER:
graph[node[RouterNetTopo.NAME]] = []
for node in config[ParallelRouterNetTopo.ALL_NODE]:
if node[ParallelRouterNetTopo.TYPE] == ParallelRouterNetTopo.QUANTUM_ROUTER:
graph[node[ParallelRouterNetTopo.NAME]] = []

bsm_to_router_map = {}
for qc in config[RouterNetTopo.ALL_Q_CHANNEL]:
router, bsm = qc[RouterNetTopo.SRC], qc[RouterNetTopo.DST]
for qc in config[ParallelRouterNetTopo.ALL_Q_CHANNEL]:
router, bsm = qc[ParallelRouterNetTopo.SRC], qc[ParallelRouterNetTopo.DST]
if not bsm in bsm_to_router_map:
bsm_to_router_map[bsm] = router
else:
Expand Down Expand Up @@ -116,7 +116,7 @@ def main(config_file: str, log_path: str):
mpi_rank = MPI.COMM_WORLD.Get_rank()
mpi_size = MPI.COMM_WORLD.Get_size()

topo = RouterNetTopo(config_file)
topo = ParallelRouterNetTopo(config_file)
tl = topo.get_timeline()
tl.stop_time = STOP_TIME

Expand Down
Loading

0 comments on commit 17107ea

Please # to comment.