microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
compiler/qsc_eval/src/output.rs
92lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use std::io::{Cursor, Write}; |
| 5 | |
| 6 | use crate::state::{fmt_complex, format_state_id}; |
| 7 | use num_bigint::BigUint; |
| 8 | use num_complex::Complex64; |
| 9 | |
| 10 | #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 11 | pub struct Error; |
| 12 | |
| 13 | pub trait Receiver { |
| 14 | /// Receive state output |
| 15 | /// # Errors |
| 16 | /// This will return an error if handling the output fails. |
| 17 | fn state(&mut self, state: Vec<(BigUint, Complex64)>, qubit_count: usize) -> Result<(), Error>; |
| 18 | |
| 19 | /// Receive generic message output |
| 20 | /// # Errors |
| 21 | /// This will return an error if handling the output fails. |
| 22 | fn message(&mut self, msg: &str) -> Result<(), Error>; |
| 23 | } |
| 24 | |
| 25 | pub struct GenericReceiver<'a> { |
| 26 | writer: &'a mut dyn Write, |
| 27 | } |
| 28 | |
| 29 | impl<'a> GenericReceiver<'a> { |
| 30 | pub fn new(writer: &'a mut impl Write) -> Self { |
| 31 | Self { writer } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl<'a> Receiver for GenericReceiver<'a> { |
| 36 | fn state(&mut self, state: Vec<(BigUint, Complex64)>, qubit_count: usize) -> Result<(), Error> { |
| 37 | writeln!(self.writer, "STATE:").map_err(|_| Error)?; |
| 38 | for (id, state) in state { |
| 39 | writeln!( |
| 40 | self.writer, |
| 41 | "{}: {}", |
| 42 | format_state_id(&id, qubit_count), |
| 43 | fmt_complex(&state), |
| 44 | ) |
| 45 | .map_err(|_| Error)?; |
| 46 | } |
| 47 | Ok(()) |
| 48 | } |
| 49 | |
| 50 | fn message(&mut self, msg: &str) -> Result<(), Error> { |
| 51 | writeln!(self.writer, "{msg}").map_err(|_| Error) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub struct CursorReceiver<'a> { |
| 56 | cursor: &'a mut Cursor<Vec<u8>>, |
| 57 | } |
| 58 | |
| 59 | impl<'a> CursorReceiver<'a> { |
| 60 | pub fn new(cursor: &'a mut Cursor<Vec<u8>>) -> Self { |
| 61 | Self { cursor } |
| 62 | } |
| 63 | pub fn dump(&mut self) -> String { |
| 64 | let v = self.cursor.get_mut(); |
| 65 | let s = match std::str::from_utf8(v) { |
| 66 | Ok(v) => v.to_owned(), |
| 67 | Err(e) => format!("Invalid UTF-8 sequence: {e}"), |
| 68 | }; |
| 69 | v.clear(); |
| 70 | s.trim().to_string() |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | impl<'a> Receiver for CursorReceiver<'a> { |
| 75 | fn state(&mut self, state: Vec<(BigUint, Complex64)>, qubit_count: usize) -> Result<(), Error> { |
| 76 | writeln!(self.cursor, "STATE:").map_err(|_| Error)?; |
| 77 | for (id, state) in state { |
| 78 | writeln!( |
| 79 | self.cursor, |
| 80 | "{}: {}", |
| 81 | format_state_id(&id, qubit_count), |
| 82 | state |
| 83 | ) |
| 84 | .map_err(|_| Error)?; |
| 85 | } |
| 86 | Ok(()) |
| 87 | } |
| 88 | |
| 89 | fn message(&mut self, msg: &str) -> Result<(), Error> { |
| 90 | writeln!(self.cursor, "{msg}").map_err(|_| Error) |
| 91 | } |
| 92 | } |
| 93 | |