From a031a2f78f1be9825004c3fa1792a65564fce86f Mon Sep 17 00:00:00 2001 From: Seyon Sivarajah Date: Wed, 20 Dec 2023 10:58:45 +0000 Subject: [PATCH] [feature] add a Circuit method for Reset (#1184) * [feature] add a Circuit method for Reset Commonly used enough that having to switch to add_gate confuses people. * try upping tket lib version * update version in conanfile * format * regen stubs * regen stubs --------- Co-authored-by: Melf --- pytket/binders/circuit/Circuit/add_op.cpp | 20 ++++++++++++++++++++ pytket/conanfile.py | 2 +- pytket/pytket/_tket/circuit.pyi | 14 ++++++++++++++ pytket/tests/add_circuit_test.py | 10 ++++++---- tket/conanfile.py | 2 +- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/pytket/binders/circuit/Circuit/add_op.cpp b/pytket/binders/circuit/Circuit/add_op.cpp index 087c84afde..5c3bfe9afb 100644 --- a/pytket/binders/circuit/Circuit/add_op.cpp +++ b/pytket/binders/circuit/Circuit/add_op.cpp @@ -1067,6 +1067,16 @@ void init_circuit_add_op(py::class_> &c) { "(Z) basis." "\n\n:return: the new :py:class:`Circuit`", py::arg("qubit"), py::arg("bit_index")) + .def( + "Reset", + [](Circuit *circ, unsigned qb, const py::kwargs &kwargs) { + return add_gate_method_noparams( + circ, OpType::Reset, {qb}, kwargs); + }, + "Appends a Reset operation. Sets a qubit to the Z-basis 0 state. " + "Non-unitary operation." + "\n\n:return: the new :py:class:`Circuit`", + py::arg("qubit")) .def( "Rz", [](Circuit *circ, const Expr &angle, unsigned qb, @@ -1680,6 +1690,16 @@ void init_circuit_add_op(py::class_> &c) { "(Z) basis." "\n\n:return: the new :py:class:`Circuit`", py::arg("qubit"), py::arg("bit")) + .def( + "Reset", + [](Circuit *circ, const Qubit &qb, const py::kwargs &kwargs) { + return add_gate_method_noparams( + circ, OpType::Reset, {qb}, kwargs); + }, + "Appends a Reset operation. Sets a qubit to the Z-basis 0 state. " + "Non-unitary operation." + "\n\n:return: the new :py:class:`Circuit`", + py::arg("qubit")) .def( "Rz", [](Circuit *circ, const Expr &angle, const Qubit &qb, diff --git a/pytket/conanfile.py b/pytket/conanfile.py index 1c115d411b..18802883b5 100644 --- a/pytket/conanfile.py +++ b/pytket/conanfile.py @@ -32,7 +32,7 @@ def package(self): cmake.install() def requirements(self): - self.requires("tket/1.2.75@tket/stable") + self.requires("tket/1.2.77@tket/stable") self.requires("tklog/0.3.3@tket/stable") self.requires("tkrng/0.3.3@tket/stable") self.requires("tkassert/0.3.4@tket/stable") diff --git a/pytket/pytket/_tket/circuit.pyi b/pytket/pytket/_tket/circuit.pyi index 52f1283406..788b155fde 100644 --- a/pytket/pytket/_tket/circuit.pyi +++ b/pytket/pytket/_tket/circuit.pyi @@ -515,6 +515,20 @@ class Circuit: :return: the new :py:class:`Circuit` """ @typing.overload + def Reset(self, qubit: int, **kwargs: Any) -> Circuit: + """ + Appends a Reset operation. Sets a qubit to the Z-basis 0 state. Non-unitary operation. + + :return: the new :py:class:`Circuit` + """ + @typing.overload + def Reset(self, qubit: pytket._tket.unit_id.Qubit, **kwargs: Any) -> Circuit: + """ + Appends a Reset operation. Sets a qubit to the Z-basis 0 state. Non-unitary operation. + + :return: the new :py:class:`Circuit` + """ + @typing.overload def Rx(self, angle: sympy.Expr | float, qubit: int, **kwargs: Any) -> Circuit: """ Appends an Rx gate with a possibly symbolic angle (specified in half-turns). diff --git a/pytket/tests/add_circuit_test.py b/pytket/tests/add_circuit_test.py index 536afb84c9..3f01ce0810 100644 --- a/pytket/tests/add_circuit_test.py +++ b/pytket/tests/add_circuit_test.py @@ -16,21 +16,23 @@ import pytest -def gen_bell_state() -> Circuit: +def gen_bell_state(reset_start: bool = False) -> Circuit: circ = Circuit(2) + if reset_start: + circ = circ.Reset(0).Reset(1) circ.H(0) circ.CX(0, 1) return circ def test_direct_add() -> None: - a = gen_bell_state() + a = gen_bell_state(True) b = Circuit(1) b.add_gate(OpType.X, [0]) a.add_circuit(b, [0]) - assert a.n_gates == 3 + assert a.n_gates == 5 assert a.n_qubits == 2 - assert a.depth() == 3 + assert a.depth() == 4 def test_swap_add() -> None: diff --git a/tket/conanfile.py b/tket/conanfile.py index 86a2dbf956..ff253dab81 100644 --- a/tket/conanfile.py +++ b/tket/conanfile.py @@ -23,7 +23,7 @@ class TketConan(ConanFile): name = "tket" - version = "1.2.75" + version = "1.2.77" package_type = "library" license = "Apache 2" homepage = "https://github.com/CQCL/tket"