From 9a6c55d613d142b4e5119c5da672fc21c8651819 Mon Sep 17 00:00:00 2001 From: BrunoLiegiBastonLiegi Date: Wed, 25 Sep 2024 16:49:36 +0200 Subject: [PATCH 1/3] feat: replaced self.gate with self.target_qubits --- src/qibo/gates/measurements.py | 2 +- src/qibo/measurements.py | 12 ++++++------ tests/test_measurements.py | 4 ++-- tests/test_states.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/qibo/gates/measurements.py b/src/qibo/gates/measurements.py index 64a7a98e50..2858685a89 100644 --- a/src/qibo/gates/measurements.py +++ b/src/qibo/gates/measurements.py @@ -57,7 +57,7 @@ def __init__( self.target_qubits = tuple(q) self.register_name = register_name self.collapse = collapse - self.result = MeasurementResult(self) + self.result = MeasurementResult(self.target_qubits) # list of measurement pulses implementing the gate # relevant for experiments only self.pulses = None diff --git a/src/qibo/measurements.py b/src/qibo/measurements.py index 3c8f6d2231..023a907e14 100644 --- a/src/qibo/measurements.py +++ b/src/qibo/measurements.py @@ -86,8 +86,8 @@ class MeasurementResult: to use for calculations. """ - def __init__(self, gate): - self.measurement_gate = gate + def __init__(self, qubits): + self.target_qubits = qubits self.circuit = None self._samples = None @@ -115,7 +115,7 @@ def nshots(self) -> int: def add_shot(self, probs, backend=None): backend = _check_backend(backend) - qubits = sorted(self.measurement_gate.target_qubits) + qubits = sorted(self.target_qubits) shot = backend.sample_shots(probs, 1) bshot = backend.samples_to_binary(shot, len(qubits)) if self._samples: @@ -153,7 +153,7 @@ def symbols(self): These symbols are useful for conditioning parametrized gates on measurement outcomes. """ if self._symbols is None: - qubits = self.measurement_gate.target_qubits + qubits = self.target_qubits self._symbols = [MeasurementSymbol(i, self) for i in range(len(qubits))] return self._symbols @@ -186,7 +186,7 @@ def samples(self, binary=True, registers=False, backend=None): if binary: return self._samples - qubits = self.measurement_gate.target_qubits + qubits = self.target_qubits return backend.samples_to_decimal(self._samples, len(qubits)) def frequencies(self, binary=True, registers=False, backend=None): @@ -213,7 +213,7 @@ def frequencies(self, binary=True, registers=False, backend=None): self.samples(binary=False) ) if binary: - qubits = self.measurement_gate.target_qubits + qubits = self.target_qubits return frequencies_to_binary(self._frequencies, len(qubits)) return self._frequencies diff --git a/tests/test_measurements.py b/tests/test_measurements.py index 5a8e6a66e6..6afa4798ac 100644 --- a/tests/test_measurements.py +++ b/tests/test_measurements.py @@ -478,7 +478,7 @@ def test_measurementsymbol_pickling(backend): def test_measurementresult_nshots(backend): gate = gates.M(*range(3)) - result = MeasurementResult(gate) + result = MeasurementResult(gate.qubits) # nshots starting from samples nshots = 10 samples = backend.cast( @@ -487,7 +487,7 @@ def test_measurementresult_nshots(backend): result.register_samples(samples) assert result.nshots == nshots # nshots starting from frequencies - result = MeasurementResult(gate) + result = MeasurementResult(gate.qubits) states, counts = np.unique(samples, axis=0, return_counts=True) to_str = lambda x: [str(item) for item in x] states = ["".join(to_str(s)) for s in states.tolist()] diff --git a/tests/test_states.py b/tests/test_states.py index 9ce556485b..b0c990c197 100644 --- a/tests/test_states.py +++ b/tests/test_states.py @@ -13,7 +13,7 @@ def test_measurement_result_repr(): def test_measurement_result_error(): - result = MeasurementResult(gates.M(0)) + result = MeasurementResult(gates.M(0).qubits) with pytest.raises(RuntimeError): samples = result.samples() From 28d2752b549de951e359e6a1bc7d1d736b57907e Mon Sep 17 00:00:00 2001 From: BrunoLiegiBastonLiegi Date: Wed, 25 Sep 2024 16:58:43 +0200 Subject: [PATCH 2/3] fix: fix measurement_gate reference --- 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 023a907e14..f7ccb249ae 100644 --- a/src/qibo/measurements.py +++ b/src/qibo/measurements.py @@ -97,7 +97,7 @@ def __init__(self, qubits): self._symbols = None def __repr__(self): - qubits = self.measurement_gate.qubits + qubits = self.target_qubits nshots = self.nshots return f"MeasurementResult(qubits={qubits}, nshots={nshots})" From 21f0d7e7a2f792e40e7f364c7e02a31f8b837dd8 Mon Sep 17 00:00:00 2001 From: BrunoLiegiBastonLiegi Date: Wed, 25 Sep 2024 17:46:22 +0200 Subject: [PATCH 3/3] fix: fix repr test --- tests/test_states.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_states.py b/tests/test_states.py index b0c990c197..3c885c13fd 100644 --- a/tests/test_states.py +++ b/tests/test_states.py @@ -8,7 +8,7 @@ def test_measurement_result_repr(): - result = MeasurementResult(gates.M(0)) + result = MeasurementResult(gates.M(0).target_qubits) assert str(result) == "MeasurementResult(qubits=(0,), nshots=None)"