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

Add from_str to TargetProfile #1937

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pip/qsharp/_native.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ class TargetProfile(Enum):
which will be used to run the Q# program.
"""

@classmethod
def from_str(cls, value: str) -> TargetProfile: ...
"""
Creates a target profile from a string.
:param value: The string to parse.
:raises ValueError: If the string does not match any target profile.
"""

Base: TargetProfile
"""
Target supports the minimal set of capabilities required to run a quantum
Expand Down
29 changes: 25 additions & 4 deletions pip/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use num_bigint::BigUint;
use num_complex::Complex64;
use pyo3::{
create_exception,
exceptions::PyException,
exceptions::{PyException, PyValueError},
prelude::*,
types::{PyComplex, PyDict, PyList, PyTuple},
types::{PyComplex, PyDict, PyList, PyTuple, PyType},
};
use qsc::{
fir,
Expand All @@ -33,7 +33,7 @@ use qsc::{
};

use resource_estimator::{self as re, estimate_expr};
use std::{cell::RefCell, fmt::Write, path::PathBuf, rc::Rc};
use std::{cell::RefCell, fmt::Write, path::PathBuf, rc::Rc, str::FromStr};

/// If the classes are not Send, the Python interpreter
/// will not be able to use them in a separate thread.
Expand Down Expand Up @@ -111,11 +111,32 @@ pub(crate) enum TargetProfile {
}

#[pymethods]
#[allow(clippy::trivially_copy_pass_by_ref)]
impl TargetProfile {
#[allow(clippy::trivially_copy_pass_by_ref)]
fn __str__(&self) -> String {
Into::<Profile>::into(*self).to_str().to_owned()
}

/// Creates a target profile from a string.
/// :param value: The string to parse.
/// :raises ValueError: If the string does not match any target profile.
#[classmethod]
#[allow(clippy::needless_pass_by_value)]
fn from_str(_cls: &Bound<'_, PyType>, key: String) -> pyo3::PyResult<Self> {
let profile = Profile::from_str(key.as_str())
.map_err(|()| PyValueError::new_err(format!("{key} is not a valid target profile")))?;
Ok(TargetProfile::from(profile))
}
}

impl From<Profile> for TargetProfile {
fn from(profile: Profile) -> Self {
match profile {
Profile::Base => TargetProfile::Base,
Profile::AdaptiveRI => TargetProfile::Adaptive_RI,
Profile::Unrestricted => TargetProfile::Unrestricted,
}
}
}

impl From<TargetProfile> for Profile {
Expand Down
15 changes: 15 additions & 0 deletions pip/tests/test_qsharp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

import pytest
import qsharp
import qsharp.utils
from contextlib import redirect_stdout
Expand Down Expand Up @@ -343,3 +344,17 @@ def test_target_profile_str_values_match_enum_values() -> None:
target_profile = qsharp.TargetProfile.Unrestricted
str_value = str(target_profile)
assert str_value == "Unrestricted"


def test_target_profile_from_str_match_enum_values() -> None:
target_profile = qsharp.TargetProfile.Base
str_value = str(target_profile)
assert qsharp.TargetProfile.from_str(str_value) == target_profile
target_profile = qsharp.TargetProfile.Adaptive_RI
str_value = str(target_profile)
assert qsharp.TargetProfile.from_str(str_value) == target_profile
target_profile = qsharp.TargetProfile.Unrestricted
str_value = str(target_profile)
assert qsharp.TargetProfile.from_str(str_value) == target_profile
with pytest.raises(ValueError):
qsharp.TargetProfile.from_str("Invalid")
Loading