Skip to content

Commit

Permalink
feat!: Add support for WASM operations (#61)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Added a `number_of_ws` field to `SerialCircuit`

Co-authored-by: Agustín Borgna <[email protected]>
  • Loading branch information
johnchildren and aborgna-q authored Aug 6, 2024
1 parent 26440e6 commit 6b531f9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/circuit_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ pub enum Classical {
},
}

/// Additional fields for Wasm operations.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct Wasm {
n: u64,
ww_n: u64,
width_i_parameter: Vec<u64>,
width_o_parameter: Vec<u64>,
func_name: String,
wasm_file_uid: String,
}

/// Serializable operation descriptor.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[non_exhaustive]
Expand Down Expand Up @@ -166,6 +177,9 @@ pub struct Operation<P = String> {
/// Data for commands which only act on Bits classically.
#[serde(skip_serializing_if = "Option::is_none")]
pub classical: Option<Box<Classical>>,
/// Data for commands which apply WASM operations.
#[serde(skip_serializing_if = "Option::is_none")]
pub wasm: Option<Box<Wasm>>,
}

/// Operation applied in a circuit, with defined arguments.
Expand Down Expand Up @@ -206,6 +220,9 @@ pub struct SerialCircuit<P = String> {
pub bits: Vec<Register>,
/// Implicit permutation of the output qubits.
pub implicit_permutation: Vec<ImplicitPermutation>,
/// Number of wasm wires in the circuit.
#[serde(skip_serializing_if = "Option::is_none")]
pub number_of_ws: Option<u64>,
}

impl<P> Default for Operation<P> {
Expand All @@ -219,6 +236,7 @@ impl<P> Default for Operation<P> {
signature: None,
conditional: None,
classical: None,
wasm: None,
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/data/wasm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"bits": [],
"commands": [
{
"args": [["_w", [0]]],
"op": {
"type": "WASM",
"wasm": {
"func_name": "add_two",
"n": 0,
"wasm_file_uid": "6a0a29e235cd5c60353254bc2b459e631d381cdd0bded7ae6cb44afb784bd2de",
"width_i_parameter": [0],
"width_o_parameter": [0],
"ww_n": 1
}
}
}
],
"implicit_permutation": [
[
["q", [0]],
["q", [0]]
],
[
["q", [1]],
["q", [1]]
]
],
"number_of_ws": 1,
"phase": "0.0",
"qubits": [
["q", [0]],
["q", [1]]
]
}
2 changes: 2 additions & 0 deletions tests/roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use tket_json_rs::SerialCircuit;
const SIMPLE: &str = include_str!("data/simple.json");
const CLASSICAL: &str = include_str!("data/classical.json");
const DIAGONAL: &str = include_str!("data/diagonal-box.json");
const WASM: &str = include_str!("data/wasm.json");

#[rstest]
#[case::simple(SIMPLE, 4)]
#[case::classical(CLASSICAL, 3)]
#[case::diagonal_box(DIAGONAL, 1)]
#[case::wasm_box(WASM, 1)]
fn roundtrip(#[case] json: &str, #[case] num_commands: usize) {
let initial_json: Value = serde_json::from_str(json).unwrap();
let ser: SerialCircuit = serde_json::from_value(initial_json.clone()).unwrap();
Expand Down

0 comments on commit 6b531f9

Please sign in to comment.