I am trying to use Stim in C++. Until now, most functionality worked as expected. However, I recently tried to construct a Stim circuit in C++ and pass it to Python via pybind11
, and this causes a weird behavior. For example, with my function:
void circuit_append(
stim::Circuit &self,
const std::string &gate_name,
const std::vector<uint32_t> &targets,
const std::vector<double> &args,
bool backwards_compat) {
if (backwards_compat && stim::GATE_DATA.at(gate_name).arg_count == 1) {
// If the gate expects exactly one argument but none provided, use a default value.
if (args.empty()) {
self.safe_append_ua(gate_name, targets, 0.0);
} else if (args.size() == 1) {
self.safe_append_ua(gate_name, targets, args[0]);
} else {
throw std::invalid_argument("Gate expects exactly one argument.");
}
} else {
// For gates that do not specifically require backwards compatibility handling or
// can accept multiple arguments.
if (args.size() == 1) {
// If only one argument is provided, you could still use safe_append_ua for consistency.
self.safe_append_ua(gate_name, targets, args[0]);
} else {
// Use safe_append_u for gates with multiple arguments or no arguments at all.
self.safe_append_u(gate_name, targets, args);
}
}
}
It checks the conditions for the wrong gates regarding the args etc. For example, trying to do:
append_custom(circuit, "DEPOLARIZE1", [1], [0.1])
corresponding to a "DEPOLARIZE1" channel on qubit 0 with 0.1 as the error probability it will throw:
ValueError: Gate H_YZ was given 1 parens arguments (0.1) but takes 0 parens arguments.
Does anyone know how to solve this issue?