Skip to content
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: 4 additions & 4 deletions source/compiler/qsc/src/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ impl Compiler {
language_features: LanguageFeatures,
mut store: PackageStore,
dependencies: &Dependencies,
qsharp_config: FxHashMap<Rc<str>, Value>,
qdk_config: FxHashMap<Rc<str>, Value>,
) -> Result<Self, Errors> {
let qsharp_config_rc = Rc::new(qsharp_config);
let mut passes = PassContext::new(qsharp_config_rc.clone());
let qdk_config_rc = Rc::new(qdk_config);
let mut passes = PassContext::new(qdk_config_rc.clone());
let (mut unit, errors) = compile_with_pass_context(
&store,
dependencies,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Compiler {
Ok(Self {
store,
source_package_id,
passes: PassContext::new(qsharp_config_rc),
passes: PassContext::new(qdk_config_rc),
frontend,
})
}
Expand Down
16 changes: 8 additions & 8 deletions source/compiler/qsc/src/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Interpreter {
language_features: LanguageFeatures,
store: PackageStore,
dependencies: &Dependencies,
qsharp_config: FxHashMap<Rc<str>, Value>,
qdk_config: FxHashMap<Rc<str>, Value>,
) -> std::result::Result<Self, Vec<Error>> {
Self::with_sources(
ExecGraphConfig::NoDebug,
Expand All @@ -230,7 +230,7 @@ impl Interpreter {
store,
dependencies,
None,
qsharp_config,
qdk_config,
)
}

Expand All @@ -243,7 +243,7 @@ impl Interpreter {
store: PackageStore,
dependencies: &Dependencies,
circuit_tracer_config: TracerConfig,
qsharp_config: FxHashMap<Rc<str>, Value>,
qdk_config: FxHashMap<Rc<str>, Value>,
) -> std::result::Result<Self, Vec<Error>> {
Self::with_sources(
ExecGraphConfig::NoDebug,
Expand All @@ -254,7 +254,7 @@ impl Interpreter {
store,
dependencies,
Some(circuit_tracer_config),
qsharp_config,
qdk_config,
)
}

Expand All @@ -270,7 +270,7 @@ impl Interpreter {
store: PackageStore,
dependencies: &Dependencies,
trace_circuit_config: TracerConfig,
qsharp_config: FxHashMap<Rc<str>, Value>,
qdk_config: FxHashMap<Rc<str>, Value>,
) -> std::result::Result<Self, Vec<Error>> {
Self::with_sources(
ExecGraphConfig::Debug,
Expand All @@ -281,7 +281,7 @@ impl Interpreter {
store,
dependencies,
Some(trace_circuit_config),
qsharp_config,
qdk_config,
)
}

Expand All @@ -295,7 +295,7 @@ impl Interpreter {
store: PackageStore,
dependencies: &Dependencies,
circuit_tracer_config: Option<TracerConfig>,
qsharp_config: FxHashMap<Rc<str>, Value>,
qdk_config: FxHashMap<Rc<str>, Value>,
) -> std::result::Result<Self, Vec<Error>> {
let compiler = Compiler::new(
sources,
Expand All @@ -304,7 +304,7 @@ impl Interpreter {
language_features,
store,
dependencies,
qsharp_config,
qdk_config,
)
.map_err(into_errors)?;

Expand Down
8 changes: 4 additions & 4 deletions source/compiler/qsc/src/interpret/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2594,8 +2594,8 @@ mod given_interpreter {
)
}

fn get_interpreter_with_config(qsharp_config: FxHashMap<Rc<str>, Value>) -> Interpreter {
get_interpreter_with_capabilities_and_config(TargetCapabilityFlags::all(), qsharp_config)
fn get_interpreter_with_config(qdk_config: FxHashMap<Rc<str>, Value>) -> Interpreter {
get_interpreter_with_capabilities_and_config(TargetCapabilityFlags::all(), qdk_config)
}

fn get_interpreter_with_capabilities(capabilities: TargetCapabilityFlags) -> Interpreter {
Expand All @@ -2604,7 +2604,7 @@ mod given_interpreter {

fn get_interpreter_with_capabilities_and_config(
capabilities: TargetCapabilityFlags,
qsharp_config: FxHashMap<Rc<str>, Value>,
qdk_config: FxHashMap<Rc<str>, Value>,
) -> Interpreter {
let (std_id, store) = crate::compile::package_store_with_stdlib(capabilities);
let dependencies = &[(std_id, None)];
Expand All @@ -2615,7 +2615,7 @@ mod given_interpreter {
LanguageFeatures::default(),
store,
dependencies,
qsharp_config,
qdk_config,
)
.expect("interpreter should be created")
}
Expand Down
8 changes: 4 additions & 4 deletions source/compiler/qsc_passes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn lower_hir_to_fir(
pub struct PassContext {
borrow_check: borrowck::Checker,
/// Read-only config values exposed to Q# via Std.Core.ConfigValue.
qsharp_config: Rc<FxHashMap<Rc<str>, Value>>,
qdk_config: Rc<FxHashMap<Rc<str>, Value>>,
}

impl Default for PassContext {
Expand All @@ -102,10 +102,10 @@ impl Default for PassContext {

impl PassContext {
#[must_use]
pub fn new(qsharp_config: Rc<FxHashMap<Rc<str>, Value>>) -> Self {
pub fn new(qdk_config: Rc<FxHashMap<Rc<str>, Value>>) -> Self {
Self {
borrow_check: borrowck::Checker::default(),
qsharp_config,
qdk_config,
}
}

Expand Down Expand Up @@ -134,7 +134,7 @@ impl PassContext {
Validator::default().visit_package(package);

let config_inline_errors =
config_inline::replace_get_config_calls(core, package, &self.qsharp_config);
config_inline::replace_get_config_calls(core, package, &self.qdk_config);

let measurement_decl_errors = measurement::validate_measurement_declarations(package);
let reset_decl_errors = reset::validate_reset_declarations(package);
Expand Down
8 changes: 4 additions & 4 deletions source/qdk_package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,23 @@ For convenience, the following helpers and types are also importable directly fr

You can provide configuration at initialization time as a Python dictionary.

In Python, pass `qsharp_config: dict[str, int | float | str | bool]` to `Context(...)`.
If `qsharp_config` is omitted, the configuration map is empty. The map is immutable
In Python, pass `qdk_config: dict[str, int | float | str | bool]` to `Context(...)`.
If `qdk_config` is omitted, the configuration map is empty. The map is immutable
after initialization. To use different configuration values, create a new `Context`.

In Q#, read values with `Std.Core.ConfigValue(name, defaultValue)`. In Q# code, config
values are immutable: in the same program, repeated calls with the same
`(name, defaultValue)` produce the same result.

Supported types: `int`, `float`, `str`, and `bool` (corresponding to `Int`, `Double`,
`String` and `Bool` in Q#). The type of each value in `qsharp_config` must match the
`String` and `Bool` in Q#). The type of each value in `qdk_config` must match the
type of its corresponding default value.

Example:

```python
import qdk
context = qdk.Context(qsharp_config={"experiment_name": "baseline", "shots": 1000})
context = qdk.Context(qdk_config={"experiment_name": "baseline", "shots": 1000})
assert context.eval('Std.Core.ConfigValue("experiment_name", "")') == "baseline"
assert context.eval('Std.Core.ConfigValue("shots", 100)') == 1000
assert context.eval('Std.Core.ConfigValue("noise_level", 0.01)') == 0.01
Expand Down
6 changes: 3 additions & 3 deletions source/qdk_package/qdk/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def __init__(
language_features: Optional[List[str]] = None,
_trace_circuit: Optional[bool] = None,
_is_global_context: bool = False,
qsharp_config: dict[str, int | float | str | bool] = {},
qdk_config: dict[str, int | float | str | bool] = {},
):
"""
Initializes a new isolated Q# context.
Expand All @@ -209,7 +209,7 @@ def __init__(
the statement form instead (``use q = Qubit();``). It also removes the requirement
to use the ``set`` keyword for mutable variable assignments.

:keyword qsharp_config: configuration parameters that will be accessible in Q#
:keyword qdk_config: configuration parameters that will be accessible in Q#
code using `Std.Core.ConfigValue`. Keys must be strings. Values must be of
type `int`, `float`, `str`, or `bool`.
"""
Expand Down Expand Up @@ -286,7 +286,7 @@ def make_class_weak(
make_callable_weak,
make_class_weak,
_trace_circuit,
qsharp_config,
qdk_config,
)

self._config = Config(
Expand Down
7 changes: 7 additions & 0 deletions source/qdk_package/qdk/_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def init(
project_root: Optional[str] = None,
language_features: Optional[List[str]] = None,
trace_circuit: Optional[bool] = None,
qdk_config: dict[str, int | float | str | bool] = {},
) -> Config:
"""
Initializes the Q# interpreter.
Expand All @@ -128,6 +129,11 @@ def init(
Passing `True` is required for the `dump_circuit` function to return a circuit.
The `circuit` function is *NOT* affected by this parameter and will always
generate a circuit.

:keyword qdk_config: configuration parameters that will be accessible in Q#
code using `Std.Core.ConfigValue`. Keys must be strings. Values must be of
type `int`, `float`, `str`, or `bool`.

:return: The Q# interpreter configuration.
:rtype: Config
"""
Expand All @@ -147,6 +153,7 @@ def init(
language_features=language_features,
_trace_circuit=trace_circuit,
_is_global_context=True,
qdk_config=qdk_config,
)
return _default_context._config

Expand Down
4 changes: 2 additions & 2 deletions source/qdk_package/qdk/_native.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class Interpreter:
make_callable: Optional[Callable[[GlobalCallable, List[str], str, bool], None]],
make_class: Optional[Callable[[TypeIR, List[str], str], None]],
trace_circuit: Optional[bool],
qsharp_config: Optional[Dict[str, int | float | str | bool]] = None,
qdk_config: Optional[Dict[str, int | float | str | bool]] = None,
) -> None:
"""
Initializes the Q# interpreter.
Expand All @@ -173,7 +173,7 @@ class Interpreter:
:param trace_circuit: Enables tracing of circuit during execution.
Passing `True` is required for the `dump_circuit` function to return a circuit.
The `circuit` function is *NOT* affected by this parameter will always generate a circuit.
:param qsharp_config: A dictionary of configuration parameters that will be accessible
:param qdk_config: A dictionary of configuration parameters that will be accessible
in Q# code using ``Std.Core.ConfigValue``.
"""
...
Expand Down
16 changes: 8 additions & 8 deletions source/qdk_package/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ pub(crate) struct Interpreter {
thread_local! { static PACKAGE_CACHE: Rc<RefCell<PackageCache>> = Rc::default(); }

// Converts Q# config from PyDict to FxHashMap.
fn convert_qsharp_config(
qsharp_config: Option<Bound<'_, PyDict>>,
fn convert_qdk_config(
qdk_config: Option<Bound<'_, PyDict>>,
) -> PyResult<FxHashMap<Rc<str>, Value>> {
let mut config = FxHashMap::default();
if let Some(config_dict) = qsharp_config {
if let Some(config_dict) = qdk_config {
for (key, value) in config_dict.iter() {
let key: String = key.extract()?;
let value = if value.is_instance_of::<PyBool>() {
Expand Down Expand Up @@ -438,7 +438,7 @@ impl Interpreter {
#[allow(clippy::too_many_arguments)]
#[allow(clippy::needless_pass_by_value)]
#[allow(clippy::too_many_lines)]
#[pyo3(signature = (target_profile, language_features=None, project_root=None, read_file=None, list_directory=None, resolve_path=None, fetch_github=None, make_callable=None, make_class=None, trace_circuit=None, qsharp_config=None))]
#[pyo3(signature = (target_profile, language_features=None, project_root=None, read_file=None, list_directory=None, resolve_path=None, fetch_github=None, make_callable=None, make_class=None, trace_circuit=None, qdk_config=None))]
#[new]
/// Initializes a new Q# interpreter.
pub(crate) fn new(
Expand All @@ -453,7 +453,7 @@ impl Interpreter {
make_callable: Option<Py<PyAny>>,
make_class: Option<Py<PyAny>>,
trace_circuit: Option<bool>,
qsharp_config: Option<Bound<'_, PyDict>>,
qdk_config: Option<Bound<'_, PyDict>>,
) -> PyResult<Self> {
let target = Into::<Profile>::into(target_profile).into();

Expand Down Expand Up @@ -489,7 +489,7 @@ impl Interpreter {
BuildableProgram::new(target, graph)
};

let qsharp_config = convert_qsharp_config(qsharp_config)?;
let qdk_config = convert_qdk_config(qdk_config)?;
let trace_circuit = trace_circuit.unwrap_or(false);
let interpreter = if trace_circuit {
interpret::Interpreter::with_circuit_trace(
Expand All @@ -508,7 +508,7 @@ impl Interpreter {
group_by_scope: false,
prune_classical_qubits: false,
},
qsharp_config,
qdk_config,
)
} else {
interpret::Interpreter::new(
Expand All @@ -518,7 +518,7 @@ impl Interpreter {
buildable_program.user_code.language_features,
buildable_program.store,
&buildable_program.user_code_dependencies,
qsharp_config,
qdk_config,
)
}
.map_err(|errors| QSharpError::new_err(format_errors(errors)))?;
Expand Down
8 changes: 4 additions & 4 deletions source/qdk_package/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ def test_context_released_after_drop() -> None:
assert ref() is None


def test_qsharp_config(context: qdk.Context) -> None:
def test_qdk_config(context: qdk.Context) -> None:
context = qdk.Context(
qsharp_config={
qdk_config={
"int_config": 123,
"bool_config": True,
"string_config": "value",
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_config_invalid_type(context: qdk.Context) -> None:
with pytest.raises(
TypeError, match="config value must be bool, int, float, or str"
):
qdk.Context(qsharp_config={"invalid": {"a": 1}}) # type: ignore
qdk.Context(qdk_config={"invalid": {"a": 1}}) # type: ignore

with pytest.raises(TypeError, match="'int' object is not an instance of 'str'"):
qdk.Context(qsharp_config={1: 1}) # type: ignore
qdk.Context(qdk_config={1: 1}) # type: ignore
5 changes: 5 additions & 0 deletions source/qdk_package/tests/test_qsharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,8 @@ def test_swap_label_circuit_from_callable() -> None:
q_0 ── X ──── X ──
q_1 ──────────────
""")


def test_qdk_config() -> None:
qsharp.init(qdk_config={"int_config": 123})
assert qsharp.eval("""Std.Core.ConfigValue("int_config", 0)""") == 123
Loading