8 #ifndef QASM_NEW_TO_OLD_HPP 19 namespace new_to_old {
51 for (
auto control_bit : indices) {
61 switch (value->as_const_axis()->value) {
62 case cqasm::primitives::Axis::X:
return "x";
63 case cqasm::primitives::Axis::Y:
return "y";
64 case cqasm::primitives::Axis::Z:
return "z";
66 throw std::runtime_error(
"unreachable code, corrupted axis enum");
73 static Operation *convert_instruction(
const cqasm::semantic::Instruction &instruction) {
77 switch (instruction.instruction->get_annotation<
ParameterType>()) {
80 instruction.instruction->name
85 instruction.instruction->name,
86 Bits(convert_indices(instruction.operands[0]->as_bit_refs()->index))
91 instruction.instruction->name,
92 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index))
97 instruction.instruction->name,
98 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index)),
99 instruction.operands[1]->as_const_real()->value
104 instruction.instruction->name,
105 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index))
108 auto mat = instruction.operands[1]->as_const_complex_matrix()->value;
109 std::vector<double> mat_elements;
110 for (
size_t row = 1; row <= mat.size_rows(); row++) {
111 for (
size_t col = 1; col <= mat.size_cols(); col++) {
112 auto val = mat.at(row, col);
113 mat_elements.push_back(val.real());
114 mat_elements.push_back(val.imag());
122 instruction.instruction->name,
123 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index)),
124 Qubits(convert_indices(instruction.operands[1]->as_qubit_refs()->index))
129 instruction.instruction->name,
130 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index)),
131 Qubits(convert_indices(instruction.operands[1]->as_qubit_refs()->index)),
132 instruction.operands[2]->as_const_real()->value
137 instruction.instruction->name,
138 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index)),
139 Qubits(convert_indices(instruction.operands[1]->as_qubit_refs()->index)),
140 instruction.operands[2]->as_const_int()->value
145 instruction.instruction->name,
146 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index)),
147 Qubits(convert_indices(instruction.operands[1]->as_qubit_refs()->index)),
148 Qubits(convert_indices(instruction.operands[2]->as_qubit_refs()->index))
153 instruction.instruction->name,
154 Qubits(convert_indices(instruction.operands[0]->as_qubit_refs()->index)),
155 convert_axis(instruction.operands[1]),
156 Qubits(convert_indices(instruction.operands[2]->as_qubit_refs()->index)),
157 convert_axis(instruction.operands[3])
162 instruction.instruction->name,
163 instruction.operands[0]->as_const_int()->value
168 instruction.instruction->name,
169 instruction.operands[0]->as_const_string()->value
174 instruction.instruction->name
176 op->
setControlBits(
Bits(convert_indices(instruction.operands[0]->as_bit_refs()->index)));
181 "unsupported parameter signature for old API",
187 if (
auto control_bits = instruction.condition->as_bit_refs()) {
189 }
else if (
auto control_const = instruction.condition->as_const_bool()) {
190 if (!control_const->value) {
199 "unsupported condition for old API",
200 &*instruction.condition
212 static void handle_parse_result(
QasmRepresentation &qasm, cqasm::parser::ParseResult &&result) {
215 if (!result.errors.empty()) {
216 throw std::runtime_error(result.errors[0]);
220 auto analyzer = cqasm::analyzer::Analyzer{
"1.0"};
221 analyzer.register_default_functions_and_mappings();
227 std::ostringstream args;
228 for (
int i = 0; i <= 50; i++) {
229 analyzer.register_error_model(
"depolarizing_channel", args.str());
235 #define REG(typ, ...) analyzer.register_instruction_with_annotation<ParameterType>(ParameterType::typ, __VA_ARGS__) 236 REG(
NoArg,
"measure_all",
"",
false,
false);
269 REG(NotGate,
"not",
"B");
270 REG(
NoArg,
"display",
"",
false,
false);
272 REG(
NoArg,
"display_binary",
"",
false,
false);
276 REG(
NoArg,
"reset-averaging",
"",
false,
false);
282 auto analysis_result = analyzer.analyze(*result.root->as_program());
283 if (!analysis_result.errors.empty()) {
284 throw std::runtime_error(analysis_result.errors[0]);
293 if (analysis_result.root->num_qubits == 0) {
294 throw std::runtime_error(
"qubits statement is missing");
297 if (!analysis_result.root->variables.empty()) {
298 throw std::runtime_error(
"variables are not supported");
302 double version = analysis_result.root->version->items[0];
303 if (analysis_result.root->version->items.size() > 1) {
304 int sub = analysis_result.root->version->items[1];
305 version += 0.1 * ((sub > 9) ? 9 : sub);
310 if (!analysis_result.root->error_model.empty()) {
311 std::vector<double> args;
312 for (
auto arg : analysis_result.root->error_model->parameters) {
313 args.push_back(arg->as_const_real()->value);
315 qasm.
setErrorModel(analysis_result.root->error_model->name, args);
320 for (
auto subcircuit : analysis_result.root->subcircuits) {
330 if (!subcircuit->name.empty()) {
333 line_number = loc->first_line;
336 subcircuit->name.c_str(),
337 (int)scs.numberOfSubCircuits(),
341 scs.addSubCircuit(sc);
345 for (
auto bundle : subcircuit->bundles) {
349 for (
auto instruction : bundle->items) {
352 Operation *op = convert_instruction(*instruction);
366 line_number = loc->first_line;
377 scs.lastSubCircuit().addOperationsCluster(opclus);
383 for (
auto mapping : analysis_result.root->mappings) {
384 if (
auto qubit_refs = mapping->value->as_qubit_refs()) {
385 qasm.
addMappings(mapping->name, convert_indices(qubit_refs->index),
true);
386 }
else if (
auto bit_refs = mapping->value->as_bit_refs()) {
387 qasm.
addMappings(mapping->name, convert_indices(bit_refs->index),
false);
ParameterType
All instruction types supported by the old API based on the types of their parameters alone...
void qubitRegister(int participating_number)
SubCircuits & getSubCircuits()
void addMappings(std::string name_key, NumericalIdentifiers indices, bool isQubit)
void setErrorModel(std::string error_model_type, std::vector< double > error_model_num_params)
Main include file for libqasm; this is what you should be #includeing.
void addToVector(const int index)
Namespace used for most of the original API.
double versionNumber() const
::tree::base::Many< T > Many
void addParallelOperation(Operation *valid_op)
void setUMatrixElements(const std::vector< double > input)
void setControlBits(Bits control_bits)
annotations::SourceLocation SourceLocation
int numberIterations() const
Exception used for analysis errors.