-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix: supporting encoding const-parametrized ops #80
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a39245
refactor json tests in to fixtures
ss2165 b902439
add test for parametrized json
ss2165 7f50371
fix: supporting encoding const-parametrized ops
ss2165 1c2a12a
Apply suggestions from code review
ss2165 aafb482
revert `oher_port` suggestion
ss2165 3247b72
use other_port_index
ss2165 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,13 @@ | |
|
||
use std::collections::HashMap; | ||
|
||
use downcast_rs::Downcast; | ||
use hugr::extension::prelude::QB_T; | ||
use hugr::ops::OpType; | ||
use hugr::std_extensions::arithmetic::float_types::ConstF64; | ||
use hugr::values::{PrimValue, Value}; | ||
use hugr::Wire; | ||
use itertools::Itertools; | ||
use tket_json_rs::circuit_json::{self, Permutation, SerialCircuit}; | ||
use itertools::{Either, Itertools}; | ||
use tket_json_rs::circuit_json::{self, Permutation, Register, SerialCircuit}; | ||
|
||
use crate::circuit::command::{CircuitUnit, Command}; | ||
use crate::circuit::Circuit; | ||
|
@@ -92,19 +91,35 @@ impl JsonEncoder { | |
// Register any output of the command that can be used as a TKET1 parameter. | ||
self.record_parameters(&command, optype); | ||
|
||
let args = command | ||
.inputs() | ||
.iter() | ||
.filter_map(|&u| self.unit_to_register(u)) | ||
.collect(); | ||
if let OpType::Const(_) | OpType::LoadConstant(_) = optype { | ||
return Ok(()); | ||
} | ||
let (args, params): (Vec<Register>, Vec<Wire>) = | ||
command | ||
.inputs() | ||
.iter() | ||
.partition_map(|&u| match self.unit_to_register(u) { | ||
Some(r) => Either::Left(r), | ||
None => match u { | ||
CircuitUnit::Wire(w) => Either::Right(w), | ||
CircuitUnit::Linear(_) => panic!("Should have been a register."), | ||
ss2165 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}); | ||
|
||
// TODO Restore the opgroup (once the decoding supports it) | ||
let opgroup = None; | ||
|
||
let op: JsonOp = optype.try_into()?; | ||
let op: circuit_json::Operation = op.into_operation(); | ||
|
||
// TODO: Update op.params. Leave untouched the ones that contain free variables. | ||
let mut op: circuit_json::Operation = op.into_operation(); | ||
if !params.is_empty() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this we recover all parameters that have been encoded as inputs. It should be easy to support transparent pass-through of parameters we don't understand yet (e.g. This is OK for now, but I'll add a followup issue. |
||
op.params = Some( | ||
params | ||
.into_iter() | ||
.filter_map(|w| self.parameters.get(&w)) | ||
.cloned() | ||
.collect(), | ||
) | ||
} | ||
// TODO: ops that contain free variables. | ||
// (update decoder to ignore them too, but store them in the wrapped op) | ||
|
||
let command = circuit_json::Command { op, args, opgroup }; | ||
|
@@ -144,8 +159,8 @@ impl JsonEncoder { | |
OpType::Const(const_op) => { | ||
// New constant, register it if it can be interpreted as a parameter. | ||
match const_op.value() { | ||
Value::Prim(PrimValue::Extension(v)) => { | ||
if let Some(f) = v.as_any().downcast_ref::<ConstF64>() { | ||
Value::Prim(PrimValue::Extension((v,))) => { | ||
if let Some(f) = v.downcast_ref::<ConstF64>() { | ||
f.to_string() | ||
} else { | ||
return; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ideally we shouldn't need to access portgraph primitives, nor should we set here what's the offset of the static input.
I can add an issue in hugr to implement an
OpType::static_input_port
(similar toOpType::other_port
).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.
yes, sounds good