From 033e9680fff4938162752b857230c1a07ccb2785 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 16 Mar 2024 08:52:06 +0400 Subject: [PATCH] fix more tests --- src/qibo/backends/numpy.py | 8 ++++--- src/qibo/quantum_info/random_ensembles.py | 16 +++++++------- tests/test_quantum_info_clifford.py | 21 ++++++++++++------- tests/test_quantum_info_random.py | 1 - tests/test_result.py | 9 ++++---- .../test_transpiler_unitary_decompositions.py | 6 ++++-- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/qibo/backends/numpy.py b/src/qibo/backends/numpy.py index ced6da526b..7b9488b8fc 100644 --- a/src/qibo/backends/numpy.py +++ b/src/qibo/backends/numpy.py @@ -641,15 +641,17 @@ def aggregate_shots(self, shots): return self.cast(shots, dtype=shots[0].dtype) def samples_to_binary(self, samples, nqubits): + ### This is faster just staying @ NumPy. qrange = np.arange(nqubits - 1, -1, -1, dtype=np.int32) samples = self.to_numpy(samples) return np.mod(np.right_shift(samples[:, None], qrange), 2) def samples_to_decimal(self, samples, nqubits): - qrange = self.np.arange(nqubits - 1, -1, -1, dtype=self.np.int32) + ### This is faster just staying @ NumPy. + qrange = np.arange(nqubits - 1, -1, -1, dtype=np.int32) qrange = (2**qrange)[:, None] - qrange = self.cast(qrange, dtype=self.np.int32) - return self.np.matmul(samples, qrange)[:, 0] + samples = np.asarray(samples.tolist()) + return np.matmul(samples, qrange)[:, 0] def calculate_frequencies(self, samples): # Samples are a list of strings so there is no advantage in using other backends diff --git a/src/qibo/quantum_info/random_ensembles.py b/src/qibo/quantum_info/random_ensembles.py index ec171feddf..997648a378 100644 --- a/src/qibo/quantum_info/random_ensembles.py +++ b/src/qibo/quantum_info/random_ensembles.py @@ -7,7 +7,7 @@ import numpy as np from scipy.stats import rv_continuous -from qibo import Circuit, gates +from qibo import Circuit, gates, matrices from qibo.backends import NumpyBackend, _check_backend_and_local_state from qibo.config import MAX_ITERATIONS, PRECISION_TOL, raise_error from qibo.quantum_info.basis import comp_basis_to_pauli @@ -810,7 +810,11 @@ def random_pauli( backend, local_state = _check_backend_and_local_state(seed, backend) - complete_set = {"I": gates.I, "X": gates.X, "Y": gates.Y, "Z": gates.Z} + complete_set = ( + {"I": gates.I, "X": gates.X, "Y": gates.Y, "Z": gates.Z} + if return_circuit + else {"I": matrices.I, "X": matrices.X, "Y": matrices.Y, "Z": matrices.Z} + ) if subset is None: subset = complete_set @@ -839,13 +843,9 @@ def random_pauli( if subset[column_item] != gates.I: gate_grid.add(subset[column_item](qubit)) else: - gate_grid = np.array( - [ - [subset[column_item](qubit).matrix(backend) for column_item in row] - for qubit, row in zip(qubits, indexes) - ] + gate_grid = backend.cast( + [[subset[column_item] for column_item in row] for row in indexes] ) - gate_grid = backend.cast(gate_grid, dtype=gate_grid.dtype) return gate_grid diff --git a/tests/test_quantum_info_clifford.py b/tests/test_quantum_info_clifford.py index 9166d3ad0c..c9e8654df9 100644 --- a/tests/test_quantum_info_clifford.py +++ b/tests/test_quantum_info_clifford.py @@ -4,7 +4,7 @@ import numpy as np import pytest -from qibo import Circuit, gates +from qibo import Circuit, gates, matrices from qibo.backends import CliffordBackend, PyTorchBackend, TensorflowBackend from qibo.backends.clifford import _get_engine_name from qibo.quantum_info._clifford_utils import ( @@ -152,9 +152,10 @@ def test_clifford_stabilizers(backend, symplectic, return_array): obj = Clifford.from_circuit(c, engine=_get_engine_name(backend)) if return_array: true_generators = [ - reduce(np.kron, [getattr(gates, gate)(0).matrix() for gate in generator]) + reduce(np.kron, [getattr(matrices, gate) for gate in generator]) for generator in ["XII", "IZI", "IIZ"] ] + true_generators = backend.cast(true_generators, dtype=true_generators[0].dtype) else: true_generators = ["XII", "IZI", "IIZ"] true_phases = [1, 1, -1] @@ -181,12 +182,13 @@ def test_clifford_stabilizers(backend, symplectic, return_array): "-IIZ", "III", ]: - tmp = reduce( - np.kron, [getattr(gates, s)(0).matrix() for s in stab.replace("-", "")] - ) + tmp = reduce(np.kron, [getattr(matrices, s) for s in stab.replace("-", "")]) if "-" in stab: tmp *= -1 true_stabilizers.append(tmp) + true_stabilizers = backend.cast( + true_stabilizers, dtype=true_stabilizers[0].dtype + ) elif not symplectic and not return_array: true_stabilizers = [ "-XZZ", @@ -220,10 +222,10 @@ def test_clifford_destabilizers(backend, symplectic, return_array): obj = Clifford.from_circuit(c, engine=_get_engine_name(backend)) if return_array: true_generators = [ - reduce(np.kron, [getattr(gates, gate)(0).matrix() for gate in generator]) + reduce(np.kron, [getattr(matrices, gate) for gate in generator]) for generator in ["ZII", "IXI", "IIX"] ] - true_generators = backend.cast(true_generators) + true_generators = backend.cast(true_generators, dtype=true_generators[0].dtype) else: true_generators = ["ZII", "IXI", "IIX"] true_phases = [1, 1, 1] @@ -252,11 +254,14 @@ def test_clifford_destabilizers(backend, symplectic, return_array): ]: tmp = reduce( np.kron, - [getattr(gates, s)(0).matrix() for s in destab.replace("-", "")], + [getattr(matrices, s) for s in destab.replace("-", "")], ) if "-" in destab: tmp *= -1 true_destabilizers.append(tmp) + true_destabilizers = backend.cast( + true_destabilizers, dtype=true_destabilizers[0].dtype + ) elif not symplectic and not return_array: true_destabilizers = [ "ZXX", diff --git a/tests/test_quantum_info_random.py b/tests/test_quantum_info_random.py index 27c250765e..f974f57dd3 100644 --- a/tests/test_quantum_info_random.py +++ b/tests/test_quantum_info_random.py @@ -9,7 +9,6 @@ from qibo.config import PRECISION_TOL from qibo.quantum_info.metrics import purity from qibo.quantum_info.random_ensembles import ( - _probability_distribution_sin, random_clifford, random_density_matrix, random_gaussian_matrix, diff --git a/tests/test_result.py b/tests/test_result.py index 53d274d896..822006ba80 100644 --- a/tests/test_result.py +++ b/tests/test_result.py @@ -91,12 +91,11 @@ def test_circuitresult_dump_load(backend, agnostic_load): # set probabilities to trigger the warning result._probs = result.probabilities() result.dump("tmp.npy") - if agnostic_load: - loaded_res = load_result("tmp.npy") - else: - loaded_res = CircuitResult.load("tmp.npy") + loaded_res = ( + load_result("tmp.npy") if agnostic_load else CircuitResult.load("tmp.npy") + ) loaded_freq = loaded_res.frequencies() for state, f in freq.items(): assert loaded_freq[state] == f - assert backend.np.sum(result.state() - loaded_res.state()) == 0 + assert backend.np.sum(result.state() - backend.cast(loaded_res.state())) == 0 remove("tmp.npy") diff --git a/tests/test_transpiler_unitary_decompositions.py b/tests/test_transpiler_unitary_decompositions.py index c72591c72c..e6d0da3426 100644 --- a/tests/test_transpiler_unitary_decompositions.py +++ b/tests/test_transpiler_unitary_decompositions.py @@ -80,7 +80,7 @@ def test_v_decomposition(backend, seed): calculate_psi(unitary, backend=backend) else: psi, _ = calculate_psi(unitary, backend=backend) - va, vb = calculate_single_qubit_unitaries(psi) + va, vb = calculate_single_qubit_unitaries(psi, backend=backend) assert_single_qubits(backend, psi, va, vb) @@ -94,7 +94,9 @@ def test_u_decomposition(backend, seed): else: psi, eigvals = calculate_psi(unitary, backend=backend) psi_tilde = np.conj(np.sqrt(eigvals)) * np.dot(unitary, psi) - ua_dagger, ub_dagger = calculate_single_qubit_unitaries(psi_tilde) + ua_dagger, ub_dagger = calculate_single_qubit_unitaries( + psi_tilde, backend=backend + ) assert_single_qubits(backend, psi_tilde, ua_dagger, ub_dagger)