Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rectify qubit id type #64

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ qsyn*
!src/qsyn*
!src/qsyn/qsyn*

# for work-in-progress things
wip/

# vscode configuration files and folders
.vscode

Expand Down
46 changes: 17 additions & 29 deletions src/convert/qcir_to_zxgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ create_multi_control_backbone(ZXGraph& g, std::vector<QubitInfo> const& qubits,
for (auto const& bitinfo : qubits) {
auto qubit = bitinfo._qubit;
ZXVertex* in = g.add_input(qubit);
ZXVertex* v = g.add_vertex(qubit, VertexType::z);
ZXVertex* v = g.add_vertex(VertexType::z, dvlab::Phase{}, static_cast<float>(qubit));
ZXVertex* out = g.add_output(qubit);
if (ax == RotationAxis::z || !bitinfo._isTarget) {
g.add_edge(in, v, EdgeType::simple);
Expand Down Expand Up @@ -164,7 +164,7 @@ ZXGraph create_single_vertex_zx_form(QCirGate* gate, VertexType vt, dvlab::Phase
auto qubit = gate->get_qubits()[0]._qubit;

ZXVertex* in = g.add_input(qubit);
ZXVertex* v = g.add_vertex(qubit, vt, ph);
ZXVertex* v = g.add_vertex(vt, ph, static_cast<float>(qubit));
ZXVertex* out = g.add_output(qubit);
g.add_edge(in, v, EdgeType::simple);
g.add_edge(v, out, EdgeType::simple);
Expand All @@ -186,8 +186,8 @@ ZXGraph create_cx_zx_form(QCirGate* gate) {

ZXVertex* in_ctrl = g.add_input(ctrl_qubit);
ZXVertex* in_targ = g.add_input(targ_qubit);
ZXVertex* ctrl = g.add_vertex(ctrl_qubit, VertexType::z, dvlab::Phase(0));
ZXVertex* targ_x = g.add_vertex(targ_qubit, VertexType::x, dvlab::Phase(0));
ZXVertex* ctrl = g.add_vertex(VertexType::z, dvlab::Phase(0), static_cast<float>(ctrl_qubit));
ZXVertex* targ_x = g.add_vertex(VertexType::x, dvlab::Phase(0), static_cast<float>(targ_qubit));
ZXVertex* out_ctrl = g.add_output(ctrl_qubit);
ZXVertex* out_targ = g.add_output(targ_qubit);
g.add_edge(in_ctrl, ctrl, EdgeType::simple);
Expand All @@ -212,7 +212,7 @@ ZXGraph create_ccx_zx_form(QCirGate* gate, size_t decomposition_mode) {
auto targ_qubit = gate->get_qubits()[0]._isTarget ? gate->get_qubits()[0]._qubit : (gate->get_qubits()[1]._isTarget ? gate->get_qubits()[1]._qubit : gate->get_qubits()[2]._qubit);
std::vector<std::pair<std::pair<VertexType, dvlab::Phase>, QubitIdType>> vertices_info;
std::vector<std::pair<std::pair<size_t, size_t>, EdgeType>> adj_pair;
std::vector<int> vertices_col;
std::vector<float> vertices_col;
std::vector<ZXVertex*> vertices_list = {};
ZXVertex* in_ctrl_1 = nullptr;
ZXVertex* in_ctrl_2 = nullptr;
Expand All @@ -230,11 +230,8 @@ ZXGraph create_ccx_zx_form(QCirGate* gate, size_t decomposition_mode) {
in_ctrl_2 = g.add_input(ctrl_qubit_2, 0);
in_targ = g.add_input(targ_qubit, 0);
for (size_t i = 0; i < vertices_info.size(); i++) {
vertices_list.emplace_back(g.add_vertex(vertices_info[i].second, vertices_info[i].first.first, vertices_info[i].first.second));
vertices_list.emplace_back(g.add_vertex(vertices_info[i].first.first, vertices_info[i].first.second, static_cast<float>(vertices_info[i].second), vertices_col[i]));
};
for (size_t i = 0; i < vertices_col.size(); i++) {
vertices_list[i]->set_col(vertices_col[i]);
}
out_ctrl_1 = g.add_output(ctrl_qubit_1, 12);
out_ctrl_2 = g.add_output(ctrl_qubit_2, 12);
out_targ = g.add_output(targ_qubit, 12);
Expand All @@ -254,11 +251,8 @@ ZXGraph create_ccx_zx_form(QCirGate* gate, size_t decomposition_mode) {
in_ctrl_2 = g.add_input(ctrl_qubit_2, 0);
in_targ = g.add_input(targ_qubit, 0);
for (size_t i = 0; i < vertices_info.size(); i++) {
vertices_list.emplace_back(g.add_vertex(vertices_info[i].second, vertices_info[i].first.first, vertices_info[i].first.second));
vertices_list.emplace_back(g.add_vertex(vertices_info[i].first.first, vertices_info[i].first.second, static_cast<float>(vertices_info[i].second), vertices_col[i]));
};
for (size_t i = 0; i < vertices_col.size(); i++) {
vertices_list[i]->set_col(vertices_col[i]);
}
out_ctrl_1 = g.add_output(ctrl_qubit_1, 12);
out_ctrl_2 = g.add_output(ctrl_qubit_2, 12);
out_targ = g.add_output(targ_qubit, 12);
Expand All @@ -277,11 +271,8 @@ ZXGraph create_ccx_zx_form(QCirGate* gate, size_t decomposition_mode) {
in_ctrl_2 = g.add_input(ctrl_qubit_2, 0);
in_targ = g.add_input(targ_qubit, 0);
for (size_t i = 0; i < vertices_info.size(); i++) {
vertices_list.emplace_back(g.add_vertex(vertices_info[i].second, vertices_info[i].first.first, vertices_info[i].first.second));
vertices_list.emplace_back(g.add_vertex(vertices_info[i].first.first, vertices_info[i].first.second, static_cast<float>(vertices_info[i].second), vertices_col[i]));
};
for (size_t i = 0; i < vertices_col.size(); i++) {
vertices_list[i]->set_col(vertices_col[i]);
}
out_ctrl_1 = g.add_output(ctrl_qubit_1, 6);
out_ctrl_2 = g.add_output(ctrl_qubit_2, 6);
out_targ = g.add_output(targ_qubit, 6);
Expand All @@ -301,14 +292,12 @@ ZXGraph create_ccx_zx_form(QCirGate* gate, size_t decomposition_mode) {
in_ctrl_2 = g.add_input(ctrl_qubit_2, 0);
in_targ = g.add_input(targ_qubit, 0);
for (size_t i = 0; i < vertices_info.size(); i++) {
vertices_list.emplace_back(g.add_vertex(vertices_info[i].second, vertices_info[i].first.first, vertices_info[i].first.second));
vertices_list.emplace_back(g.add_vertex(vertices_info[i].first.first, vertices_info[i].first.second, static_cast<float>(vertices_info[i].second), vertices_col[i]));
};
for (size_t i = 0; i < vertices_col.size(); i++) {
vertices_list[i]->set_col(vertices_col[i]);
}
out_ctrl_1 = g.add_output(ctrl_qubit_1, 12);
out_ctrl_2 = g.add_output(ctrl_qubit_2, 12);
out_targ = g.add_output(targ_qubit, 12);

g.add_edge(in_ctrl_1, vertices_list[16], EdgeType::simple);
g.add_edge(in_ctrl_2, vertices_list[10], EdgeType::simple);
g.add_edge(in_targ, vertices_list[0], EdgeType::hadamard);
Expand Down Expand Up @@ -349,8 +338,8 @@ ZXGraph create_cz_zx_form(QCirGate* gate) {

ZXVertex* in_ctrl = g.add_input(ctrl_qubit);
ZXVertex* in_targ = g.add_input(targ_qubit);
ZXVertex* ctrl = g.add_vertex(ctrl_qubit, VertexType::z, dvlab::Phase(0));
ZXVertex* targ_z = g.add_vertex(targ_qubit, VertexType::z, dvlab::Phase(0));
ZXVertex* ctrl = g.add_vertex(VertexType::z, dvlab::Phase(0), static_cast<float>(ctrl_qubit));
ZXVertex* targ_z = g.add_vertex(VertexType::z, dvlab::Phase(0), static_cast<float>(targ_qubit));
ZXVertex* out_ctrl = g.add_output(ctrl_qubit);
ZXVertex* out_targ = g.add_output(targ_qubit);
g.add_edge(in_ctrl, ctrl, EdgeType::simple);
Expand All @@ -372,9 +361,9 @@ ZXGraph create_ry_zx_form(QCirGate* gate, dvlab::Phase ph) {
auto qubit = gate->get_qubits()[0]._qubit;

ZXVertex* in = g.add_input(qubit);
ZXVertex* sdg = g.add_vertex(qubit, VertexType::z, dvlab::Phase(-1, 2));
ZXVertex* rx = g.add_vertex(qubit, VertexType::x, ph);
ZXVertex* s = g.add_vertex(qubit, VertexType::z, dvlab::Phase(1, 2));
ZXVertex* sdg = g.add_vertex(VertexType::z, dvlab::Phase(-1, 2), static_cast<float>(qubit));
ZXVertex* rx = g.add_vertex(VertexType::x, ph, static_cast<float>(qubit));
ZXVertex* s = g.add_vertex(VertexType::z, dvlab::Phase(1, 2), static_cast<float>(qubit));
ZXVertex* out = g.add_output(qubit);
g.add_edge(in, sdg, EdgeType::simple);
g.add_edge(sdg, rx, EdgeType::simple);
Expand Down Expand Up @@ -454,9 +443,8 @@ std::optional<ZXGraph> to_zxgraph(QCir const& qcir, size_t decomposition_mode) {
ZXGraph g;
spdlog::debug("Add boundaries");
for (size_t i = 0; i < qcir.get_qubits().size(); i++) {
ZXVertex* input = g.add_input(qcir.get_qubits()[i]->get_id());
ZXVertex* input = g.add_input(qcir.get_qubits()[i]->get_id(), 0);
ZXVertex* output = g.add_output(qcir.get_qubits()[i]->get_id());
input->set_col(0);
g.add_edge(input, output, EdgeType::simple);
}

Expand All @@ -468,7 +456,7 @@ std::optional<ZXGraph> to_zxgraph(QCir const& qcir, size_t decomposition_mode) {
assert(tmp.has_value());

for (auto& v : tmp->get_vertices()) {
v->set_col(v->get_col() + static_cast<double>(gate->get_time()));
v->set_col(v->get_col() + static_cast<float>(gate->get_time()));
}

g.concatenate(*tmp);
Expand Down
17 changes: 10 additions & 7 deletions src/extractor/extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void Extractor::initialize(bool from_empty_qcir) {
}
print_frontier(spdlog::level::level_enum::trace);
print_neighbors(spdlog::level::level_enum::trace);
_graph->print_vertices_by_qubits(spdlog::level::level_enum::trace);
_graph->print_vertices_by_rows(spdlog::level::level_enum::trace);
_logical_circuit->print_circuit_diagram(spdlog::level::level_enum::trace);
}

Expand All @@ -105,12 +105,12 @@ QCir* Extractor::extract() {

spdlog::info("Finished Extracting!");
_logical_circuit->print_circuit_diagram(spdlog::level::level_enum::trace);
_graph->print_vertices_by_qubits(spdlog::level::level_enum::trace);
_graph->print_vertices_by_rows(spdlog::level::level_enum::trace);

if (PERMUTE_QUBITS) {
permute_qubits();
_logical_circuit->print_circuit_diagram(spdlog::level::level_enum::trace);
_graph->print_vertices_by_qubits(spdlog::level::level_enum::trace);
_graph->print_vertices_by_rows(spdlog::level::level_enum::trace);
}

return _logical_circuit;
Expand All @@ -133,7 +133,7 @@ bool Extractor::extraction_loop(std::optional<size_t> max_iter) {
if (remove_gadget()) {
spdlog::debug("Gadget(s) are removed.");
print_frontier(spdlog::level::level_enum::trace);
_graph->print_vertices_by_qubits(spdlog::level::level_enum::trace);
_graph->print_vertices_by_rows(spdlog::level::level_enum::trace);
_logical_circuit->print_circuit_diagram(spdlog::level::level_enum::trace);
continue;
}
Expand All @@ -156,7 +156,7 @@ bool Extractor::extraction_loop(std::optional<size_t> max_iter) {

print_frontier(spdlog::level::level_enum::trace);
print_neighbors(spdlog::level::level_enum::trace);
_graph->print_vertices_by_qubits(spdlog::level::level_enum::trace);
_graph->print_vertices_by_rows(spdlog::level::level_enum::trace);
_logical_circuit->print_circuit_diagram(spdlog::level::level_enum::trace);

if (max_iter.has_value()) (*max_iter)--;
Expand Down Expand Up @@ -199,7 +199,7 @@ void Extractor::extract_singles() {
_graph->remove_edge(s, t, EdgeType::hadamard);
}
_logical_circuit->print_circuit_diagram(spdlog::level::level_enum::trace);
_graph->print_vertices_by_qubits(spdlog::level::level_enum::trace);
_graph->print_vertices_by_rows(spdlog::level::level_enum::trace);
}

/**
Expand Down Expand Up @@ -245,7 +245,7 @@ bool Extractor::extract_czs(bool check) {
prepend_series_gates(ops);
}
_logical_circuit->print_circuit_diagram(spdlog::level::level_enum::trace);
_graph->print_vertices_by_qubits(spdlog::level::level_enum::trace);
_graph->print_vertices_by_rows(spdlog::level::level_enum::trace);

return true;
}
Expand Down Expand Up @@ -382,6 +382,7 @@ bool Extractor::remove_gadget(bool check) {
}
for (auto& [candidate, _] : _graph->get_neighbors(n)) {
if (_frontier.contains(candidate)) {
auto const qubit = candidate->get_qubit();
_axels.erase(n);
_frontier.erase(candidate);

Expand All @@ -396,6 +397,8 @@ bool Extractor::remove_gadget(bool check) {
PivotBoundaryRule().apply(*_graph, {{candidate, n}});

assert(target_boundary != nullptr);
auto new_frontier = _graph->get_first_neighbor(target_boundary).first;
new_frontier->set_qubit(qubit);
_frontier.emplace(_graph->get_first_neighbor(target_boundary).first);
// REVIEW - qubit_map
removed_some_gadgets = true;
Expand Down
4 changes: 1 addition & 3 deletions src/zx/simplifier/rules/state_copy_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void StateCopyRule::apply(ZXGraph& graph, std::vector<MatchType> const& matches)
op.vertices_to_remove.emplace_back(a);
for (auto neighbor : neighbors) {
if (neighbor->get_type() == VertexType::boundary) {
ZXVertex* const new_v = graph.add_vertex(neighbor->get_qubit(), VertexType::z, npi->get_phase());
ZXVertex* const new_v = graph.add_vertex(VertexType::z, npi->get_phase(), neighbor->get_row(), (neighbor->get_col() + a->get_col()) / 2);
auto const is_simple_edge = graph.get_first_neighbor(neighbor).second == EdgeType::simple;

op.edges_to_remove.emplace_back(std::make_pair(a, neighbor), graph.get_first_neighbor(neighbor).second);
Expand All @@ -78,8 +78,6 @@ void StateCopyRule::apply(ZXGraph& graph, std::vector<MatchType> const& matches)
// a to new
op.edges_to_add.emplace_back(std::make_pair(a, new_v), EdgeType::hadamard);

new_v->set_col((neighbor->get_col() + a->get_col()) / 2);

} else {
neighbor->set_phase(npi->get_phase() + neighbor->get_phase());
}
Expand Down
Loading
Loading