Skip to content

Commit

Permalink
added stub for extended gflow
Browse files Browse the repository at this point in the history
  • Loading branch information
akissinger committed Nov 4, 2024
1 parent 24f87ce commit c1aa214
Show file tree
Hide file tree
Showing 2 changed files with 511 additions and 46 deletions.
132 changes: 93 additions & 39 deletions pyzx/gflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

r"""
Based on algorithm by Perdrix and Mhalla. Here is the pseudocode from
dx.doi.org/10.1007/978-3-540-70575-8_70
```
input : An open graph
output: A generalised flow
gFlow (V,Gamma,In,Out) =
begin
for all v in Out do
l(v) := 0
end
return gFlowaux (V,Gamma,In,Out,1)
end
gFlowaux (V,Gamma,In,Out,k) =
begin
C := {}
for all u in V \\ Out do
Solve in F2 : Gamma[V \\ Out, Out \\ In] * I[X] = I[{u}]
if there is a solution X0 then
C := C union {u}
g(u) := X0
l(u) := k
end
end
if C = {} then
return (Out = V,(g,l))
else
return gFlowaux (V, Gamma, In, Out union C, k + 1)
end
end
```
"""

from typing import Dict, Set, Tuple, Optional

from .extract import bi_adj
Expand All @@ -60,10 +24,41 @@
def gflow(
g: BaseGraph[VT, ET]
) -> Optional[Tuple[Dict[VT, int], Dict[VT, Set[VT]], int]]:
"""Compute the maximally delayed gflow of a diagram in graph-like form.
r"""Compute the maximally delayed gflow of a diagram in graph-like form.
Based on algorithm by Perdrix and Mhalla.
See dx.doi.org/10.1007/978-3-540-70575-8_70
```
input : An open graph
output: A generalised flow
gFlow (V,Gamma,In,Out) =
begin
for all v in Out do
l(v) := 0
end
return gFlowaux (V,Gamma,In,Out,1)
end
gFlowaux (V,Gamma,In,Out,k) =
begin
C := {}
for all u in V \\ Out do
Solve in F2 : Gamma[V \\ Out, Out \\ In] * I[X] = I[{u}]
if there is a solution X0 then
C := C union {u}
g(u) := X0
l(u) := k
end
end
if C = {} then
return (Out = V,(g,l))
else
return gFlowaux (V, Gamma, In, Out union C, k + 1)
end
end
```
"""
l: Dict[VT, int] = {}
gflow: Dict[VT, Set[VT]] = {}
Expand Down Expand Up @@ -96,12 +91,13 @@ def gflow(
if any(w in processed_prime for w in g.neighbors(v))
]

zerovec = Mat2([[0] for i in range(len(candidates))])
zerovec = Mat2.zeros(len(candidates), 1)

# print(unprocessed, processed_prime, zerovec)
m = bi_adj(g, processed_prime, candidates)
for u in candidates:
vu = zerovec.copy()
vu.data[candidates.index(u)] = [1]
vu.data[candidates.index(u)] = [1] # type:ignore
x = m.solve(vu)
if x:
correct.add(u)
Expand All @@ -115,3 +111,61 @@ def gflow(
else:
processed.update(correct)
k += 1


def extended_gflow() -> None:
r"""Compute the maximally delayed extended (i.e. 3-plane) gflow of a diagram in graph-like form.
Based on the algorithm in "There and Back Again"
See https://arxiv.org/pdf/2003.01664
For reference, here is the pseudocode from that paper:
```
input: an open graph, given as:
- M: an adjancency matrix M
- I: input rows/cols
- O: output rows/cols
- lambda: a choice of measurement plane for each vertex
output: extended gflow, given as a triple (success, g, d) where:
- g: assigns each vertex its correction set
- d: assigns each vertex a "depth", i.e. the number of layers away from the output layer. This
is related to the flow ordering as v ≺ w <=> d(v) > d(w)
EXT_GFLOW(M, I, O, lambda)
initialise functions g and d
foreach (v in O)
d(v) <- 0 // Outputs have depth 0
end
return GFLOWAUX(M, I, O, lambda, 1, d, g) // Start the recursive process of finding Vj^≺
end
GFLOWAUX(M, I, O, lambda, k, d, g)
O' <- O \\ I
C <- {}
foreach (u in O^⟂)
if lambda(u) = XY
K' <- Solution for K Δ O' where Odd(K) ∩ O^⟂ = {u}
elseif lambda(u) = XZ
K' <- {u} ∪ (Solution for K Δ O' where Odd(K ∪ {u}) ∩ O^⟂ = {u})
elseif lambda(u) = YZ
K' <- {u} ∪ (Solution for K Δ O' where Odd(K ∪ {u}) ∩ O^⟂ = {})
end
if K' exists
C <- C ∪ {u}
g(u) <- K' // Assign a correction set for u
d(u) <- k // Assign a depth value for u
end
end
if C = {}
if O = V
return (true, g, d) // Halt, returning maximally delayed g and depth values d
else
return (false, {}, {}) // Halt if no gflow exists
end
else
return GFLOWAUX(M, I, O ∪ C, lambda, k+1, d, g)
end
end
```
"""
pass # TODO stub
Loading

0 comments on commit c1aa214

Please sign in to comment.