Skip to content

Commit

Permalink
CZ gate tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
arsalan-motamedi committed Nov 6, 2024
1 parent c923193 commit 50917c8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/test_lab_dev/test_transformations/test_cxgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class TestCXgate:
r"""
Tests for the ``Pgate`` class.
Tests for the ``CXgate`` class.
"""

def test_init(self):
Expand Down
55 changes: 55 additions & 0 deletions tests/test_lab_dev/test_transformations/test_czgate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2024 Xanadu Quantum Technologies Inc.

# 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.

"""Tests for the ``CZgate`` class."""

# pylint: disable=missing-function-docstring, expression-not-assigned

import re

import pytest

from mrmustard import math
from mrmustard.lab_dev.states import Coherent, Vacuum
from mrmustard.lab_dev.transformations import CZgate


class TestCZgate:
r"""
Tests for the ``CZgate`` class.
"""

def test_init(self):
"Tests the CZgate initialization."
cz = CZgate([0, 1], 0.3)
assert cz.modes == [0, 1]
assert cz.name == "CZgate"
assert cz.s.value == 0.3

with pytest.raises(
ValueError,
match=re.escape(
"The number of modes for a CZgate must be 2 (your input has 3 many modes)."
),
):
CZgate([0, 1, 2], 0.2)

@pytest.mark.parametrize("s", [0.1, 0.2, 1.5])
def test_application(self, s):
"Tests the application of CZgate"
psi = Coherent([0], 0, 1) >> Coherent([1], 1, 0) >> CZgate([0, 1], 1)
_, d, _ = psi.phase_space(s=0)
psi = Coherent([0], 0, 1) >> Coherent([1], 1, 0) >> CZgate([0, 1], 1)
d_by_hand = math.astensor([0, math.sqrt(complex(2)), (1 + s) * math.sqrt(complex(2)), 0])
assert math.allclose(d[0], d_by_hand)

0 comments on commit 50917c8

Please sign in to comment.