Skip to content

Commit c3031c6

Browse files
authored
Merge pull request #1288 from qiboteam/cov_fix
Coverage fix
2 parents e3d8008 + 6be8f28 commit c3031c6

8 files changed

+639
-579
lines changed

poetry.lock

+594-561
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/qibo/gates/abstract.py

-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ def raw(self) -> dict:
8484
if key in REQUIRED_FIELDS_INIT_KWARGS
8585
}
8686

87-
for value in encoded_simple:
88-
if isinstance(encoded[value], set):
89-
encoded_simple[value] = list(encoded_simple[value])
90-
9187
encoded_simple["_class"] = type(self).__name__
9288

9389
return encoded_simple

src/qibo/models/circuit.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -699,13 +699,7 @@ def add(self, gate):
699699
@property
700700
def measurement_tuples(self):
701701
# used for testing only
702-
registers = {}
703-
for m in self.measurements:
704-
if m.register_name not in registers:
705-
registers[m.register_name] = m.target_qubits
706-
else:
707-
registers[m.register_name] += m.target_qubits
708-
return {name: qubits for name, qubits in registers.items()}
702+
return {m.register_name: m.target_qubits for m in self.measurements}
709703

710704
@property
711705
def ngates(self) -> int:

src/qibo/result.py

-3
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@ def __init__(
186186
for m in measurements:
187187
indices = [self.measurement_gate.qubits.index(q) for q in m.qubits]
188188
m.result.register_samples(samples[:, indices])
189-
else:
190-
for gate in self.measurements:
191-
gate.result.reset()
192189

193190
def frequencies(self, binary: bool = True, registers: bool = False):
194191
"""Returns the frequencies of measured samples.

tests/test_backends_clifford.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,15 @@ def test_noise_channels(backend, seed):
267267
clifford_bkd = construct_clifford_backend(backend)
268268
clifford_bkd.set_seed(seed)
269269

270-
noise = NoiseModel()
271-
noise.add(PauliError([("X", 0.5)]), gates.X)
272-
noise.add(DepolarizingError(0.1), gates.CZ)
273-
274270
nqubits = 3
275271

276272
c = random_clifford(nqubits, density_matrix=True, seed=seed, backend=backend)
273+
274+
noise = NoiseModel()
275+
noisy_gates = np.random.choice(c.queue, size=1, replace=False)
276+
noise.add(PauliError([("X", 0.3)]), gates.H)
277+
noise.add(DepolarizingError(0.3), noisy_gates[0].__class__)
278+
277279
c.add(gates.M(*range(nqubits)))
278280
c_copy = c.copy()
279281

tests/test_hamiltonians_from_symbols.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test dense matrix of Hamiltonians constructed using symbols."""
22

3+
import pickle
4+
35
import numpy as np
46
import pytest
57
import sympy
@@ -10,6 +12,16 @@
1012
from qibo.symbols import I, Symbol, X, Y, Z
1113

1214

15+
@pytest.mark.parametrize("symbol", [I, X, Y, Z])
16+
def test_symbols_pickling(symbol):
17+
symbol = symbol(int(np.random.randint(4)))
18+
dumped_symbol = pickle.dumps(symbol)
19+
new_symbol = pickle.loads(dumped_symbol)
20+
for attr in ("target_qubit", "name", "_gate"):
21+
assert getattr(symbol, attr) == getattr(new_symbol, attr)
22+
np.testing.assert_allclose(symbol.matrix, new_symbol.matrix)
23+
24+
1325
@pytest.mark.parametrize("nqubits", [4, 5])
1426
@pytest.mark.parametrize("hamtype", ["normal", "symbolic"])
1527
@pytest.mark.parametrize("calcterms", [False, True])

tests/test_measurements.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test circuit result measurements and measurement gate and as part of circuit."""
22

3+
import pickle
4+
35
import numpy as np
46
import pytest
57

@@ -457,3 +459,17 @@ def test_measurement_same_qubit_different_registers_error(backend):
457459
c.add(gates.M(0, 1, 3, register_name="a"))
458460
with pytest.raises(KeyError):
459461
c.add(gates.M(1, 2, 3, register_name="a"))
462+
463+
464+
def test_measurementsymbol_pickling(backend):
465+
from qibo.models import QFT
466+
467+
c = QFT(3)
468+
c.add(gates.M(0, 2, basis=[gates.X, gates.Z]))
469+
backend.execute_circuit(c).samples()
470+
for symbol in c.measurements[0].result.symbols:
471+
dumped_symbol = pickle.dumps(symbol)
472+
new_symbol = pickle.loads(dumped_symbol)
473+
assert symbol.index == new_symbol.index
474+
assert symbol.name == new_symbol.name
475+
backend.assert_allclose(symbol.result.samples(), new_symbol.result.samples())

tests/test_result.py

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ def test_measurementoutcomes_probabilities(backend, qubits):
4040
backend.assert_allclose(result.probabilities(qubits), probabilities, atol=1e-1)
4141

4242

43+
def test_measurementoutcomes_samples_from_measurements(backend):
44+
c = Circuit(3)
45+
c.add(gates.H(0))
46+
c.add(gates.M(0, 2))
47+
res = backend.execute_circuit(c)
48+
samples = res.samples()
49+
outcome = MeasurementOutcomes(c.measurements, backend=backend)
50+
backend.assert_allclose(samples, outcome.samples())
51+
52+
4353
def test_circuit_result_error(backend):
4454
c = models.Circuit(1)
4555
state = np.array([1, 0])

0 commit comments

Comments
 (0)