Filtering models
by: Chris Cave & Meera Machado
(Feyn version 3.0 or newer)
You can filter a list of Model
s you've sampled from a QLattice
, to only include Model
s that you're interested in. This can be achieved using the builtin Python filter function. Some of the more common use cases require some code, so we've included convenience functions to help.
Example
Here's a code snippet to sample some models we can filter on:
import feyn
# sample a list of models
ql = feyn.QLattice()
models = ql.sample_models(
input_names = ["Hello", "World"],
output_name = "Hola!"
)
Use the selector to navigate some examples for filter
functions that Feyn
ships with, but you can also make your own custom function:
Complexity
This filters Model
s of a certain complexity.
from feyn.filters import Complexity
# Get only models of complexity 2
f = Complexity(complexity=2)
f_models = list(filter(f, models))
ContainsInputs
This filters Model
s that contain the provided input(s).
from feyn.filters import ContainsInputs
# Get models that contain the "World" input
f = ContainsInputs("World")
f_models = list(filter(f, models))
# Get models that contain both "Hello" and "World" inputs
f = ContainsInputs(["Hello", "World"])
f_models = list(filter(f, models))
ContainsFunctions
This filters Model
s that only contain the provided function(s).
from feyn.filters import ContainsFunctions
# Get models that contains only the "Gaussian" function
f = ContainsFunctions("gaussian")
f_models = list(filter(f, models))
# Get models that contain only the "Gaussian" and "log" function
f = ContainsFunctions(["gaussian", "log"])
f_models = list(filter(f, models))
ExcludeFunctions
This filters Model
s that exclude the provided function(s).
from feyn.filters import ExcludeFunctions
# Get models that does not contains the "Gaussian" function
f = ExcludeFunctions("gaussian")
f_models = list(filter(f, models))
# Get models that do not contain either a "Gaussian" or a "log" function
f = ExcludeFunctions(["gaussian", "log"])
f_models = list(filter(f, models))
filter
Custom You can supply any function with the following signature to create your own filter:
def my_custom_filter(model):
return "World" in model.inputs
# Get models that contains the "World" input
f_models = list(filter(my_custom_filter, models))