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

fix wasm issues #1691

Merged
merged 23 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 1 addition & 8 deletions pytket/binders/circuit/Circuit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,7 @@ void def_circuit(py::class_<Circuit, std::shared_ptr<Circuit>> &pyCircuit) {
":return: the number of gates in the Circuit")
.def_property_readonly(
"wasm_uid",
[](const Circuit &circ) {
std::optional<std::string> result;
try {
result = circ.get_wasm_file_uid();
} catch (const std::exception &) {
}
return result;
},
[](const Circuit &circ) { return circ.get_wasm_file_uid(); },
cqc-alec marked this conversation as resolved.
Show resolved Hide resolved
":return: the unique wasm uid of the circuit")
cqc-melf marked this conversation as resolved.
Show resolved Hide resolved
.def_property_readonly(
"n_qubits", &Circuit::n_qubits,
Expand Down
4 changes: 4 additions & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog
1.36.0 (unreleased)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to keep this as "Unreleased" only. We don't know if it will be 1.36.0 or 1.35.1 ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 37f0f1b

-------------------

API changes:

* Remove the deprecated methods `auto_rebase_pass()` and `auto_squash_pass()`.

Features:

* Add `Circuit.wasm_uid` property to get the wasm UID from the circuit
Expand Down
2 changes: 1 addition & 1 deletion pytket/tests/zx_diagram_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from math import isclose, pow # noqa: A004
from math import isclose
from typing import Tuple

import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion tket/include/tket/Circuit/Circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class Circuit {
UnitID get_id_from_out(const Vertex &out) const;

opt_reg_info_t get_reg_info(std::string reg_name) const;
std::string get_wasm_file_uid() const;
std::optional<std::string> get_wasm_file_uid() const;
register_t get_reg(std::string reg_name) const;

// returns the total number of vertices in dag
Expand Down
3 changes: 2 additions & 1 deletion tket/src/Circuit/macro_manipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void Circuit::append_with_map(const Circuit& c2, const unit_map_t& qm) {
if (copy.get_wasm_file_uid() != get_wasm_file_uid()) {
throw Unsupported(
"Cannot append circuits with different wasm uids: " +
get_wasm_file_uid() + " and " + copy.get_wasm_file_uid());
get_wasm_file_uid().emplace(" - ") + " and " +
copy.get_wasm_file_uid().emplace(" - "));
cqc-melf marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
47 changes: 23 additions & 24 deletions tket/src/Circuit/setters_and_getters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,45 +291,44 @@ opt_reg_info_t Circuit::get_reg_info(std::string reg_name) const {
return found->reg_info();
}

std::string Circuit::get_wasm_file_uid() const {
std::optional<std::string> Circuit::get_wasm_file_uid() const {
std::optional<std::string> result = std::nullopt;
BGL_FORALL_VERTICES(v, dag, DAG) {
if (get_OpType_from_Vertex(v) == OpType::WASM) {
return (static_cast<const WASMOp &>(*get_Op_ptr_from_Vertex(v)))
.get_wasm_file_uid();
result = (static_cast<const WASMOp &>(*get_Op_ptr_from_Vertex(v)))
.get_wasm_file_uid();
return result;
} else if (
(get_OpType_from_Vertex(v) == OpType::Conditional) &&
(static_cast<const Conditional &>(*get_Op_ptr_from_Vertex(v))
.get_op()
->get_type() == OpType::WASM)) {
return static_cast<const WASMOp &>(
*(static_cast<const Conditional &>(*get_Op_ptr_from_Vertex(v)))
.get_op())
.get_wasm_file_uid();
result =
static_cast<const WASMOp &>(
*(static_cast<const Conditional &>(*get_Op_ptr_from_Vertex(v)))
.get_op())
.get_wasm_file_uid();
return result;
} else if (get_OpType_from_Vertex(v) == OpType::CircBox) {
try {
return (static_cast<const CircBox &>(*get_Op_ptr_from_Vertex(v)))
.to_circuit()
->get_wasm_file_uid();
} catch (std::domain_error const &) {
continue;
}
result = (static_cast<const CircBox &>(*get_Op_ptr_from_Vertex(v)))
.to_circuit()
->get_wasm_file_uid();
if (result != std::nullopt) return result;
} else if (
(get_OpType_from_Vertex(v) == OpType::Conditional) &&
(static_cast<const Conditional &>(*get_Op_ptr_from_Vertex(v))
.get_op()
->get_type() == OpType::CircBox)) {
try {
return (static_cast<const CircBox &>(*(static_cast<const Conditional &>(
*get_Op_ptr_from_Vertex(v)))
.get_op()))
.to_circuit()
->get_wasm_file_uid();
} catch (std::domain_error const &) {
continue;
}
result =
(static_cast<const CircBox &>(
*(static_cast<const Conditional &>(*get_Op_ptr_from_Vertex(v)))
.get_op()))
.to_circuit()
->get_wasm_file_uid();
if (result != std::nullopt) return result;
}
}
throw std::domain_error("Can't find WASM Op in circuit");
return result;
}

register_t Circuit::get_reg(std::string reg_name) const {
Expand Down
44 changes: 44 additions & 0 deletions tket/test/src/test_wasm.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the appending tests here involve appending to an empty circuit. Could we add some tests appending to a circuit that already contains WASM wires?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added one, see d9e6dac

There is also one in the python tests already.

Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,50 @@ SCENARIO("generating circ with wasm") {
REQUIRE(u.depth() == 2);
REQUIRE(u.get_wasm_file_uid() == wasm_file);
}
GIVEN("circuit get wasm uid 2") {
Circuit u(0, 1);

const std::shared_ptr<WASMOp> wop_ptr =
std::make_shared<WASMOp>(2, 1, uv_2, uv_2, wasm_func, wasm_file);

const std::shared_ptr<WASMOp> wop_ptr_2 =
std::make_shared<WASMOp>(1, 3, uv_2, uv_3, wasm_func, wasm_file);

u.add_op<UnitID>(wop_ptr, {Bit(0), Bit(0), WasmState(0)});
u.add_op<UnitID>(
wop_ptr_2, {Bit(0), WasmState(0), WasmState(1), WasmState(2)});

u.assert_valid();
REQUIRE(u.depth() == 2);
REQUIRE(u.get_wasm_file_uid() == wasm_file);

CircBox circbox(u);
Circuit major_circ(0, 1);
major_circ.add_box(circbox, {0});

REQUIRE(major_circ.depth() == 1);
REQUIRE(major_circ.get_wasm_file_uid() == wasm_file);
}
GIVEN("circuit get wasm uid 3") {
Circuit u(0, 1);

const std::shared_ptr<WASMOp> wop_ptr =
std::make_shared<WASMOp>(2, 1, uv_2, uv_2, wasm_func, wasm_file);

const std::shared_ptr<WASMOp> wop_ptr_2 =
std::make_shared<WASMOp>(1, 3, uv_2, uv_3, wasm_func, wasm_file);

u.assert_valid();
REQUIRE(u.depth() == 0);
REQUIRE(u.get_wasm_file_uid() == std::nullopt);

CircBox circbox(u);
Circuit major_circ(0, 1);
major_circ.add_box(circbox, {0});

REQUIRE(major_circ.depth() == 1);
REQUIRE(major_circ.get_wasm_file_uid() == std::nullopt);
}
GIVEN("circuit with wasm append") {
Circuit u(1, 1);
Circuit u2(1, 1);
Expand Down
Loading