-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom_deserialisation argument to BasePass.from_dict #1647
Conversation
[](const py::dict &base_pass_dict, | ||
const std::map< | ||
std::string, std::function<Circuit(const Circuit &)>> | ||
&custom_deserialisation) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This argument should be optional, then we don't break the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
pytket/pytket/_tket/passes.pyi
Outdated
@@ -54,7 +54,7 @@ class BasePass: | |||
:param after_apply: Invoked after a pass is applied. The CompilationUnit and a summary of the pass configuration are passed into the callback. | |||
:return: True if pass modified the circuit, else False | |||
""" | |||
def to_dict(self) -> dict: | |||
def to_dict(self) -> json: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't this still return a dict
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is auto generated based on the serialise
method now returning json
in the c++
nlohmann::json serialise(const BasePass& bp); | ||
nlohmann::json serialise(const PassPtr& pp); | ||
nlohmann::json serialise(const std::vector<PassPtr>& pp); | ||
|
||
PassPtr deserialise( | ||
const nlohmann::json& j, | ||
const std::map<std::string, std::function<Circuit(const Circuit&)>>& |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we called these to_json
and from_json
as before, can we avoid some of the changes in CompilerPass.cpp
(not sure how the magic in nlohmann::json
works).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(And also change the signature to match the signatures in JSON_DECL
but with the addition of the one optional argument.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My look through the nlohmann docs/code suggested that this wasn't possible, with the nlohmann magic relying one using well defined to_json/from_json with two arguments - the json and the object. So I don't think this is possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add new tests for cusom-pass round serialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
What about typing Then we can do something like this: config ={
"optimisation_level":optimisation_level,
"seed":seed
}
def get_routing_pass(config:Dict) -> CustomPass:
def transformation(circ: Circuit) -> Circuit:
# code for transforming the circuit
# that uses the config
return Circuit()
return CustomPass(
transformation,
label="MyPass",
config=config
)
# serialisation
serialised_pass = get_routing_pass(config).to_dict()
# deserialisation
deserialised_pass = BasePass.from_dict(
serialised_pass,
custom_deserialisation={
"MyPass": get_routing_pass
}
) This does require adding a config field to |
…CQCL/tket into custom-deserialisation-basepass
I think we would have to force the config to be std::map<std::string, int> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, just a couple of small comments.
Please also update changelog.
pytket/binders/passes.cpp
Outdated
}, | ||
"Construct a new Pass instance from a JSON serializable dictionary " | ||
"representation.") | ||
"representation.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs more documentation of the custom_deserialization
parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
Co-authored-by: Alec Edgington <54802828+cqc-alec@users.noreply.github.com>
…CQCL/tket into custom-deserialisation-basepass
Description
Replaces the use of
nlohmann::json
implicit construction of c++ objects from json usingjson
andfrom_json
with a new methoddeserialise
that directly produces thePassPtr
object. This allows a new argumentcustom_deserialisation
to be supplied, which is a map betweenstd::string
corrresponding toCustomPass
label and a Circuit->Circuit function that can be used to generate acustomPass
.Related issues
Please mention any github issues addressed by this PR.
Checklist