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 @@ -18,12 +18,14 @@ set(QBIN_COMPILER_SOURCES
src/main.cpp
src/compiler.cpp
src/tools.cpp
src/custom_gate.cpp
src/qasm_frontend.cpp
)

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

Expand Down
67 changes: 67 additions & 0 deletions compiler/include/qbin_compiler/custom_gate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <regex>

namespace qbin_compiler {

/**
* Represents a user-defined gate from OpenQASM.
* Example:
* gate majority a,b,c {
* cx a,b;
* cx a,c;
* ccx b,c,a;
* }
*/
class CustomGate {
public:
CustomGate() = default;
CustomGate(std::string name,
std::vector<std::string> params,
std::vector<std::string> body);

/// Gate name (e.g. "majority")
const std::string& getName() const { return name_; }

/// Parameter list (e.g. ["a","b","c"])
const std::vector<std::string>& getParams() const { return params_; }

/// Body (raw QASM statements, not yet expanded)
const std::vector<std::string>& getBody() const { return body_; }

/**
* Expand this gate with concrete arguments.
* Example:
* expand(["q[0]","q[1]","q[2]"])
* returns vector of QASM lines with params substituted.
*/
std::vector<std::string> expand(const std::vector<std::string>& args) const;

private:
std::string name_;
std::vector<std::string> params_;
std::vector<std::string> body_;
};

/**
* Registry for all custom gates.
* Provides storage and lookup.
*/
class GateRegistry {
public:
/// Register a new gate definition
void addGate(const CustomGate& gate);

/// Check if a gate is defined
bool hasGate(const std::string& name) const;

/// Get a gate definition (throws if not found)
const CustomGate& getGate(const std::string& name) const;

private:
std::unordered_map<std::string, CustomGate> gates_;
};

} // namespace qbin_compiler
59 changes: 59 additions & 0 deletions compiler/src/custom_gate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "qbin_compiler/custom_gate.hpp"
#include <unordered_map>

namespace qbin_compiler {

// ---------------- CustomGate ----------------

CustomGate::CustomGate(std::string name,
std::vector<std::string> params,
std::vector<std::string> body)
: name_(std::move(name)),
params_(std::move(params)),
body_(std::move(body)) {}

std::vector<std::string> CustomGate::expand(const std::vector<std::string>& args) const {
std::vector<std::string> out;

if (args.size() != params_.size()) {
// argument count mismatch return empty for now
return out;
}

// Build substitution map param -> arg
std::unordered_map<std::string, std::string> subs;
for (size_t i = 0; i < params_.size(); ++i) {
subs[params_[i]] = args[i];
}

// Apply substitutions to each body line
for (auto& line : body_) {
std::string replaced = line;
for (auto& kv : subs) {
size_t pos = 0;
while ((pos = replaced.find(kv.first, pos)) != std::string::npos) {
replaced.replace(pos, kv.first.size(), kv.second);
pos += kv.second.size();
}
}
out.push_back(replaced);
}

return out;
}

// ---------------- GateRegistry ----------------

void GateRegistry::addGate(const CustomGate& gate) {
gates_[gate.getName()] = gate;
}

bool GateRegistry::hasGate(const std::string& name) const {
return gates_.find(name) != gates_.end();
}

const CustomGate& GateRegistry::getGate(const std::string& name) const {
return gates_.at(name);
}

} // namespace qbin_compiler
Loading