Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomello committed Mar 16, 2024
1 parent 4632a6c commit 033e968
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
8 changes: 5 additions & 3 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/qibo/quantum_info/random_ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
21 changes: 13 additions & 8 deletions tests/test_quantum_info_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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]
Expand All @@ -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",
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion tests/test_quantum_info_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
6 changes: 4 additions & 2 deletions tests/test_transpiler_unitary_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)


Expand Down

0 comments on commit 033e968

Please sign in to comment.