Skip to content

Commit

Permalink
Merge branch 'main' into conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
vsoftco authored Jan 9, 2025
2 parents 9f0ea90 + bfbbf16 commit 9b11f30
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
- Bugfix in `qpp::adjoint(QCircuit)`
- Added `cond_func_t` type alias in ["qpp/types.hpp"] for boolean predicates of
the form `std::vector<idx> -> bool`
- Added `qpp::read_from_string()` to ["qpp/qasm/qasm.hpp"] and an associated
pyqpp wrapper

# Version 5.1 - 1 March 2024

Expand Down
18 changes: 18 additions & 0 deletions include/qpp/qasm/qasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <list>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -776,6 +777,23 @@ inline QCircuit read(std::istream& stream) {
return *qc;
}

/**
* \brief Reads an OpenQASM circuit from a string and returns its qpp::QCircuit
* representation
*
* \return qpp::QCircuit
*/
inline QCircuit read_from_string(std::string qasm_string) {
std::istringstream stream(qasm_string);
ast::ptr<ast::Program> program = parser::parse_stream(stream);

std::unique_ptr<QCircuit> qc(
new QCircuit(program->qubits(), program->bits()));
QCircuitBuilder builder(qc.get());
program->accept(builder);
return *qc;
}

/**
* \brief Reads an OpenQASM circuit from a file and returns its qpp::QCircuit
* representation
Expand Down
3 changes: 3 additions & 0 deletions pyqpp/include/pyqpp/qasm/qasm_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ inline void init_qasm_qasm(py::module_& m) {
auto py_qasm = m.def_submodule("qasm");
py_qasm.def("read_from_file", &qpp::qasm::read_from_file,
"Get QCircuit representation of OpenQASM circuit");

py_qasm.def("read_from_string", &qpp::qasm::read_from_string,
"Get QCircuit representation of OpenQASM circuit");
}

#endif /* PYQPP_QASM_QASM_BIND_HPP_ */
5 changes: 5 additions & 0 deletions unit_tests/tests/qasm/qasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ TEST(qpp_qasm_read_from_file, SciNot) {
// Check norm
EXPECT_NEAR(1, norm(adjoint(psi) * phi), 1e-5);
}

TEST(qpp_qasm_read_from_string, BasicParsing) {
EXPECT_NO_THROW(qasm::read_from_string(
"OPENQASM 2.0;\ninclude \"qelib1.inc\";\n qreg q[10];"));
}

0 comments on commit 9b11f30

Please sign in to comment.