-
Notifications
You must be signed in to change notification settings - Fork 1
/
draw.go
150 lines (127 loc) · 3.66 KB
/
draw.go
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
package qrad
import (
"fmt"
"strings"
)
var RightJoint = "├"
var LeftJoint = "┤"
var TopJoint = "┴"
var BottomJoint = "┬"
var Dash = "─"
var Bar = "│"
var TopLeftC = "┌"
var TopRightC = "┐"
var BottomLeftC = "└"
var BottomRightC = "┘"
var Control = "■"
func RenderMoment(m Moment) []string {
out := []string{}
width := len(m.Gate.Symbol) + 2
widthExtra := 0
if width%2 == 0 {
widthExtra = -1
}
for q := 0; q < m.Size; q++ {
if m.IsBarrier {
for i := 0; i < m.Size; i++ {
out = append(out, Bar+Bar)
out = append(out, Bar+Bar)
out = append(out, Bar+Bar)
}
return out
}
if m.IsGateAt(q) {
if m.Gate.IsBoxed {
if m.HasConnectionAbove(q) {
connectedTop := strings.Repeat(Dash, width/2) + TopJoint + strings.Repeat(Dash, width/2+widthExtra)
out = append(out, TopLeftC+connectedTop+TopRightC)
} else {
out = append(out, TopLeftC+strings.Repeat(Dash, width)+TopRightC)
}
out = append(out, LeftJoint+" "+m.Gate.Symbol+" "+RightJoint)
if m.HasConnectionBelow(q) {
connectedBottom := strings.Repeat(Dash, width/2) + BottomJoint + strings.Repeat(Dash, width/2+widthExtra)
out = append(out, BottomLeftC+connectedBottom+BottomRightC)
} else {
out = append(out, BottomLeftC+strings.Repeat(Dash, width)+BottomRightC)
}
} else {
if m.HasConnectionAbove(q) {
connectedTop := strings.Repeat(" ", width/2) + Bar + strings.Repeat(" ", width/2+widthExtra)
out = append(out, " "+connectedTop+" ")
} else {
out = append(out, strings.Repeat(" ", width+2))
}
out = append(out, Dash+" "+m.Gate.Symbol+" "+Dash)
if m.HasConnectionBelow(q) {
connectedTop := strings.Repeat(" ", width/2) + Bar + strings.Repeat(" ", width/2+widthExtra)
out = append(out, " "+connectedTop+" ")
} else {
out = append(out, strings.Repeat(" ", width+2))
}
}
} else {
if m.HasConnectionAbove(q) && (m.IsControlAt(q) || m.HasConnectionBelow(q)) {
connectedTop := strings.Repeat(" ", width/2) + Bar + strings.Repeat(" ", width/2+widthExtra)
out = append(out, " "+connectedTop+" ")
} else {
out = append(out, strings.Repeat(" ", width+2))
}
if m.IsControlAt(q) {
out = append(out, strings.Repeat(Dash, width/2+1)+Control+strings.Repeat(Dash, width/2+1+widthExtra))
} else {
out = append(out, strings.Repeat(Dash, width+2))
}
if m.HasConnectionBelow(q) && (m.IsControlAt(q) || m.HasConnectionAbove(q)) {
connectedBottom := strings.Repeat(" ", width/2) + Bar + strings.Repeat(" ", width/2+widthExtra)
out = append(out, " "+connectedBottom+" ")
} else {
out = append(out, strings.Repeat(" ", width+2))
}
}
}
return out
}
func JoinBuffers(a, b []string) []string {
if len(a) != len(b) {
fmt.Println(len(a), len(b))
panic("mis-matching lengths")
}
out := []string{}
for i := range a {
out = append(out, a[i]+b[i])
}
return out
}
func RenderMoments(moments []Moment) []string {
if len(moments) == 0 {
return []string{}
}
out := make([]string, moments[0].Size*3)
for _, m := range moments {
a := RenderMoment(m)
out = JoinBuffers(out, a)
}
return out
}
func DrawMoments(moments []Moment) {
buf := RenderMoments(moments)
fmt.Println(strings.Join(buf, "\n"))
}
func RenderInitialState(s []int) []string {
out := []string{}
for _, a := range s {
out = append(out, " ")
out = append(out, fmt.Sprintf("|%d>─", a))
out = append(out, " ")
}
return out
}
func DrawCircuit(c Circuit) {
fmt.Println(strings.Join(RenderCircuit(c), "\n"))
}
func RenderCircuit(c Circuit) []string {
initial := RenderInitialState(c.InitialState)
moments := RenderMoments(c.Moments)
return JoinBuffers(initial, moments)
}