(&self, context: Context, arguments_types: Vec<Type>)
| 47 | #[typetag::serde] |
| 48 | impl CustomOperationBody for Mux { |
| 49 | fn instantiate(&self, context: Context, arguments_types: Vec<Type>) -> Result<Graph> { |
| 50 | if arguments_types.len() != 3 { |
| 51 | return Err(runtime_error!("Invalid number of arguments for Mux")); |
| 52 | } |
| 53 | let t = arguments_types[0].clone(); |
| 54 | if !t.is_scalar() && !t.is_array() { |
| 55 | return Err(runtime_error!("Flag for Mux must be a scalar or an array")); |
| 56 | } |
| 57 | if t.get_scalar_type() != BIT { |
| 58 | return Err(runtime_error!("Flag for Mux must consist of bits")); |
| 59 | } |
| 60 | if arguments_types[1].get_scalar_type() != arguments_types[2].get_scalar_type() { |
| 61 | return Err(runtime_error!( |
| 62 | "Choices for Mux must have the same scalar type" |
| 63 | )); |
| 64 | } |
| 65 | |
| 66 | let g = context.create_graph()?; |
| 67 | let i_flag = g.input(arguments_types[0].clone())?; |
| 68 | let i_choice1 = g.input(arguments_types[1].clone())?; |
| 69 | let i_choice0 = g.input(arguments_types[2].clone())?; |
| 70 | if arguments_types[1].get_scalar_type() == BIT { |
| 71 | i_choice0 |
| 72 | .add(i_flag.multiply(i_choice0.add(i_choice1)?)?)? |
| 73 | .set_as_output()?; |
| 74 | } else { |
| 75 | let i_choice0 = i_choice0.mixed_multiply(i_flag.clone())?; |
| 76 | let i_choice1 = i_choice1.mixed_multiply(i_flag.add(g.ones(scalar_type(BIT))?)?)?; |
| 77 | i_choice0.add(i_choice1)?.set_as_output()?; |
| 78 | } |
| 79 | g.finalize()?; |
| 80 | Ok(g) |
| 81 | } |
| 82 | |
| 83 | fn get_name(&self) -> String { |
| 84 | "Mux".to_owned() |
nothing calls this directly
no test coverage detected