|
20 | 20 |
|
21 | 21 | import numpy as np
|
22 | 22 | import pytest
|
| 23 | +import scipy as sp |
23 | 24 | from gate_data import (
|
24 | 25 | CCZ,
|
25 | 26 | CH,
|
@@ -826,7 +827,6 @@ def test_repr(self):
|
826 | 827 | qml.PauliZ(0),
|
827 | 828 | qml.Hadamard("a"),
|
828 | 829 | qml.SWAP(wires=(0, 1)),
|
829 |
| - qml.ISWAP(wires=(0, 1)), |
830 | 830 | qml.ECR(wires=(0, 1)),
|
831 | 831 | # Controlled operations
|
832 | 832 | qml.CNOT(wires=(0, 1)),
|
@@ -918,16 +918,37 @@ def test_pauliz_general_power(self, n):
|
918 | 918 | assert op_pow[0].__class__ is qml.PhaseShift
|
919 | 919 | assert qml.math.allclose(op_pow[0].data[0], np.pi * (n % 2))
|
920 | 920 |
|
921 |
| - @pytest.mark.parametrize("n", (0.5, 2.5, -1.5)) |
922 |
| - def test_ISWAP_sqaure_root(self, n): |
923 |
| - """Test that SISWAP is the square root of ISWAP.""" |
924 |
| - op = qml.ISWAP(wires=(0, 1)) |
925 |
| - |
926 |
| - assert op.pow(n)[0].__class__ is qml.SISWAP |
| 921 | + @pytest.mark.parametrize( |
| 922 | + "n, expected", |
| 923 | + [ |
| 924 | + (0, []), |
| 925 | + (4, []), |
| 926 | + (-4, []), |
| 927 | + (-3, [qml.ISWAP(wires=(0, 1))]), |
| 928 | + (5, [qml.ISWAP(wires=(0, 1))]), |
| 929 | + (0.5, [qml.SISWAP(wires=(0, 1))]), |
| 930 | + (4.5, [qml.SISWAP(wires=(0, 1))]), |
| 931 | + (2, [qml.Z(0), qml.Z(1)]), |
| 932 | + (-2, [qml.Z(0), qml.Z(1)]), |
| 933 | + (6, [qml.Z(0), qml.Z(1)]), |
| 934 | + ], |
| 935 | + ) |
| 936 | + def test_ISWAP_powers(self, n, expected): |
| 937 | + """Check that the special powers of ISWAP are correct.""" |
927 | 938 |
|
928 |
| - sqrt_mat = qml.matrix(op.pow, wire_order=[0, 1])(n) |
929 |
| - sqrt_mat_squared = qml.math.linalg.matrix_power(sqrt_mat, 2) |
930 |
| - assert qml.math.allclose(sqrt_mat_squared, qml.matrix(op)) |
| 939 | + op = qml.ISWAP(wires=(0, 1)) |
| 940 | + op_mat = qml.matrix(op) |
| 941 | + pow_ops = op.pow(n) |
| 942 | + assert pow_ops == expected |
| 943 | + if not pow_ops: |
| 944 | + pow_ops.append(qml.I(wires=(0, 1))) |
| 945 | + mat = qml.matrix(qml.prod(*pow_ops), wire_order=[0, 1]) |
| 946 | + expected = ( |
| 947 | + qml.math.linalg.matrix_power(op_mat, n) |
| 948 | + if isinstance(n, int) |
| 949 | + else sp.linalg.fractional_matrix_power(op_mat, n) |
| 950 | + ) |
| 951 | + assert qml.math.allclose(mat, expected) |
931 | 952 |
|
932 | 953 | @pytest.mark.parametrize("offset", (0, 4, -4))
|
933 | 954 | def test_S_pow(self, offset):
|
@@ -971,17 +992,28 @@ def test_SX_pow(self, offset):
|
971 | 992 | with pytest.raises(qml.operation.PowUndefinedError):
|
972 | 993 | op.pow(2.43 + offset)
|
973 | 994 |
|
974 |
| - @pytest.mark.parametrize("offset", (0, 4, -4)) |
975 |
| - def test_SISWAP_pow(self, offset): |
976 |
| - """Test powers of the SISWAP operator""" |
977 |
| - op = qml.SISWAP(wires=("b", "c")) |
978 |
| - |
979 |
| - assert len(op.pow(0 + offset)) == 0 |
980 |
| - assert op.pow(1 + offset)[0].__class__ is qml.SISWAP |
981 |
| - assert op.pow(2 + offset)[0].__class__ is qml.ISWAP |
| 995 | + @pytest.mark.parametrize( |
| 996 | + "n, expected", |
| 997 | + [ |
| 998 | + (0, []), |
| 999 | + (8, []), |
| 1000 | + (9, [qml.SISWAP(wires=(0, 1))]), |
| 1001 | + (2, [qml.ISWAP(wires=(0, 1))]), |
| 1002 | + (4, [qml.Z(0), qml.Z(1)]), |
| 1003 | + ], |
| 1004 | + ) |
| 1005 | + def test_SISWAP_powers(self, n, expected): |
| 1006 | + """Check that the special powers of SISWAP are correct.""" |
982 | 1007 |
|
983 |
| - with pytest.raises(qml.operation.PowUndefinedError): |
984 |
| - op.pow(2.34 + offset) |
| 1008 | + op = qml.SISWAP(wires=(0, 1)) |
| 1009 | + op_mat = qml.matrix(op) |
| 1010 | + pow_ops = op.pow(n) |
| 1011 | + assert pow_ops == expected |
| 1012 | + if not pow_ops: |
| 1013 | + pow_ops.append(qml.I(wires=(0, 1))) |
| 1014 | + mat = qml.matrix(qml.prod(*pow_ops), wire_order=[0, 1]) |
| 1015 | + expected = qml.math.linalg.matrix_power(op_mat, n) |
| 1016 | + assert qml.math.allclose(mat, expected) |
985 | 1017 |
|
986 | 1018 | @pytest.mark.parametrize("op", (qml.WireCut(0), qml.Barrier(0)))
|
987 | 1019 | @pytest.mark.parametrize("n", (2, 0.123, -2.3))
|
|
0 commit comments