Skip to content

Commit

Permalink
add handling of circuit box
Browse files Browse the repository at this point in the history
  • Loading branch information
cqc-melf committed Nov 22, 2024
1 parent 8c28202 commit c035e24
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pytket/binders/circuit/Circuit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +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", &Circuit::get_wasm_file_uid,
":return: the wasm uid of the first wasmop found in the circuit")
":return: the unique wasm uid of the circuit")
.def_property_readonly(
"n_qubits", &Circuit::n_qubits,
":return: the number of qubits in the circuit")
Expand Down
39 changes: 38 additions & 1 deletion pytket/tests/classical_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
QubitRegister,
RangePredicateOp,
SetBitsOp,
CircBox,
)
from pytket.circuit.logic_exp import (
BinaryOp,
Expand Down Expand Up @@ -538,6 +539,42 @@ def test_wasm_uid_from_circuit_2() -> None:
)


def test_wasm_uid_from_circuit_3() -> None:
wfh = wasm.WasmFileHandler("testfile.wasm")

c = Circuit(0)
a = c.add_c_register("c", 8)
c.add_wasm_to_reg("add_one", wfh, [a], [a])
assert c.depth() == 1

cbox = CircBox(c)
d = Circuit(0, 8)

d.add_circbox(cbox, [0, 1, 2, 3, 4, 5, 6, 7])

assert (
d.wasm_uid == "6a0a29e235cd5c60353254bc2b459e631d381cdd0bded7ae6cb44afb784bd2de"
)


def test_wasm_uid_from_circuit_4() -> None:
wfh = wasm.WasmFileHandler("testfile.wasm")

c = Circuit(0)
a = c.add_c_register("c", 8)
c.add_wasm_to_reg("add_one", wfh, [a], [a])
assert c.depth() == 1

cbox = CircBox(c)
d = Circuit(0, 8)

d.add_circbox(cbox, [0, 1, 2, 3, 4, 5, 6, 7], condition=Bit(0))

assert (
d.wasm_uid == "6a0a29e235cd5c60353254bc2b459e631d381cdd0bded7ae6cb44afb784bd2de"
)


def test_wasm_uid_from_circuit_3() -> None:
w = wasm.WasmFileHandler("testfile.wasm")

Expand Down Expand Up @@ -600,7 +637,7 @@ def test_wasm_append_3() -> None:
d.add_c_setbits([False], [bit])
d.add_wasm_to_reg("no_return", wfh2, [a], [])

with pytest.raises(ValueError):
with pytest.raises(RuntimeError):
d.append(c)


Expand Down
24 changes: 23 additions & 1 deletion tket/src/Circuit/setters_and_getters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ 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::string Circuit::get_wasm_file_uid() const { // melf
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)))
Expand All @@ -305,6 +305,28 @@ std::string Circuit::get_wasm_file_uid() const {
*(static_cast<const Conditional &>(*get_Op_ptr_from_Vertex(v)))
.get_op())
.get_wasm_file_uid();
} 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;
}
} 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;
}
}
}
throw std::domain_error("Can't find WASM Op in circuit");
Expand Down

0 comments on commit c035e24

Please sign in to comment.