Skip to content

Commit

Permalink
Merge pull request #1288 from qiboteam/cov_fix
Browse files Browse the repository at this point in the history
Coverage fix
  • Loading branch information
renatomello authored Jul 12, 2024
2 parents e3d8008 + 6be8f28 commit c3031c6
Show file tree
Hide file tree
Showing 8 changed files with 639 additions and 579 deletions.
1,155 changes: 594 additions & 561 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/qibo/gates/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ def raw(self) -> dict:
if key in REQUIRED_FIELDS_INIT_KWARGS
}

for value in encoded_simple:
if isinstance(encoded[value], set):
encoded_simple[value] = list(encoded_simple[value])

encoded_simple["_class"] = type(self).__name__

return encoded_simple
Expand Down
8 changes: 1 addition & 7 deletions src/qibo/models/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,13 +699,7 @@ def add(self, gate):
@property
def measurement_tuples(self):
# used for testing only
registers = {}
for m in self.measurements:
if m.register_name not in registers:
registers[m.register_name] = m.target_qubits
else:
registers[m.register_name] += m.target_qubits
return {name: qubits for name, qubits in registers.items()}
return {m.register_name: m.target_qubits for m in self.measurements}

@property
def ngates(self) -> int:
Expand Down
3 changes: 0 additions & 3 deletions src/qibo/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ def __init__(
for m in measurements:
indices = [self.measurement_gate.qubits.index(q) for q in m.qubits]
m.result.register_samples(samples[:, indices])
else:
for gate in self.measurements:
gate.result.reset()

def frequencies(self, binary: bool = True, registers: bool = False):
"""Returns the frequencies of measured samples.
Expand Down
10 changes: 6 additions & 4 deletions tests/test_backends_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,15 @@ def test_noise_channels(backend, seed):
clifford_bkd = construct_clifford_backend(backend)
clifford_bkd.set_seed(seed)

noise = NoiseModel()
noise.add(PauliError([("X", 0.5)]), gates.X)
noise.add(DepolarizingError(0.1), gates.CZ)

nqubits = 3

c = random_clifford(nqubits, density_matrix=True, seed=seed, backend=backend)

noise = NoiseModel()
noisy_gates = np.random.choice(c.queue, size=1, replace=False)
noise.add(PauliError([("X", 0.3)]), gates.H)
noise.add(DepolarizingError(0.3), noisy_gates[0].__class__)

c.add(gates.M(*range(nqubits)))
c_copy = c.copy()

Expand Down
12 changes: 12 additions & 0 deletions tests/test_hamiltonians_from_symbols.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test dense matrix of Hamiltonians constructed using symbols."""

import pickle

import numpy as np
import pytest
import sympy
Expand All @@ -10,6 +12,16 @@
from qibo.symbols import I, Symbol, X, Y, Z


@pytest.mark.parametrize("symbol", [I, X, Y, Z])
def test_symbols_pickling(symbol):
symbol = symbol(int(np.random.randint(4)))
dumped_symbol = pickle.dumps(symbol)
new_symbol = pickle.loads(dumped_symbol)
for attr in ("target_qubit", "name", "_gate"):
assert getattr(symbol, attr) == getattr(new_symbol, attr)
np.testing.assert_allclose(symbol.matrix, new_symbol.matrix)


@pytest.mark.parametrize("nqubits", [4, 5])
@pytest.mark.parametrize("hamtype", ["normal", "symbolic"])
@pytest.mark.parametrize("calcterms", [False, True])
Expand Down
16 changes: 16 additions & 0 deletions tests/test_measurements.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test circuit result measurements and measurement gate and as part of circuit."""

import pickle

import numpy as np
import pytest

Expand Down Expand Up @@ -457,3 +459,17 @@ def test_measurement_same_qubit_different_registers_error(backend):
c.add(gates.M(0, 1, 3, register_name="a"))
with pytest.raises(KeyError):
c.add(gates.M(1, 2, 3, register_name="a"))


def test_measurementsymbol_pickling(backend):
from qibo.models import QFT

c = QFT(3)
c.add(gates.M(0, 2, basis=[gates.X, gates.Z]))
backend.execute_circuit(c).samples()
for symbol in c.measurements[0].result.symbols:
dumped_symbol = pickle.dumps(symbol)
new_symbol = pickle.loads(dumped_symbol)
assert symbol.index == new_symbol.index
assert symbol.name == new_symbol.name
backend.assert_allclose(symbol.result.samples(), new_symbol.result.samples())
10 changes: 10 additions & 0 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def test_measurementoutcomes_probabilities(backend, qubits):
backend.assert_allclose(result.probabilities(qubits), probabilities, atol=1e-1)


def test_measurementoutcomes_samples_from_measurements(backend):
c = Circuit(3)
c.add(gates.H(0))
c.add(gates.M(0, 2))
res = backend.execute_circuit(c)
samples = res.samples()
outcome = MeasurementOutcomes(c.measurements, backend=backend)
backend.assert_allclose(samples, outcome.samples())


def test_circuit_result_error(backend):
c = models.Circuit(1)
state = np.array([1, 0])
Expand Down

0 comments on commit c3031c6

Please sign in to comment.