microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
alex/bounds

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_eval/src/output.rs

123lines · 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 if qubit_count > 0 {
44 for (id, state) in state {
45 writeln!(
46 self.writer,
47 "{}: {}",
48 format_state_id(&id, qubit_count),
49 fmt_complex(&state),
50 )
51 .map_err(|_| Error)?;
52 }
53 } else {
54 writeln!(self.writer, "No qubits allocated").map_err(|_| Error)?;
55 }
56 Ok(())
57 }
58
59 fn matrix(&mut self, matrix: Vec<Vec<Complex64>>) -> Result<(), Error> {
60 writeln!(self.writer, "MATRIX:").map_err(|_| Error)?;
61 for row in matrix {
62 let row_str = row.iter().map(fmt_complex).collect::<Vec<_>>().join(" ");
63 writeln!(self.writer, "{row_str}").map_err(|_| Error)?;
64 }
65 Ok(())
66 }
67
68 fn message(&mut self, msg: &str) -> Result<(), Error> {
69 writeln!(self.writer, "{msg}").map_err(|_| Error)
70 }
71}
72
73pub struct CursorReceiver<'a> {
74 cursor: &'a mut Cursor<Vec<u8>>,
75}
76
77impl<'a> CursorReceiver<'a> {
78 pub fn new(cursor: &'a mut Cursor<Vec<u8>>) -> Self {
79 Self { cursor }
80 }
81 pub fn dump(&mut self) -> String {
82 let v = self.cursor.get_mut();
83 let s = match std::str::from_utf8(v) {
84 Ok(v) => v.to_owned(),
85 Err(e) => format!("Invalid UTF-8 sequence: {e}"),
86 };
87 v.clear();
88 s.trim().to_string()
89 }
90}
91
92impl<'a> Receiver for CursorReceiver<'a> {
93 fn state(&mut self, state: Vec<(BigUint, Complex64)>, qubit_count: usize) -> Result<(), Error> {
94 writeln!(self.cursor, "STATE:").map_err(|_| Error)?;
95 if qubit_count > 0 {
96 for (id, state) in state {
97 writeln!(
98 self.cursor,
99 "{}: {}",
100 format_state_id(&id, qubit_count),
101 state
102 )
103 .map_err(|_| Error)?;
104 }
105 } else {
106 writeln!(self.cursor, "No qubits allocated").map_err(|_| Error)?;
107 }
108 Ok(())
109 }
110
111 fn matrix(&mut self, matrix: Vec<Vec<Complex64>>) -> Result<(), Error> {
112 writeln!(self.cursor, "MATRIX:").map_err(|_| Error)?;
113 for row in matrix {
114 let row_str = row.iter().map(fmt_complex).collect::<Vec<_>>().join(" ");
115 writeln!(self.cursor, "{row_str}").map_err(|_| Error)?;
116 }
117 Ok(())
118 }
119
120 fn message(&mut self, msg: &str) -> Result<(), Error> {
121 writeln!(self.cursor, "{msg}").map_err(|_| Error)
122 }
123}
124