Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(QBIN_COMPILER_SOURCES
src/main.cpp
src/compiler.cpp
src/tools.cpp
src/qasm_frontend.cpp
)

set(QBIN_COMPILER_HEADERS
include/qbin_compiler/compiler.hpp
include/qbin_compiler/qasm_frontend.hpp
include/qbin_compiler/tools.hpp
)

add_executable(qbin-compile ${QBIN_COMPILER_SOURCES} ${QBIN_COMPILER_HEADERS})
Expand Down
60 changes: 17 additions & 43 deletions compiler/include/qbin_compiler/qasm_frontend.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#ifndef QBIN_COMPILER_QASM_FRONTEND_HPP
#define QBIN_COMPILER_QASM_FRONTEND_HPP

#pragma once
#include <cstdint>
#include <string>
#include <string_view>
Expand All @@ -9,56 +7,32 @@
namespace qbin_compiler {
namespace frontend {

// Core opcode values (aligned with spec draft)
enum class Opcode : uint8_t {
X = 0x01, Y = 0x02, Z = 0x03, H = 0x04,
S = 0x05, SDG = 0x06, T = 0x07, TDG = 0x08,
SX = 0x09, SXDG = 0x0A,
enum class Op : uint8_t {
X = 0x01, Y = 0x02, Z = 0x03, H = 0x04, S = 0x05, SDG = 0x06, T = 0x07, TDG = 0x08, SX = 0x09, SXDG = 0x0A,
RX = 0x0B, RY = 0x0C, RZ = 0x0D, PHASE = 0x0E,
U = 0x0F,
CX = 0x10, CZ = 0x11, ECR = 0x12, SWAP = 0x13, CSX = 0x14,
CRX = 0x15, CRY = 0x16, CRZ = 0x17, CU = 0x18,
CX = 0x10, CZ = 0x11, ECR = 0x12, SWAP = 0x13, CSX = 0x14, CRX = 0x15, CRY = 0x16, CRZ = 0x17, CU = 0x18,
RXX = 0x20, RYY = 0x21, RZZ = 0x22,
MEASURE = 0x30, RESET = 0x31, BARRIER = 0x32,
DELAY = 0x38, FRAME = 0x39,
// Structured control (MVP subset)
IF_EQ = 0x81, IF_NEQ = 0x82, ENDIF = 0x8F,
CALLG = 0x40
MEASURE = 0x30, RESET = 0x31, BARRIER = 0x32, DELAY = 0x38, FRAME = 0x39, CALLG = 0x40,
IF_EQ = 0x81, IF_NEQ = 0x82, ENDIF = 0x8F
};

struct Instr {
Opcode op;
int a = -1; // qubit a
int b = -1; // qubit b
int c = -1; // qubit c (unused here)

// Angle slot 0
bool has_angle0 = false;
bool angle0_is_param = false; // reserved
float angle0 = 0.0f;

// Aux 32-bit payload (e.g., classical bit index for MEASURE/IF)
bool has_aux = false;
uint32_t aux_u32 = 0;

// Small immediate byte (e.g., IF compare value 0/1)
bool has_imm8 = false;
uint8_t imm8 = 0;
Op op;
int a = -1, b = -1, c = -1; // qubit indices
bool has_angle = false; float angle = 0.0f; // for single-angle ops (rx/ry/rz/phase)
bool has_aux = false; uint32_t aux = 0; // for measure bit index or IF bit index
bool has_imm8 = false; uint8_t imm8 = 0; // for IF compare constant
};

struct Program {
std::vector<Instr> instrs;
std::vector<Instr> code;
int max_qubit = -1;
int max_bit = -1;
};

// Parse minimal OpenQASM subset used by the MVP compiler:
// - h/x/y/z/s/sdg/t/tdg/sx/sxdg q[i];
// - rx/ry/rz/phase(<angle>) q[i];
// - cx/cz/swap q[i], q[j];
// - c[k] = measure q[i];
// - if (c[k] == 1) { <single stmt>; } (also supports != 0/1)
Program parse_qasm_subset(std::string_view text, bool verbose);
// Auto-detects QASM 2.0 or 3.0 and parses into our canonical IR.
// Supports QASM2 'gate' definitions with parameter substitution and 'u' decomposition.
Program parse_qasm_subset(std::string_view text, bool verbose = false);

} // namespace frontend
} // namespace qbin_compiler

#endif // QBIN_COMPILER_QASM_FRONTEND_HPP
27 changes: 27 additions & 0 deletions compiler/include/qbin_compiler/tools.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <cstddef>
#include <string>
#include <string_view>
#include <vector>

namespace qbin_compiler { namespace util {

// String helpers
std::string to_lower_ascii(std::string s);
std::string trim(std::string_view sv);

// Split a comma-separated list, respecting parentheses depth if requested.
std::vector<std::string> split_commas(const std::string& s, bool respect_parens = true);

// Find the index of the matching ')' given an index of '(' in s.
// Returns std::string::npos if not found.
std::size_t find_matching_paren(const std::string& s, std::size_t open_pos);

// Evaluate a numeric expression with: numbers, pi, + - * /, parentheses.
// Uses double precision; caller can cast to float if needed.
double eval_expr(const std::string& expr);

// Very small logger: prints to stderr if enabled == true.
void vlog(bool enabled, const std::string& msg);

}} // namespace
Loading