Skip to content

Commit

Permalink
modify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Aug 10, 2023
1 parent dd7792c commit 6f7c5ea
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 24 deletions.
14 changes: 7 additions & 7 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from qibo import gates

####################### Test `asmatrix` #######################
####################### Test `matrix` #######################
GATES = [
("H", (0,), np.array([[1, 1], [1, -1]]) / np.sqrt(2)),
("X", (0,), np.array([[0, 1], [1, 0]])),
Expand Down Expand Up @@ -47,9 +47,9 @@


@pytest.mark.parametrize("gate,qubits,target_matrix", GATES)
def test_asmatrix(backend, gate, qubits, target_matrix):
def test_matrix(backend, gate, qubits, target_matrix):
gate = getattr(gates, gate)(*qubits)
backend.assert_allclose(gate.asmatrix(backend), target_matrix)
backend.assert_allclose(gate.matrix(backend), target_matrix)


GATES = [
Expand All @@ -75,15 +75,15 @@ def test_asmatrix(backend, gate, qubits, target_matrix):


@pytest.mark.parametrize("gate,target_matrix", GATES)
def test_asmatrix_rotations(backend, gate, target_matrix):
def test_matrix_rotations(backend, gate, target_matrix):
"""Check that `_construct_unitary` method constructs the proper matrix."""
theta = 0.1234
if gate == "CU1":
gate = getattr(gates, gate)(0, 1, theta)
else:
gate = getattr(gates, gate)(0, theta)
backend.assert_allclose(gate.asmatrix(backend), target_matrix(theta))
backend.assert_allclose(gate.asmatrix(backend), target_matrix(theta))
backend.assert_allclose(gate.matrix(backend), target_matrix(theta))
backend.assert_allclose(gate.matrix(backend), target_matrix(theta))


def test_control_matrix(backend):
Expand All @@ -97,7 +97,7 @@ def test_control_matrix(backend):
target_matrix = np.eye(4, dtype=rotation.dtype)
target_matrix[2:, 2:] = rotation
gate = gates.RY(0, theta).controlled_by(1)
backend.assert_allclose(gate.asmatrix(backend), target_matrix)
backend.assert_allclose(gate.matrix(backend), target_matrix)

gate = gates.RY(0, theta).controlled_by(1, 2)
with pytest.raises(NotImplementedError):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gates_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def test_pauli_noise_channel_init(backend):
assert gate.target_qubits == (0,)
for g, p in zip(gate.gates, [matrices.X, matrices.Y, matrices.Z]):
p = backend.cast(p, dtype=p.dtype)
backend.assert_allclose(g.asmatrix(backend), p, atol=PRECISION_TOL)
backend.assert_allclose(g.matrix(backend), p, atol=PRECISION_TOL)


def test_reset_channel_init():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_gates_density_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_one_qubit_gates(backend, gatename, gatekwargs):
gate = getattr(gates, gatename)(0, **gatekwargs)
final_rho = apply_gates(backend, [gate], 1, initial_rho)

matrix = backend.to_numpy(gate.asmatrix(backend))
matrix = backend.to_numpy(gate.matrix(backend))
target_rho = np.einsum("ab,bc,cd->ad", matrix, initial_rho, matrix.conj().T)
backend.assert_allclose(final_rho, target_rho)

Expand All @@ -82,7 +82,7 @@ def test_controlled_by_one_qubit_gates(backend, gatename):
gate = getattr(gates, gatename)(1).controlled_by(0)
final_rho = apply_gates(backend, [gate], 2, initial_rho)

matrix = backend.to_numpy(backend.asmatrix(getattr(gates, gatename)(1)))
matrix = backend.to_numpy(backend.matrix(getattr(gates, gatename)(1)))
cmatrix = np.eye(4, dtype=matrix.dtype)
cmatrix[2:, 2:] = matrix
target_rho = np.einsum("ab,bc,cd->ad", cmatrix, initial_rho, cmatrix.conj().T)
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_two_qubit_gates(backend, gatename, gatekwargs):
gate = getattr(gates, gatename)(0, 1, **gatekwargs)
final_rho = apply_gates(backend, [gate], 2, initial_rho)

matrix = backend.to_numpy(gate.asmatrix(backend))
matrix = backend.to_numpy(gate.matrix(backend))
target_rho = np.einsum("ab,bc,cd->ad", matrix, initial_rho, matrix.conj().T)
backend.assert_allclose(final_rho, target_rho, atol=PRECISION_TOL)

Expand All @@ -123,7 +123,7 @@ def test_toffoli_gate(backend):
gate = gates.TOFFOLI(0, 1, 2)
final_rho = apply_gates(backend, [gate], 3, initial_rho)

matrix = backend.to_numpy(gate.asmatrix(backend))
matrix = backend.to_numpy(gate.matrix(backend))
target_rho = np.einsum("ab,bc,cd->ad", matrix, initial_rho, matrix.conj().T)
backend.assert_allclose(final_rho, target_rho)

Expand Down
7 changes: 4 additions & 3 deletions tests/test_gates_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_align(backend):

backend.assert_allclose(final_state, target_state)

gate_matrix = gate.asmatrix(backend)
gate_matrix = gate.matrix(backend)
identity = backend.identity_density_matrix(nqubits, normalize=False)
backend.assert_allclose(gate_matrix, identity)

Expand Down Expand Up @@ -559,7 +559,8 @@ def test_cun(backend, name, params):

final_state = apply_gates(backend, [gate], initial_state=initial_state)

gate = backend.cast(gate.matrix, dtype=gate.matrix.dtype)
_matrix = gate.matrix(backend)
gate = backend.cast(_matrix, dtype=_matrix.dtype)

target_state = np.dot(gate, initial_state)

Expand Down Expand Up @@ -1472,7 +1473,7 @@ def test_gate_basis_rotation(backend):
gate = gates.Y(0).basis_rotation()
assert isinstance(gate, gates.Unitary)
target_matrix = np.array([[1, -1j], [1j, -1]]) / np.sqrt(2)
backend.assert_allclose(gate.asmatrix(backend), target_matrix)
backend.assert_allclose(gate.matrix(backend), target_matrix)
with pytest.raises(NotImplementedError):
gates.RX(0, np.pi / 2).basis_rotation()

Expand Down
4 changes: 2 additions & 2 deletions tests/test_gates_special.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_callback_gate_errors(backend):
with pytest.raises(NotImplementedError):
gate.on_qubits(2)
with pytest.raises(NotImplementedError):
gate.asmatrix(backend)
gate.matrix(backend)


@pytest.mark.parametrize("nqubits", [2, 3])
Expand All @@ -29,4 +29,4 @@ def test_fused_gate_construct_unitary(backend, nqubits):
toffoli = np.eye(8)
toffoli[-2:, -2:] = np.array([[0, 1], [1, 0]])
target_matrix = toffoli @ np.kron(target_matrix, np.eye(2))
backend.assert_allclose(gate.asmatrix(backend), target_matrix)
backend.assert_allclose(gate.matrix(backend), target_matrix)
2 changes: 1 addition & 1 deletion tests/test_hamiltonians.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def test_hamiltonian_matmul_states(backend, sparse_type):
m = random_complex((2**nqubits, 2**nqubits), dtype=hm.dtype)
Hv = H @ backend.cast(v)
Hm = H @ backend.cast(m)
backend.assert_allclose(Hv, hm.dot(v))
backend.assert_allclose(Hv, hm.dot(v), atol=1e-7) # needs atol for cuquantum
backend.assert_allclose(Hm, (hm @ m))

Hstate = H @ backend.cast(v)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_hamiltonians_from_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sympy

from qibo import hamiltonians, matrices
from qibo.backends import NumpyBackend
from qibo.quantum_info import random_hermitian
from qibo.symbols import I, Symbol, X, Y, Z

Expand Down Expand Up @@ -42,8 +43,9 @@ def test_tfim_hamiltonian_from_symbols(backend, nqubits, hamtype, calcterms):
@pytest.mark.parametrize("calcterms", [False, True])
def test_from_symbolic_with_power(backend, hamtype, calcterms):
"""Check ``from_symbolic`` when the expression contains powers."""
npbackend = NumpyBackend()
if hamtype == "symbolic":
matrix = random_hermitian(2)
matrix = random_hermitian(2, backend=npbackend)
symham = (
Symbol(0, matrix) ** 2
- Symbol(1, matrix) ** 2
Expand All @@ -55,12 +57,13 @@ def test_from_symbolic_with_power(backend, hamtype, calcterms):
else:
z = sympy.symbols(" ".join(f"Z{i}" for i in range(3)))
symham = z[0] ** 2 - z[1] ** 2 + 3 * z[1] - 2 * z[0] * z[2] + 1
matrix = random_hermitian(2)
matrix = random_hermitian(2, backend=npbackend)
symmap = {x: (i, matrix) for i, x in enumerate(z)}
ham = hamiltonians.Hamiltonian.from_symbolic(symham, symmap, backend=backend)

if calcterms:
_ = ham.terms

final_matrix = ham.matrix
matrix2 = matrix.dot(matrix)
eye = np.eye(2, dtype=matrix.dtype)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hamiltonians_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_hamiltonian_term_gates(backend):
term = terms.HamiltonianTerm(matrix, 1, 2)
gate = term.gate
assert gate.target_qubits == (1, 2)
backend.assert_allclose(gate.asmatrix(backend), matrix)
backend.assert_allclose(gate.matrix(backend), matrix)

initial_state = random_statevector(2**nqubits, backend=backend)
final_state = term(backend, np.copy(initial_state), nqubits)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_measurement_gate_errors(backend):
gate.controlled_by(1)
# attempting to construct unitary
with pytest.raises(NotImplementedError):
matrix = gate.matrix
matrix = gate.matrix(backend)


def test_measurement_circuit(backend, accelerators):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models_circuit_fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_fused_gate_construct_unitary(backend, nqubits):
toffoli = np.eye(8)
toffoli[-2:, -2:] = np.array([[0, 1], [1, 0]])
target_matrix = toffoli @ np.kron(target_matrix, np.eye(2))
backend.assert_allclose(gate.asmatrix(backend), target_matrix)
backend.assert_allclose(gate.matrix(backend), target_matrix)


def test_single_fusion_gate():
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_fusedgate_matrix_calculation(backend):
h = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
cnot = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
target_matrix = np.kron(x, x) @ cnot @ np.kron(h, h)
fused_matrix = fused_gate.asmatrix(backend)
fused_matrix = fused_gate.matrix(backend)
backend.assert_allclose(fused_matrix, target_matrix)


Expand Down

0 comments on commit 6f7c5ea

Please sign in to comment.