diff --git a/tests/test_backends.py b/tests/test_backends.py index 9155efec8c..a4714bd378 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -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]])), @@ -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 = [ @@ -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): @@ -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): diff --git a/tests/test_gates_abstract.py b/tests/test_gates_abstract.py index 7711bb61a8..133f103ca0 100644 --- a/tests/test_gates_abstract.py +++ b/tests/test_gates_abstract.py @@ -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(): diff --git a/tests/test_gates_density_matrix.py b/tests/test_gates_density_matrix.py index 8c53048ffa..19d6c66bf8 100644 --- a/tests/test_gates_density_matrix.py +++ b/tests/test_gates_density_matrix.py @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index 5826975276..3bb906c60d 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -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) @@ -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) @@ -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() diff --git a/tests/test_gates_special.py b/tests/test_gates_special.py index 53ec1846c2..472d4e371f 100644 --- a/tests/test_gates_special.py +++ b/tests/test_gates_special.py @@ -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]) @@ -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) diff --git a/tests/test_hamiltonians.py b/tests/test_hamiltonians.py index 5c3c04c3d8..960dbc797d 100644 --- a/tests/test_hamiltonians.py +++ b/tests/test_hamiltonians.py @@ -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) diff --git a/tests/test_hamiltonians_from_symbols.py b/tests/test_hamiltonians_from_symbols.py index 34c0c596ae..4208ab5fb7 100644 --- a/tests/test_hamiltonians_from_symbols.py +++ b/tests/test_hamiltonians_from_symbols.py @@ -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 @@ -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 @@ -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) diff --git a/tests/test_hamiltonians_terms.py b/tests/test_hamiltonians_terms.py index c13b8dc995..f0a0703757 100644 --- a/tests/test_hamiltonians_terms.py +++ b/tests/test_hamiltonians_terms.py @@ -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) diff --git a/tests/test_measurements.py b/tests/test_measurements.py index 276b6e67b1..3b0bcea4d5 100644 --- a/tests/test_measurements.py +++ b/tests/test_measurements.py @@ -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): diff --git a/tests/test_models_circuit_fuse.py b/tests/test_models_circuit_fuse.py index 8ff7c20f4c..4e7c323f2e 100644 --- a/tests/test_models_circuit_fuse.py +++ b/tests/test_models_circuit_fuse.py @@ -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(): @@ -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)