Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Jun 25, 2024
1 parent f41dbd0 commit dc78b1d
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/test_quantum_info_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import numpy as np
import pytest

from qibo import matrices
from qibo.quantum_info.operations import anticommutator, commutator


def test_commutator(backend):
matrix_1 = np.random.rand(2, 2, 2)
matrix_1 = backend.cast(matrix_1, dtype=matrix_1.dtype)

matrix_2 = np.random.rand(2, 2)
matrix_2 = backend.cast(matrix_2, dtype=matrix_2.dtype)

with pytest.raises(TypeError):
test = commutator(matrix_1, matrix_2)
with pytest.raises(TypeError):
test = commutator(matrix_2, matrix_1)

I, X, Y, Z = matrices.I, matrices.X, matrices.Y, matrices.Z
I = backend.cast(I, dtype=I.dtype)
X = backend.cast(X, dtype=X.dtype)
Y = backend.cast(Y, dtype=Y.dtype)
Z = backend.cast(Z, dtype=Z.dtype)

comm = commutator(X, I)
backend.assert_allclose(comm, 0.0)

comm = commutator(X, X)
backend.assert_allclose(comm, 0.0)

comm = commutator(X, Y)
backend.assert_allclose(comm, 2j * Z)

comm = commutator(X, Z)
backend.assert_allclose(comm, -2j * Y)


def test_anticommutator(backend):
matrix_1 = np.random.rand(2, 2, 2)
matrix_1 = backend.cast(matrix_1, dtype=matrix_1.dtype)

matrix_2 = np.random.rand(2, 2)
matrix_2 = backend.cast(matrix_2, dtype=matrix_2.dtype)

with pytest.raises(TypeError):
test = anticommutator(matrix_1, matrix_2)
with pytest.raises(TypeError):
test = anticommutator(matrix_2, matrix_1)

I, X, Y, Z = matrices.I, matrices.X, matrices.Y, matrices.Z
I = backend.cast(I, dtype=I.dtype)
X = backend.cast(X, dtype=X.dtype)
Y = backend.cast(Y, dtype=Y.dtype)
Z = backend.cast(Z, dtype=Z.dtype)

anticomm = anticommutator(X, I)
backend.assert_allclose(anticomm, 2 * X)

anticomm = anticommutator(X, X)
backend.assert_allclose(anticomm, 2 * I)

anticomm = anticommutator(X, Y)
backend.assert_allclose(anticomm, 0.0)

anticomm = anticommutator(X, Z)
backend.assert_allclose(anticomm, 0.0)

0 comments on commit dc78b1d

Please sign in to comment.