-
Notifications
You must be signed in to change notification settings - Fork 0
new: define (de)serializers for chip specs #27
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,304 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //! The supported Instruction Set Architecture for a chip specification | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use serde::{de::Deserializer, Deserialize, Serializer}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::collections::HashMap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub struct Isa { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The degree-0 (i.e. single qubit) hardware objects and their | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// supported instructions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(deserialize = "1Q"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(serialize = "1Q"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+11
to
+12
Contributor
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub one_q: HashMap<String, Option<OneQ>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The degree-1 (i.e. two qubit) hardware objects and their | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// supported instructions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(deserialize = "2Q"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(serialize = "2Q"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+16
to
+17
Contributor
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub two_q: HashMap<String, Option<TwoQ>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(skip_serializing_if = "Option::is_none")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub metadata: Option<Metadata>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub struct Metadata {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. Should this include a flattened map of miscellaneous fields, so that it's round-trip-able? https://serde.rs/attr-flatten.html#capture-additional-fields
Comment on lines
+23
to
+24
Contributor
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. What is the purpose of this struct? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(serialize = "1Q"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename_all = "lowercase")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(untagged)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum OneQ { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The set of supported gates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Gates { gates: Vec<Gate> }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// DEPRECATED. A gateset identifier known to quilc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// In practice, the only supported identifier here is "Xhalves" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// and this style of specifying gates in the ISA is deprecated. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ty { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(deserialize = "type"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(serialize = "type"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+38
to
+39
Contributor
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ty: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+38
to
+40
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. Style: why not just make the field-name |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// DEPRECATED. Define gates by their associated specs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Specs { specs: crate::chip::specs::SpecsMap }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Qubit exists physically but should not be used for computation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dead { dead: bool }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Use a set of (quilc-specified) default gates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Defaults {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. Why not this?
Suggested change
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. I wondered about this as well and did some experimentation with how |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(serialize = "1Q"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename_all = "lowercase")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(untagged)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum TwoQ { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+54
Contributor
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The set of supported gates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Gates { gates: Vec<Gate> }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// See documentation for [`OneQ::Ty`]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ty { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(deserialize = "type"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename(serialize = "type"))] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ty: Vec<String>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// See documentation for [`OneQ::Specs`]. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Specs { specs: crate::chip::specs::SpecsMap }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Qubit exists physically but should not be used for computation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dead { dead: bool }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Use a set of (quilc-specified) default gates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Defaults {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. Same question here
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename = "lowercase")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(untagged)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum Gate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Measure { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| operator: monostate::MustBe!("MEASURE"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qubit: Qubit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target: Option<MeasurementTarget>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| duration: f64, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fidelity: f64, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Quantum { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| operator: String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters: Vec<Parameter>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| arguments: Vec<Argument>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| duration: f64, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fidelity: f64, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+73
to
+89
Contributor
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. I think this does the same thing and does not require the extra crate.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(rename_all = "lowercase")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(untagged)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum Qubit { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(deserialize_with = "deserialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(serialize_with = "serialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+95
to
+97
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. Would |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Index(usize), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(untagged)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum MeasurementTarget { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(deserialize_with = "deserialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(serialize_with = "serialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MemoryReference(String), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(untagged)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum Parameter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(deserialize_with = "deserialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(serialize_with = "serialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Numeric(f64), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(untagged)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum Argument { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(deserialize_with = "deserialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[serde(serialize_with = "serialize_wildcard")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+122
to
+123
Contributor
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. Would this do the same thing and be simpler?
Suggested change
Contributor
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. (Same for all the other cases as well) 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. Doesn't |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Index(usize), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn deserialize_wildcard<'de, D>(deserializer: D) -> Result<(), D::Error> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| D: Deserializer<'de>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let buf = String::deserialize(deserializer)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if &buf == "_" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Err(serde::de::Error::custom("input does not match wildcard")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn serialize_wildcard<S>(serializer: S) -> Result<S::Ok, S::Error> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| where | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| S: Serializer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| serializer.serialize_str("_") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mod test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use monostate::MustBe; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use serde_json::json; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn deserialize_oneq() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
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. Don't see a test for |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: serde_json::Value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: OneQ, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let tests = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!({}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: OneQ::Defaults {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!({"gates": []}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: OneQ::Gates { gates: vec![] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for test in tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let actual: OneQ = serde_json::from_value(test.input).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(actual, test.expected); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn deserialize_gate() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: serde_json::Value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Gate, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let tests = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!({"operator": "MEASURE", "qubit": 42, "target": "ro", "duration": 0.1, "fidelity": 0.9}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Gate::Measure { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| operator: MustBe!("MEASURE"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qubit: Qubit::Index(42), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target: Some(MeasurementTarget::MemoryReference("ro".to_string())), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| duration: 0.1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fidelity: 0.9, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!({"operator": "RX", "parameters": [1.5], "arguments": [42], "duration": 0.1, "fidelity": 0.9}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Gate::Quantum { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| operator: "RX".to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parameters: vec![Parameter::Numeric(1.5)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| arguments: vec![Argument::Index(42)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| duration: 0.1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fidelity: 0.9, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for test in tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let actual: Gate = serde_json::from_value(test.input).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(actual, test.expected); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn deserialize_qubit() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: serde_json::Value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Qubit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let tests = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!("_"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Qubit::Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!(42), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Qubit::Index(42), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for test in tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let actual: Qubit = serde_json::from_value(test.input).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(actual, test.expected); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn deserialize_argument() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: serde_json::Value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Argument, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let tests = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!("_"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Argument::Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!(42), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Argument::Index(42), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for test in tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let actual: Argument = serde_json::from_value(test.input).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(actual, test.expected); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn deserialize_parameter() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: serde_json::Value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Parameter, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let tests = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!("_"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Parameter::Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!(1.5), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: Parameter::Numeric(1.5), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for test in tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let actual: Parameter = serde_json::from_value(test.input).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(actual, test.expected); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn deserialize_measurement_target() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: serde_json::Value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: MeasurementTarget, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let tests = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!("_"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: MeasurementTarget::Wildcard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input: json!("ro"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expected: MeasurementTarget::MemoryReference("ro".to_string()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for test in tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let actual: MeasurementTarget = serde_json::from_value(test.input).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(actual, test.expected); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //! Quilc-compatible chip specification | ||
| use serde::Serializer; | ||
| use serde_aux::prelude::deserialize_string_from_number; | ||
|
|
||
| pub mod isa; | ||
| mod parity_test; | ||
| pub mod specs; | ||
|
|
||
| /// A `ChipSpec` defines the various hardware objects that are available | ||
| /// when compiling a quantum program. | ||
| #[derive(Debug, serde::Serialize, serde::Deserialize)] | ||
| pub struct ChipSpec { | ||
| /// The "Instruction Set Architecture" of the chip; i.e. the | ||
| /// instructions (or "gates") that a particular hardware object | ||
| /// supports. | ||
| pub isa: isa::Isa, | ||
| /// The various operating characteristics of a hardware object (e.g | ||
| /// readout fidelity). | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub specs: Option<specs::Specs>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub metadata: Option<Metadata>, | ||
| } | ||
|
|
||
| #[derive(Debug, serde::Serialize, serde::Deserialize)] | ||
| pub struct Metadata { | ||
| pub name: String, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub timestamp: Option<String>, | ||
| #[serde(deserialize_with = "deserialize_string_from_number")] | ||
| #[serde(serialize_with = "maybe_serialize_string_to_integer")] | ||
| pub version: String, | ||
|
Contributor
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. Seems like it might be better to have a |
||
| } | ||
|
|
||
| /// Serialize the input to an `i32` if it can be parsed as such; otherwise | ||
| /// serialize it as a string. | ||
| fn maybe_serialize_string_to_integer<S>(s: &str, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| match s.parse::<i32>() { | ||
| Ok(i) => serializer.serialize_i32(i), | ||
| Err(_) => serializer.serialize_str(s), | ||
| } | ||
| } | ||
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.
Style: throughout, why not rename these together?