From 1fbbfe0d7c283c906966e58c5e737f3cb7069c18 Mon Sep 17 00:00:00 2001 From: SS <66886825+EarlMilktea@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:38:34 +0900 Subject: [PATCH] :bento: Add examples --- examples/flow.py | 40 ++++++++++++++++++++++++++++++++++++++++ examples/gflow.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ examples/pflow.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 4 ++++ 4 files changed, 133 insertions(+) create mode 100644 examples/flow.py create mode 100644 examples/gflow.py create mode 100644 examples/pflow.py diff --git a/examples/flow.py b/examples/flow.py new file mode 100644 index 00000000..b4a7296f --- /dev/null +++ b/examples/flow.py @@ -0,0 +1,40 @@ +"""Example code for finding causal flow.""" + +# %% + +from __future__ import annotations + +import networkx as nx +from fastflow import flow + +g: nx.Graph[int] + +# %% + +# 1 - 3 - 5 +# | +# 2 - 4 - 6 +g = nx.Graph([(1, 3), (2, 4), (3, 5), (4, 6)]) +iset = {1, 2} +oset = {5, 6} + +result = flow.find(g, iset, oset) + +# Found +assert result is not None + +# %% + +# 1 - 3 +# \ / +# X +# / \ +# 2 - 4 +g = nx.Graph([(1, 3), (1, 4), (2, 3), (2, 4)]) +iset = {1, 2} +oset = {3, 4} + +# Not found +result = flow.find(g, iset, oset) + +assert result is None diff --git a/examples/gflow.py b/examples/gflow.py new file mode 100644 index 00000000..7c63aad7 --- /dev/null +++ b/examples/gflow.py @@ -0,0 +1,45 @@ +"""Example code for finding generalized flow.""" + +# %% + +from __future__ import annotations + +import networkx as nx +from fastflow import gflow +from fastflow.common import Plane + +g: nx.Graph[int] + +# %% + +# 0 - 1 +# /| | +# 4 | | +# \| | +# 2 - 5 - 3 +g = nx.Graph([(0, 1), (0, 2), (0, 4), (1, 5), (2, 4), (2, 5), (3, 5)]) +iset = {0, 1} +oset = {4, 5} +planes = {0: Plane.XY, 1: Plane.XY, 2: Plane.XZ, 3: Plane.YZ} + +result = gflow.find(g, iset, oset, planes) + +# Found +assert result is not None + +# %% + +# 1 - 3 +# \ / +# X +# / \ +# 2 - 4 +g = nx.Graph([(1, 3), (1, 4), (2, 3), (2, 4)]) +iset = {1, 2} +oset = {3, 4} +# Omitting planes (all Plane.XY) + +result = gflow.find(g, iset, oset) + +# Not found +assert result is None diff --git a/examples/pflow.py b/examples/pflow.py new file mode 100644 index 00000000..3d3be1bf --- /dev/null +++ b/examples/pflow.py @@ -0,0 +1,44 @@ +"""Example code for finding Pauli flow.""" + +# %% + +from __future__ import annotations + +import networkx as nx +from fastflow import pflow +from fastflow.common import PPlane + +g: nx.Graph[int] + +# %% + +# 1 2 3 +# | / | +# 0 - - - 4 +g = nx.Graph([(0, 1), (0, 2), (0, 4), (3, 4)]) +iset = {0} +oset = {4} +pplanes = {0: PPlane.Z, 1: PPlane.Z, 2: PPlane.Y, 3: PPlane.Y} + +result = pflow.find(g, iset, oset, pplanes) + +# Found +assert result is not None + +# %% + +# 1 - 3 +# \ / +# X +# / \ +# 2 - 4 +g = nx.Graph([(1, 3), (1, 4), (2, 3), (2, 4)]) +iset = {1, 2} +oset = {3, 4} +# Omitting pplanes (all PPlane.XY) + +# NOTE: This results in warning (use gflow.find if pplanes has no Pauli measurements) +result = pflow.find(g, iset, oset) + +# Not found +assert result is None diff --git a/pyproject.toml b/pyproject.toml index 4838e38b..879f172f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -117,6 +117,10 @@ convention = "numpy" "D1", # undocumented-XXX "INP", ] +"examples/**/*.py" = [ + "INP", + "S101", # assert +] "tests/*.py" = [ "D1", # undocumented-XXX "INP",