Skip to content

Commit

Permalink
Add test for projector.
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandMacDoland committed Nov 22, 2023
1 parent 4e3fb3f commit 168e03e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyqtorch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
H,
I,
N,
Proj,
S,
SDagger,
T,
Expand Down
3 changes: 2 additions & 1 deletion pyqtorch/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class Proj(Primitive):
def __init__(self, target: int, state: str = "1"):
if state == "0":
super().__init__(OPERATIONS_DICT["PROJ0"], target)
super()._init__(OPERATIONS_DICT["PROJ1"], target)
else:
super().__init__(OPERATIONS_DICT["PROJ1"], target)


class N(Primitive):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_digital.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def test_N() -> None:
assert torch.allclose(product_state("1"), pyq.N(0)(product_state("1"), None))


def test_projectors() -> None:
t0 = torch.tensor([[0.0], [0.0]], dtype=torch.cdouble)
t1 = torch.tensor([[1.0], [0.0]], dtype=torch.cdouble)
t2 = torch.tensor([[0.0], [1.0]], dtype=torch.cdouble)
assert torch.allclose(t1, pyq.Proj(0, "0")(product_state("0")))
assert torch.allclose(t0, pyq.Proj(0, "0")(product_state("1")))
assert torch.allclose(t2, pyq.Proj(0, "1")(product_state("1")))
assert torch.allclose(t0, pyq.Proj(0, "1")(product_state("0")))


def test_CNOT_state00_controlqubit_0() -> None:
result: torch.Tensor = pyq.CNOT(0, 1)(product_state("00"), None)
assert torch.equal(product_state("00"), result)
Expand Down

0 comments on commit 168e03e

Please sign in to comment.