Skip to content

Commit

Permalink
Merge pull request #1417 from qiboteam/fix-complex-samples
Browse files Browse the repository at this point in the history
Fix root cause of bug reported in PR #1412
  • Loading branch information
scarrazza authored Aug 14, 2024
2 parents 2a1a504 + ad3eb17 commit 37e1010
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/qibo/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,7 @@ 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):
Expand Down
11 changes: 6 additions & 5 deletions src/qibo/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ def samples(self, binary=True, registers=False):
# calculate samples for the whole circuit so that
# individual register samples are registered here
self.circuit.final_state.samples()

if binary:
return self._samples
else:
qubits = self.measurement_gate.target_qubits
return self.backend.samples_to_decimal(self._samples, len(qubits))

qubits = self.measurement_gate.target_qubits
return self.backend.samples_to_decimal(self._samples, len(qubits))

def frequencies(self, binary=True, registers=False):
"""Returns the frequencies of measured samples.
Expand All @@ -198,8 +199,8 @@ def frequencies(self, binary=True, registers=False):
if binary:
qubits = self.measurement_gate.target_qubits
return frequencies_to_binary(self._frequencies, len(qubits))
else:
return self._frequencies

return self._frequencies

def apply_bitflips(self, p0, p1=None): # pragma: no cover
return apply_bitflips(self, p0, p1)
7 changes: 1 addition & 6 deletions src/qibo/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,7 @@ def samples(self, binary: bool = True, registers: bool = False):
if self._samples is None:
if self.measurements[0].result.has_samples():
self._samples = self.backend.np.concatenate(
[
self.backend.cast(
gate.result.samples(), dtype=self.backend.np.int32
)
for gate in self.measurements
],
[gate.result.samples() for gate in self.measurements],
axis=1,
)
else:
Expand Down

0 comments on commit 37e1010

Please sign in to comment.