Skip to content

Commit 300c434

Browse files
author
Your Name
committed
Updated cli example
1 parent 555f750 commit 300c434

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

examples/cpp/cli/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5)
22
project(cliMiniQbt)
33
set (CMAKE_CXX_STANDARD 14)
44
set(CMAKE_C_FLAGS_RELEASE "-O3")
5-
5+
find_library(MiniQbt REQUIRED)
66

77
SET(srcfiles
88
Main.cpp

examples/cpp/cli/Main.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,31 @@ int main(int argc, char** argv){
7373
printInfo(
7474
"Interactive shell for the Qasm language\n",
7575
"Exit program -> exit \n",
76-
"Show version -> about \n"
76+
"Show version -> about \n",
77+
"show a classic register (and collapse the quantum)-> collapse <classic register name> \n",
78+
"Reset the quantum register to super position -> reset <quantum register name>\n"
7779
);
7880
continue;
7981
}
8082

81-
if(command == "collapse"){
82-
for(const std::string& reg: intepreter.getRegisters()){
83-
std::vector<bool> result = intepreter.readClassicRegister(reg);
84-
std::cout << "Result registery " << reg << ": ";
85-
for(const bool& r : result){
86-
std::cout << r;
87-
}
88-
std::cout << "\n";
89-
}
90-
continue;
91-
}
83+
std::regex resetQuantum("\\s*(reset|collapse)\\s*([a-z|A-Z|0-9]*)", std::regex_constants::ECMAScript);
84+
std::smatch m;
85+
std::regex_match(command, m,resetQuantum);
86+
if (m[1] == "reset") {
87+
intepreter.resetSuperPosition(m[2]);
88+
continue;
89+
}
90+
else if (m[1] == "collapse") {
91+
std::vector<bool> result = intepreter.readClassicRegister(m[2]);
92+
std::cout << "Result registery " << m[1] << ": ";
93+
for (const bool& r : result) {
94+
std::cout << r;
95+
}
96+
std::cout << "\n";
97+
continue;
98+
}
9299

100+
93101
if(command[0] == '!'){
94102
std::cout.flush();
95103
system(command.substr(1).c_str());

examples/cpp/cli/Utils.hpp

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,46 @@
44
#include <string>
55
#include <iostream>
66

7+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
8+
#include <windows.h>
9+
#endif
10+
711
template<typename T>
812
constexpr void printInfo(T& msg){
913
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
14+
HANDLE hConsole;
15+
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
16+
SetConsoleTextAttribute(hConsole, 2);
1017
std::cout << msg;
18+
SetConsoleTextAttribute(hConsole, 7);
1119
#else
1220
std::cout << "\033[1;32m" << msg << "\033[0m";
1321
#endif
1422
}
1523

24+
template<typename T, typename... arguments>
25+
constexpr void printInfo(T& msg, arguments... args) {
26+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
27+
HANDLE hConsole;
28+
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
29+
SetConsoleTextAttribute(hConsole, 2);
30+
std::cout << msg;
31+
SetConsoleTextAttribute(hConsole, 7);
32+
#else
33+
std::cout << "\033[1;32m" << msg;
34+
#endif
35+
printInfo(args...);
36+
}
37+
38+
1639
template<typename T>
1740
constexpr void printWarning(T& msg){
1841
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
19-
std::cout << msg;
42+
HANDLE hConsole;
43+
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
44+
SetConsoleTextAttribute(hConsole, 14);
45+
std::cout << msg;
46+
SetConsoleTextAttribute(hConsole, 7);
2047
#else
2148
std::cout << "\033[1;33m" << msg << "\033[0m";
2249
#endif
@@ -25,27 +52,26 @@ constexpr void printWarning(T& msg){
2552
template<typename T, typename... arguments>
2653
constexpr void printWarning(T& msg, arguments... args){
2754
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
28-
std::cout<< msg;
55+
HANDLE hConsole;
56+
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
57+
SetConsoleTextAttribute(hConsole, 14);
58+
std::cout << msg;
59+
SetConsoleTextAttribute(hConsole, 7);
2960
#else
3061
std::cout << "\033[1;33m" << msg;
3162
#endif
3263
printWarning(args...);
3364
}
3465

35-
template<typename T, typename... arguments>
36-
constexpr void printInfo(T& msg, arguments... args){
37-
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
38-
std::cout<< msg;
39-
#else
40-
std::cout << "\033[1;32m" << msg;
41-
#endif
42-
printInfo(args...);
43-
}
4466

4567
template<typename T>
4668
constexpr void printError(T& msg){
4769
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
48-
std::cout << msg;
70+
HANDLE hConsole;
71+
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
72+
SetConsoleTextAttribute(hConsole, 4);
73+
std::cout << msg;
74+
SetConsoleTextAttribute(hConsole, 7);
4975
#else
5076
std::cout << msg << "\033[0m";
5177
#endif
@@ -56,7 +82,11 @@ constexpr void printError(T& msg){
5682
template<typename T, typename... arguments>
5783
constexpr void printError(T& msg, arguments... args){
5884
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
59-
std::cout<< msg;
85+
HANDLE hConsole;
86+
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
87+
SetConsoleTextAttribute(hConsole, 4);
88+
std::cout << msg;
89+
SetConsoleTextAttribute(hConsole, 7);
6090
#else
6191
std::cout << "\033[1;31m" << msg;
6292
#endif

modules/core/tests/IntepreterTests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required (VERSION 3.5)
22
project(IntepreterTests)
33
set (CMAKE_CXX_STANDARD 14)
4-
4+
find_library(MiniQbt REQUIRED)
55
SET(srcfiles
66
Main.cpp
77
SimpleAsyncTests.cpp

0 commit comments

Comments
 (0)