Skip to content

Commit

Permalink
using new overlap method
Browse files Browse the repository at this point in the history
  • Loading branch information
ziofil committed Aug 19, 2024
1 parent d9d23ed commit e049b1b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
6 changes: 3 additions & 3 deletions mrmustard/lab_dev/bb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
class GraphComponent:
r"""A lightweight "CircuitComponent" without the actual representation.
Basically a wrapper around Wires, so that it can emulate components in
a circuit. It exposes the repr, wires, shape, name and order (in the circuit).
a circuit. It exposes the repr, wires, shape, name and cost of obtaining
the component from previous contractions.
"""

def __init__(self, repr: str, wires: Wires, shape: list[int], name: str = "", cost: int = 0):
Expand Down Expand Up @@ -207,8 +208,7 @@ def parse_components(components: list[CircuitComponent]) -> Graph:
comp = GraphComponent.from_circuitcomponent(A)
graph.add_node(i, component=comp.copy())
for j, B in enumerate(components[i + 1 :]):
ovlp_ket = comp.wires.output.ket.modes & B.wires.input.ket.modes
ovlp_bra = comp.wires.output.bra.modes & B.wires.input.bra.modes
ovlp_bra, ovlp_ket = A.wires.overlap(B.wires)
if ovlp_ket or ovlp_bra:
graph.add_edge(i, i + j + 1)
comp.wires = Wires(
Expand Down
25 changes: 19 additions & 6 deletions mrmustard/lab_dev/wires.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from __future__ import annotations
from functools import cached_property
from typing import Optional, Sequence
import numpy as np

from IPython.display import display
Expand Down Expand Up @@ -194,7 +193,12 @@ def adjoint(self) -> Wires:
New ``Wires`` object obtained by swapping ket and bra wires.
"""
return Wires(
self.args[2], self.args[3], self.args[0], self.args[1], self.args[4], self.args[5]
self.args[2],
self.args[3],
self.args[0],
self.args[1],
self.args[4],
self.args[5],
)

@cached_property
Expand All @@ -221,7 +225,12 @@ def dual(self) -> Wires:
New ``Wires`` object obtained by swapping input and output wires.
"""
return Wires(
self.args[1], self.args[0], self.args[3], self.args[2], self.args[5], self.args[4]
self.args[1],
self.args[0],
self.args[3],
self.args[2],
self.args[5],
self.args[4],
)

@cached_property
Expand Down Expand Up @@ -343,6 +352,11 @@ def output(self) -> Wires:
ret._original = self.original or self # pylint: disable=protected-access
return ret

def overlap(self, other: Wires) -> tuple[set[int], set[int]]:
ovlp_ket = self.output.ket.modes & other.input.ket.modes
ovlp_bra = self.output.bra.modes & other.input.bra.modes
return ovlp_bra, ovlp_ket

@cached_property
def adjoint(self) -> Wires:
r"New ``Wires`` object obtained by swapping ket and bra wires."
Expand Down Expand Up @@ -492,9 +506,8 @@ def __repr__(self) -> str:
return f"Wires{self.args}"

def contracted_indices(self, other: Wires):
"""Returns the indices being contracted on self and other"""
ovlp_bra = sorted(self.output.bra.modes & other.input.bra.modes)
ovlp_ket = sorted(self.output.ket.modes & other.input.ket.modes)
"""Returns the indices being contracted on self and other on quantum wires."""
ovlp_bra, ovlp_ket = self.overlap(other)
idxA = self.output.bra[ovlp_bra].indices + self.output.ket[ovlp_ket].indices
idxB = other.input.bra[ovlp_bra].indices + other.input.ket[ovlp_ket].indices
return idxA, idxB
Expand Down

0 comments on commit e049b1b

Please sign in to comment.