Skip to content

Commit

Permalink
Version Check Qoqo Support (#524)
Browse files Browse the repository at this point in the history
* min_supported_version current_version qoqo support

* more feature attributes for warnings

* missing implementations

* tests

* fmt

* consistent #[staticmethod] placing

* even more

* imports consolidation
  • Loading branch information
mlodi-hqs authored Jul 27, 2023
1 parent ced677b commit af00c7d
Show file tree
Hide file tree
Showing 33 changed files with 649 additions and 26 deletions.
20 changes: 20 additions & 0 deletions qoqo-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,31 @@ pub fn wrap(
let json_schema_quote = if attribute_arguments.contains("JsonSchema") {
quote! {
#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}

#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) = #ident::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}

#[cfg(feature = "json_schema")]
/// Return the JsonSchema for the json serialisation of the class.
///
/// Returns:
/// str: The json schema serialized to json
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(#ident);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
Expand Down
25 changes: 23 additions & 2 deletions qoqo/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ impl CircuitWrapper {
Ok(b)
}

#[staticmethod]
/// Convert the bincode representation of the Circuit to a Circuit using the [bincode] crate.
///
/// Args:
Expand All @@ -275,6 +274,7 @@ impl CircuitWrapper {
/// Raises:
/// TypeError: Input cannot be converted to byte array.
/// ValueError: Input cannot be deserialized to Circuit.
#[staticmethod]
pub fn from_bincode(input: &PyAny) -> PyResult<Self> {
let bytes = input
.extract::<Vec<u8>>()
Expand All @@ -300,17 +300,37 @@ impl CircuitWrapper {
}

#[cfg(feature = "json_schema")]
#[staticmethod]
/// Return the JsonSchema for the json serialisation of the class.
///
/// Returns:
/// str: The json schema serialized to json
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(Circuit);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}

#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}

#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
Circuit::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}

/// Convert the json representation of a Circuit to a Circuit.
///
/// Args:
Expand All @@ -321,6 +341,7 @@ impl CircuitWrapper {
///
/// Raises:
/// ValueError: Input cannot be deserialized to Circuit.
#[staticmethod]
pub fn from_json(json_string: &str) -> PyResult<Self> {
Ok(Self {
internal: serde_json::from_str(json_string)
Expand Down
25 changes: 24 additions & 1 deletion qoqo/src/devices/all_to_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use pyo3::prelude::*;
use pyo3::types::PyByteArray;
use qoqo_macros::devicewrapper;
use roqoqo::devices::{AllToAllDevice, Device};
#[cfg(feature = "json_schema")]
use roqoqo::{operations::SupportedVersion, ROQOQO_VERSION};

/// A generic device with all-to-all connectivity.
///
Expand Down Expand Up @@ -172,15 +174,36 @@ impl AllToAllDeviceWrapper {
}

#[cfg(feature = "json_schema")]
#[staticmethod]
/// Return the JsonSchema for the json serialisation of the class.
///
/// Returns:
/// str: The json schema serialized to json
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(AllToAllDevice);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}

#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}

#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
AllToAllDevice::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}
}

impl AllToAllDeviceWrapper {
Expand Down
25 changes: 24 additions & 1 deletion qoqo/src/devices/generic_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use pyo3::prelude::*;
use pyo3::types::PyByteArray;
use qoqo_macros::devicewrapper;
use roqoqo::devices::{Device, GenericDevice};
#[cfg(feature = "json_schema")]
use roqoqo::{operations::SupportedVersion, ROQOQO_VERSION};

/// A generic device assuming all-to-all connectivity between all involved qubits.
///
Expand Down Expand Up @@ -47,15 +49,36 @@ impl GenericDeviceWrapper {
}

#[cfg(feature = "json_schema")]
#[staticmethod]
/// Return the JsonSchema for the json serialisation of the class.
///
/// Returns:
/// str: The json schema serialized to json
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(GenericDevice);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}

#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}

#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
GenericDevice::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}
}

impl GenericDeviceWrapper {
Expand Down
25 changes: 24 additions & 1 deletion qoqo/src/devices/square_lattice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use pyo3::prelude::*;
use pyo3::types::PyByteArray;
use qoqo_macros::devicewrapper;
use roqoqo::devices::{Device, SquareLatticeDevice};
#[cfg(feature = "json_schema")]
use roqoqo::{operations::SupportedVersion, ROQOQO_VERSION};

/// A generic square lattice device with only next-neighbours-connectivity.
///
Expand Down Expand Up @@ -194,15 +196,36 @@ impl SquareLatticeDeviceWrapper {
}

#[cfg(feature = "json_schema")]
#[staticmethod]
/// Return the JsonSchema for the json serialisation of the class.
///
/// Returns:
/// str: The json schema serialized to json
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(SquareLatticeDevice);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}

#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}

#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
SquareLatticeDevice::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}
}

impl SquareLatticeDeviceWrapper {
Expand Down
28 changes: 26 additions & 2 deletions qoqo/src/measurements/basis_rotation_measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use roqoqo::measurements::PauliZProduct;
use roqoqo::prelude::*;
use roqoqo::registers::{BitOutputRegister, ComplexOutputRegister, FloatOutputRegister};
use roqoqo::Circuit;
#[cfg(feature = "json_schema")]
use roqoqo::ROQOQO_VERSION;
use std::collections::HashMap;

#[pyclass(name = "PauliZProduct", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
/// Collected information for executing a measurement of PauliZ product.
Expand Down Expand Up @@ -239,7 +242,6 @@ impl PauliZProductWrapper {
Ok(b)
}

#[staticmethod]
/// Convert the bincode representation of the PauliZProduct to a PauliZProduct using the [bincode] crate.
///
/// Args:
Expand All @@ -251,6 +253,7 @@ impl PauliZProductWrapper {
/// Raises:
/// TypeError: Input cannot be converted to byte array.
/// ValueError: Input cannot be deserialized to PauliZProduct.
#[staticmethod]
pub fn from_bincode(input: &PyAny) -> PyResult<Self> {
let bytes = input
.extract::<Vec<u8>>()
Expand Down Expand Up @@ -331,15 +334,36 @@ impl PauliZProductWrapper {
}

#[cfg(feature = "json_schema")]
#[staticmethod]
/// Return the JsonSchema for the json serialisation of the class.
///
/// Returns:
/// str: The json schema serialized to json
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(PauliZProduct);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}

#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}

#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
PauliZProduct::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}
}

impl PauliZProductWrapper {
Expand Down
28 changes: 26 additions & 2 deletions qoqo/src/measurements/cheated_basis_rotation_measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use roqoqo::measurements::CheatedPauliZProduct;
use roqoqo::prelude::*;
use roqoqo::registers::{BitOutputRegister, ComplexOutputRegister, FloatOutputRegister};
use roqoqo::Circuit;
#[cfg(feature = "json_schema")]
use roqoqo::ROQOQO_VERSION;
use std::collections::HashMap;

#[pyclass(name = "CheatedPauliZProduct", module = "qoqo.measurements")]
#[derive(Clone, Debug)]
/// Collected information for executing a cheated measurement of PauliZ product.
Expand Down Expand Up @@ -238,7 +241,6 @@ impl CheatedPauliZProductWrapper {
Ok(b)
}

#[staticmethod]
/// Convert the bincode representation of the CheatedPauliZProduct to a CheatedPauliZProduct using the [bincode] crate.
///
/// Args:
Expand All @@ -250,6 +252,7 @@ impl CheatedPauliZProductWrapper {
/// Raises:
/// TypeError: Input cannot be converted to byte array.
/// ValueError: Input cannot be deserialized to CheatedPauliZProduct.
#[staticmethod]
pub fn from_bincode(input: &PyAny) -> PyResult<Self> {
let bytes = input
.extract::<Vec<u8>>()
Expand Down Expand Up @@ -330,15 +333,36 @@ impl CheatedPauliZProductWrapper {
}

#[cfg(feature = "json_schema")]
#[staticmethod]
/// Return the JsonSchema for the json serialisation of the class.
///
/// Returns:
/// str: The json schema serialized to json
#[staticmethod]
pub fn json_schema() -> String {
let schema = schemars::schema_for!(CheatedPauliZProduct);
serde_json::to_string_pretty(&schema).expect("Unexpected failure to serialize schema")
}

#[cfg(feature = "json_schema")]
/// Returns the current version of the qoqo library .
///
/// Returns:
/// str: The current version of the library.
#[staticmethod]
pub fn current_version() -> String {
ROQOQO_VERSION.to_string()
}

#[cfg(feature = "json_schema")]
/// Return the minimum version of qoqo that supports this object.
///
/// Returns:
/// str: The minimum version of the qoqo library to deserialize this object.
pub fn min_supported_version(&self) -> String {
let min_version: (u32, u32, u32) =
CheatedPauliZProduct::minimum_supported_roqoqo_version(&self.internal);
format!("{}.{}.{}", min_version.0, min_version.1, min_version.2)
}
}

impl CheatedPauliZProductWrapper {
Expand Down
Loading

0 comments on commit af00c7d

Please sign in to comment.