-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR replaces the existing OpenQASM 2.0 parser with a new OpenQASM 3.0 parser. The new parser now builds a syntax tree, where type checking, constant evaluation, and translation to the Quantum circuit. The parser can handle the following new features: ### New Syntax New syntax for declaring bits, qubit, measure operations. The old syntax (`creg`, `qreg`) is still supported. ```qasm qubit[8] q; bit[8] c; c[0] = measure q[0]; measure q[1] -> c[1]; if (c[0] == 1) { x q[0]; } ``` ### Gate modifiers Gate modifiers (`inv`, `ctrl`, and `negctrl`) are now supported. This replaces the `c` prefix. See the OpenQASM 3.0 specification for more information: https://openqasm.com/language/gates.html#quantum-gate-modifiers ```qasm ctrl @ x q[0], q[1]; // Equivalent to cx q ctrl(2) @ x q[0], q[1], q[2]; // Equivalent to ccx q; ``` ### Classical constant values The parser now supports classical computation with constant values. This can be used to e.g. define the number of quantum registers. ```qasm const uint N = 4; qubit[N * 2]; x qubit[N * 2 - 1]; ``` Additionally, all features of the previous parser are still supported. The big features from OpenQASM 3.0 still missing are: - classical computational features such as loops, functions, etc. (see #33) - types such as bools, floats, angles, complex types, etc. (see #30, #32) - `pow` modifier (#27) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lukas Burgholzer <[email protected]>
- Loading branch information
1 parent
d181d45
commit e193ddb
Showing
54 changed files
with
7,999 additions
and
2,059 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include "Statement.hpp" | ||
|
||
namespace qasm3 { | ||
class CompilerError final : std::exception { | ||
public: | ||
std::string message{}; | ||
std::shared_ptr<DebugInfo> debugInfo{}; | ||
|
||
CompilerError(std::string msg, std::shared_ptr<DebugInfo> debug) | ||
: message(std::move(msg)), debugInfo(std::move(debug)) {} | ||
|
||
[[nodiscard]] std::string toString() const { | ||
std::stringstream ss{}; | ||
ss << debugInfo->toString(); | ||
|
||
auto parentDebugInfo = debugInfo->parent; | ||
while (parentDebugInfo != nullptr) { | ||
ss << "\n (included from " << parentDebugInfo->toString() << ")"; | ||
parentDebugInfo = parentDebugInfo->parent; | ||
} | ||
|
||
ss << ":\n" << message; | ||
|
||
return ss.str(); | ||
} | ||
}; | ||
} // namespace qasm3 | ||
|
||
class ConstEvalError final : std::exception { | ||
public: | ||
std::string message{}; | ||
|
||
explicit ConstEvalError(std::string msg) : message(std::move(msg)) {} | ||
|
||
[[nodiscard]] std::string toString() const { | ||
return "Constant Evaluation: " + message; | ||
} | ||
}; | ||
|
||
class TypeCheckError final : std::exception { | ||
public: | ||
std::string message{}; | ||
|
||
explicit TypeCheckError(std::string msg) : message(std::move(msg)) {} | ||
|
||
[[nodiscard]] std::string toString() const { | ||
return "Type Check Error: " + message; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include "NestedEnvironment.hpp" | ||
#include "QuantumComputation.hpp" | ||
#include "Statement.hpp" | ||
|
||
namespace qasm3 { | ||
struct GateInfo { | ||
size_t nControls; | ||
size_t nTargets; | ||
size_t nParameters; | ||
qc::OpType type; | ||
}; | ||
|
||
struct Gate { | ||
virtual ~Gate() = default; | ||
|
||
virtual size_t getNControls() = 0; | ||
virtual size_t getNTargets() = 0; | ||
virtual size_t getNParameters() = 0; | ||
}; | ||
|
||
struct StandardGate : Gate { | ||
GateInfo info; | ||
|
||
explicit StandardGate(const GateInfo& gateInfo) : info(gateInfo) {} | ||
|
||
size_t getNControls() override { return info.nControls; } | ||
|
||
size_t getNTargets() override { return info.nTargets; } | ||
size_t getNParameters() override { return info.nParameters; } | ||
}; | ||
|
||
struct CompoundGate : Gate { | ||
std::vector<std::string> parameterNames; | ||
std::vector<std::string> targetNames; | ||
std::vector<std::shared_ptr<QuantumStatement>> body; | ||
|
||
explicit CompoundGate( | ||
std::vector<std::string> parameters, std::vector<std::string> targets, | ||
std::vector<std::shared_ptr<QuantumStatement>> bodyStatements) | ||
: parameterNames(std::move(parameters)), targetNames(std::move(targets)), | ||
body(std::move(bodyStatements)) {} | ||
|
||
size_t getNControls() override { return 0; } | ||
|
||
size_t getNTargets() override { return targetNames.size(); } | ||
size_t getNParameters() override { return parameterNames.size(); } | ||
}; | ||
} // namespace qasm3 |
Oops, something went wrong.