-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
110 lines (101 loc) · 4.33 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "walker.h"
#include <QAbstract3DGraph>
using namespace std::complex_literals;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
for (auto & graph : {&cGraph, &qGraph}) {
graph->setFlags(graph->flags() ^ Qt::FramelessWindowHint);
graph->columnAxis()->setRange(0,6);
graph->valueAxis()->setRange(0, 1);
auto graphWidget = QWidget::createWindowContainer(graph, this);
graph->setSelectionMode(QtDataVisualization::QAbstract3DGraph::SelectionFlags(QtDataVisualization::QAbstract3DGraph::SelectionSlice | QtDataVisualization::QAbstract3DGraph::SelectionRow));
graphLayout.addWidget(graphWidget);
}
initStateCombo.addItems({tr("tails"), tr("heads"), tr("tails / heads"), tr("tails / imaginary heads")});
initStateCombo.setCurrentIndex(static_cast<int>(InitState::Tails));
QObject::connect(&initStateCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](const int){ simulate(); });
int row = 0;
auto addNumberControl = [&row, this](QSpinBox & spin, QSlider & slider, const int value, const int min, const int max, const QString & prefix, const QString & suffix) {
QObject::connect(&spin, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), [&slider, this](const int value) {
if (value != slider.value()) {
slider.setValue(value);
}
simulate();
});
QObject::connect(&slider, &QSlider::valueChanged, [&spin, this](const int value) {
if (value != spin.value()) {
spin.setValue(value);
}
});
spin.setPrefix(prefix);
spin.setSuffix(suffix);
spin.setRange(min, max);
slider.setRange(spin.minimum(), spin.maximum());
spin.setValue(value);
spin.setFixedWidth(spin.sizeHint().width());
slider.setOrientation(Qt::Vertical);
controlsLayout.addWidget(&spin, row, 0, Qt::AlignTop);
controlsLayout.addWidget(&slider, row++, 1);
};
controlsLayout.addWidget(&initStateCombo, row++, 0);
addNumberControl(pathLenSpin, pathLenSlider, 7, 1, 100, "length: ", "");
addNumberControl(stepsSpin, stepsSlider, 3, 1, 100, "", " steps");
controlsLayout.setAlignment(Qt::AlignTop);
mainLayout.addLayout(&graphLayout);
mainLayout.addLayout(&controlsLayout);
setCentralWidget(new QWidget(this));
centralWidget()->setLayout(&mainLayout);
setMinimumSize(500, 500);
}
void MainWindow::init(InitState const s) {
const auto length = pathLenSpin.value();
qInitState.setZero(length * 2);
cInitState.setZero(length * 2);
switch(s) {
case InitState::Tails:
qInitState[length - 1] = 1;
break;
case InitState::Heads:
qInitState[length] = 1;
break;
case InitState::TailsHeads:
qInitState[length - 1] = 1/std::sqrt(2);
qInitState[length] = 1/std::sqrt(2);
break;
case InitState::TailsImagHeads:
qInitState[length - 1] = 1/std::sqrt(2);
qInitState[length] = 1i/std::sqrt(2);
break;
}
if (s == InitState::Tails || s == InitState::Heads) {
cInitState = qInitState;
} else {
cInitState[length - 1] = 1;
}
}
void MainWindow::simulate() {
init(static_cast<InitState>(initStateCombo.currentIndex()));
Walker qWalker{qInitState, Walker::hadamardCoin()};
Walker cWalker{cInitState, Walker::classicCoin()};
auto sim = [this](Walker & walker, QtDataVisualization::Q3DBars & graph) {
for (auto * series : graph.seriesList()) {
graph.removeSeries(series);
}
graph.columnAxis()->setRange(0, pathLenSpin.value() - 1);
QtDataVisualization::QBar3DSeries * series = new QtDataVisualization::QBar3DSeries();
QtDataVisualization::QBarDataRow * data = new QtDataVisualization::QBarDataRow();
walker.step(stepsSpin.value());
const auto state = walker.getProbabilities();
double max = 0;
for (std::size_t i = 0; i < state.rows(); ++i) {
max = std::max(max, state[i]);
*data << state[i];
}
graph.valueAxis()->setRange(0, max);
series->dataProxy()->addRow(data);
graph.addSeries(series);
graph.seriesList().at(0)->setSelectedBar({0, 0});
};
sim(cWalker, cGraph);
sim(qWalker, qGraph);
}