Skip to content
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

new: define (de)serializers for chip specs #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

notmgsk
Copy link
Contributor

@notmgsk notmgsk commented Aug 9, 2023

This MR adds crate::chip::ChipSpec (and related types) which provide a Quilc-compatible definition of chip specifications.

There is a corpus of data in ./test-data used for parity testing (deserialize -> serialize -> compare with original). It has been copied from the Quilc repository with small edits to fix JSON syntax and to change the types of some data (e.g. replace "true" with true).

@notmgsk notmgsk marked this pull request as ready for review August 9, 2023 13:49
Copy link

@BatmanAoD BatmanAoD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor questions/suggestions

Comment on lines +11 to +12
#[serde(rename(deserialize = "1Q"))]
#[serde(rename(serialize = "1Q"))]

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?

Suggested change
#[serde(rename(deserialize = "1Q"))]
#[serde(rename(serialize = "1Q"))]
#[serde(rename = "1Q"))]

}

#[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct Metadata {}

Choose a reason for hiding this comment

The 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 +38 to +40
#[serde(rename(deserialize = "type"))]
#[serde(rename(serialize = "type"))]
ty: String,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: why not just make the field-name type?

Comment on lines +95 to +97
#[serde(deserialize_with = "deserialize_wildcard")]
#[serde(serialize_with = "serialize_wildcard")]
Wildcard,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would Wildcard(monostate::MustBe!("_")) work here?

Comment on lines +11 to +12
#[serde(rename(deserialize = "1Q"))]
#[serde(rename(serialize = "1Q"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[serde(rename(deserialize = "1Q"))]
#[serde(rename(serialize = "1Q"))]
#[serde(rename = "1Q")]

Comment on lines +16 to +17
#[serde(rename(deserialize = "2Q"))]
#[serde(rename(serialize = "2Q"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[serde(rename(deserialize = "2Q"))]
#[serde(rename(serialize = "2Q"))]
#[serde(rename = "2Q")]

Comment on lines +23 to +24
#[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct Metadata {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this struct?

Comment on lines +51 to +54
#[serde(rename(serialize = "1Q"))]
#[serde(rename_all = "lowercase")]
#[serde(untagged)]
pub enum TwoQ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[serde(rename(serialize = "1Q"))]
#[serde(rename_all = "lowercase")]
#[serde(untagged)]
pub enum TwoQ {
#[serde(rename(serialize = "2Q"))]
#[serde(rename_all = "lowercase")]
#[serde(untagged)]
pub enum TwoQ {

Comment on lines +38 to +39
#[serde(rename(deserialize = "type"))]
#[serde(rename(serialize = "type"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[serde(rename(deserialize = "type"))]
#[serde(rename(serialize = "type"))]
#[serde(rename = "type")]

Comment on lines +73 to +89
#[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,
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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
#[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,
},
}
#[serde(tag = "operator")]
pub enum Gate {
#[serde(rename = "MEASURE")]
Measure {
qubit: Qubit,
target: Option<MeasurementTarget>,
duration: f64,
fidelity: f64,
},
#[serde(other)]
Quantum {
operator: String,
parameters: Vec<Parameter>,
arguments: Vec<Argument>,
duration: f64,
fidelity: f64,
},
}

Comment on lines +122 to +123
#[serde(deserialize_with = "deserialize_wildcard")]
#[serde(serialize_with = "serialize_wildcard")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this do the same thing and be simpler?

Suggested change
#[serde(deserialize_with = "deserialize_wildcard")]
#[serde(serialize_with = "serialize_wildcard")]
#[serde(rename = "_")]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Same for all the other cases as well)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't rename change the key name, not the value?

use serde_json::json;

#[test]
fn deserialize_oneq() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see a test for 2Q

pub timestamp: Option<String>,
#[serde(deserialize_with = "deserialize_string_from_number")]
#[serde(serialize_with = "maybe_serialize_string_to_integer")]
pub version: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it might be better to have a Version struct instead of the manual serialization? Would also mean not needing the extra crate.

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum Version {
    Number(i32),
    String(String),
}

Comment on lines +18 to +19
#[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct SpecsMap(HashMap<String, f64>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason for using a newtype pattern here instead of a type alias? Why not this?

Suggested change
#[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct SpecsMap(HashMap<String, f64>);
pub type SpecsMap = HashMap<String, f64>;

@notmgsk notmgsk force-pushed the main branch 2 times, most recently from 9b564fa to 586eaeb Compare October 16, 2023 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants