microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
iadavis/pipeline-issue-debugging

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/pip/src/displayable_output.rs

102lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#[cfg(test)]
5mod tests;
6
7use num_bigint::BigUint;
8use num_complex::{Complex64, ComplexFloat};
9use qsc::{
10 fmt_basis_state_label, fmt_complex, format_state_id, get_matrix_latex, get_phase,
11 get_state_latex,
12};
13use std::fmt::Write;
14
15#[derive(Clone)]
16pub struct DisplayableState(pub Vec<(BigUint, Complex64)>, pub usize);
17pub struct DisplayableMatrix(pub Vec<Vec<Complex64>>);
18
19impl DisplayableState {
20 pub fn to_plain(&self) -> String {
21 if self.1 > 0 {
22 format!(
23 "STATE:{}",
24 self.0
25 .iter()
26 .fold(String::new(), |mut output, (id, state)| {
27 let _ = write!(
28 output,
29 "\n{}: {}",
30 format_state_id(id, self.1),
31 fmt_complex(state)
32 );
33 output
34 })
35 )
36 } else {
37 "STATE:\nNo qubits allocated".to_string()
38 }
39 }
40
41 pub fn to_html(&self) -> String {
42 if self.1 > 0 {
43 format!(
44 include_str!("state_header_template.html"),
45 self.0
46 .iter()
47 .fold(String::new(), |mut output, (id, state)| {
48 let amplitude = state.abs().powi(2) * 100.0;
49 let _ = write!(
50 output,
51 include_str!("state_row_template.html"),
52 fmt_basis_state_label(id, self.1),
53 fmt_complex(state),
54 amplitude,
55 amplitude,
56 get_phase(state),
57 get_phase(state)
58 );
59 output
60 })
61 )
62 } else {
63 format!(
64 include_str!("state_header_template.html"),
65 "<tr><td>No qubits allocated</td></tr>".to_string()
66 )
67 }
68 }
69
70 pub fn to_latex(&self) -> Option<String> {
71 get_state_latex(&self.0, self.1)
72 }
73}
74
75impl DisplayableMatrix {
76 pub fn to_plain(&self) -> String {
77 format!(
78 "MATRIX:{}",
79 self.0.iter().fold(String::new(), |mut output, row| {
80 let _ = write!(
81 output,
82 "\n{}",
83 row.iter().fold(String::new(), |mut row_output, element| {
84 let _ = write!(row_output, " {}", fmt_complex(element));
85 row_output
86 })
87 );
88 output
89 })
90 )
91 }
92
93 pub fn to_latex(&self) -> String {
94 get_matrix_latex(&self.0)
95 }
96}
97
98pub enum DisplayableOutput {
99 State(DisplayableState),
100 Message(String),
101 Matrix(DisplayableMatrix),
102}
103