Skip to content

Commit

Permalink
Implement dagger() and transpose() for CustomGate (#1671)
Browse files Browse the repository at this point in the history
  • Loading branch information
cqc-alec authored Nov 12, 2024
1 parent d6d5801 commit 3395993
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pytket/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def requirements(self):
self.requires("pybind11_json/0.2.14")
self.requires("symengine/0.12.0")
self.requires("tkassert/0.3.4@tket/stable")
self.requires("tket/1.3.43@tket/stable")
self.requires("tket/1.3.44@tket/stable")
self.requires("tklog/0.3.3@tket/stable")
self.requires("tkrng/0.3.3@tket/stable")
self.requires("tktokenswap/0.3.9@tket/stable")
Expand Down
1 change: 1 addition & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Features:
* Add `only_reduce` argument to `GreedyPauliSimp`.
* Add option to not relabel `ClassicalExpBox` when calling `rename_units`
and `flatten_registers`
* Implement `dagger()` and `transpose()` for `CustomGate`.

Fixes:

Expand Down
12 changes: 12 additions & 0 deletions pytket/tests/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,23 @@ def test_custom_gates() -> None:
op0 = cmd0.op
assert gate.type == op0.type
assert gate.params == op0.params
c_d = c.dagger()
c_t = c.transpose()
Transform.DecomposeBoxes().apply(c)
coms = c.get_commands()
assert str(coms[0]) == "CX q[0], q[3];"
assert str(coms[1]) == "Rz(0.7) q[1];"
assert str(coms[2]) == "CRz(1.3) q[0], q[1];"
Transform.DecomposeBoxes().apply(c_d)
coms_d = c_d.get_commands()
assert str(coms_d[0]) == "CRz(2.7) q[0], q[1];"
assert str(coms_d[1]) == "CX q[0], q[3];"
assert str(coms_d[2]) == "Rz(3.3) q[1];"
Transform.DecomposeBoxes().apply(c_t)
coms_t = c_t.get_commands()
assert str(coms_t[0]) == "CRz(1.3) q[0], q[1];"
assert str(coms_t[1]) == "CX q[0], q[3];"
assert str(coms_t[2]) == "Rz(0.7) q[1];"


def test_errors() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tket/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class TketConan(ConanFile):
name = "tket"
version = "1.3.43"
version = "1.3.44"
package_type = "library"
license = "Apache 2"
homepage = "https://github.com/CQCL/tket"
Expand Down
4 changes: 4 additions & 0 deletions tket/include/tket/Circuit/Boxes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ class CustomGate : public Box {

bool is_clifford() const override;

Op_ptr dagger() const override;

Op_ptr transpose() const override;

protected:
void generate_circuit() const override;
CustomGate() : Box(OpType::CustomGate), gate_(), params_() {}
Expand Down
16 changes: 16 additions & 0 deletions tket/src/Circuit/Boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ bool CustomGate::is_clifford() const {
return true;
}

Op_ptr CustomGate::dagger() const {
Circuit inner_c_dag = gate_->get_def()->dagger();
composite_def_ptr_t dag_def_ptr = std::make_shared<CompositeGateDef>(
gate_->get_name() + "_dagger", gate_->get_def()->dagger(),
gate_->get_args());
return std::make_shared<CustomGate>(dag_def_ptr, params_);
}

Op_ptr CustomGate::transpose() const {
Circuit inner_c_dag = gate_->get_def()->transpose();
composite_def_ptr_t dag_def_ptr = std::make_shared<CompositeGateDef>(
gate_->get_name() + "_transpose", gate_->get_def()->transpose(),
gate_->get_args());
return std::make_shared<CustomGate>(dag_def_ptr, params_);
}

QControlBox::QControlBox(
const Op_ptr &op, unsigned n_controls,
const std::vector<bool> &control_state)
Expand Down
7 changes: 3 additions & 4 deletions tket/src/OpType/OpTypeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,9 @@ bool is_oneway_type(OpType optype) {
// or we do not yet have the dagger gate as an OpType.
// If the gate can have an dagger, define it in the dagger() method.
static const OpTypeSet no_defined_inverse = {
OpType::Input, OpType::Output, OpType::Measure,
OpType::ClInput, OpType::ClOutput, OpType::Barrier,
OpType::Reset, OpType::Collapse, OpType::CustomGate,
OpType::PhasePolyBox, OpType::Create, OpType::Discard};
OpType::Input, OpType::Output, OpType::Measure, OpType::ClInput,
OpType::ClOutput, OpType::Barrier, OpType::Reset, OpType::Collapse,
OpType::PhasePolyBox, OpType::Create, OpType::Discard};
return find_in_set(optype, no_defined_inverse);
}

Expand Down

0 comments on commit 3395993

Please sign in to comment.