Skip to content

Commit

Permalink
🔀 Merge pull request #71 from DVLab-NTU/refactor/tensor-decomp-remove…
Browse files Browse the repository at this point in the history
…-num-qubit-injection

♻️ remove the need to inject num qubit for tensor decomposer
  • Loading branch information
JoshuaLau0220 authored Feb 29, 2024
2 parents d25473a + 0dd9703 commit 170d761
Show file tree
Hide file tree
Showing 3 changed files with 505 additions and 459 deletions.
5 changes: 1 addition & 4 deletions src/convert/conversion_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,7 @@ Command conversion_cmd(QCirMgr& qcir_mgr, qsyn::tensor::TensorMgr& tensor_mgr, q
if (!dvlab::utils::mgr_has_data(tensor_mgr)) return CmdExecResult::error;

spdlog::info("Converting Tensor {} to QCir {}...", tensor_mgr.focused_id(), qcir_mgr.get_next_id());
auto ts = tensor_mgr.get();
const size_t qreg = size_t(log2((*ts).shape()[0]));
tensor::Decomposer decomp(qreg);
auto result = decomp.decompose(*ts);
auto const result = tensor::Decomposer{}.decompose(*tensor_mgr.get());

if (result) {
qcir_mgr.add(qcir_mgr.get_next_id(), std::make_unique<qcir::QCir>(std::move(*result)));
Expand Down
48 changes: 48 additions & 0 deletions src/tensor/decomposer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/****************************************************************************
PackageName [ tensor ]
Synopsis [ Define tensor decomposer structure ]
Author [ Design Verification Lab ]
Copyright [ Copyright(c) 2023 DVLab, GIEE, NTU, Taiwan ]
****************************************************************************/

#include "./decomposer.hpp"

namespace qsyn::tensor {

/**
* @brief Append gate and save the encoding gate sequence
*
* @param target
* @param qubit_list
* @param gate_list
*/
void Decomposer::encode_control_gate(QubitIdList const& target, std::vector<QubitIdList>& qubit_list, std::vector<std::string>& gate_list) {
qubit_list.emplace_back(target);
gate_list.emplace_back(target.size() == 2 ? "cx" : "x");
_quantum_circuit.add_gate(target.size() == 2 ? "cx" : "x", target, {}, true);
}

/**
* @brief Gray encoder
*
* @param origin_pos Origin qubit
* @param targ_pos Target qubit
* @param qubit_list
* @param gate_list
*/
void Decomposer::encode(size_t origin_pos, size_t targ_pos, std::vector<QubitIdList>& qubit_list, std::vector<std::string>& gate_list) {
bool x_given = 0;
if (((origin_pos >> targ_pos) & 1) == 0) {
encode_control_gate({int(targ_pos)}, qubit_list, gate_list);
x_given = 1;
}
for (size_t i = 0; i < _n_qubits; i++) {
if (i == targ_pos) continue;
if (((origin_pos >> i) & 1) == 0)
encode_control_gate({int(targ_pos), int(i)}, qubit_list, gate_list);
}
if (x_given)
encode_control_gate({int(targ_pos)}, qubit_list, gate_list);
}

} // namespace qsyn::tensor
Loading

0 comments on commit 170d761

Please sign in to comment.