microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
alex/telemmocks

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

compiler/qsc_eval/src/output.rs

115lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use std::io::{Cursor, Write};
5
6use crate::state::{fmt_complex, format_state_id};
7use num_bigint::BigUint;
8use num_complex::Complex64;
9
10#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
11pub struct Error;
12
13pub 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 matrix output
20 /// # Errors
21 /// This will return an error if handling the output fails.
22 fn matrix(&mut self, matrix: Vec<Vec<Complex64>>) -> Result<(), Error>;
23
24 /// Receive generic message output
25 /// # Errors
26 /// This will return an error if handling the output fails.
27 fn message(&mut self, msg: &str) -> Result<(), Error>;
28}
29
30pub struct GenericReceiver<'a> {
31 writer: &'a mut dyn Write,
32}
33
34impl<'a> GenericReceiver<'a> {
35 pub fn new(writer: &'a mut impl Write) -> Self {
36 Self { writer }
37 }
38}
39
40impl<'a> Receiver for GenericReceiver<'a> {
41 fn state(&mut self, state: Vec<(BigUint, Complex64)>, qubit_count: usize) -> Result<(), Error> {
42 writeln!(self.writer, "STATE:").map_err(|_| Error)?;
43 for (id, state) in state {
44 writeln!(
45 self.writer,
46 "{}: {}",
47 format_state_id(&id, qubit_count),
48 fmt_complex(&state),
49 )
50 .map_err(|_| Error)?;
51 }
52 Ok(())
53 }
54
55 fn matrix(&mut self, matrix: Vec<Vec<Complex64>>) -> Result<(), Error> {
56 writeln!(self.writer, "MATRIX:").map_err(|_| Error)?;
57 for row in matrix {
58 let row_str = row.iter().map(fmt_complex).collect::<Vec<_>>().join(" ");
59 writeln!(self.writer, "{row_str}").map_err(|_| Error)?;
60 }
61 Ok(())
62 }
63
64 fn message(&mut self, msg: &str) -> Result<(), Error> {
65 writeln!(self.writer, "{msg}").map_err(|_| Error)
66 }
67}
68
69pub struct CursorReceiver<'a> {
70 cursor: &'a mut Cursor<Vec<u8>>,
71}
72
73impl<'a> CursorReceiver<'a> {
74 pub fn new(cursor: &'a mut Cursor<Vec<u8>>) -> Self {
75 Self { cursor }
76 }
77 pub fn dump(&mut self) -> String {
78 let v = self.cursor.get_mut();
79 let s = match std::str::from_utf8(v) {
80 Ok(v) => v.to_owned(),
81 Err(e) => format!("Invalid UTF-8 sequence: {e}"),
82 };
83 v.clear();
84 s.trim().to_string()
85 }
86}
87
88impl<'a> Receiver for CursorReceiver<'a> {
89 fn state(&mut self, state: Vec<(BigUint, Complex64)>, qubit_count: usize) -> Result<(), Error> {
90 writeln!(self.cursor, "STATE:").map_err(|_| Error)?;
91 for (id, state) in state {
92 writeln!(
93 self.cursor,
94 "{}: {}",
95 format_state_id(&id, qubit_count),
96 state
97 )
98 .map_err(|_| Error)?;
99 }
100 Ok(())
101 }
102
103 fn matrix(&mut self, matrix: Vec<Vec<Complex64>>) -> Result<(), Error> {
104 writeln!(self.cursor, "MATRIX:").map_err(|_| Error)?;
105 for row in matrix {
106 let row_str = row.iter().map(fmt_complex).collect::<Vec<_>>().join(" ");
107 writeln!(self.cursor, "{row_str}").map_err(|_| Error)?;
108 }
109 Ok(())
110 }
111
112 fn message(&mut self, msg: &str) -> Result<(), Error> {
113 writeln!(self.cursor, "{msg}").map_err(|_| Error)
114 }
115}
116