From cc6568348662660ef26cbd496a9019a6adbf540f Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Wed, 6 Dec 2023 15:51:08 +0400 Subject: [PATCH 1/6] fix GPU test --- src/qibo/measurements.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/measurements.py b/src/qibo/measurements.py index 978f0beae4..3a36251ef8 100644 --- a/src/qibo/measurements.py +++ b/src/qibo/measurements.py @@ -161,7 +161,7 @@ def samples(self, binary=True, registers=False): # individual register samples are registered here self.circuit.final_state.samples() if binary: - return np.array(self._samples, dtype="int32") + return self.backend.cast(self._samples, dtype="int32") else: qubits = self.measurement_gate.target_qubits return self.backend.samples_to_decimal(self._samples, len(qubits)) From a1f2865a2960115fb785630b55ebdd37f24f098a Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 7 Dec 2023 10:23:00 +0400 Subject: [PATCH 2/6] fix result --- src/qibo/result.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/result.py b/src/qibo/result.py index 007fb2ea36..c60f08d3b6 100644 --- a/src/qibo/result.py +++ b/src/qibo/result.py @@ -288,7 +288,7 @@ def probabilities(self, qubits: Optional[Union[list, set]] = None): probs = [0 for _ in range(2**nqubits)] for state, freq in self.frequencies(binary=False).items(): - probs[state] = freq / self.nshots + probs[int(state)] = freq / self.nshots probs = self.backend.cast(probs) self._probs = probs return self.backend.calculate_probabilities(np.sqrt(probs), qubits, nqubits) From 6e814c09eb476b952dcc9605feadda27e7d56ac6 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 7 Dec 2023 10:24:46 +0400 Subject: [PATCH 3/6] fix tests --- src/qibo/measurements.py | 45 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/qibo/measurements.py b/src/qibo/measurements.py index 3a36251ef8..8573e88280 100644 --- a/src/qibo/measurements.py +++ b/src/qibo/measurements.py @@ -1,31 +1,10 @@ import collections -import numpy as np import sympy -from qibo import gates from qibo.config import raise_error -def frequencies_to_binary(frequencies, nqubits): - return collections.Counter( - {"{:b}".format(k).zfill(nqubits): v for k, v in frequencies.items()} - ) - - -def apply_bitflips(result, p0, p1=None): - gate = result.measurement_gate - if p1 is None: - probs = 2 * (gate._get_bitflip_tuple(gate.qubits, p0),) - else: - probs = ( - gate._get_bitflip_tuple(gate.qubits, p0), - gate._get_bitflip_tuple(gate.qubits, p1), - ) - noiseless_samples = result.samples() - return result.backend.apply_bitflips(noiseless_samples, probs) - - class MeasurementSymbol(sympy.Symbol): """``sympy.Symbol`` connected to measurement results. @@ -190,9 +169,29 @@ def frequencies(self, binary=True, registers=False): ) if binary: qubits = self.measurement_gate.target_qubits - return frequencies_to_binary(self._frequencies, len(qubits)) + return _frequencies_to_binary(self._frequencies, len(qubits)) else: return self._frequencies def apply_bitflips(self, p0, p1=None): # pragma: no cover - return apply_bitflips(self, p0, p1) + return _apply_bitflips(self, p0, p1) + + +def _frequencies_to_binary(frequencies, nqubits): + return collections.Counter( + {"{:b}".format(int(k)).zfill(nqubits): v for k, v in frequencies.items()} + ) + + +def _apply_bitflips(result, p0, p1=None): + gate = result.measurement_gate + if p1 is None: + probs = 2 * (gate._get_bitflip_tuple(gate.qubits, p0),) + else: + probs = ( + gate._get_bitflip_tuple(gate.qubits, p0), + gate._get_bitflip_tuple(gate.qubits, p1), + ) + noiseless_samples = result.samples() + + return result.backend.apply_bitflips(noiseless_samples, probs) From 385317c8d6ce85a580c2e3e6896bffbbf0eb75ad Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 7 Dec 2023 10:26:45 +0400 Subject: [PATCH 4/6] rename functions --- src/qibo/result.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/qibo/result.py b/src/qibo/result.py index c60f08d3b6..5470df6e08 100644 --- a/src/qibo/result.py +++ b/src/qibo/result.py @@ -6,7 +6,7 @@ from qibo import __version__, backends, gates from qibo.config import raise_error -from qibo.measurements import apply_bitflips, frequencies_to_binary +from qibo.measurements import _apply_bitflips, _frequencies_to_binary def load_result(filename: str): @@ -236,7 +236,7 @@ def frequencies(self, binary: bool = True, registers: bool = False): # register frequencies to individual gate ``MeasurementResult`` qubit_map = {q: i for i, q in enumerate(qubits)} reg_frequencies = {} - binary_frequencies = frequencies_to_binary( + binary_frequencies = _frequencies_to_binary( self._frequencies, len(qubits) ) for gate in self.measurements: @@ -261,7 +261,7 @@ def frequencies(self, binary: bool = True, registers: bool = False): } if binary: - return frequencies_to_binary(self._frequencies, len(qubits)) + return _frequencies_to_binary(self._frequencies, len(qubits)) return self._frequencies @@ -395,7 +395,7 @@ def apply_bitflips(self, p0: float, p1: Optional[float] = None): p0 (float): Probability of the 0->1 flip. p1 (float): Probability of the 1->0 flip. """ - return apply_bitflips(self, p0, p1) + return _apply_bitflips(self, p0, p1) def expectation_from_samples(self, observable): """Computes the real expectation value of a diagonal observable from frequencies. From 1c5bdd6cd7d47a00dcb931029964746d6c4cc172 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 7 Dec 2023 10:51:15 +0400 Subject: [PATCH 5/6] change in counters --- src/qibo/backends/numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/backends/numpy.py b/src/qibo/backends/numpy.py index 1153eb0c01..08fb89c70c 100644 --- a/src/qibo/backends/numpy.py +++ b/src/qibo/backends/numpy.py @@ -643,7 +643,7 @@ def samples_to_decimal(self, samples, nqubits): def calculate_frequencies(self, samples): res, counts = self.np.unique(samples, return_counts=True) res, counts = self.np.array(res), self.np.array(counts) - return collections.Counter({k: v for k, v in zip(res, counts)}) + return collections.Counter({str(k): int(v) for k, v in zip(res, counts)}) def update_frequencies(self, frequencies, probabilities, nsamples): samples = self.sample_shots(probabilities, nsamples) From c6256eede0c6fb2fa7100a3dbc5cd500a4b523bb Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 7 Dec 2023 15:40:33 +0400 Subject: [PATCH 6/6] revert changes --- src/qibo/backends/numpy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qibo/backends/numpy.py b/src/qibo/backends/numpy.py index 08fb89c70c..1153eb0c01 100644 --- a/src/qibo/backends/numpy.py +++ b/src/qibo/backends/numpy.py @@ -643,7 +643,7 @@ def samples_to_decimal(self, samples, nqubits): def calculate_frequencies(self, samples): res, counts = self.np.unique(samples, return_counts=True) res, counts = self.np.array(res), self.np.array(counts) - return collections.Counter({str(k): int(v) for k, v in zip(res, counts)}) + return collections.Counter({k: v for k, v in zip(res, counts)}) def update_frequencies(self, frequencies, probabilities, nsamples): samples = self.sample_shots(probabilities, nsamples)