Skip to content

Commit edebe14

Browse files
authored
Support NoiseConfig for Q# and OpenQASM on sparse simulation (#3062)
This change supports granular noise configuration for the mechanisms that use sparse state simulator, namely `qsharp.run` and `openqasm.run`. This brings it up to parity with our QIR-based simulation in the `NeutralAtomDevice` on cpu and gpu (and clifford).
1 parent a1bdbe8 commit edebe14

File tree

16 files changed

+955
-45
lines changed

16 files changed

+955
-45
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/compiler/qsc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ log = { workspace = true }
1515
miette = { workspace = true }
1616
num-bigint = { workspace = true }
1717
num-complex = { workspace = true }
18+
qdk_simulators = { path = "../../simulators" }
1819
qsc_codegen = { path = "../qsc_codegen" }
1920
qsc_data_structures = { path = "../qsc_data_structures" }
2021
qsc_doc_gen = { path = "../qsc_doc_gen" }

source/compiler/qsc/src/interpret.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use debug::format_call_stack;
2424
use miette::Diagnostic;
2525
use num_bigint::BigUint;
2626
use num_complex::Complex;
27+
use qdk_simulators::noise_config::NoiseConfig;
2728
use qsc_circuit::{
2829
Circuit, CircuitTracer, TracerConfig,
2930
operations::{entry_expr_for_qubit_operation, qubit_param_info},
@@ -833,18 +834,28 @@ impl Interpreter {
833834

834835
// Invokes the given callable with the given arguments using the current compilation but with a fresh
835836
// environment and simulator configured with the given noise, if any.
837+
#[allow(clippy::too_many_arguments)]
836838
pub fn invoke_with_noise(
837839
&mut self,
838840
receiver: &mut impl Receiver,
839841
callable: Value,
840842
args: Value,
841843
noise: Option<PauliNoise>,
842844
qubit_loss: Option<f64>,
845+
noise_config: Option<NoiseConfig<f64, f64>>,
843846
seed: Option<u64>,
844847
) -> InterpretResult {
848+
let qubit_loss = if noise_config.is_none() {
849+
qubit_loss
850+
} else {
851+
None
852+
};
845853
let mut sim = match noise {
846854
Some(noise) => SparseSim::new_with_noise(&noise),
847-
None => SparseSim::new(),
855+
None => match noise_config {
856+
Some(config) => SparseSim::new_with_noise_config(config.into()),
857+
None => SparseSim::new(),
858+
},
848859
};
849860
if let Some(loss) = qubit_loss {
850861
sim.set_loss(loss);
@@ -863,11 +874,20 @@ impl Interpreter {
863874
expr: Option<&str>,
864875
noise: Option<PauliNoise>,
865876
qubit_loss: Option<f64>,
877+
noise_config: Option<NoiseConfig<f64, f64>>,
866878
seed: Option<u64>,
867879
) -> InterpretResult {
880+
let qubit_loss = if noise_config.is_none() {
881+
qubit_loss
882+
} else {
883+
None
884+
};
868885
let mut sim = match noise {
869886
Some(noise) => SparseSim::new_with_noise(&noise),
870-
None => SparseSim::new(),
887+
None => match noise_config {
888+
Some(config) => SparseSim::new_with_noise_config(config.into()),
889+
None => SparseSim::new(),
890+
},
871891
};
872892
if let Some(loss) = qubit_loss {
873893
sim.set_loss(loss);

source/compiler/qsc/src/interpret/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod given_interpreter {
2323
fn run(interpreter: &mut Interpreter, expr: &str) -> (InterpretResult, String) {
2424
let mut cursor = Cursor::new(Vec::<u8>::new());
2525
let mut receiver = CursorReceiver::new(&mut cursor);
26-
let res = interpreter.run(&mut receiver, Some(expr), None, None, None);
26+
let res = interpreter.run(&mut receiver, Some(expr), None, None, None, None);
2727
(res, receiver.dump())
2828
}
2929

0 commit comments

Comments
 (0)