Skip to content

Commit

Permalink
use filter function in noise filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles MOUSSA committed Nov 21, 2024
1 parent 5efe7c1 commit 0e49465
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions qadence/noise/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,22 @@ def list(cls) -> list:
return list(filter(lambda el: not el.startswith("__"), dir(cls)))

def filter(self, protocol: NoiseEnum | str) -> NoiseHandler | None:
is_protocol: list = list()
if protocol == NoiseProtocol.READOUT:
is_protocol = [p == protocol for p in self.protocol]

def filter_fn(p: NoiseEnum | str) -> bool:
return p == protocol

else:
is_protocol = [isinstance(p, protocol) for p in self.protocol] # type: ignore[arg-type]

def filter_fn(p: NoiseEnum | str) -> bool:
return isinstance(p, protocol) # type: ignore[arg-type]

protocol_matches: list = list(filter(filter_fn, self.protocol))
# if we have at least a match
if sum(is_protocol) > 0:
if True in protocol_matches:
return NoiseHandler(
list(compress(self.protocol, is_protocol)),
list(compress(self.options, is_protocol)),
list(compress(self.protocol, protocol_matches)),
list(compress(self.options, protocol_matches)),
)
return None

Expand Down

0 comments on commit 0e49465

Please sign in to comment.