Skip to content

Commit

Permalink
refactor fusion-related codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii committed Jun 28, 2019
1 parent 8bb3d2a commit da589d1
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 54 deletions.
35 changes: 0 additions & 35 deletions src/framework/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ struct Op {
// Mat and Kraus
std::vector<cmatrix_t> mats;

#ifdef DEBUG
// Fusion
std::vector<OpType> fusioned_types;
std::vector<std::string> fusioned_names;
std::vector<reg_t> fusioned_qubits;
std::vector<std::vector<complex_t>> fusioned_params;
std::vector<std::vector<cmatrix_t>> fusioned_mats;
#endif

// Readout error
std::vector<rvector_t> probs;

Expand Down Expand Up @@ -162,18 +153,6 @@ inline std::ostream& operator<<(std::ostream& s, const Op& op) {
first = false;
}
s << "]";
#ifdef DEBUG
if (!op.fusioned_names.empty()) {
s << ",[";
first = true;
for (std::string fusioned_name: op.fusioned_names) {
if (!first)
s << ",";
s << fusioned_name;
first = false;
}
}
#endif
return s;
}

Expand Down Expand Up @@ -434,16 +413,6 @@ inline Op make_fusion(const reg_t &qubits, const cmatrix_t &mat, const std::vect
if (label != "")
op.string_params = {label};

#ifdef DEBUG
for (const Op& fusioned_op: fusioned_ops) {
op.fusioned_types.push_back(fusioned_op.type);
op.fusioned_names.push_back(fusioned_op.name);
op.fusioned_qubits.push_back(fusioned_op.qubits);
op.fusioned_params.push_back(fusioned_op.params);
op.fusioned_mats.push_back(fusioned_op.mats);
}
#endif

return op;
}

Expand Down Expand Up @@ -656,10 +625,6 @@ json_t op_to_json(const Op &op) {
ret["register"] = op.registers;
if (!op.mats.empty())
ret["mats"] = op.mats;
#ifdef DEBUG
if (!op.fusioned_names.empty())
ret["fusion"] = op.fusioned_names;
#endif
return ret;
}

Expand Down
11 changes: 0 additions & 11 deletions src/framework/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ class Matrix {
// Matrix Functions
//------------------------------------------------------------------------------

// Initialization
template<class T> void initialize_matrix(const matrix<T>& mat, T v);
// Vector conversion
template<class T> matrix<T> make_matrix(const std::vector<std::vector<T>> &mat);
template<class T> matrix<T> devectorize_matrix(const std::vector<T> &vec);
Expand Down Expand Up @@ -488,15 +486,6 @@ cvector_t VMatrix::u3(double theta, double phi, double lambda) {
// Implementations: Matrix functions
//==============================================================================

template<class T>
void initialize_matrix(const matrix<T>& mat, T v) {
std::vector<T> vec;
for (size_t col=0; col < mat.GetRows(); col++)
for (size_t row=0; row < mat.GetColumns(); row++) {
mat(row, col) = v;
}
}

template<class T>
matrix<T> devectorize_matrix(const std::vector<T>& vec) {
size_t dim = std::sqrt(vec.size());
Expand Down
10 changes: 2 additions & 8 deletions src/transpile/fusion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ void Fusion::optimize_circuit(Circuit& circ,
if (can_ignore(circ.ops[op_idx]))
continue;
if (!can_apply_fusion(circ.ops[op_idx])) {
if (fusion_start != op_idx)
if(aggregate_operations(circ.ops, fusion_start, op_idx))
applied = true;
applied = fusion_start != op_idx && aggregate_operations(circ.ops, fusion_start, op_idx);
fusion_start = op_idx + 1;
}
}
Expand Down Expand Up @@ -333,7 +331,6 @@ op_t Fusion::generate_fusion_operation(const std::vector<op_t>& fusioned_ops) co
for (size_t m = 1; m < sorted_mats.size(); m++) {

cmatrix_t u_tmp(U.GetRows(), U.GetColumns());
Utils::initialize_matrix(u_tmp, complex_t(.0));
const cmatrix_t& u = sorted_mats[m];

for (size_t i = 0; i < dim; ++i)
Expand All @@ -353,7 +350,6 @@ cmatrix_t Fusion::expand_matrix(const reg_t& src_qubits, const reg_t& dst_sorted

// generate a matrix for op
cmatrix_t u(dst_dim, dst_dim);
Utils::initialize_matrix(u, complex_t(.0));
std::vector<bool> filled(dst_dim, false);

if (src_qubits.size() == 1) { //1-qubit operation
Expand Down Expand Up @@ -422,9 +418,7 @@ cmatrix_t Fusion::expand_matrix(const reg_t& src_qubits, const reg_t& dst_sorted
}
//TODO: } else if (src_qubits.size() == 3) {
} else {
std::stringstream ss;
ss << "Fusion::illegal qubit number:" << src_qubits.size();
throw std::runtime_error(ss.str());
throw std::runtime_error("Fusion::illegal qubit number: " + std::to_string(src_qubits.size()));
}

return u;
Expand Down

0 comments on commit da589d1

Please sign in to comment.