1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
use super::visualizer::Visualizer;
use core::fmt;
use libpsi_core::{GateOp, QuantumCircuit};
pub struct VerticalRenderer<'a> {
circuit: &'a QuantumCircuit,
}
impl<'a> VerticalRenderer<'a> {
pub fn new(circuit: &'a QuantumCircuit) -> Self {
VerticalRenderer { circuit }
}
fn gate_label(op: &GateOp) -> String {
match op {
GateOp::H(_) => "[H]".to_string(),
GateOp::X(_) => "[X]".to_string(),
GateOp::Y(_) => "[Y]".to_string(),
GateOp::Z(_) => "[Z]".to_string(),
GateOp::S(_) => "[S]".to_string(),
GateOp::T(_) => "[T]".to_string(),
GateOp::CNOT(_, _) => "●".to_string(),
GateOp::CZ(_, _) => "●".to_string(),
GateOp::SWAP(_, _) => "╳".to_string(),
GateOp::CCNOT(_, _, _) => "●".to_string(),
GateOp::CSWAP(_, _, _) => "●".to_string(),
GateOp::Measure(_, _) => "[M]".to_string(),
GateOp::Custom(gate, _) => format!("[{}]", gate.name),
}
}
fn calculate_col_width(&self) -> usize {
let min_width = 3;
let mut max_label_len = min_width;
for op in self.circuit.operations() {
let label = Self::gate_label(op);
let char_count: usize = label.chars().count();
if char_count > max_label_len {
max_label_len = char_count;
}
}
let width = max_label_len + 2;
if width % 2 == 0 {
width + 1
} else {
width
}
}
}
impl<'a> Visualizer for VerticalRenderer<'a> {
fn export(&self) -> String {
format!("{}", self)
}
}
impl<'a> fmt::Display for VerticalRenderer<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let nq = self.circuit.num_qubits();
let nc = self.circuit.num_classical();
let ops = self.circuit.operations();
let col_width = self.calculate_col_width();
let gap_width = 3;
let q_header: String = (0..nq)
.map(|i| format!("{:^width$}", format!("q{}", i), width = col_width))
.collect::<Vec<_>>()
.join(" ");
let c_header: String = (0..nc)
.map(|i| format!("{:^width$}", format!("c{}", i), width = col_width))
.collect::<Vec<_>>()
.join(" ");
if nc > 0 {
writeln!(f, "{}{}{}", q_header, " ".repeat(gap_width), c_header)?;
} else {
writeln!(f, "{}", q_header)?;
}
let q_wires: String = (0..nq)
.map(|_| format!("{:^width$}", "│", width = col_width))
.collect::<Vec<_>>()
.join(" ");
let c_wires: String = (0..nc)
.map(|_| format!("{:^width$}", "║", width = col_width))
.collect::<Vec<_>>()
.join(" ");
let full_wires = if nc > 0 {
format!("{}{}{}", q_wires, " ".repeat(gap_width), c_wires)
} else {
q_wires.clone()
};
if ops.is_empty() {
writeln!(f, "{}", full_wires)?;
return Ok(());
}
let q_total = nq * col_width + (nq - 1);
let c_total = if nc > 0 { nc * col_width + (nc - 1) } else { 0 };
let total_width = q_total + gap_width + c_total;
for op in ops {
writeln!(f, "{}", full_wires)?;
let q_targets = op.quantum_targets();
let min_q = q_targets.iter().min().copied().unwrap_or(0);
let max_q = q_targets.iter().max().copied().unwrap_or(0);
let label = Self::gate_label(op);
match op {
GateOp::H(t)
| GateOp::X(t)
| GateOp::Y(t)
| GateOp::Z(t)
| GateOp::S(t)
| GateOp::T(t) => {
let mut line: Vec<char> = vec![' '; total_width];
for i in 0..nq {
let col_start = i * (col_width + 1);
let center = col_start + col_width / 2;
if i == *t {
let label_start = col_start + (col_width - label.chars().count()) / 2;
for (j, ch) in label.chars().enumerate() {
line[label_start + j] = ch;
}
} else {
line[center] = '│';
}
}
for i in 0..nc {
let center = q_total + gap_width + i * (col_width + 1) + col_width / 2;
line[center] = '║';
}
let gate_line: String = line.into_iter().collect();
writeln!(f, "{}", gate_line)?;
}
GateOp::CNOT(c, t) | GateOp::CZ(c, t) | GateOp::SWAP(c, t) => {
let (sym1, sym2) = match op {
GateOp::CNOT(_, _) => ('●', '⊕'),
GateOp::CZ(_, _) => ('●', '●'),
GateOp::SWAP(_, _) => ('╳', '╳'),
_ => unreachable!(),
};
let mut line: Vec<char> = vec![' '; total_width];
for i in 0..nq {
let center = i * (col_width + 1) + col_width / 2;
if i < min_q || i > max_q {
line[center] = '│';
} else if i == *c {
line[center] = sym1;
} else if i == *t {
line[center] = sym2;
}
}
let min_center = min_q * (col_width + 1) + col_width / 2;
let max_center = max_q * (col_width + 1) + col_width / 2;
for pos in (min_center + 1)..max_center {
if line[pos] == ' ' {
line[pos] = '─';
}
}
for i in 0..nc {
let center = q_total + gap_width + i * (col_width + 1) + col_width / 2;
line[center] = '║';
}
let gate_line: String = line.into_iter().collect();
writeln!(f, "{}", gate_line)?;
}
GateOp::CCNOT(c1, c2, t) | GateOp::CSWAP(c1, c2, t) => {
let (sym_c, sym_t) = match op {
GateOp::CCNOT(_, _, _) => ('●', '⊕'),
GateOp::CSWAP(_, _, _) => ('●', '╳'),
_ => unreachable!(),
};
let is_cswap = matches!(op, GateOp::CSWAP(_, _, _));
let mut line: Vec<char> = vec![' '; total_width];
for i in 0..nq {
let center = i * (col_width + 1) + col_width / 2;
if i < min_q || i > max_q {
line[center] = '│';
} else if i == *c1 {
line[center] = sym_c;
} else if i == *c2 {
line[center] = if is_cswap { sym_t } else { sym_c };
} else if i == *t {
line[center] = sym_t;
}
}
let min_center = min_q * (col_width + 1) + col_width / 2;
let max_center = max_q * (col_width + 1) + col_width / 2;
for pos in (min_center + 1)..max_center {
if line[pos] == ' ' {
line[pos] = '─';
}
}
for i in 0..nc {
let center = q_total + gap_width + i * (col_width + 1) + col_width / 2;
line[center] = '║';
}
let gate_line: String = line.into_iter().collect();
writeln!(f, "{}", gate_line)?;
}
GateOp::Measure(mq, mc) => {
let mut line: Vec<char> = vec![' '; total_width];
for i in 0..nq {
let col_start = i * (col_width + 1);
let center = col_start + col_width / 2;
if i < *mq {
line[center] = '│';
} else if i == *mq {
let label_start = col_start + (col_width - label.chars().count()) / 2;
for (j, ch) in label.chars().enumerate() {
line[label_start + j] = ch;
}
}
}
let mq_col_start = *mq * (col_width + 1);
let mq_center = mq_col_start + col_width / 2;
let mc_start = q_total + gap_width;
let mc_center = mc_start + *mc * (col_width + 1) + col_width / 2;
for pos in (mq_center + 2)..=mc_center {
if line[pos] == ' ' {
line[pos] = '═';
}
}
line[mc_center] = '╣';
for i in 0..nc {
let center = mc_start + i * (col_width + 1) + col_width / 2;
if i > *mc {
line[center] = '║';
}
}
let measure_line: String = line.into_iter().collect();
writeln!(f, "{}", measure_line)?;
}
GateOp::Custom(_, targets) => {
let mut line: Vec<char> = vec![' '; total_width];
if targets.len() == 1 {
for i in 0..nq {
let col_start = i * (col_width + 1);
let center = col_start + col_width / 2;
if i == targets[0] {
let label_start =
col_start + (col_width - label.chars().count()) / 2;
for (j, ch) in label.chars().enumerate() {
line[label_start + j] = ch;
}
} else {
line[center] = '│';
}
}
for i in 0..nc {
let center = q_total + gap_width + i * (col_width + 1) + col_width / 2;
line[center] = '║';
}
} else {
for i in 0..nq {
let col_start = i * (col_width + 1);
let center = col_start + col_width / 2;
if i < min_q || i > max_q {
line[center] = '│';
} else if i == targets[0] {
let label_start =
col_start + (col_width - label.chars().count()) / 2;
for (j, ch) in label.chars().enumerate() {
line[label_start + j] = ch;
}
} else if targets.contains(&i) {
line[center] = '□';
}
}
let min_center = min_q * (col_width + 1) + col_width / 2;
let max_center = max_q * (col_width + 1) + col_width / 2;
for pos in (min_center + 1)..max_center {
if line[pos] == ' ' {
line[pos] = '─';
}
}
for i in 0..nc {
let center = q_total + gap_width + i * (col_width + 1) + col_width / 2;
line[center] = '║';
}
}
let gate_line: String = line.into_iter().collect();
writeln!(f, "{}", gate_line)?;
}
}
}
writeln!(f, "{}", full_wires)?;
let end_line: String = "░".repeat(total_width);
writeln!(f, "{}", end_line)?;
Ok(())
}
}
|