Extending qalgora-Q with a Custom Simulator
The simulator layer is an open interface. By subclassing the circuit-simulator base, registering
it, and shipping a small target config, your own state representation becomes a first-class
--target — the same path our built-in backends take.
Subclass the simulator base
Derive from CircuitSimulatorBase<FloatType> and override the handful of methods that
describe how your representation allocates qubits, applies a gate, measures, and samples:
#include "CircuitSimulator.h"
namespace {
class MySimulator : public qalgora::CircuitSimulatorBase<double> {
protected:
void addQubitToState() override { /* grow the state by one qubit */ }
void addQubitsToState(std::size_t count) override { /* grow by count */ }
void resetQubitStateImpl() override { /* clear back to |0...0> */ }
void applyGate(const GateApplicationTask &task) override { /* core: apply one gate */ }
public:
MySimulator() = default;
virtual ~MySimulator() = default;
bool measureQubit(std::size_t qubitIdx) override { /* collapse + return bit */ }
void resetQubit(std::size_t &qubitIdx) override { /* reset one qubit */ }
qalgora::SampleResult sample(std::vector<std::size_t> &measuredBits,
int shots) override { /* draw shots */ }
const std::string_view name() const override { return "MySimulator"; }
};
} // namespace
QALGORA_REGISTER_SIMULATOR(MySimulator)Ship a target config
A MySimulator.config file tells the runtime which shared library backs the target:
# MySimulator.config
QALGORA_SIMULATION_BACKEND="MySimulator"
Build and use it
Compile with the provided CMake helper, install the libqsim-MySimulator.so plus its config,
and the new backend is selectable like any other:
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project(CustomBackend LANGUAGES CXX)
find_package(QSIR REQUIRED)
qsir_add_backend(MySimulator MySimulator.cpp "")
export QALGORA_PATH=/path/to/qalgora/install
mkdir build && cd build
cmake .. -G Ninja -DQSIR_DIR="$QALGORA_PATH/lib/cmake/qsir"
ninja install
qalgora++ file.cpp --target MySimulator
使用自定义模拟器扩展 qalgora-Q
模拟器层是一个开放接口。只要继承电路模拟器基类、完成注册,再配上一份简短的目标配置,你自己的态表示就能成为一等的
--target——和内置后端走同一条路径。
继承模拟器基类
从 CircuitSimulatorBase<FloatType> 派生,重写几个方法,说明你的态表示如何分配量子比特、施加门、测量和采样:
#include "CircuitSimulator.h"
namespace {
class MySimulator : public qalgora::CircuitSimulatorBase<double> {
protected:
void addQubitToState() override { /* grow the state by one qubit */ }
void addQubitsToState(std::size_t count) override { /* grow by count */ }
void resetQubitStateImpl() override { /* clear back to |0...0> */ }
void applyGate(const GateApplicationTask &task) override { /* core: apply one gate */ }
public:
MySimulator() = default;
virtual ~MySimulator() = default;
bool measureQubit(std::size_t qubitIdx) override { /* collapse + return bit */ }
void resetQubit(std::size_t &qubitIdx) override { /* reset one qubit */ }
qalgora::SampleResult sample(std::vector<std::size_t> &measuredBits,
int shots) override { /* draw shots */ }
const std::string_view name() const override { return "MySimulator"; }
};
} // namespace
QALGORA_REGISTER_SIMULATOR(MySimulator)附带目标配置
用一份 MySimulator.config 文件告诉运行时,该目标由哪个共享库支撑:
# MySimulator.config
QALGORA_SIMULATION_BACKEND="MySimulator"
构建并使用它
用配套的 CMake 辅助工具编译,把 libqsim-MySimulator.so 连同它的配置文件一起安装好,新后端就能像其它后端一样选用:
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project(CustomBackend LANGUAGES CXX)
find_package(QSIR REQUIRED)
qsir_add_backend(MySimulator MySimulator.cpp "")
export QALGORA_PATH=/path/to/qalgora/install
mkdir build && cd build
cmake .. -G Ninja -DQSIR_DIR="$QALGORA_PATH/lib/cmake/qsir"
ninja install
qalgora++ file.cpp --target MySimulator