Skip to content

Commit

Permalink
🔀 Merge pull request #67 from DVLab-NTU/feature/matrix-decomp
Browse files Browse the repository at this point in the history
Matrix Decomposition and Tensor IO
  • Loading branch information
JoshuaLau0220 authored Feb 28, 2024
2 parents 27d537b + 335aa95 commit d25473a
Show file tree
Hide file tree
Showing 7 changed files with 796 additions and 10 deletions.
27 changes: 24 additions & 3 deletions src/convert/conversion_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
#include "cli/cli.hpp"
#include "extractor/extract.hpp"
#include "qcir/qcir_mgr.hpp"
#include "tensor/decomposer.hpp"
#include "tensor/tensor_mgr.hpp"
#include "util/data_structure_manager_common_cmd.hpp"
#include "util/dvlab_string.hpp"
#include "util/util.hpp"
#include "zx/zx_cmd.hpp"

using namespace dvlab::argparse;

Expand Down Expand Up @@ -95,7 +95,7 @@ Command conversion_cmd(QCirMgr& qcir_mgr, qsyn::tensor::TensorMgr& tensor_mgr, q

if (get_data_type(from) == data_type::qcir && get_data_type(to) == data_type::tensor) {
if (!dvlab::utils::mgr_has_data(qcir_mgr)) return CmdExecResult::error;
spdlog::info("Converting to QCir {} to tensor {}...", qcir_mgr.focused_id(), tensor_mgr.get_next_id());
spdlog::info("Converting to QCir {} to Tensor {}...", qcir_mgr.focused_id(), tensor_mgr.get_next_id());
auto tensor = to_tensor(*qcir_mgr.get());

if (tensor.has_value()) {
Expand Down Expand Up @@ -157,6 +157,26 @@ Command conversion_cmd(QCirMgr& qcir_mgr, qsyn::tensor::TensorMgr& tensor_mgr, q
return CmdExecResult::done;
}

// ts2qc

if (get_data_type(from) == data_type::tensor && get_data_type(to) == data_type::qcir) {
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);

if (result) {
qcir_mgr.add(qcir_mgr.get_next_id(), std::make_unique<qcir::QCir>(std::move(*result)));
qcir_mgr.get()->add_procedures(tensor_mgr.get()->get_procedures());
qcir_mgr.get()->add_procedure("TS2QC");
qcir_mgr.get()->set_filename(tensor_mgr.get()->get_filename());
}
return CmdExecResult::done;
}

spdlog::error("Conversion from {} to {} is not yet supported!!", from, to);

return CmdExecResult::error;
Expand All @@ -168,7 +188,8 @@ bool add_conversion_cmds(dvlab::CommandLineInterface& cli, QCirMgr& qcir_mgr, qs
cli.add_alias("qc2zx", "convert qcir zx") &&
cli.add_alias("qc2ts", "convert qcir tensor") &&
cli.add_alias("zx2ts", "convert zx tensor") &&
cli.add_alias("zx2qc", "convert zx qcir"))) {
cli.add_alias("zx2qc", "convert zx qcir") &&
cli.add_alias("ts2qc", "convert tensor qcir"))) {
fmt::println(stderr, "Registering \"conversion\" commands fails... exiting");
return false;
}
Expand Down
Loading

0 comments on commit d25473a

Please sign in to comment.