microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/resource_estimator/src/lib.rs
80lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | //! A Q# backend to compute logical overheads to compute the overhead based on |
| 5 | //! the PSSPC layout method. |
| 6 | |
| 7 | // This crate uses lots of converstions between floating point numbers and integers, so this helps |
| 8 | // avoid many needed allow statements. Comment out individual lines to see where they are needed. |
| 9 | #![allow( |
| 10 | clippy::cast_possible_truncation, |
| 11 | clippy::cast_sign_loss, |
| 12 | clippy::cast_precision_loss, |
| 13 | clippy::cast_lossless |
| 14 | )] |
| 15 | |
| 16 | pub mod counts; |
| 17 | /// Provides traits to define a fault-tolerant quantum computing architecture |
| 18 | /// and functions to perform resource estimation on such architectures. |
| 19 | pub mod estimates; |
| 20 | /// Models a fault-tolerant quantum computing architecture based on |
| 21 | /// customizaable gate-based and Majorana qubits, planar codes, and T-factories. |
| 22 | pub mod system; |
| 23 | |
| 24 | pub use system::estimate_physical_resources_from_json; |
| 25 | |
| 26 | use counts::LogicalCounter; |
| 27 | use miette::Diagnostic; |
| 28 | use qsc::interpret::{self, GenericReceiver, Interpreter, Value}; |
| 29 | use system::estimate_physical_resources; |
| 30 | use thiserror::Error; |
| 31 | |
| 32 | #[derive(Debug, Diagnostic, Error)] |
| 33 | #[error(transparent)] |
| 34 | #[diagnostic(transparent)] |
| 35 | pub enum Error { |
| 36 | Interpreter(interpret::Error), |
| 37 | Estimation(system::Error), |
| 38 | } |
| 39 | |
| 40 | pub fn estimate_entry(interpreter: &mut Interpreter, params: &str) -> Result<String, Vec<Error>> { |
| 41 | let mut counter = LogicalCounter::default(); |
| 42 | let mut stdout = std::io::sink(); |
| 43 | let mut out = GenericReceiver::new(&mut stdout); |
| 44 | interpreter |
| 45 | .eval_entry_with_sim(&mut counter, &mut out) |
| 46 | .map_err(|e| e.into_iter().map(Error::Interpreter).collect::<Vec<_>>())?; |
| 47 | estimate_physical_resources(counter.logical_resources(), params) |
| 48 | .map_err(|e| vec![Error::Estimation(e)]) |
| 49 | } |
| 50 | |
| 51 | pub fn estimate_expr( |
| 52 | interpreter: &mut Interpreter, |
| 53 | expr: &str, |
| 54 | params: &str, |
| 55 | ) -> Result<String, Vec<Error>> { |
| 56 | let mut counter = LogicalCounter::default(); |
| 57 | let mut stdout = std::io::sink(); |
| 58 | let mut out = GenericReceiver::new(&mut stdout); |
| 59 | interpreter |
| 60 | .run_with_sim(&mut counter, &mut out, Some(expr)) |
| 61 | .map_err(|e| e.into_iter().map(Error::Interpreter).collect::<Vec<_>>())?; |
| 62 | estimate_physical_resources(counter.logical_resources(), params) |
| 63 | .map_err(|e| vec![Error::Estimation(e)]) |
| 64 | } |
| 65 | |
| 66 | pub fn estimate_call( |
| 67 | interpreter: &mut Interpreter, |
| 68 | callable: Value, |
| 69 | args: Value, |
| 70 | params: &str, |
| 71 | ) -> Result<String, Vec<Error>> { |
| 72 | let mut counter = LogicalCounter::default(); |
| 73 | let mut stdout = std::io::sink(); |
| 74 | let mut out = GenericReceiver::new(&mut stdout); |
| 75 | interpreter |
| 76 | .invoke_with_sim(&mut counter, &mut out, callable, args) |
| 77 | .map_err(|e| e.into_iter().map(Error::Interpreter).collect::<Vec<_>>())?; |
| 78 | estimate_physical_resources(counter.logical_resources(), params) |
| 79 | .map_err(|e| vec![Error::Estimation(e)]) |
| 80 | } |
| 81 | |