Skip to content

Commit

Permalink
fix: Add copy implementations for Unit IDs (#1550)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnchildren authored Aug 23, 2024
1 parent 79bd0b5 commit 552e612
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pytket/binders/unitid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ PYBIND11_MODULE(unit_id, m) {
"``UnitType.bit``");

py::class_<Qubit, UnitID>(m, "Qubit", "A handle to a qubit")
.def("__copy__", [](const Qubit &id) { return Qubit(id); })
.def(
"__deepcopy__",
[](const Qubit &id, const py::dict &) { return Qubit(id); })
.def(
py::init<unsigned>(),
"Constructs an id for some index in the default qubit "
Expand Down Expand Up @@ -172,6 +176,10 @@ PYBIND11_MODULE(unit_id, m) {
"list representation of the Qubit.");

py::class_<Bit, UnitID>(m, "Bit", "A handle to a bit")
.def("__copy__", [](const Bit &id) { return Bit(id); })
.def(
"__deepcopy__",
[](const Bit &id, const py::dict &) { return Bit(id); })
.def(
py::init<unsigned>(),
"Constructs an id for some index in the default classical "
Expand Down Expand Up @@ -226,6 +234,10 @@ PYBIND11_MODULE(unit_id, m) {
"list representation of the Bit.");

py::class_<Node, Qubit>(m, "Node", "A handle to a device node")
.def("__copy__", [](const Node &id) { return Node(id); })
.def(
"__deepcopy__",
[](const Node &id, const py::dict &) { return Node(id); })
.def(
py::init<unsigned>(),
"Constructs an id for some index in the default physical "
Expand Down
12 changes: 12 additions & 0 deletions pytket/pytket/_tket/unit_id.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Bit(UnitID):
"""
def __and__(self: typing.Union[pytket.circuit.logic_exp.LogicExp, pytket._tket.unit_id.Bit, int], other: typing.Union[pytket.circuit.logic_exp.LogicExp, pytket._tket.unit_id.Bit, int]) -> pytket.circuit.logic_exp.BitLogicExp:
...
def __copy__(self) -> Bit:
...
def __deepcopy__(self, arg0: dict) -> Bit:
...
def __eq__(self, arg0: typing.Any) -> bool:
...
def __getstate__(self) -> tuple:
Expand Down Expand Up @@ -172,6 +176,10 @@ class Node(Qubit):
"""
Construct Node instance from JSON serializable list representation of the Node.
"""
def __copy__(self) -> Node:
...
def __deepcopy__(self, arg0: dict) -> Node:
...
@typing.overload
def __init__(self, index: int) -> None:
"""
Expand Down Expand Up @@ -227,6 +235,10 @@ class Qubit(UnitID):
"""
Construct Qubit instance from JSON serializable list representation of the Qubit.
"""
def __copy__(self) -> Qubit:
...
def __deepcopy__(self, arg0: dict) -> Qubit:
...
def __getstate__(self) -> tuple:
...
@typing.overload
Expand Down
49 changes: 49 additions & 0 deletions pytket/tests/unit_id/copy_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2019-2024 Cambridge Quantum Computing
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from copy import copy, deepcopy

from pytket.unit_id import Bit, Qubit, Node


def test_copying_qubits() -> None:
q = Qubit(0)

q1 = copy(q)
q2 = deepcopy(q)

assert type(q) is Qubit
assert type(q1) is Qubit
assert type(q2) is Qubit


def test_copying_bits() -> None:
b = Bit(0)

b1 = copy(b)
b2 = deepcopy(b)

assert type(b) is Bit
assert type(b1) is Bit
assert type(b2) is Bit


def test_copying_nodes() -> None:
n = Node(0)

n1 = copy(n)
n2 = deepcopy(n)

assert type(n) is Node
assert type(n1) is Node
assert type(n2) is Node

0 comments on commit 552e612

Please sign in to comment.