Skip to content

Commit

Permalink
change Expr to CExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Sep 1, 2023
1 parent 722c9c2 commit 5cb5c0f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
24 changes: 12 additions & 12 deletions qiskit_aer/backends/wrappers/aer_circuit_binding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ void bind_aer_circuit(MODULE m) {
aer_bool(m, "AerBool");
aer_bool.def(py::init([]() { return new Operations::Bool(); }));

py::class_<Operations::Expr, std::shared_ptr<Operations::Expr>> aer_expr(
py::class_<Operations::CExpr, std::shared_ptr<Operations::CExpr>> aer_expr(
m, "AerExpr");

aer_expr.def("eval_bool", &Operations::Expr::eval_bool);
aer_expr.def("eval_uint", &Operations::Expr::eval_uint);
aer_expr.def("eval_bool", &Operations::CExpr::eval_bool);
aer_expr.def("eval_uint", &Operations::CExpr::eval_uint);

py::class_<Operations::CastExpr, Operations::Expr,
py::class_<Operations::CastExpr, Operations::CExpr,
std::shared_ptr<Operations::CastExpr>>
aer_cast_expr(m, "AerCast");
aer_cast_expr.def(
py::init([](const std::shared_ptr<Operations::ScalarType> type,
const std::shared_ptr<Operations::Expr> expr) {
const std::shared_ptr<Operations::CExpr> expr) {
return new Operations::CastExpr(type, expr);
}));

py::class_<Operations::VarExpr, Operations::Expr,
py::class_<Operations::VarExpr, Operations::CExpr,
std::shared_ptr<Operations::VarExpr>>
aer_var_expr(m, "AerVar");
aer_var_expr.def(
Expand All @@ -98,7 +98,7 @@ void bind_aer_circuit(MODULE m) {
return new Operations::VarExpr(type, cbit_idxs);
}));

py::class_<Operations::ValueExpr, Operations::Expr,
py::class_<Operations::ValueExpr, Operations::CExpr,
std::shared_ptr<Operations::ValueExpr>>
aer_val_expr(m, "AerValue");

Expand All @@ -115,21 +115,21 @@ void bind_aer_circuit(MODULE m) {
aer_bool_expr.def(
py::init([](const bool val) { return new Operations::BoolValue(val); }));

py::class_<Operations::UnaryExpr, Operations::Expr,
py::class_<Operations::UnaryExpr, Operations::CExpr,
std::shared_ptr<Operations::UnaryExpr>>
aer_unary_expr(m, "AerUnaryExpr");
aer_unary_expr.def(py::init([](const Operations::UnaryOp op,
const std::shared_ptr<Operations::Expr> expr) {
const std::shared_ptr<Operations::CExpr> expr) {
return new Operations::UnaryExpr(op, expr);
}));

py::class_<Operations::BinaryExpr, Operations::Expr,
py::class_<Operations::BinaryExpr, Operations::CExpr,
std::shared_ptr<Operations::BinaryExpr>>
aer_binary_expr(m, "AerBinaryExpr");
aer_binary_expr.def(
py::init([](const Operations::BinaryOp op,
const std::shared_ptr<Operations::Expr> left,
const std::shared_ptr<Operations::Expr> right) {
const std::shared_ptr<Operations::CExpr> left,
const std::shared_ptr<Operations::CExpr> right) {
return new Operations::BinaryExpr(op, left, right);
}));

Expand Down
14 changes: 7 additions & 7 deletions src/framework/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Circuit {
const std::vector<complex_t> &params,
const std::vector<std::string> &string_params,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::Expr> expr = nullptr,
const std::shared_ptr<Operations::CExpr> expr = nullptr,
const std::string label = "") {
ops.push_back(Operations::make_gate(name, qubits, params, string_params,
cond_regidx, expr, label));
Expand All @@ -145,7 +145,7 @@ class Circuit {

void unitary(const reg_t &qubits, const cmatrix_t &mat,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::Expr> expr = nullptr,
const std::shared_ptr<Operations::CExpr> expr = nullptr,
const std::string label = "") {
ops.push_back(
Operations::make_unitary(qubits, mat, cond_regidx, expr, label));
Expand All @@ -163,21 +163,21 @@ class Circuit {

void multiplexer(const reg_t &qubits, const std::vector<cmatrix_t> &mats,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::Expr> expr = nullptr,
const std::shared_ptr<Operations::CExpr> expr = nullptr,
std::string label = "") {
ops.push_back(
Operations::make_multiplexer(qubits, mats, cond_regidx, expr, label));
}

void kraus(const reg_t &qubits, const std::vector<cmatrix_t> &mats,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::Expr> expr = nullptr) {
const std::shared_ptr<Operations::CExpr> expr = nullptr) {
ops.push_back(Operations::make_kraus(qubits, mats, cond_regidx, expr));
}

void superop(const reg_t &qubits, const cmatrix_t &mat,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::Expr> expr = nullptr) {
const std::shared_ptr<Operations::CExpr> expr = nullptr) {
ops.push_back(Operations::make_superop(qubits, mat, cond_regidx, expr));
}

Expand Down Expand Up @@ -209,7 +209,7 @@ class Circuit {

void set_qerror_loc(const reg_t &qubits, const std::string &label,
const int_t conditional = -1,
const std::shared_ptr<Operations::Expr> expr = nullptr) {
const std::shared_ptr<Operations::CExpr> expr = nullptr) {
ops.push_back(
Operations::make_qerror_loc(qubits, label, conditional, expr));
}
Expand Down Expand Up @@ -249,7 +249,7 @@ class Circuit {

void jump(const reg_t &qubits, const std::vector<std::string> &params,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::Expr> expr = nullptr) {
const std::shared_ptr<Operations::CExpr> expr = nullptr) {
ops.push_back(Operations::make_jump(qubits, params, cond_regidx, expr));
}

Expand Down
60 changes: 30 additions & 30 deletions src/framework/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ uint_t truncate(const uint_t val, const size_t width) {
return (val << shift) >> shift;
}

enum class ExprType { Expr, Var, Value, Cast, Unary, Binary, Nop };
enum class CExprType { Expr, Var, Value, Cast, Unary, Binary, Nop };

enum class ValueType { Bool, Uint };

Expand Down Expand Up @@ -95,23 +95,23 @@ class Bool : public ScalarType {
Bool() : ScalarType(ValueType::Bool, 1) {}
};

class Expr {
class CExpr {
public:
Expr(const ExprType expr_type_, const std::shared_ptr<ScalarType> type_)
CExpr(const CExprType expr_type_, const std::shared_ptr<ScalarType> type_)
: expr_type(expr_type_), type(type_) {}
virtual bool eval_bool(const std::string &memory) { return false; };
virtual uint_t eval_uint(const std::string &memory) { return 0ul; };

public:
const ExprType expr_type;
const CExprType expr_type;
const std::shared_ptr<ScalarType> type;
};

class CastExpr : public Expr {
class CastExpr : public CExpr {
public:
CastExpr(std::shared_ptr<ScalarType> type,
const std::shared_ptr<Expr> operand_)
: Expr(ExprType::Cast, type), operand(operand_) {}
const std::shared_ptr<CExpr> operand_)
: CExpr(CExprType::Cast, type), operand(operand_) {}

virtual bool eval_bool(const std::string &memory) {
if (type->type != ValueType::Bool)
Expand All @@ -138,14 +138,14 @@ class CastExpr : public Expr {
}

public:
const std::shared_ptr<Expr> operand;
const std::shared_ptr<CExpr> operand;
};

class VarExpr : public Expr {
class VarExpr : public CExpr {
public:
VarExpr(std::shared_ptr<ScalarType> type,
const std::vector<uint_t> &cbit_idxs)
: Expr(ExprType::Var, type), cbit_idxs(cbit_idxs) {}
: CExpr(CExprType::Var, type), cbit_idxs(cbit_idxs) {}

virtual bool eval_bool(const std::string &memory) {
if (type->type != ValueType::Bool)
Expand Down Expand Up @@ -180,9 +180,9 @@ class VarExpr : public Expr {
const std::vector<uint_t> cbit_idxs;
};

class ValueExpr : public Expr {
class ValueExpr : public CExpr {
public:
ValueExpr(std::shared_ptr<ScalarType> type) : Expr(ExprType::Value, type) {}
ValueExpr(std::shared_ptr<ScalarType> type) : CExpr(CExprType::Value, type) {}
};

class UintValue : public ValueExpr {
Expand Down Expand Up @@ -217,10 +217,10 @@ class BoolValue : public ValueExpr {
const bool value;
};

class UnaryExpr : public Expr {
class UnaryExpr : public CExpr {
public:
UnaryExpr(const UnaryOp op_, const std::shared_ptr<Expr> operand_)
: Expr(ExprType::Unary, operand_->type), op(op_), operand(operand_) {
UnaryExpr(const UnaryOp op_, const std::shared_ptr<CExpr> operand_)
: CExpr(CExprType::Unary, operand_->type), op(op_), operand(operand_) {
if (op == UnaryOp::LogicNot && operand_->type->type != ValueType::Bool)
throw std::invalid_argument(
R"(LogicNot unary expression must has Bool expression as its operand.)");
Expand Down Expand Up @@ -248,14 +248,14 @@ class UnaryExpr : public Expr {

public:
const UnaryOp op;
const std::shared_ptr<Expr> operand;
const std::shared_ptr<CExpr> operand;
};

class BinaryExpr : public Expr {
class BinaryExpr : public CExpr {
public:
BinaryExpr(const BinaryOp op_, const std::shared_ptr<Expr> left_,
const std::shared_ptr<Expr> right_)
: Expr(ExprType::Binary, isBoolBinaryOp(op_)
BinaryExpr(const BinaryOp op_, const std::shared_ptr<CExpr> left_,
const std::shared_ptr<CExpr> right_)
: CExpr(CExprType::Binary, isBoolBinaryOp(op_)
? std::make_shared<Bool>()
: get_wider_type(left_->type, right_->type)),
op(op_), left(left_), right(right_) {
Expand Down Expand Up @@ -353,8 +353,8 @@ class BinaryExpr : public Expr {

public:
const BinaryOp op;
const std::shared_ptr<Expr> left;
const std::shared_ptr<Expr> right;
const std::shared_ptr<CExpr> left;
const std::shared_ptr<CExpr> right;
};

// Enum class for operation types
Expand Down Expand Up @@ -611,7 +611,7 @@ struct Op {
uint_t conditional_reg; // (opt) the (single) register location to look up for
// conditional
BinaryOp binary_op; // (opt) boolean function relation
std::shared_ptr<Expr> expr; // (opt) classical expression
std::shared_ptr<CExpr> expr; // (opt) classical expression

// Measurement
reg_t memory; // (opt) register operation it acts on (measure)
Expand Down Expand Up @@ -761,7 +761,7 @@ inline Op make_initialize(const reg_t &qubits,

inline Op make_unitary(const reg_t &qubits, const cmatrix_t &mat,
const int_t conditional = -1,
const std::shared_ptr<Expr> expr = nullptr,
const std::shared_ptr<CExpr> expr = nullptr,
std::string label = "") {
Op op;
op.type = OpType::matrix;
Expand Down Expand Up @@ -821,7 +821,7 @@ inline Op make_diagonal(const reg_t &qubits, cvector_t &&vec,

inline Op make_superop(const reg_t &qubits, const cmatrix_t &mat,
const int_t conditional = -1,
const std::shared_ptr<Expr> expr = nullptr) {
const std::shared_ptr<CExpr> expr = nullptr) {
Op op;
op.type = OpType::superop;
op.name = "superop";
Expand All @@ -847,7 +847,7 @@ inline Op make_superop(const reg_t &qubits, cmatrix_t &&mat) {

inline Op make_kraus(const reg_t &qubits, const std::vector<cmatrix_t> &mats,
const int_t conditional = -1,
const std::shared_ptr<Expr> expr = nullptr) {
const std::shared_ptr<CExpr> expr = nullptr) {
Op op;
op.type = OpType::kraus;
op.name = "kraus";
Expand Down Expand Up @@ -931,7 +931,7 @@ inline Op make_bfunc(const std::string &mask, const std::string &val,
Op make_gate(const std::string &name, const reg_t &qubits,
const std::vector<complex_t> &params,
const std::vector<std::string> &string_params,
const int_t conditional, const std::shared_ptr<Expr> expr,
const int_t conditional, const std::shared_ptr<CExpr> expr,
const std::string &label) {
Op op;
op.type = OpType::gate;
Expand Down Expand Up @@ -999,7 +999,7 @@ inline Op make_reset(const reg_t &qubits, uint_t state = 0) {
inline Op make_multiplexer(const reg_t &qubits,
const std::vector<cmatrix_t> &mats,
const int_t conditional = -1,
const std::shared_ptr<Expr> expr = nullptr,
const std::shared_ptr<CExpr> expr = nullptr,
std::string label = "") {

// Check matrices are N-qubit
Expand Down Expand Up @@ -1212,7 +1212,7 @@ inline Op make_set_clifford(const reg_t &qubits, const std::string &name,

inline Op make_jump(const reg_t &qubits, const std::vector<std::string> &params,
const int_t conditional,
const std::shared_ptr<Expr> expr = nullptr) {
const std::shared_ptr<CExpr> expr = nullptr) {
Op op;
op.type = OpType::jump;
op.name = "jump";
Expand Down Expand Up @@ -1266,7 +1266,7 @@ inline Op make_measure(const reg_t &qubits, const reg_t &memory,

inline Op make_qerror_loc(const reg_t &qubits, const std::string &label,
const int_t conditional = -1,
const std::shared_ptr<Expr> expr = nullptr) {
const std::shared_ptr<CExpr> expr = nullptr) {
Op op;
op.type = OpType::qerror_loc;
op.name = label;
Expand Down

0 comments on commit 5cb5c0f

Please sign in to comment.