-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Matrix encoding roundtrip losing precision. Use
f64
instead of…
… `f32`s. (#48) Fixes #45. The elements in the encoded matrix where deserialized into `f32`, which caused a precision loss.
- Loading branch information
Showing
7 changed files
with
305 additions
and
21 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
{ | ||
"bits": [], | ||
"commands": [ | ||
{ | ||
"args": [ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
2 | ||
] | ||
] | ||
], | ||
"op": { | ||
"box": { | ||
"control_state": 1, | ||
"id": "0dadbfea-5391-41c2-ad71-fb01fdaecddd", | ||
"n_controls": 1, | ||
"op": { | ||
"box": { | ||
"diagonal": [ | ||
[ | ||
[ | ||
1.0, | ||
0.0 | ||
] | ||
], | ||
[ | ||
[ | ||
1.0, | ||
0.0 | ||
] | ||
], | ||
[ | ||
[ | ||
0.7071067811865476, | ||
0.7071067811865475 | ||
] | ||
], | ||
[ | ||
[ | ||
0.9238795325112867, | ||
0.3826834323650898 | ||
] | ||
] | ||
], | ||
"id": "919b6c13-b9c4-4ffc-ad1a-660088402d02", | ||
"type": "DiagonalBox", | ||
"upper_triangle": true | ||
}, | ||
"type": "DiagonalBox" | ||
}, | ||
"type": "QControlBox" | ||
}, | ||
"type": "QControlBox" | ||
} | ||
} | ||
], | ||
"created_qubits": [], | ||
"discarded_qubits": [], | ||
"implicit_permutation": [ | ||
[ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
] | ||
], | ||
[ | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
] | ||
], | ||
[ | ||
[ | ||
"q", | ||
[ | ||
2 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
2 | ||
] | ||
] | ||
] | ||
], | ||
"phase": "0.0", | ||
"qubits": [ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
2 | ||
] | ||
] | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
{ | ||
"bits": [ | ||
[ | ||
"c", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"c", | ||
[ | ||
1 | ||
] | ||
] | ||
], | ||
"commands": [ | ||
{ | ||
"args": [ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
] | ||
], | ||
"op": { | ||
"type": "H" | ||
} | ||
}, | ||
{ | ||
"args": [ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
] | ||
], | ||
"op": { | ||
"type": "CX" | ||
} | ||
}, | ||
{ | ||
"args": [ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"c", | ||
[ | ||
0 | ||
] | ||
] | ||
], | ||
"op": { | ||
"type": "Measure" | ||
} | ||
}, | ||
{ | ||
"args": [ | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
], | ||
[ | ||
"c", | ||
[ | ||
1 | ||
] | ||
] | ||
], | ||
"op": { | ||
"type": "Measure" | ||
} | ||
} | ||
], | ||
"implicit_permutation": [ | ||
[ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
] | ||
], | ||
[ | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
] | ||
] | ||
], | ||
"phase": "0.0", | ||
"qubits": [ | ||
[ | ||
"q", | ||
[ | ||
0 | ||
] | ||
], | ||
[ | ||
"q", | ||
[ | ||
1 | ||
] | ||
] | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! Roundtrip tests | ||
use assert_json_diff::assert_json_include; | ||
use rstest::rstest; | ||
use serde_json::Value; | ||
use tket_json_rs::SerialCircuit; | ||
|
||
const SIMPLE: &str = include_str!("data/simple.json"); | ||
const DIAGONAL: &str = include_str!("data/diagonal-box.json"); | ||
|
||
#[rstest] | ||
#[case::simple(SIMPLE, 4)] | ||
#[case::diagonal_box(DIAGONAL, 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(); | ||
|
||
assert_eq!(ser.commands.len(), num_commands); | ||
|
||
let reencoded_json = serde_json::to_value(&ser).unwrap(); | ||
// Do a partial comparison. The re-encoded circuit does not include "created_qubits" nor "discarded_qubits". | ||
assert_json_include!(actual: initial_json, expected: reencoded_json); | ||
|
||
let reser: SerialCircuit = serde_json::from_value(reencoded_json).unwrap(); | ||
|
||
assert_eq!(ser, reser); | ||
} |