Loading handlers/token_collector/pyxasm/pyxasm_visitor.hpp +37 −11 Original line number Diff line number Diff line Loading @@ -19,14 +19,13 @@ class pyxasm_visitor : public pyxasmBaseVisitor { std::shared_ptr<xacc::IRProvider> provider; public: pyxasm_visitor() : provider(xacc::getIRProvider("quantum")) {} pyxasm_visitor() : provider(xacc::getIRProvider("quantum")) {} pyxasm_result_type result; bool in_for_loop = false; antlrcpp::Any visitAtom_expr( pyxasmParser::Atom_exprContext *context) override { antlrcpp::Any visitAtom_expr(pyxasmParser::Atom_exprContext *context) override { if (context->atom()->NAME() != nullptr) { auto inst_name = context->atom()->NAME()->getText(); Loading Loading @@ -67,9 +66,9 @@ class pyxasm_visitor : public pyxasmBaseVisitor { auto found_bracket = bit_expr_str.find_first_of("["); if (found_bracket != std::string::npos) { auto buffer_name = bit_expr_str.substr(0, found_bracket); auto bit_idx_expr = bit_expr_str.substr( found_bracket + 1, bit_expr_str.length() - found_bracket - 2); auto bit_idx_expr = bit_expr_str.substr(found_bracket + 1, bit_expr_str.length() - found_bracket - 2); buffer_names.push_back(buffer_name); inst->setBitExpression(i, bit_idx_expr); } else { Loading @@ -89,8 +88,35 @@ class pyxasm_visitor : public pyxasmBaseVisitor { counter++; } } } result.second = inst; } else { // Composite instructions, e.g. exp_i_theta if (inst_name == "exp_i_theta") { // Expected 3 params: if (context->trailer()[0]->arglist()->argument().size() != 3) { xacc::error( "Invalid number of arguments for the 'exp_i_theta' " "instruction. Expected 3, got " + std::to_string( context->trailer()[0]->arglist()->argument().size()) + ". Please check your input."); } std::stringstream ss; // Delegate to the QRT call directly. ss << "quantum::exp(" << context->trailer()[0]->arglist()->argument(0)->getText() << ", " << context->trailer()[0]->arglist()->argument(1)->getText() << ", " << context->trailer()[0]->arglist()->argument(2)->getText() << ");\n"; result.first = ss.str(); } else { xacc::error("Composite instruction '" + inst_name + "' is not currently supported."); } } } } return 0; Loading python/examples/exp_i_theta.py 0 → 100644 +40 −0 Original line number Diff line number Diff line # Run this from the command line like this # # python3 exp_i_theta.py -shots 100 from qcor import qjit, qalloc, qreg # To create QCOR quantum kernels in Python one # simply creates a Python function, writes Pythonic, # XASM-like quantum code, and annotates the kernel # to indicate it is meant for QCOR just in time compilation # NOTE Programmers must type annotate their function arguments # Define a XASM kernel @qjit def exp_circuit(q : qreg, t0: float, t1: float): exponent_op1 = X(0) * Y(1) - Y(0) * X(1) exponent_op2 = X(0) * Z(1) * Y(2) - X(2) * Z(1) * Y(0) X(q[0]) exp_i_theta(q, t0, exponent_op1) exp_i_theta(q, t1, exponent_op2) for i in range(q.size()): Measure(q[i]) # Allocate 3 qubits q = qalloc(3) # Run the experiment with some random angles theta1 = 1.234 theta2 = 2.345 # Examine the circuit QASM comp = exp_circuit.extract_composite(q, theta1, theta2) print(comp.toString()) # Execute exp_circuit(q, theta1, theta2) # Print the results q.print() No newline at end of file Loading
handlers/token_collector/pyxasm/pyxasm_visitor.hpp +37 −11 Original line number Diff line number Diff line Loading @@ -19,14 +19,13 @@ class pyxasm_visitor : public pyxasmBaseVisitor { std::shared_ptr<xacc::IRProvider> provider; public: pyxasm_visitor() : provider(xacc::getIRProvider("quantum")) {} pyxasm_visitor() : provider(xacc::getIRProvider("quantum")) {} pyxasm_result_type result; bool in_for_loop = false; antlrcpp::Any visitAtom_expr( pyxasmParser::Atom_exprContext *context) override { antlrcpp::Any visitAtom_expr(pyxasmParser::Atom_exprContext *context) override { if (context->atom()->NAME() != nullptr) { auto inst_name = context->atom()->NAME()->getText(); Loading Loading @@ -67,9 +66,9 @@ class pyxasm_visitor : public pyxasmBaseVisitor { auto found_bracket = bit_expr_str.find_first_of("["); if (found_bracket != std::string::npos) { auto buffer_name = bit_expr_str.substr(0, found_bracket); auto bit_idx_expr = bit_expr_str.substr( found_bracket + 1, bit_expr_str.length() - found_bracket - 2); auto bit_idx_expr = bit_expr_str.substr(found_bracket + 1, bit_expr_str.length() - found_bracket - 2); buffer_names.push_back(buffer_name); inst->setBitExpression(i, bit_idx_expr); } else { Loading @@ -89,8 +88,35 @@ class pyxasm_visitor : public pyxasmBaseVisitor { counter++; } } } result.second = inst; } else { // Composite instructions, e.g. exp_i_theta if (inst_name == "exp_i_theta") { // Expected 3 params: if (context->trailer()[0]->arglist()->argument().size() != 3) { xacc::error( "Invalid number of arguments for the 'exp_i_theta' " "instruction. Expected 3, got " + std::to_string( context->trailer()[0]->arglist()->argument().size()) + ". Please check your input."); } std::stringstream ss; // Delegate to the QRT call directly. ss << "quantum::exp(" << context->trailer()[0]->arglist()->argument(0)->getText() << ", " << context->trailer()[0]->arglist()->argument(1)->getText() << ", " << context->trailer()[0]->arglist()->argument(2)->getText() << ");\n"; result.first = ss.str(); } else { xacc::error("Composite instruction '" + inst_name + "' is not currently supported."); } } } } return 0; Loading
python/examples/exp_i_theta.py 0 → 100644 +40 −0 Original line number Diff line number Diff line # Run this from the command line like this # # python3 exp_i_theta.py -shots 100 from qcor import qjit, qalloc, qreg # To create QCOR quantum kernels in Python one # simply creates a Python function, writes Pythonic, # XASM-like quantum code, and annotates the kernel # to indicate it is meant for QCOR just in time compilation # NOTE Programmers must type annotate their function arguments # Define a XASM kernel @qjit def exp_circuit(q : qreg, t0: float, t1: float): exponent_op1 = X(0) * Y(1) - Y(0) * X(1) exponent_op2 = X(0) * Z(1) * Y(2) - X(2) * Z(1) * Y(0) X(q[0]) exp_i_theta(q, t0, exponent_op1) exp_i_theta(q, t1, exponent_op2) for i in range(q.size()): Measure(q[i]) # Allocate 3 qubits q = qalloc(3) # Run the experiment with some random angles theta1 = 1.234 theta2 = 2.345 # Examine the circuit QASM comp = exp_circuit.extract_composite(q, theta1, theta2) print(comp.toString()) # Execute exp_circuit(q, theta1, theta2) # Print the results q.print() No newline at end of file